<?php

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

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

        /*
         * Add a coupon.
         */
        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->coupons,
                          array('user_id'     => wp_get_current_user()->ID,
                                'name'        => $DOPBSP->text('COUPONS_ADD_COUPON_NAME'),
                                'translation' => $DOPBSP->classes->translation->encodeJSON('COUPONS_ADD_COUPON_LABEL')));

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

            die();
        }

        /*
         * Prints out the coupon.
         *
         * @post id (integer): coupon ID
         * @post language (string): coupon current editing language
         *
         * @return coupon 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_coupon->template(array('id'       => $id,
                                                           'language' => $language));

            die();
        }

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

                $translation = json_decode($coupon_data->translation);
                $translation->{$language} = $value;

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

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

            die();
        }

        /*
         * Delete coupon.
         *
         * @post id (integer): coupon ID
         *
         * @return number of coupons 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 coupon.
             */
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->delete($DOPBSP->tables->coupons,
                          array('id' => $id));
            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->get_results($wpdb->prepare('SELECT * FROM %i ORDER BY id DESC',
                                              $DOPBSP->tables->coupons));

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

            die();
        }

        /*
         * Update coupon availability.
         *
         * @param id (integer): coupon ID
         * @param action (string): "use" or "restore" coupon
         */
        function update($id,
                        $action){
            global $wpdb;
            global $DOPBSP;

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

            if ($coupon->no_coupons != ''){
                if ($action == 'use'){
                    $no_coupons = (int)$coupon->no_coupons-1;
                }
                else{
                    $no_coupons = (int)$coupon->no_coupons+1;
                }

                //phpcs:ignore WordPress.DB.DirectDatabaseQuery
                $wpdb->update($DOPBSP->tables->coupons,
                              array('no_coupons' => $no_coupons),
                              array('id' => $id));
            }
        }

        /*
         * Check if coupon is still available.
         *
         * @param id (integer): coupon ID
         *
         * @return true/false
         */
        function validate($id){
            global $wpdb;
            global $DOPBSP;

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

            if (($coupon->no_coupons == ''
                            || (int)$coupon->no_coupons>0)
                    && (int)$coupon->price>0){
                return true;
            }
            else{
                return false;
            }
        }
    }
}