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/list_customers.php
<?php

if (!class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
global $postNumber;
$postNumber = 1;


function render_siab_customers(){

    class Customer_List_Table 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(
                'id' => 'Customer ID',
                'display_name'    => 'Customer Name',
                'phone' => 'Phone Number',
                'session_count'    => 'Number of sessions attended',
                'user_email' => 'Email',
                '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('session_count' => array('session_count', false));
        }

        /**
         * Get the table data
         *
         * @return Array
         */
        private function table_data()
        {
            global $wpdb;
            
            if (!empty($_GET['search'])) 
            {
                $cust_name =$_GET['search'];
				$orderby = 'session_count';
				$order = 'desc';
				if (!empty($_GET['orderby'])) {
                	$orderby = $_GET['orderby'];
            	}
			 // If order is set use this as the order
				if (!empty($_GET['order'])) {
					$order = $_GET['order'];
				}
                // $query = "SELECT DISTINCT ".DB_USERS.".*,COUNT(".DB_APPOINTMENTS.".customer_id ) as session_count FROM " . DB_USERS .
                // " LEFT JOIN ".DB_APPOINTMENTS."  ON " . DB_APPOINTMENTS . ".customer_id=" . DB_USERS . ".ID AND " . DB_APPOINTMENTS . ".has_attended=1 AND " . DB_APPOINTMENTS . ".instructor_id IS NOT NULL
                // WHERE ". DB_USERS . ".ID IN(SELECT ".DB_WC_GF_CUSTOMER_PURCHASES.".customer_id FROM ".DB_WC_GF_CUSTOMER_PURCHASES." GROUP BY ".DB_WC_GF_CUSTOMER_PURCHASES.".customer_id)  AND " . DB_USERS.".display_name LIKE '%".$cust_name."%'
                // GROUP BY ". DB_USERS . ".ID ORDER BY ".$orderby." ".$order;
                $query = "SELECT DISTINCT ".DB_USERS.".*,COUNT(".DB_APPOINTMENTS.".customer_id ) as session_count FROM " . DB_USERS .
                " LEFT JOIN ".DB_APPOINTMENTS."  ON " . DB_APPOINTMENTS . ".customer_id=" . DB_USERS . ".ID AND " . DB_APPOINTMENTS . ".has_attended=1 AND " . DB_APPOINTMENTS . ".instructor_id IS NOT NULL
                WHERE " . DB_USERS.".display_name LIKE '%".$cust_name."%'
                GROUP BY ". DB_USERS . ".ID ORDER BY ".$orderby." ".$order;
				
            }else{

                // $query = "SELECT DISTINCT *,COUNT(".DB_APPOINTMENTS.".customer_id) as session_count FROM " . DB_USERS . " JOIN " . DB_WC_GF_CUSTOMER_PURCHASES . "
                // ON " . DB_WC_GF_CUSTOMER_PURCHASES . ".customer_id=" . DB_USERS . ".ID 
                // JOIN ".DB_APPOINTMENTS."  ON " . DB_APPOINTMENTS . ".customer_id=" . DB_USERS . ".ID
                // GROUP BY ". DB_USERS . ".ID";
				$orderby = 'session_count';
				$order = 'desc';
				  if (!empty($_GET['orderby'])) {
                $orderby = $_GET['orderby'];
            }

            // If order is set use this as the order
            if (!empty($_GET['order'])) {
                $order = $_GET['order'];
            }
                // $query = "SELECT DISTINCT ".DB_USERS.".*,COUNT(".DB_APPOINTMENTS.".customer_id ) as session_count FROM " . DB_USERS .
                // " LEFT JOIN ".DB_APPOINTMENTS."  ON " . DB_APPOINTMENTS . ".customer_id=" . DB_USERS . ".ID AND " . DB_APPOINTMENTS . ".has_attended=1 AND " . DB_APPOINTMENTS . ".instructor_id IS NOT NULL
                // WHERE ". DB_USERS . ".ID IN(SELECT ".DB_WC_GF_CUSTOMER_PURCHASES.".customer_id FROM ".DB_WC_GF_CUSTOMER_PURCHASES." GROUP BY ".DB_WC_GF_CUSTOMER_PURCHASES.".customer_id) 
                // GROUP BY ". DB_USERS . ".ID ORDER BY ".$orderby." ".$order;
                $query = "SELECT DISTINCT ".DB_USERS.".*,COUNT(".DB_APPOINTMENTS.".customer_id ) as session_count FROM " . DB_USERS .
                " LEFT JOIN ".DB_APPOINTMENTS."  ON " . DB_APPOINTMENTS . ".customer_id=" . DB_USERS . ".ID AND " . DB_APPOINTMENTS . ".has_attended=1 AND " . DB_APPOINTMENTS . ".instructor_id IS NOT NULL
                GROUP BY ". DB_USERS . ".ID ORDER BY ".$orderby." ".$order;
               
            }
            $data = $wpdb->get_results($query, ARRAY_A);
			//echo $wpdb->last_query;
            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 'session_count':
                    return $item[$column_name];
                    // global $wpdb;
                    // $cid = $item['ID'];
                    // $attended = 1;
                  
                    // $ipquery = $wpdb->get_results("SELECT * FROM ".DB_APPOINTMENTS." WHERE customer_id = $cid AND has_attended = '$attended' AND instructor_id IS NOT NULL");
                    
                    // return count($ipquery);
                   
                case 'id':
                    global $wpdb;
                    $cid = $item['ID'];
                    $childrenIDs = $wpdb->get_col("SELECT id FROM ".DB_CHILD_DETAILS." WHERE user_id = $cid");
                    
                    // echo $wpdb->last_query;
                    // echo "<pre>";
                    // print_r($childrenIDs);
                //   $pendingMsgChecks = $wpdb->get_results("SELECT * FROM " . DB_FORUM . " WHERE playerID IN (" . implode(',', $childrenIDs) . ") AND userType = 3 AND status = 0");
                  //$pendingMsgChecks = $wpdb->get_var("SELECT COUNT(*) FROM " . DB_FORUM . " WHERE playerID IN (" . implode(',', $childrenIDs) . ") AND userType = 3 AND staus = 0");
				  //$pendingMsgChecks = $wpdb->get_var("SELECT COUNT(*) FROM " . DB_FORUM . " WHERE playerID IN (" . implode(',', $childrenIDs) . ") AND userType IN (2,3) AND status = 0");
				  $pendingMsgChecks = $wpdb->get_var("SELECT COUNT(*) FROM " . DB_FORUM . " WHERE playerID IN (" . implode(',', $childrenIDs) . ") AND (userType = 2 OR userType = 3) AND staus = 0");


                    if($pendingMsgChecks > 0){
                        return $item['ID']."<img src='https://shootinschool.spericorn.com/wp-content/plugins/shootin-school-plugin/custom/img/new.gif' alt='' style='width: 20px; height: 20px;'>";
                    }else{

                        return $item['ID'];
                    }

                case 'phone': 
                    return get_user_meta( $item['ID'], 'billing_billing_phone', true );
                case 'display_name': 
               
               
                case 'user_email':
                    return $item[$column_name];
                default:
                    return print_r($item, true);
            }
            $postNumber++;
        }

        function column_actions($item)
        {
            $isBwc = get_user_meta($item['ID'], 'isBwc', true);
            $isselfregister = get_user_meta($item['ID'], 'isselfregister', true);
            if ($isBwc == 1) {
                if ($isselfregister == 1) {
                    $actions = array(
                        'view_purchases' => sprintf('<a href="?page=siab-customer-credits&id=' . $item['ID'] . '">View Purchases</a>'),
                        'view_history' => sprintf('<a href="?page=customer-history&id=' . $item['ID'] . '">View Appointments</a>'),
                        'view_children' => sprintf('<br><a href="?page=siab-customer-children&id=' . $item['ID'] . '">Manage Children</a>'),
                        'mark_bwc' => sprintf('<br><button class="btb btn-xs btn-warning" onclick="unmarkasbwc(' . $item["ID"] . ')">Unmark as BWC Customer</a>'),
                        'mark_selfregister' => sprintf('<br><button class="btb btn-xs btn-warning" style="margin-top:5px;" onclick="unmarkasselfregister(' . $item["ID"] . ')">Unmark as Self Direction Customer</a>'),
                    );
                } else {
                    $actions = array(
                        'view_purchases' => sprintf('<a href="?page=siab-customer-credits&id=' . $item['ID'] . '">View Purchases</a>'),
                        'view_history' => sprintf('<a href="?page=customer-history&id=' . $item['ID'] . '">View Appointments</a>'),
                        'view_children' => sprintf('<br><a href="?page=siab-customer-children&id=' . $item['ID'] . '">Manage Children</a>'),
                        'mark_bwc' => sprintf('<br><button class="btb btn-xs btn-warning" onclick="unmarkasbwc(' . $item["ID"] . ')">Unmark as BWC Customer</a>'),
                        'mark_selfregister' => sprintf('<br><button class="btb btn-xs btn-success" style="margin-top:5px;" onclick="markasselfregister(' . $item["ID"] . ')">Mark as Self Direction Customer</a>'),
                    );
                }
            } else {
                $actions = array(
                    'view_purchases' => sprintf('<a href="?page=siab-customer-credits&id=' . $item['ID'] . '">View Purchases</a>'),
                    'view_history' => sprintf('<a href="?page=customer-history&id=' . $item['ID'] . '">View Appointments</a>'),
                    'view_children' => sprintf('<br><a href="?page=siab-customer-children&id=' . $item['ID'] . '">Manage Children</a>'),
                    'mark_bwc' => sprintf('<br><button class="btb btn-xs btn-success" onclick="markasbwc(' . $item["ID"] . ')">Mark as BWC Customer</a>'),
                );
            }
           

            // if ($item['credits']) {

                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 = 'session_count';
            $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 === 'desc') {
                return $result;
            }

            return -$result;
        }
    }
    $importedListTable = new Customer_List_Table();
    $importedListTable->prepare_items();

    $cust_name ='';

    if (!empty($_GET['search'])) {
        $cust_name = $_GET['search'];
    }
if(isset($_POST["Export"])){

									global $wpdb;
							
									 // Use headers so the data goes to a file and not displayed
									 header('Content-Type: text/csv');
									 header('Content-Disposition: attachment; filename="export.csv"');
							
									 // clean out other output buffers
									 ob_end_clean();
							
									 $fp = fopen('php://output', 'w');
							
									 // CSV/Excel header label
									 $header_row = array(
													0 => 'Parent Name',
													1 => 'Player Names',
													2 => 'Credits Remaining(Group)',
													3 => 'Credits Remaining(Individual)',
                                                                                                        4 => 'Is unlimited',
													5 => 'Cell Number',
													6 => 'Email',
													7 => 'Address',
													8 => 'City',
													9 => 'State',
													10 => 'Zip',
												);
									//write the header
									fputcsv($fp, $header_row);
							
									$query = "SELECT * FROM ".$wpdb->prefix . "users";
									$data = $wpdb->get_results($query,ARRAY_A);
							
									$final_data = array();
									foreach ($data as $key => $value) {
							
										$fname = get_user_meta( $value['ID'], 'first_name', true );
										$lname = get_user_meta( $value['ID'], 'last_name', true );
										$address_1 = get_user_meta( $value['ID'], 'billing_address_1', true );
										$address_2 = get_user_meta( $value['ID'], 'billing_address_2', true );
										$city = get_user_meta( $value['ID'], 'billing_city', true );
										$state = get_user_meta( $value['ID'], 'billing_state', true );
										$phone = get_user_meta( $value['ID'], 'billing_billing_phone', true );
										$postcode = get_user_meta( $value['ID'], 'billing_postcode', true );
							
										$fullname = $fname." ".$lname;
										// $fulladdress = $address_1.",".$address_2;
										$fulladdress = "";
										if((!empty($address_1)) && (!empty($address_2))){
											$fulladdress = $address_1.",".$address_2;
										}
										else if(!empty($address_1)){
											$fulladdress = $address_1;
										}
										else if(!empty($address_2)){
											$fulladdress = $address_2;
										}
							
										$query = "SELECT * FROM ".DB_CHILD_DETAILS." WHERE user_id=".$value['ID'];

										$query2 = "SELECT * FROM ".DB_WC_GF_CUSTOMER_PURCHASES." WHERE package_hidden_name = 'group' AND customer_id=".$value['ID'];

										$per_sessions = $wpdb->get_results($query2);
							
										$per_credits = 0;
										foreach ($per_sessions as $key4=> $p_itm) {
									
											$per_credits += $p_itm->credits;
										}

										$query3 = "SELECT * FROM ".DB_WC_GF_CUSTOMER_PURCHASES." WHERE package_hidden_name = 'individual' AND customer_id=".$value['ID'];

										$ind_sessions = $wpdb->get_results($query3);
							
										$ind_credits = 0;
										foreach ($ind_sessions as $key5=> $i_itm) {
											$ind_credits += $i_itm->credits;
										}
 $query4 = "SELECT * FROM ".DB_WC_GF_CUSTOMER_PURCHASES." WHERE package_hidden_name = 'unlimited' AND customer_id=".$value['ID'];

										$ul_session = $wpdb->get_results($query4);
							
										if(count($ul_session)>0){
                                            $ul = 1;
                                        }else{
                                            $ul = 0;
                                        }
										$children = $wpdb->get_results($query);
										$childs = "";
										foreach ($children as $key => $child) {
										$childs .=$child->first_name." ".$child->last_name.",";
										}
							
										$email =  $value['user_email'];
										// $mod_data = array(
										// 'Parent_Name' => $fname." ".$lname,
										// 'Player_Names' => $childs,
										// 'Cell_Number' => $phone,
										// // 'Email' => $value['user_email'],
										// 'Address' => $address_1.",".$address_2,
										// 'City' => $city,
										// 'State' => $state,
										// 'Zip' => $postcode
										// );
										// array_push($final_data,$mod_data);
							
										$OutputRecord = array(
														$fullname,
														$childs,
														$per_credits,
														$ind_credits,
                                                                                                                $ul,
														$phone,
														$email,
														$fulladdress,
														$city,
														$state,
														$postcode
													);  
										fputcsv($fp, $OutputRecord);  
									}
							
							
									fclose( $fp );
									exit; 
								}
								

    
    ?>

    <div class="wrap">

        <div class="alert alert-info" role="alert" style="border: 1px solid green;border-radius: 5px;">
            <h3> Customer Management </h3>
        </div>
            <div class="container" style="margin-top:19px">
            <input type="text" value="<?php echo $cust_name;?>" name="cust_name" id="cust_name" placeholder="Search by name">
            
            <button class="btn btn-primary btn-sm" type="button" onclick="ApplyCustNameFilter()">Search</button>
            <a class="btn btn-default btn-sm" href="admin.php?page=siab-customers"> Reset</a>
<div style="float: right;margin-right: 9%;">
            <form class="form-horizontal" action="" enctype="multipart/form-data" method="post" name="upload_excel">
            <div class="form-group">
            <div class="col-md-4 col-md-offset-4">
                <input class="btn btn-info" name="Export" type="submit" value="Export To CSV" />
            </div>
            </div>
            </form>
        </div>
        <a class="btn btn-success btn-sm pull-right" href="admin.php?page=manual_assign_package">
            Assign Package Manually
        </a>

        <?php $importedListTable->display(); ?>
    </div>

<?php } ?>