HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/shootinschool/wp-content/plugins/shootin-school-plugin/customer_appointments.php
<?php

function render_siab_customer_appointments(){

    $user_id = $_GET['id'];
    global $wpdb;
    $instructor = $wpdb->get_row("SELECT display_name FROM ".DB_USERS." WHERE ID = $user_id");
    $query = "SELECT display_name FROM " . DB_USERS . " WHERE ID = $user_id";
    $user = $wpdb->get_row($query, ARRAY_A);
    class Customer_Appointment extends WP_List_Table
    {
        /**
         * Prepare the items for the table to process
         *
         * @return Void
         */
        public function prepare_items()
        {
            $columns = $this->get_columns();
            $hidden = $this->get_hidden_columns();
            $sortable = $this->get_sortable_columns();

            $data = $this->table_data();
            usort($data, array(&$this, 'sort_data'));

            $perPage = 10;
            $currentPage = $this->get_pagenum();
            $totalItems = count($data);

            $this->set_pagination_args(array(
                'total_items' => $totalItems,
                'per_page'    => $perPage
            ));

            $data = array_slice($data, (($currentPage - 1) * $perPage), $perPage);

            $this->_column_headers = array($columns, $hidden, $sortable);
            $this->items = $data;
        }

        /**
         * Override the parent columns method. Defines the columns to use in your listing table
         *
         * @return Array
         */
        public function get_columns()
        {
            $columns = array(
                'session_name' => 'Assigned Session',
                'appointment_date' => 'Appointment date',
                'appointment_time' => 'Appointment Time',
                'instructor_name' => 'Assigned Instructor',
                'is_cancelled' => 'Status',
                'actions' => 'Actions'
            );

            return $columns;
        }

        /**
         * Define which columns are hidden
         *
         * @return Array
         */
        public function get_hidden_columns()
        {
            return array();
        }

        /**
         * Define the sortable columns
         *
         * @return Array
         */
        public function get_sortable_columns()
        {
            // return array('display_name' => array('display_name', false));
            return array('appointment_date' => array('appointment_date', false),'appointment_time' => array('appointment_time', false));
        }

        /**
         * Get the table data
         *
         * @return Array
         */
        private function table_data()
        {
            global $wpdb;
            $user_id = $_GET['id'];
            $query = "SELECT * ,".DB_APPOINTMENTS.".id as appointments_id FROM "
             .DB_APPOINTMENTS." JOIN ".DB_USERS."
              as us ON us.ID = ".DB_APPOINTMENTS.".customer_id JOIN ".DB_PACKAGE_SESSIONS." as ps
               ON ps.id = ".DB_APPOINTMENTS.".session_id WHERE
                ".DB_APPOINTMENTS.".customer_id = $user_id";
            $data = $wpdb->get_results($query, ARRAY_A);

            foreach($data as $key => $dt){
                $instructor_id = $dt['instructor_id'];
                $query = "SELECT * FROM ".DB_USERS." WHERE ID = $instructor_id";
                $inst = $wpdb->get_row($query);
                if($inst){
                    $data[$key]['instructor_name'] = $inst->display_name;
                }else{
                    $data[$key]['instructor_name'] = "- No Instructor Assigned -";
                }
            }

            return $data;
        }

        /**
         * Define what data to show on each column of the table
         *
         * @param  Array $item        Data
         * @param  String $column_name - Current column name
         *
         * @return Mixed
         */
        public function column_default($item, $column_name)
        {
            switch ($column_name) {
                case 'display_name':
                case 'instructor_name':
                case 'session_name':
                    return $item[$column_name];
                case 'appointment_date':
                    return date('l, F d Y ', strtotime($item[$column_name]));
                case 'appointment_time':
                    if($item[$column_name] !=""){
                        $split = explode(' - ',$item[$column_name]);
                    return date('g:i A', strtotime($split[0]))." - ".date('g:i A', strtotime($split[1]));
                    }else{
                        $split = explode(' - ',$item['cust_appointment_time']);
                        return date('g:i A', strtotime($split[0]))." - ".date('g:i A', strtotime($split[1]));
                    }
                case 'is_cancelled':
                    if($item['is_approved'] == 0 && $item['is_cancelled'] == 0 && $item['instructor_id'] != NULL ){
                        return "Instructor Approval pending";
                    }else if ($item['is_cancelled'] == 0){
                        if($item['instructor_id'] == NULL ){
                            return "Booked";
                        } else if($item['has_attended'] != NULL && $item['has_attended'] == 1){
                            return "Attended";
                        } else if($item['has_attended'] != NULL && $item['has_attended'] == 0){
                            return "Not Attended";
                        } else{
                            return " Instructor Assigned";
                        }
                    } else {
                        return "Cancelled";
                    }
                default:
                    return print_r($item, true);
            }
        }

        function column_actions($item){

            if ($item["is_cancelled"] == 0 && $item['has_attended'] == NULL ){
                $actions = array(
                    'Cancel' => sprintf('<button class="btn btn-danger btn-xs" onclick="openCancelModal(' . $item["appointments_id"] . ')">Cancel</button>'),

                );
            } else {
                echo '- Not Available -';
            }

            return sprintf(
                // '%1$s <span style="color:silver">(Edit:%2$s)</span>%3$s',
                /*$1%s*/
                // $item['title'],
                /*$2%s*/
                // $item['id'],
                /*$3%s*/
                $this->row_actions($actions)
            );
        }

        /**
         * Allows you to sort the data by the variables set in the $_GET
         *
         * @return Mixed
         */
        private function sort_data($a, $b)
        {
            // Set defaults
            // $orderby = 'id';
            // $order = 'asc';
            $orderby = 'appointment_date';
            $order = 'desc';

            // If orderby is set, use this as the sort column
            if (!empty($_GET['orderby'])) {
                $orderby = $_GET['orderby'];
            }

            // If order is set use this as the order
            if (!empty($_GET['order'])) {
                $order = $_GET['order'];
            }

            $result = strcmp($a[$orderby], $b[$orderby]);

            if ($order === 'asc') {
                return $result;
            }

            return -$result;
        }
    }

    /**
     * Display the list table page
     *
     * @return Void
     */

    $importedListTable = new Customer_Appointment();
    $importedListTable->prepare_items();
     ?>
     <div class="css_loader">Loading&#8230;</div>
    <div class="wrap">
        <div class="alert alert-info" role="alert">
            <h3> Appointment Booking History </h3>
        </div>
    </div>
    <div class="wrap">
        <div id="icon-users" class="icon32"></div>
        <h2>All Bookings of <?php echo $user['display_name'] ?></h2>
        <?php $importedListTable->display(); ?>
    </div>

    <div class="modal fade" id="appointmentModal" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Cancel Appointment</h4>
                </div>
                <div class="modal-body">
                    <div class="cancel-msg"></div>
                    <p>Are you sure want to cancel this Customer Appointment ?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" id="cancelbtn" data-appointment_id="" class="btn btn-primary" onclick="cancelAppoint(this)">Yes</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                </div>
            </div>
        </div>
    </div>
<?php
}