<?php
defined( 'ABSPATH' ) or exit;   // show a blank page if try to access this file directly

require_once( EWZ_PLUGIN_DIR . "classes/validation/ewz-input.php");
require_once( EWZ_PLUGIN_DIR . "classes/validation/ewz-webform-data-input.php");
require_once( EWZ_PLUGIN_DIR . "classes/validation/ewz-upload-input.php");
require_once( EWZ_PLUGIN_DIR . "classes/ewz-settings.php");

/* Validation for the main form in the Item List page */

class Ewz_Item_List_Input extends Ewz_Input
{
    /**
     * @param mixed $form_data
     *
     * @return void
     */
    function __construct( $form_data ) {
        parent::__construct( $form_data );

        $act = '';
        if ( isset( $form_data['action'] ) && -1 != $form_data['action'] ){
            $act = $form_data['action'];
        } else {
            if ( isset( $form_data['action2'] ) && -1 != $form_data['action2'] ){
                $act = $form_data['action2'];
            }
        }

        $is_del = ( 'ewz_admin_del_items' ==  $act );
        $is_att = ( 'ewz_attach_imgs' == $act );

        /*** rules('type') specifies the function called to validate, defined either here or in ewz-input.php */
        /*** rules('val') defines extra args to pass */
        $this->rules
            = array(
                    'webform_id'   => array( 'type' => 'to_seq',    'req' => true,    'val' => ['len' => 10]  ),
                    'ewznonce'     => array( 'type' => 'anonce',    'req' => true,    'val' => ''            ),
                 'ewzuploadnonce'  => array( 'type' => 'anonce',    'req' => false,   'val' => ''            ),
                    'fopt'         => array( 'type' => 'v_fopts',   'req' => false,   'val' => ''       ),
                    'copt'         => array( 'type' => 'v_copts',   'req' => false,   'val' => ''       ),
                    '_wpnonce'     => array( 'type' => 'to_string', 'req' => false,   'val' => ''            ),  // for WP_List_Table
                    'action'       => array( 'type' => 'limited',   'req' => false,   'val' => array('ewz_attach_imgs',
                                                                                                     'ewz_set_ipp',
                                                                                                     'ewz_batch_delete_all',
                                                                                                     'ewz_batch_delete_data',
                                                                                                     'ewz_select_for_edit',
                                                                                                     'ewz_save_edits',
                                                                                                     '-1' ) ),
                    'action2'      => array( 'type' => 'limited',   'req' => false,   'val' => array('ewz_attach_imgs',
                                                                                                     'ewz_set_ipp',
                                                                                                     'ewz_batch_delete_all',
                                                                                                     'ewz_batch_delete_data',
                                                                                                     'ewz_select_for_edit',
                                                                                                     'ewz_save_edits',
                                                                                                     '-1' ) ),
                    'ewz_check'    => array( 'type' => 'int_arr',   'req' => $is_del, 'val' => array( 1, 1000 ) ), // selected rows
                    'ewz_ipp'      => array( 'type' => 'to_seq',    'req' => false,   'val' => ['len' => 4]     ),
                    'ewzmode'      => array( 'type' => 'limited',   'req' => false,   'val' => array('list')    ),  // needed to override other modes in webform
                    'ewz_page_sel' => array( 'type' => 'to_seq',    'req' => $is_att, 'val' => ['len' => 20]     ), // page for attaching (post_id)
                    'ifield'       => array( 'type' => 'to_seq',    'req' => false,   'val' => ['len' => 10]     ),  // img col for attach, if more than one
                    'dups_ok'      => array( 'type' => 'to_bool',   'req' => false,   'val' => ''               ),  // allow images to be attached twice
                    'img_comment'  => array( 'type' => 'to_bool',   'req' => false,   'val' => ''               ),  // comments allowed on imgs being attached?
                    'img_size'     => array( 'type' => 'limited',   'req' => $is_att, 'val' => ewz_get_img_size_names() ), // copy image when attaching
                    'img_qual'     => array( 'type' => 'limited',   'req' => false,   'val' => Ewz_Settings::get_qual_opts()),
                    'orderby'      => array( 'type' => 'to_seq',    'req' => false,   'val' => ['len' => 4]     ),  // column to order by
                    'order'        => array( 'type' => 'limited',   'req' => false,   'val' => array( 'asc','desc' ) ),
                    'page'         => array( 'type' => 'fixed',     'req' => false,   'val' => 'entrywizlist' ),
                    'paged'        => array( 'type' => 'to_seq',    'req' => false,   'val' => ['len' => 4]  ),  // pagination
                    'uploaddays'   => array( 'type' => 'to_seq',    'req' => false,   'val' => ['len' => 4]        ),
                    '_wp_http_referer' => array( 'type' => 'to_string', 'req' => false,  'val' => ''               ),
                    'btitle'       => array( 'type' => 'to_string', 'req' => false,   'val' => ''            ),
                    'bexcerpt'       => array( 'type' => 'to_string', 'req' => false,   'val' => ''            ),
                    'btag'       => array( 'type' => 'to_string', 'req' => false,   'val' => ''            ),
                    'bcontent'       => array( 'type' => 'to_string', 'req' => false,   'val' => ''            ),
                    'rdata'        => array( 'type' => 'v_rdata',   'req' => false,  'val' => '' ),
                    'user_id'       => array( 'type' => 'to_seq',    'req' => false,  'val' => '' ),
            );
        $this->validate();
    }

    /**
     * @return boolean  true on success
     */
   function validate(){

        parent::validate();
        // an unchecked checkbox does not create any matching value in $_POST
        if ( !array_key_exists( 'img_comment', $this->input_data ) ) {
            $this->input_data['img_comment'] = false;
        }
        if ( !array_key_exists( 'dups_ok', $this->input_data ) ) {
            $this->input_data['dups_ok'] = false;
        }
        // fopt and copt are input (field & custom) used to select the data for the list
        // in the "With selected items:" data section of the webform
        // not actually used for processing item-list input
        if ( !array_key_exists( 'fopt', $this->input_data ) ) {
            $this->input_data['fopt'] = array();
        }
        if ( !array_key_exists( 'copt', $this->input_data ) ) {
            $this->input_data['copt'] = array();
        }
        return true;
   }

    /**
     * @param mixed $value
     * @param string $arg
     *
     * @return boolean
     */
    function v_fopts( $value, $arg ){
        return Ewz_Webform_Data_Input::v_fopts( $value, $arg );
    }
    /**
     * @param mixed $value
     * @param string $arg
     *
     * @return boolean
     */

    function v_copts( $value, $arg ){
        return Ewz_Webform_Data_Input::v_copts( $value, $arg );
    }
     /**
     * @param mixed $value
     * @param string $arg
     *
     * @return boolean
     */
   function v_rdata( &$value, $arg ){
        if( $value ){
            $webform = new Ewz_Webform( $this->input_data['webform_id'] );
            $layout = new Ewz_Layout( $webform->layout_id );

            // all input fields  may not be here, so don't check restrictions.
            $layout->restrictions = array();
            return Ewz_Upload_Input::check_rdata( $value, $layout, 0 );
        } else {
            return true;
        }
    }
}
