<?php 
/*
	osConcert, Online Seat Booking 
	https://www.osconcert.com
	Copyright (c) 2009-2020 osConcert
	Released under the GNU General Public License 
*/	
// Check to ensure this file is included in osConcert!
defined('_FEXEC') or die();
// ----------------------------------------------------------------------
// 1. Get Category Heading Data using categories_lookup()
// ----------------------------------------------------------------------
//$id = $current_category_id; // Use $current_category_id instead of $products_id
require_once(DIR_WS_INCLUDES . 'functions/categories_lookup.php');
//list($heading_name, $heading_venue, $heading_date, $heading_time) = categories_lookup();

// Fetch formatted date and time using get_category_datetime()
list($formatted_date, $formatted_time) = get_category_datetime($current_category_id);

	// Debug: Print the formatted date and time
	//echo "Debug: formatted_date = $formatted_date, formatted_time = $formatted_time<br>";

	// ----------------------------------------------------------------------
	// 2. Load Template Content for Seat Plan Display
	// ----------------------------------------------------------------------
	$template_content = tep_load_template_content('seatplan.tpl.php');

	// ----------------------------------------------------------------------
	// 3. Set Up Template Details (Placeholders)
	// ----------------------------------------------------------------------
	$template_details = array();

	// Basic placeholders
	$template_details["VALUE_NO_PRODUCT"] = 'NO_PRODUCTS';

	// Determine category image (default if empty or "NULL")
	$cat_image = (empty($category['categories_image']) || $category['categories_image'] == 'NULL') 
				  ? "theatre.png" 
				  : $category['categories_image'];

	$cPath = $current_category_id;

	foreach ($cPath_array as $cat_item) {
		// Use $cPath for top-level (if parent_id==0) or the first item for deeper levels
		$cat = ($parent_id == 0) ? $cPath : $cat_item;
		break;
	}

	// ----------------------------------------------------------------------
	// 4. Get Category Description Data
	// ----------------------------------------------------------------------
	$categories_query = tep_db_query("SELECT * FROM " . TABLE_CATEGORIES_DESCRIPTION . " 
									  WHERE categories_id = '" . (int)$cat . "' 
										AND language_id = '" . (int)$FSESSION->languages_id . "'");

	if ($categories = tep_db_fetch_array($categories_query)) {
		$heading_name           = $categories['categories_name'];
		$heading_title          = $categories['categories_heading_title'];
		$categories_description = $categories['categories_description'];
		$heading_venue          = $categories['concert_venue'];
		$concert_date           = $categories['concert_date'];
		$concert_time           = $categories['concert_time'];
	}

			// Set template placeholders
			$template_details["VALUE_CATEGORIES_NAME"] = $heading_name;
			$template_details["VALUE_CATEGORIES_TITLE"] = $heading_title;
			$template_details["VALUE_CATEGORIES_DESC"] = $categories_description;
			$template_details["VALUE_CATEGORIES_VENUE"] = $heading_venue;
			$template_details["VALUE_CONCERT_UNIX"] = $category['concert_date_unix'];
			$template_details["VALUE_CATEGORIES_DATE"] = $formatted_date;
			$template_details["VALUE_CATEGORIES_TIME"] = $formatted_time;


		//this data comes from index.php for the current category ID
		$template_details["VALUE_CATEGORY_NAME"]=$category["categories_name"];
		$template_details["VALUE_CATEGORY_TITLE"]='<h2>'.$category["categories_heading_title"].'</h2>';
		$template_details["VALUE_CATEGORY_DESC"]=$category["categories_description"];
		$template_details["VALUE_CATEGORY_VENUE"]=$category["concert_venue"];
		$template_details["VALUE_CATEGORY_DATE"]=$category["concert_date"];
		$template_details["VALUE_CATEGORY_TIME"]=$category["concert_time"];
		
		
		$template_details["VALUE_CATEGORY_IMAGE"]=$cat_image;
		
		$template_details["VALUE_TEXT_TYPE"]=TEXT_TYPE;
		$template_details["VALUE_TEXT_PRICE"]=TEXT_PRICE;
		$template_details["VALUE_TEXT_QUANTITY"]=TEXT_QUANTITY;
		$template_details["VALUE_SALEMAKER"]='';
		$template_details["VALUE_GA_TOTAL"]='';
		$template_details["VALUE_SPACER"]="&nbsp;&nbsp;";
		$spacer="&nbsp;&nbsp;";

		$template_details["VALUE_FORM_START"]='';
		
		$template_details["REPEAT_PRODUCT_LIST"]=$product_repeat;
		unset($product_repeat);

		$template_details["SECTION_PRODUCT"]=1;
		$template_details["SECTION_NO_PRODUCT"]=0;

		$replaced_content=$template_content;
		$template_name = "";

		$scnt=0;
		foreach($template_details as $key => $value)
		{
			switch(substr($key,0,strpos($key,'_')))
			{
				case "VALUE":
					$replaced_content=str_replace("{{" . $key . "}}",$value,$replaced_content);
					break;
				case "SECTION":
					if ($value!=1)
					{
						$start_pos=strpos($replaced_content,"{{" . $key . "_START}}");
						$end_pos=strpos($replaced_content,"{{" . $key . "_END}}");
						$temp_content=substr($replaced_content,0,$start_pos);
						$temp_content.=substr($replaced_content,$end_pos+strlen("{{" . $key ."_END}}"));
						$replaced_content=$temp_content;
						unset($temp_content);
					} else 
					{
						$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
					}
					break;
					//I think this is it
				case "REPEAT":
					$start_pos=strpos($replaced_content,"{{" . $key . "_START}}")+strlen("{{" . $key ."_START}}");
					$end_pos=strpos($replaced_content,"{{" . $key . "_END}}");
					$repeat_content=substr($replaced_content,$start_pos,$end_pos-$start_pos);
					$merged_total_content='' . "\n";
					$col=0;
					$row=0;
					for ($icnt=0,$n=count((array)$value);$icnt<$n;$icnt++)
					{
						$merged_content='';
						if ($col==0 || $ttotal_cols==1)
						{
							$merged_content.='';
						}
							$merged_content.='' . $repeat_content;

						reset($value[$icnt]);

						foreach ( array_keys($value[$icnt]) as $itemkey )
						{
							$merged_content=str_replace("{{" . $key . "_" . $itemkey . "}}",$value[$icnt][$itemkey],$merged_content);
						}
						$col++;
						if ($col==$ttotal_cols) 
						{
							$col=0;
							$row++;
						}
						if ($col==0 || $ttotal_cols==1) 
						$merged_content.='';
						$merged_total_content.="\n" . $merged_content;
					}
					
					$merged_total_content.="";
					$replaced_content=substr($replaced_content,0,$start_pos) . $merged_total_content . substr($replaced_content,$end_pos);
					unset($merged_total_content);
					$replaced_content=str_replace(array("{{" .$key . "_START}}","{{" .$key . "_END}}"),"",$replaced_content);
			}
			$scnt++;
		}
		echo $replaced_content;
		unset($replaced_content);
		
		// Retrieve category status and plan_id from the passed $category variable
		$status   = $category['categories_status'];
		$plan_id  = $category['plan_id'];
		$categories_content = ''; // Will hold the HTML for nested categories
		$show_the_plan = 0;

		if($plan_id>3)
		{
		require_once(DIR_WS_MODULES  . 'nested_categories.php');
		}
		else
		{

// Query to check category status (for the current category id: $cat)
$status_categories_query = tep_db_query(
    "SELECT categories_status 
     FROM " . TABLE_CATEGORIES . " 
     WHERE categories_id = '" . (int)$cat . "'"
);

$show_the_plan = 0;
while ($status_categories = tep_db_fetch_array($status_categories_query)) {
    if ($status_categories['categories_status'] == 1) {
        $show_the_plan = 1;
    }
}
// Who is viewing?

	// BO can always view (BoxOffice==999 or country==999) AUG 2025
	$is_box_office =
		((int)($_SESSION['BoxOffice'] ?? 0) === 999) ||
		((int)($_SESSION['customer_country_id'] ?? 0) === 999);

	// Event active? (1=active, 0=disabled)
	if (isset($category['categories_status'])) {
		$is_active = ((int)$category['categories_status'] === 1);
	} else {
		// Fallback: read from DB if $category not in scope
		$row = tep_db_fetch_array(
			tep_db_query("SELECT categories_status
							FROM " . TABLE_CATEGORIES . "
						   WHERE categories_id = " . (int)$cat . "
						   LIMIT 1")
		);
		$is_active = ((int)($row['categories_status'] ?? 0) === 1);
	}

	// Show seat plan if BO or event is active; otherwise redirect to home
	if ($is_box_office || $is_active) {
		require_once(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/content/seatplan.php');
		require_once(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/content/products_ga.php');

		if (defined('SHOW_LOGIN') && SHOW_LOGIN === 'yes') {
			include(DIR_FS_CATALOG . DIR_WS_MODULES . '/double_boxes.php');
		}
	} else {
		tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'SSL'));
	}



	}
?>