<?php

/*
* Title                   : Pinpoint Booking System WordPress Plugin (PRO)
* Version                 : 2.1.1
* File                    : includes/discounts/class-backend-discount-item-rule.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 discount item rule PHP class.
*/

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

        /*
         * Add discount item rule.
         *
         * @post item_id (integer): item ID
         * @post position (integer): item rule position
         * @post language (string): current item rule language
         *
         * @return new item 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.
             */

            $item_id = $DOT->post('item_id',
                                  'int');
            $position = $DOT->post('position',
                                   'int');
            $language = $DOT->post('language');

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->insert($DOPBSP->tables->discounts_items_rules,
                          array('discount_item_id' => $item_id,
                                'position'         => $position));
            $id = $wpdb->insert_id;
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $rule = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id=%d',
                                                  $DOPBSP->tables->discounts_items_rules,
                                                  $id));

            $DOPBSP->views->backend_discount_item_rule->template(array('rule'     => $rule,
                                                                       'language' => $language));

            die();
        }

        /*
         * Edit discount item rule.
         *
         * @post id (integer): item rule ID
         * @post field (string): item rule field
         * @post value (string): item rule value
         * @post language (string): discount 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
                $item_data = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id=%d',
                                                           $DOPBSP->tables->discounts_items_rules,
                                                           $id));

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

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

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

            die();
        }

        /*
         * Delete discount item rule.
         *
         * @post id (integer): item rule 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->discounts_items_rules,
                          array('id' => $id));

            die();
        }
    }
}