<?php
global $arf_prevent_duplicate;
$arf_prevent_duplicate = new arf_prevent_duplicate();

class arf_prevent_duplicate {

    function __construct() {

        add_action('arf_additional_onsubmit_settings', array($this, 'arf_prevent_duplicate_data_onsubmit'), 10, 2);

        /* arf_dev_flag from action to filter*/
        add_filter('arf_predisplay_form', array($this, 'arf_prevent_duplicate_before_form_render'), 10, 2);

        add_filter('arf_additional_form_content_outside', array($this, 'arf_prevent_duplicate_after_form_render'), 10,5);

        add_filter('arf_validate_form_outside_errors', array($this, 'arf_prevent_duplicate_card'), 10, 4);

        add_filter('arf_prevent_duplicate_entry',array($this,'arf_prevent_duplicate_entry_before_create'), 10,3);
    }

    function arf_prevent_duplicate_entry_before_create($error_message, $form_id, $values){
        global $wpdb, $MdlDb, $arfsettings;

        $validate = false;

        $arf_errors = array();

        $arf_errors = $this->arf_prevent_duplicate_card($arf_errors, $form_id, $values, array() );

        $error_message = "";
        
        if( !empty($arf_errors) && $arf_errors['arf_message_error'] != '' ){
            $validate = true;
            $error_message = $arf_errors['arf_message_error'];
            $arfsettings->failed_msg = $error_message;
        }


        return $validate;
    }



    function arf_prevent_duplicate_data_onsubmit($id, $values) {

        global $armainhelper, $arformcontroller,$arfieldhelper, $maincontroller;
        ?>
        <div class="arfsettingspacer"></div>
        <div class="arfcolumnleft">
            <div class="arf_popup_checkbox_wrapper">
                <div class="arf_custom_checkbox_div" >
                    <div class="arf_custom_checkbox_wrapper" onclick="preventduplicatefield();">
                        <input type="checkbox" <?php isset($values["arf_pre_dup_check"]) ? checked($values["arf_pre_dup_check"], 1) : ''; ?> value="1" id="arf_pre_dup_check" name="options[arf_pre_dup_check]" class="chkstanard" >
                        <svg width="18px" height="18px">
                            <?php echo ARF_CUSTOM_UNCHECKED_ICON; ?>
                            <?php echo ARF_CUSTOM_CHECKED_ICON; ?>
                        </svg>
                    </div>
                    <span>
                        <label for="arf_pre_dup_check" onclick="preventduplicatefield();"><?php echo addslashes(esc_html__('Prevent duplicate form entries', 'ARForms')) ?></label>
                    </span>
                </div>
            </div>
        </div>
        <div class="arfsettingspacer"></div>
        <?php
        if (isset($values["arf_pre_dup_check"]) && $values["arf_pre_dup_check"] == 1) {
            $arf_pre_dup_class = 'arf_pre_dup_show';
        } else {
            $arf_pre_dup_class = 'arf_pre_dup_hide';
        }
        ?>
        <div class="arf_clear_both"></div>
        <div class="sltstandard <?php echo $arf_pre_dup_class; ?>" id="prevent_duplicate_field">

            <div class="arfsettingspacer prevent_duplicate_field_settingspacer"></div>
            <?php
                if( !isset($values['arf_pre_dup_check_type']) || $values['arf_pre_dup_check_type'] == '' ){
                    $values['arf_pre_dup_check_type'] = "ip_address";
                }
            ?>
            <div class="arf_prevent_duplicate_entry_options arfmarginl20">
                <div class="arf_prevent_duplicate_entry_opt_label"><?php echo addslashes(esc_html__('Check duplicate based on','ARForms')); ?> </div>
                <div class="arf_prevent_duplicate_entry_opt_input">
                    <div class="arf_radio_wrapper">
                        <div class="arf_custom_radio_div">
                            <div class="arf_custom_radio_wrapper">
                                <input type="radio" <?php isset($values["arf_pre_dup_check_type"]) ? checked($values["arf_pre_dup_check_type"],"ip_address") : ''; ?> value="ip_address" class="arf_pre_dup_check_type_object" id="arf_pre_dup_check_type_ip" name="options[arf_pre_dup_check_type]" />
                                <svg width="18px" height="18px">
                                    <?php echo ARF_CUSTOM_UNCHECKEDRADIO_ICON; ?>
                                    <?php echo ARF_CUSTOM_CHECKEDRADIO_ICON; ?>
                                </svg>
                            </div>
                        </div>
                        <span>
                            <label id="arf_pre_dup_check_type_ip" for="arf_pre_dup_check_type_ip"><?php echo addslashes(esc_html__('IP address', 'ARForms')) ?></label>
                        </span>
                    </div> 
                    <div class="arf_radio_wrapper">
                        <div class="arf_custom_radio_div">
                            <div class="arf_custom_radio_wrapper">
                                <input type="radio" <?php isset($values['arf_pre_dup_check_type']) ? checked($values['arf_pre_dup_check_type'],"current_user") : ''; ?> value="current_user" class="arf_pre_dup_check_type_object" id="arf_pre_dup_check_type_user" name="options[arf_pre_dup_check_type]" />
                                <svg width="18px" height="18px">
                                    <?php echo ARF_CUSTOM_UNCHECKEDRADIO_ICON; ?>
                                    <?php echo ARF_CUSTOM_CHECKEDRADIO_ICON; ?>
                                </svg>
                            </div>
                        </div>
                        <span>
                            <label id="arf_pre_dup_check_type_user" for="arf_pre_dup_check_type_user"><?php echo addslashes(esc_html__('Current logged in user','ARForms')); ?></label>
                        </span>
                    </div>  
                    <div class="arf_radio_wrapper">
                        <div class="arf_custom_radio_div">
                            <div class="arf_custom_radio_wrapper">
                                <input type="radio" <?php isset($values['arf_pre_dup_check_type']) ? checked($values['arf_pre_dup_check_type'],"fields") : ''; ?> value="fields" class="arf_pre_dup_check_type_object" id="arf_pre_dup_check_type_form_fields" name="options[arf_pre_dup_check_type]" />
                                <svg width="18px" height="18px">
                                    <?php echo ARF_CUSTOM_UNCHECKEDRADIO_ICON; ?>
                                    <?php echo ARF_CUSTOM_CHECKEDRADIO_ICON; ?>
                                </svg>
                            </div>
                        </div>
                        <span>
                            <label id="arf_pre_dup_check_type_form_fields" for="arf_pre_dup_check_type_form_fields"><?php echo addslashes(esc_html__('Form Fields','ARForms')); ?></label>
                        </span>
                    </div>
                </div>                                                   
            </div>

            <div class="arfsettingspacer prevent_duplicate_field_settingspacer"></div>
            <?php
            $enable_dropdown = "display:none;";
            if( isset($values['arf_pre_dup_check_type']) && $values['arf_pre_dup_check_type'] == "fields" ){
                $enable_dropdown = "display:block;";
            }
            ?>
            <span id="select_ar_prevent_duplicate_field" style="<?php echo $enable_dropdown; ?>">
                <div class="arf_prevent_duplicate_entry_field_label"><?php echo addslashes(esc_html__('Select Field','ARForms')); ?> </div>
                <div class="sltstandard">
                    <?php
                    $selectbox_field_options = array('' => addslashes(esc_html__('Select Field', 'ARForms')));
                    $selectbox_field_value_label = "";
                    $user_responder_email = "";

                    if (!empty($values['fields'])) {
                        foreach ($values['fields'] as $field2) {
                            if (in_array($field2['type'], array('email', 'text', 'hidden', 'radio', 'select', 'phone')) && $field2['type'] != 'arf_repeater') {

                                if( isset( $field2['parent_field_type'] ) && $field2['parent_field_type'] == 'arf_repeater' ){
                                    continue;
                                }

                                if (isset($values['arf_pre_dup_field']) && ($field2["id"] == $values['arf_pre_dup_field'])) {
                                    $selectbox_field_value_label = $field2["name"];
                                    $user_responder_email = $values['arf_pre_dup_field'];
                                } 

                                $current_field_id = $field2["id"];
                                if($current_field_id !="" && $arfieldhelper->arf_execute_function($field2["name"],'strip_tags')=="" ){
                                    $selectbox_field_options[$current_field_id] = '[Field Id:'.$current_field_id.']';
                                }else{
                                    $selectbox_field_options[$current_field_id] = $arfieldhelper->arf_execute_function($field2["name"],'strip_tags');
                                }

                            }
                        }
                    }

                    $user_responder_email = ( !empty( $user_responder_email ) ) ? $user_responder_email : '';

                    echo $maincontroller->arf_selectpicker_dom( 'options[arf_pre_dup_field]', 'arf_pre_dup_field', 'arf_pre_dup_field_dropdown', 'width:240px;', $user_responder_email, array(), $selectbox_field_options, false, array(), false, array(), false, array(), true );
                    ?>  

                    <div class="arferrmessage" id="arf_pre_dup_field_error" style="display:none;top:5px;"><?php echo addslashes(esc_html__('This field cannot be blank.', 'ARForms')); ?></div>
                </div>
            </span>
        </div>

        <div class="arfsettingspacer"></div>
        <div id="prevent_duplicate_message_box" class="arftablerow prevent_duplicate_message_box prevent_duplicate_box <?php echo $arf_pre_dup_class; ?>" style="margin-left: 45px;">
            <div class="arfcolumnleft arfsettingsubtitle"><?php echo esc_html__('Duplicate entry messages', 'ARForms'); ?></div>
            <div class="arfcolumnright arf_pre_dup_msg_width">
                <textarea id="arf_pre_dup_msg" name="options[arf_pre_dup_msg]" class="txtmodal1 auto_responder_webform_code_area" style="padding: 10px;"><?php echo (isset($values['arf_pre_dup_msg']) && $values['arf_pre_dup_msg']!='') ? $armainhelper->esc_textarea($arformcontroller->br2nl($values['arf_pre_dup_msg'])) : addslashes(esc_html__('You have already submitted this form before. You are not allowed to submit this form again.','ARForms')); ?></textarea><br />
                <div class="arferrmessage" id="arf_pre_dup_msg_error" style="display:none;"><?php echo addslashes(esc_html__('This field cannot be blank.', 'ARForms')); ?></div>
            </div>
        </div>
        <div class="arfsettingspacer"></div>
    <?php
    }

    function arf_prevent_duplicate_card($arf_errors, $form_id, $values, $arf_form_data = array()) {
        global $wpdb, $arfrecordmeta, $arfsettings, $current_user,$MdlDb, $arfform;
        $form_data = wp_cache_get( 'arf_form_options_' . $form_id);
        if( false == $form_data ){
            $form_data = $arfform->arf_select_db_data( true, '', $MdlDb->forms, 'options', 'WHERE id = %d', array( $form_id ), '', '', '', false, true );
            wp_cache_set('arf_form_options_' . $form_id, $form_data );
        }

        if ( empty( $form_data ) )
            return $arf_errors;

        $form_options = maybe_unserialize($form_data->options);
        $arf_pre_dup_field = '';
        $arf_pre_dup_type = '';
        if ($form_options['arf_pre_dup_check'] && $form_options['arf_pre_dup_check'] == 1) {

            if( isset($form_options['arf_pre_dup_check_type']) && $form_options['arf_pre_dup_check_type']) {
                $arf_pre_dup_type = $form_options['arf_pre_dup_check_type'];
            }

            if (isset($form_options['arf_pre_dup_field']) && $form_options['arf_pre_dup_field']){
                $arf_pre_dup_field = $form_options['arf_pre_dup_field'];
            }

            if (isset($form_options['arf_pre_dup_msg']) && $form_options['arf_pre_dup_msg']){
                $arf_pre_dup_msg = apply_filters('arffrm_prevent_duplicate_msg',$form_options['arf_pre_dup_msg'],$form_id);
            }

            if ($arf_pre_dup_type && $arf_pre_dup_type == 'fields') {
                $pre_dup_form_data = $wpdb->get_results($wpdb->prepare('SELECT em.*,e.* FROM ' .$MdlDb->entry_metas.' em LEFT JOIN '.$MdlDb->entries.' e ON em.entry_id=e.id  WHERE em.field_id = %d AND e.form_id = %d', $arf_pre_dup_field, $form_id)); //phpcs:ignore

                if (isset($values['item_meta']) && $values['item_meta']) {

                    foreach ($values['item_meta'] as $curr_field_id => $curr_field_value) {

                        if ($arf_pre_dup_field == $curr_field_id && $curr_field_value != '')
                        {
                            if(!empty($pre_dup_form_data)){

                                foreach ($pre_dup_form_data as $pre_dup_form) {

                                    if(isset($pre_dup_form->entry_value) && $pre_dup_form->entry_value!= '' && $pre_dup_form->entry_value == $curr_field_value)
                                    {
                                        $arf_errors['arf_message_error'] =$arf_pre_dup_msg;
                                        break;
                                    }

                                 }
                            }

                        }
                    }
                }

            } elseif ($arf_pre_dup_type && $arf_pre_dup_type == 'ip_address') {
                $pre_dup_form_data = $wpdb->get_results($wpdb->prepare('SELECT * FROM  ' .$MdlDb->entries.' WHERE form_id = %d', $form_id)); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Reason: $MdlDb->entries is table name defined globally. False Positive alarm

                if(!empty($pre_dup_form_data)){
                    foreach ($pre_dup_form_data as $pre_dup_form) {

                        if(isset($pre_dup_form->ip_address) && $pre_dup_form->ip_address!= '' && $_SERVER['REMOTE_ADDR']!= '' && $pre_dup_form->ip_address == $_SERVER['REMOTE_ADDR'])
                        {
                            $arf_errors['arf_message_error'] = $arf_pre_dup_msg;
                            break;
                        }

                    }
                }
            } elseif ($arf_pre_dup_type && $arf_pre_dup_type == 'current_user') {
                global $user_ID;

                if($user_ID){
                    $pre_dup_form_data = $wpdb->get_results($wpdb->prepare('SELECT * FROM  ' .$MdlDb->entries.' WHERE form_id = %d', $form_id)); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Reason: $MdlDb->entries is table name defined globally. False Positive alarm

                    if(!empty($pre_dup_form_data)){
                        foreach ($pre_dup_form_data as $pre_dup_form) {

                            if(isset($pre_dup_form->user_id) && $pre_dup_form->user_id != '' && $user_ID != '' && $pre_dup_form->user_id == $user_ID)
                            {
                                $arf_errors['arf_message_error'] = $arf_pre_dup_msg;
                                break;
                            }

                        }
                    }
                }
            }
        }
        return $arf_errors;
    }

    function arf_prevent_duplicate_before_form_render($arf_form,$form) {
        if( !isset( $form->form_css['arfsuccessmsgposition'] ) || ( isset($form->form_css['arfsuccessmsgposition']) && $form->form_css['arfsuccessmsgposition'] != 'bottom' ) ){
            global $wpdb,$MdlDb;

            $form_id2 = $form->id;

                $form_data2 = wp_cache_get('arf_form_data_'.$form_id2);
                if( false == $form_data2){
                    $form_data2 = $wpdb->get_row($wpdb->prepare('SELECT * FROM '.$MdlDb->forms.' WHERE id = %d', $form_id2), 'ARRAY_A'); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Reason: $MdlDb->forms is table name defined globally. False Positive alarm
                    wp_cache_set('arf_form_data_'.$form_id2, $form_data2);
                }

            if (count($form_data2) < 1){
                return $arf_form;
            }   

            $form_options = maybe_unserialize($form_data2['options']);

            if (isset($form_options['arf_pre_dup_check']) && $form_options['arf_pre_dup_check'] == 1) {
                return $arf_form.'<div id="arf_message_error" class="frm_error_style" style="display:none;"><div class="msg-detail"><div class="msg-description-success">' . addslashes(esc_html__('Prevent Duplicate Entry', 'ARForms')) . '</div></div></div>';
            }
        }
        return $arf_form;
    }

    function arf_prevent_duplicate_after_form_render($arf_form, $form, $arf_data_uniq_id, $arfbrowser_name, $browser_info) {

        if( isset($form->form_css['arfsuccessmsgposition']) && $form->form_css['arfsuccessmsgposition'] == 'bottom'){
            global $wpdb,$MdlDb;
            $form_id2 = $form->id;

            $form_data2 = wp_cache_get('arf_form_data_'.$form_id2);
            if( false == $form_data2){
                $form_data2 = $wpdb->get_row($wpdb->prepare('SELECT * FROM '.$MdlDb->forms.' WHERE id = %d', $form_id2), 'ARRAY_A'); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Reason: $MdlDb->forms is table name defined globally. False Positive alarm
                wp_cache_set('arf_form_data_'.$form_id2, $form_data2);
            }

            if (count($form_data2) < 1){
                return $arf_form;
            }

            $form_options = maybe_unserialize($form_data2['options']);

            if (isset($form_options['arf_pre_dup_check']) && $form_options['arf_pre_dup_check'] == 1) {
                return $arf_form.'<div id="arf_message_error" class="frm_error_style" style="display:none;"><div class="msg-detail"><div class="msg-description-success">' . addslashes(esc_html__('Prevent Duplicate Entry', 'ARForms')) . '</div></div></div>';
            }
        }
        return $arf_form;
    }


}
?>