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

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

/* Validation for the forms on Webforms page */

class Ewz_Webform_Set_Input extends Ewz_Input
{

    /**
     * @param array<mixed> $form_data
     *
     * @return void
     */
     function __construct( $form_data ) {
         parent::__construct( $form_data );

        /* 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(

                  '_wp_http_referer' => array( 'type' => 'to_string', 'req' => false, 'val' => '' ),
                  'ewzmode'          => array( 'type' => 'fixed',     'req' => true,  'val' => 'wf_set' ),
                  'action'           => array( 'type' => 'limited',   'req' => true,  'val' => ['ewz_webforms_batch_delete',
                                                                                                'ewz_save_webform_order',
                                                                                                'ewz_hide_webforms',
                                                                                                'ewz_remove_zipfiles']),
                  'hide_webform'        => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                  'ewznonce'         => array( 'type' => 'anonce',    'req' => true,  'val' => '' ),
                  'wforder'          => array( 'type' => 'v_order',   'req' => false, 'val' => ['len' => 10] ),
                  'webform_ids'      => array( 'type' => 'v_wfids',   'req' => false, 'val' => ['len' => 10] ),
                  'target'           => array( 'type' => 'limited',   'req' => false, 'val' => ['forms','items','zipfiles']),
                 );
        $this->validate();
     }

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

        // an unchecked checkbox does not create any matching value in $_POST
        if ( !array_key_exists( 'hide_webform', $this->input_data ) ) {
            $this->input_data['hide_webform'] = false;
        }

        parent::validate();

        return true;
     }

    /**
     * @param mixed $value
     * @param array<mixed> $arg
     *
     * @return boolean
     */
    function v_order( $value, $arg ) {
        if ( is_array( $value ) ) {
            $seen = array();
            $count = count($value);
            foreach ( $value as $key => $nm ) {
                self::to_seq( $key, $arg);
                if ( !preg_match( '/^\d+$/', $nm ) ) {
                    throw new EWZ_Exception( "Invalid order $nm" );
                }
                if( $nm < 0 || $nm >= $count ){
                    throw new EWZ_Exception( "Invalid value for order $nm" );
                }
                if( isset($seen[$nm]) ){
                    throw new EWZ_Exception( "Duplicate value for order $nm" );
                }
                $seen[$nm] = true;
            }
        } else {
            throw new EWZ_Exception( "Bad input data for webform order" );
        }
        return true;
     }


    /**
     * @param mixed $value
     * @param array<mixed> $arg
     *
     * @return boolean
     */
    function v_wfids( &$value, $arg ) {
        if ( is_array( $value ) ) {
            $seen = array();
            foreach ( $value as $key => $nm ) {
                if ( !is_int( $key ) ) {
                    throw new EWZ_Exception( "Invalid key $key" );
                }
                self::to_seq($nm, $arg);
                $value[$key] = intval( $nm );
                if( isset($seen[$key]) ){
                    throw new EWZ_Exception( "Duplicate value for key $nm" );
                }
                $seen[$key] = true;
            }
        } else {
            throw new EWZ_Exception( "Bad input data for webform batch delete" );
        }
        return true;
     }
}
