<?php

/*
* Title                   : Pinpoint Booking System WordPress Plugin (PRO)
* File                    : includes/smses/class-backend-smses.php
* Author                  : PINPOINT.WORLD
* Copyright               : © 2018 PINPOINT.WORLD
* Website                 : http://www.pinpoint.world
* Description             : Back end smses PHP class.
*/

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

        /*
         * Prints out the SMSes page.
         *
         * @return HTML page
         */
        function view(){
            global $DOPBSP;

            $DOPBSP->views->backend_smses->template();
        }

        /*
         * Display SMSes list.
         *
         * @return SMSes list HTML
         */
        function display(){
            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.
             */

            $html = array();
            $user_roles = array_values(wp_get_current_user()->roles);

            if ($user_roles[0] == 'administrator'){
                //phpcs:ignore WordPress.DB.DirectDatabaseQuery
                $smses = $wpdb->get_results($wpdb->prepare('SELECT * FROM %i ORDER BY id DESC',
                                                           $DOPBSP->tables->smses));
            }
            else{
                //phpcs:ignore WordPress.DB.DirectDatabaseQuery
                $smses = $wpdb->get_results($wpdb->prepare('SELECT * FROM %i WHERE user_id=%d OR user_id=0 ORDER BY id DESC',
                                                           $DOPBSP->tables->smses,
                                                           wp_get_current_user()->ID));
            }

            /*
             * Create SMSes list HTML.
             */
            $html[] = '<ul>';

            if ($wpdb->num_rows != 0){
                if ($smses){
                    foreach ($smses as $sms){
                        $sms->name = esc_js($sms->name);
                        $html[] = $this->listItem($sms);
                    }
                }
            }
            else{
                $html[] = '<li class="dopbsp-no-data">'.$DOPBSP->text('SMSES_NO_SMS').'</li>';
            }
            $html[] = '</ul>';

            $DOT->echo(implode('',
                               $html),
                       'content',
                       $DOT->models->allowed_html->items());

            die();
        }

        /*
         * Returns SMSes list item HTML.
         *
         * @param SMS (object): SMS data
         *
         * @return SMS list item HTML
         */
        function listItem($sms){
            global $DOPBSP;

            $html = array();
            $user = get_userdata($sms->user_id); // Get data about the user who created the SMSes.

            $html[] = '<li class="dopbsp-item" id="DOPBSP-sms-ID-'.$sms->id.'" onclick="DOPBSPBackEndSms.display('.$sms->id.')">';
            $html[] = ' <div class="dopbsp-header">';

            /*
             * Display SMS ID.
             */
            $html[] = '     <span class="dopbsp-id">ID: '.$sms->id.'</span>';

            /*
             * Display data about the user who created the SMS.
             */
            if ($sms->user_id>0){
                $html[] = '     <span class="dopbsp-header-item dopbsp-avatar">'.get_avatar($sms->user_id,
                                                                                            17);
                $html[] = '         <span class="dopbsp-info">'.$DOPBSP->text('SMSES_CREATED_BY').': '.$user->data->display_name.'</span>';
                $html[] = '         <br class="dopbsp-clear" />';
                $html[] = '     </span>';
            }
            $html[] = '     <br class="dopbsp-clear" />';
            $html[] = ' </div>';
            $html[] = ' <div class="dopbsp-name">'.($sms->name == ''
                            ? '&nbsp;'
                            : $sms->name).'</div>';
            $html[] = '</li>';

            return implode('',
                           $html);
        }
    }
}