<?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/ewz-settings.php" );

/* Validation for the Settings page */

class Ewz_Settings_Input extends Ewz_Input
{

    /**
     * @param  mixed $form_data
     *
     * @return void
     */
    function __construct( $form_data ) {
        parent::__construct( $form_data );
        $this->rules = array(
                             'ewznonce'          => array( 'type' => 'anonce',    'req' => true,  'val' => '' ),
                             'ewzmode'           => array( 'type' => 'fixed',     'req' => true,  'val' => 'settings' ),
                             '_wp_http_referer'  => array( 'type' => 'to_string', 'req' => false, 'val' => '' ),
                             'del_data_on_uninst'=> array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'del_imgs_on_uninst'=> array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'upload_closed_txt' => array( 'type' => 'to_string', 'req' => true,  'val' => ['len' => 120] ),
                           'show_title_if_closed'=> array( 'type' => 'to_bool',  'req' => false, 'val' => '' ),
                             'curr_stored_txt'   => array( 'type' => 'to_string', 'req' => true,  'val' => ['len' => 80] ),
                             'spec_char_replace' => array( 'type' => 'limited',   'req' => true,  'val' => array('_','-','X','x') ),
                             'terminal_underscore'=>array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'margin'            => array( 'type' => 'limited',   'req' => true,  'val' => Ewz_Settings::get_margin_opts()),
                             'attach_img_comment'=> array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'attach_jpeg_qual'  => array( 'type' => 'limited',   'req' => false, 'val' => Ewz_Settings::get_qual_opts()),
                             'attach_img_size'   => array( 'type' => 'limited',   'req' => true,  'val' => array_keys(Ewz_Settings::get_img_size_names())),
                             'webform_alias'     => array( 'type' => 'to_string', 'req' => false, 'val' =>  ['len' => 40]),
                             'ss_usr_labels'     => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                           'gen_ssheet_on_close' => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'show_auto_close'   => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'show_auto_open'    => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'will_close_txt'    => array( 'type' => 'to_string', 'req' => false, 'val' => ['len' =>  200] ),
                             'will_open_txt'     => array( 'type' => 'to_string', 'req' => false, 'val' => ['len' =>  200] ),
                             'csv_ascii'         => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             'cs_warn'           => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             ' '                 => array( 'type' => 'to_bool',   'req' => false, 'val' => '' ),
                             );
        $this->rules = apply_filters( 'ewz_after_settings_input', $this->rules );

        $this->validate();
    }

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

        foreach( $this->rules as $key => $data ){
            if( $data['type'] == 'to_bool' && !isset( $this->input_data[$key] )){
                // an unchecked checkbox does not create any matching value in $_POST
                $this->input_data[$key] = false;
            }
        }
        return true;
     }

}
