<?php

/*
* Title                   : Pinpoint Booking System WordPress Plugin (PRO)
* Version                 : 2.1.2
* File                    : includes/class-frontend.php
* File Version            : 1.1.1
* Created / Last Modified : 08 November 2015
* Author                  : Dot on Paper
* Copyright               : © 2012 Dot on Paper
* Website                 : http://www.dotonpaper.net
* Description             : Front end PHP class.
*/

if (!class_exists('DOPBSPFrontEnd')){
    class DOPBSPFrontEnd{
        /*
         * Constructor
         */
        function __construct(){
            add_action('wp_enqueue_scripts',
                       array(&$this,
                             'addStyles'));
            add_action('wp_enqueue_scripts',
                       array(&$this,
                             'addScripts'));

            add_shortcode('dopbsp',
                          array(&$this,
                                'shortcode'));
        }

        /*
         * Add plugin's CSS files.
         */
        function addStyles(){
            global $DOPBSP;

            /*
             * Register styles.
             */
            wp_register_style('DOPBSP-css-dopselect',
                              $DOPBSP->paths->url.'libraries/css/jquery.dop.Select.css',
                              [],
                              DOT_VERSION);

            /*
             * Enqueue styles.
             */
            wp_enqueue_style('DOPBSP-css-dopselect');
        }

        /*
         * Add plugin's JavaScript files.
         */
        function addScripts(){
            global $DOPBSP;

            /*
             * Register JavaScript.
             */

            /*
             * Libraries.
             */
            wp_register_script('DOP-js-prototypes',
                               $DOPBSP->paths->url.'libraries/js/dop-prototypes.js',
                               array('jquery'),
                               DOT_VERSION);
            wp_register_script('DOP-js-jquery-dopselect',
                               $DOPBSP->paths->url.'libraries/js/jquery.dop.Select.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             * Calendar & search
             */
            wp_register_script('DOPBSP-js-frontend-calendar',
                               $DOPBSP->paths->url.'assets/js/jquery.dop.frontend.BSPCalendar.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('DOPBSP-js-frontend-search',
                               $DOPBSP->paths->url.'assets/js/jquery.dop.frontend.BSPSearch.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('DOPBSP-js-frontend-search-widget',
                               $DOPBSP->paths->url.'assets/js/jquery.dop.frontend.BSPSearchWidget.js',
                               array('jquery'),
                               DOT_VERSION,
                               false);

            /*
             * Front end.
             */
            wp_register_script('DOPBSP-js-frontend',
                               $DOPBSP->paths->url.'assets/js/frontend.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             * Deposit
             */
            wp_register_script('DOPBSP-js-frontend-deposit',
                               $DOPBSP->paths->url.'assets/js/deposit/frontend-deposit.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             * Discounts
             */
            wp_register_script('DOPBSP-js-frontend-discounts',
                               $DOPBSP->paths->url.'assets/js/discounts/frontend-discounts.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             * Fees
             */
            wp_register_script('DOPBSP-js-frontend-fees',
                               $DOPBSP->paths->url.'assets/js/fees/frontend-fees.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             * Rules
             */
            wp_register_script('DOPBSP-js-frontend-rules',
                               $DOPBSP->paths->url.'assets/js/rules/frontend-rules.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             * Framework
             */
            wp_register_script('dot-js',
                               $DOPBSP->paths->url.'application/assets/js/dot.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('dot-js-calendar',
                               $DOPBSP->paths->url.'application/assets/js/calendars/calendar.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('dot-js-calendar-availability',
                               $DOPBSP->paths->url.'application/assets/js/calendars/calendar-availability.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('dot-js-calendar-days',
                               $DOPBSP->paths->url.'application/assets/js/calendars/calendar-days.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('dot-js-calendar-day',
                               $DOPBSP->paths->url.'application/assets/js/calendars/calendar-day.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);
            wp_register_script('dot-js-calendar-schedule',
                               $DOPBSP->paths->url.'application/assets/js/calendars/calendar-schedule.js',
                               array('jquery'),
                               DOT_VERSION,
                               true);

            /*
             *  Enqueue JavaScript.
             */

            /*
             * Libraries.
             */
            if (!wp_script_is('jquery',
                              'queue')){
                wp_enqueue_script('jquery');
            }

            if (!wp_script_is('jquery-ui-datepicker',
                              'queue')){
                wp_enqueue_script('jquery-ui-datepicker');
            }

            if (!wp_script_is('jquery-ui-slider',
                              'queue')){
                wp_enqueue_script('jquery-ui-slider');
            }

            wp_enqueue_script('DOP-js-prototypes');
            wp_enqueue_script('DOP-js-jquery-dopselect');

            /*
             * Calendar & search
             */
            wp_enqueue_script('DOPBSP-js-frontend-calendar');
            wp_enqueue_script('DOPBSP-js-frontend-search');
            wp_enqueue_script('DOPBSP-js-frontend-search-widget');

            /*
             * Front end.
             */
            wp_enqueue_script('DOPBSP-js-frontend');

            /*
             * Deposit
             */
            wp_enqueue_script('DOPBSP-js-frontend-deposit');

            /*
             * Discounts
             */
            wp_enqueue_script('DOPBSP-js-frontend-discounts');

            /*
             * Fees
             */
            wp_enqueue_script('DOPBSP-js-frontend-fees');

            /*
             * Rules
             */
            wp_enqueue_script('DOPBSP-js-frontend-rules');

            /*
             * Framework
             */
            wp_enqueue_script('dot-js');
            wp_enqueue_script('dot-js-calendar');
            wp_enqueue_script('dot-js-calendar-availability');
            wp_enqueue_script('dot-js-calendar-days');
            wp_enqueue_script('dot-js-calendar-day');
            wp_enqueue_script('dot-js-calendar-schedule');
        }

        /*
         * Initialize shortcode.
         *
         * @param atts (array): shortcode attributes
         */
        function shortcode($atts){
            global $DOPBSP;
            global $post;

            extract(shortcode_atts(array('class' => 'dopbsp'),
                                   $atts));

            if (!array_key_exists('item',
                                  $atts)){
                $atts['item'] = 'calendar';
            }

            if (!array_key_exists('id',
                                  $atts)){
                $atts['id'] = 1;
            }

            if (!array_key_exists('view',
                                  $atts)){
                $atts['view'] = 'false';
            }

            if (!array_key_exists('lang',
                                  $atts)){
                $atts['lang'] = DOPBSP_CONFIG_TRANSLATION_DEFAULT_LANGUAGE;
            }

            if (defined('ICL_LANGUAGE_CODE')){
                $atts['lang'] = ICL_LANGUAGE_CODE;
            }

            function_exists('pll_current_language')
                    ? $atts['lang'] = pll_current_language()
                    : false;

            $atts['lang'] = substr($atts['lang'],
                                   0,
                                   2);

            if (!array_key_exists('woocommerce',
                                  $atts)){
                $atts['woocommerce'] = 'false';
            }

            if (!array_key_exists('woocommerce_add_to_cart',
                                  $atts)){
                $atts['woocommerce_add_to_cart'] = 'false';
            }

            if (!array_key_exists('woocommerce_position',
                                  $atts)){
                $atts['woocommerce_position'] = 'summary';
            }

            if (!array_key_exists('woocommerce_product_id',
                                  $atts)){
                $atts['woocommerce_product_id'] = 0;
            }

            switch ($atts['item']){
                case 'search':
                    $content = $DOPBSP->classes->frontend_search->display($atts);
                    break;
                case 'search-widget':
                    $content = $DOPBSP->classes->frontend_search->displayWidget($atts);
                    break;
                default:
                    $atts['lang'] = get_post_meta($post->ID,
                                                  'dopbsp_woocommerce_language',
                                                  true) == ''
                            ? $atts['lang']
                            : get_post_meta($post->ID,
                                            'dopbsp_woocommerce_language',
                                            true);

                    if (defined('ICL_LANGUAGE_CODE')){
                        $atts['lang'] = ICL_LANGUAGE_CODE;
                    }

                    function_exists('pll_current_language')
                            ? $atts['lang'] = pll_current_language()
                            : false;

                    $atts['lang'] = substr($atts['lang'],
                                           0,
                                           2);
                    return $DOPBSP->classes->frontend_calendar->display($atts);
            }

            return $content;
        }
    }
}