<?php

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

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

        /*
         * Add a discount.
         */
        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.
             */

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->insert($DOPBSP->tables->discounts,
                          array('user_id' => wp_get_current_user()->ID,
                                'name'    => $DOPBSP->text('DISCOUNTS_ADD_DISCOUNT_NAME')));

            $DOPBSP->classes->backend_discounts->display();

            die();
        }

        /*
         * Prints out the discount.
         *
         * @post id (integer): discount ID
         * @post language (string): discount current editing language
         *
         * @return discount HTML
         */
        function display(){
            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');
            $language = $DOT->post('language');

            $DOPBSP->views->backend_discount->template(array('id'       => $id,
                                                             'language' => $language));
            $DOPBSP->views->backend_discount_items->template(array('id'       => $id,
                                                                   'language' => $language));

            die();
        }

        /*
         * Edit discount fields.
         *
         * @post id (integer): discount ID
         * @post field (string): discount field
         * @post value (string): item new value
         */
        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.
             */

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

            die();
        }

        /*
         * Delete discount.
         *
         * @post id (integer): discount ID
         *
         * @return number of discounts left
         */
        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');

            /*
             * Delete discount.
             */
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->delete($DOPBSP->tables->discounts,
                          array('id' => $id));

            /*
             * Delete discount items.
             */
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $items = $wpdb->get_results($wpdb->prepare('SELECT * FROM %i WHERE discount_id=%d',
                                                       $DOPBSP->tables->discounts_items,
                                                       $id));
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->delete($DOPBSP->tables->discounts_items,
                          array('discount_id' => $id));

            /*
             * Delete discount items rules.
             */
            foreach ($items as $item){
                //phpcs:ignore WordPress.DB.DirectDatabaseQuery
                $wpdb->delete($DOPBSP->tables->discounts_items_rules,
                              array('discount_item_id' => $item->id));
            }

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->get_results($wpdb->prepare('SELECT * FROM %i ORDER BY id DESC',
                                              $DOPBSP->tables->discounts));

            $DOT->echo($wpdb->num_rows,
                       'int');

            die();
        }
    }
}