<?php

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

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

        /*
         * Add a fee.
         */
        function add(){
            global $wpdb;
            global $DOPBSP;
            global $DOT;

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

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->insert($DOPBSP->tables->fees,
                          array('user_id'     => wp_get_current_user()->ID,
                                'name'        => $DOPBSP->text('FEES_ADD_FEE_NAME'),
                                'translation' => $DOPBSP->classes->translation->encodeJSON('FEES_ADD_FEE_LABEL')));

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

            die();
        }

        /*
         * Prints out the fee.
         *
         * @post id (integer): fee ID
         * @post language (string): fee current editing language
         *
         * @return fee HTML
         */
        function display(){
            global $DOT;
            global $DOPBSP;

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

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }

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

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

            die();
        }

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

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

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }

            $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
                $fee_data = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id=%d',
                                                          $DOPBSP->tables->fees,
                                                          $id));

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

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

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

            die();
        }

        /*
         * Delete fee.
         *
         * @post id (integer): fee ID
         *
         * @return number of fees left
         */
        function delete(){
            global $DOT;
            global $wpdb;
            global $DOPBSP;

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

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }

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

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

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

            die();
        }
    }
}