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_sessions.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_sessions(){

   class Sessions_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' => 'Sl No',
				'session_name'	=> 'Session Name',
				'remarks' => 'Remarks',
				'min_capacity' => 'Min Capacity',
				'max_capacity' => 'Max Capacity',
				'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_name' => array('session_name', false));
		}

		/**
		 * Get the table data
		 *
		 * @return Array
		 */
		private function table_data()
		{
			global $wpdb;
			$data = $wpdb->get_results("SELECT * FROM " . DB_PACKAGE_SESSIONS, ARRAY_A);

			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 'id':
				  echo $postNumber;
				case 'session_name':
				case 'min_capacity':
				case 'max_capacity':
				 	return $item[$column_name];
				case 'remarks':
					return 'Data comes from <strong>`' . $item['session_name'] . '`</strong> in Gravity Forms';

				default:
					return print_r($item, true);
			}

			$postNumber++;
		}

		function column_actions($item){

			$actions = array(
			    'edit' => sprintf('<button class="btn btn-primary btn-xs" onclick="openCapacityModal(' . $item["id"] . ')">Edit Capacity</button>'),
			);

			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';

			// 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;
		}

	}
	$importedListTable = new Sessions_List_Table();
	$importedListTable->prepare_items();
	?>
	<div class="wrap">

		<!-- Edit Modal start-->
		<div id="editCapacityModal" class="modal fade" role="dialog">
		    <div class="modal-dialog">
		        <div class="modal-content">
		            <div class="modal-header">
		                <button type="button" class="close" data-dismiss="modal">&times;</button>
		                <h4 class="modal-title">Update Session Capacity</h4>
		            </div>
		            <div class="modal-body" id="appendCapacityDiv">
						<!-- Append Here -->
		            </div>
		            <div class="modal-footer">
		                <button type="button" onclick="updateCapacity()" class="btn btn-primary">Update</button>
		                <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
		            </div>
		        </div>

		    </div>
		</div>
		 <!-- Edit Modal End-->

		<div class="alert alert-info" role="alert" style="border: 1px solid green;border-radius: 5px;">
			<h3> Instructor Sessions (inherited from Gravity Forms)</h3>
		</div>
		<?php $importedListTable->display(); ?>
	</div>

<?php }

add_action( 'wp_ajax_nopriv_render_capacity_modal', 'render_capacity_modal' );
add_action( 'wp_ajax_render_capacity_modal', 'render_capacity_modal' );
function render_capacity_modal(){

	$session_id = $_REQUEST['session_id'];

	global $wpdb;
	$getSession = $wpdb->get_row("SELECT * FROM " . DB_PACKAGE_SESSIONS ." WHERE id = '$session_id' ");

	if ( class_exists( 'GFCommon' ) ) {

		$form = GFFormsModel::get_form_meta( $getSession->gf_form_id );
		$field = GFFormsModel::get_field( $form, 1 ); // first select box
		$choicesArr = $field->choices;

		ob_start(); ?>

		<div class="row ">
			<div class="col-md-12">
				Data comes from `<?php echo $getSession->session_name; ?>` in Gravity Forms
			</div>
		</div>
		<br/>

		<input type="hidden" id="pass_session_id" value="<?php echo $session_id; ?>">

		<div class="row ">
			<div class="col-md-12">
				<div class="form-group">
				    <label for="">Session Name</label>
				    <input type="text" class="form-control" value="<?php echo $getSession->session_name; ?>" disabled>
				</div>
			</div>
		</div>

		<?php foreach ($choicesArr as $choice) {
			$credits = explode( chr( 1 ), str_replace( array(' ', '|' ), chr( 1 ), $choice['value'] ) ); ?>

			<div class="row ">
				<div class="col-md-4">
					<div class="form-group">
					    <label for="">Session Choice</label>
					    <input type="text" class="form-control" value="<?php echo $choice['text']; ?>" disabled>
					</div>
				</div>
				<div class="col-md-4">
					<div class="form-group">
					    <label for="">Session Credits</label>
					    <input type="text" class="form-control" value="<?php echo $credits[0]; ?>" disabled>
					</div>
				</div>
				<div class="col-md-4">
					<div class="form-group">
					    <label for="">Session Price</label>
					    <input type="text" class="form-control" value="<?php echo $choice['price']; ?>" disabled>
					</div>
				</div>
			</div>

		<?php } ?>

		<div class="row ">
			<div class="col-md-6">
				<div class="form-group">
				    <label for="">Minimum Capacity</label>
				    <input type="number" class="form-control" id="min_capacity" value="<?php echo $getSession->min_capacity; ?>" placeholder="Enter atleast Capacity as 1">
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group">
				    <label for="">Maximum Capacity</label>
				    <input type="number" class="form-control" id="max_capacity" value="<?php echo $getSession->max_capacity; ?>" placeholder="Enter atleast Capacity as 1">
				</div>
			</div>
		</div>

		<?php $renderedHtml = ob_get_clean();

		echo json_encode(['status' => TRUE, 'renderedHtml' => $renderedHtml]);
		die();
	}
}


add_action( 'wp_ajax_nopriv_update_session_capacity', 'update_session_capacity' );
add_action( 'wp_ajax_update_session_capacity', 'update_session_capacity' );
function update_session_capacity(){

	$session_id = $_REQUEST['pass_session_id'];
	$min_capacity = $_REQUEST['min_capacity'];
	$max_capacity = $_REQUEST['max_capacity'];

	global $wpdb;

	$args = array(
		"min_capacity" => (int)$min_capacity,
		"max_capacity" => (int)$max_capacity
	);

	$sqlUpdate = $wpdb->update(DB_PACKAGE_SESSIONS, $args, array("id" => $session_id));

	echo json_encode(['status' => TRUE, 'message' => 'Session Capacity has been updated successfully']);
	die();

}

?>