<?php

/*
* Title                   : Pinpoint Booking System WordPress Plugin (PRO)
* Version                 : 2.1.1
* File                    : includes/forms/class-backend-form-field.php
* File Version            : 1.0.4
* Created / Last Modified : 25 August 2015
* Author                  : Dot on Paper
* Copyright               : © 2012 Dot on Paper
* Website                 : http://www.dotonpaper.net
* Description             : Back end form field PHP class.
*/

if (!class_exists('DOPBSPBackEndFormField')){
    class DOPBSPBackEndFormField{
        /*
         * Constructor
         */
        function __construct(){
        }

        /*
         * Add form field.
         *
         * @post form_id (integer): form ID
         * @post type (string): field type
         * @post position (integer): field position
         * @post language (string): current field language
         *
         * @return new field HTML
         */
        function add(){
            global $wpdb;
            global $DOPBSP;
            global $DOT;

            /*
             * Verify nonce.
             */
            $nonce = $DOT->post('nonce');

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }
            /*
             * End verify nonce.
             */

            $form_id = $DOT->post('form_id',
                                  'int');
            $type = $DOT->post('type');
            $position = $DOT->post('position',
                                   'int');
            $language = $DOT->post('language');
            $key = '';

            switch ($type){
                case 'checkbox':
                    $key = 'FORMS_FORM_ADD_FIELD_CHECKBOX_LABEL';
                    break;
                case 'select':
                    $key = 'FORMS_FORM_ADD_FIELD_SELECT_LABEL';
                    break;
                case 'text':
                    $key = 'FORMS_FORM_ADD_FIELD_TEXT_LABEL';
                    break;
                case 'textarea':
                    $key = 'FORMS_FORM_ADD_FIELD_TEXTAREA_LABEL';
                    break;
            }
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->insert($DOPBSP->tables->forms_fields,
                          array('form_id'            => $form_id,
                                'type'               => $type,
                                'position'           => $position,
                                'allowed_characters' => '',
                                'translation'        => $DOPBSP->classes->translation->encodeJSON($key)));
            $id = $wpdb->insert_id;
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $field = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id=%d',
                                                   $DOPBSP->tables->forms_fields,
                                                   $id));

            switch ($type){
                case 'checkbox':
                    $DOPBSP->views->backend_form_field->templateCheckbox(array('field'    => $field,
                                                                               'language' => $language));
                    break;
                case 'select':
                    $DOPBSP->views->backend_form_field->templateSelect(array('field'    => $field,
                                                                             'language' => $language));
                    break;
                case 'text':
                    $DOPBSP->views->backend_form_field->templateText(array('field'    => $field,
                                                                           'language' => $language));
                    break;
                case 'textarea':
                    $DOPBSP->views->backend_form_field->templateTextarea(array('field'    => $field,
                                                                               'language' => $language));
                    break;
            }

            die();
        }

        /*
         * Edit form field.
         *
         * @post id (integer): form field ID
         * @post field (string): form field
         * @post value (string): form field value
         * @post language (string): form selected language
         */
        function edit(){
            global $wpdb;
            global $DOPBSP;
            global $DOT;

            /*
             * Verify nonce.
             */
            $nonce = $DOT->post('nonce');

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }
            /*
             * End verify nonce.
             */

            $id = $DOT->post('id',
                             'int');
            $field = $DOT->post('field');
            $value = $DOT->post('value');
            $language = $DOT->post('language');

            if ($field == 'label'){
                $value = str_replace("\n",
                                     '<<new-line>>',
                                     $value);
                $value = str_replace("\'",
                                     '<<single-quote>>',
                                     $value);
                $value = mb_convert_encoding($value,
                                             'UTF-8',
                                             'ISO-8859-1');

                //phpcs:ignore WordPress.DB.DirectDatabaseQuery
                $field_data = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id=%d',
                                                            $DOPBSP->tables->forms_fields,
                                                            $id));

                $translation = json_decode($field_data->translation);
                $translation->$language = $value;

                $value = json_encode($translation);
                $field = 'translation';
            }

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->update($DOPBSP->tables->forms_fields,
                          array($field => $value),
                          array('id' => $id));

            die();
        }

        /*
         * Delete form field.
         *
         * @post id (integer): form field ID
         */
        function delete(){
            global $wpdb;
            global $DOPBSP;
            global $DOT;

            /*
             * Verify nonce.
             */
            $nonce = $DOT->post('nonce');

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }
            /*
             * End verify nonce.
             */

            $id = $DOT->post('id',
                             'int');

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->delete($DOPBSP->tables->forms_fields,
                          array('id' => $id));
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->delete($DOPBSP->tables->forms_fields_options,
                          array('field_id' => $id));

            die();
        }
    }
}