<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

	if(ADMIN_FULL == "admin" || ADMIN_VENUE == "venue") 
	{
		$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=venue&function=venuelist">Venue List</a>';
		/******************************************************/
		/*  Venue List Page
		/******************************************************/
		if($_GET['function'] == 'venuelist' || $_GET['function'] == '') 
		{
			$page_num = $_GET['page_num'];
			if($page_num < 1)
			{
				$page_num = 1;
			}
			$venueObj = new VenueObj();
			$venueObj->set_venue_name($_GET['venue_name']);
			$venueObj->set_station_id($_GET['station_id']);
			$venueObj->set_venue_city($_GET['city_name']);
			$venueObj->set_venue_state($_GET['city_state']);
			$venueObj->set_city_metro($_GET['city_metro']);
			$venueObj->set_num_rows(NUM_ROWS);
			$venueObj->set_start_row(($page_num-1)*NUM_ROWS);
			$venueList = $venueObj -> listVenue();
			foreach($_GET AS $key => $value)
			{
				if($key != "page_num")
				{
					$get_str .= $key."=".$value."&";
				}
				if($key != "action" && $key != "function" && $value != "0" && $value != "" && $key != "page_num")
				{
					if($key == "station_id")
					{
						$stationObj = new StationObj();
						$stationObj->set_station_id($_GET['station_id']);
						$stationObj->getStation();
						$search_str .= "Station: ".$stationObj->get_station_letters()."; ";
					}
					else
					{
						$search_str .= ucwords(str_replace("_"," ",$key.": ".$value."; "));
					}
				}
			}
			if($search_str == "")
			{
				$search_str = "No search criteria provided.  Listing all venues.";
			}
			if($_GET['city_metro'] == "true")
			{
				$metro_check = " CHECKED";
			}
			$template -> assign_block_vars("venue_list",array(
				"GET_STR" => $get_str,
				"SEARCH_STR" => $search_str,
				"VENUE_NAME" => $_GET['venue_name'],
				"STATION_ID" => $_GET['station_id'],
				"CITY_NAME" => $_GET['city_name'],
				"CITY_STATE" => $_GET['city_state'],
				"CITY_METRO" => $_GET['city_metro'],
				"METRO_CHECK" => $metro_check,
				"NUM_VENUE" => $venueObj->get_query_count()));
			if($venueObj->get_query_count() > NUM_ROWS)
			{
				$num_pages = intval($venueObj->get_query_count()/NUM_ROWS);
				if($venueObj->get_query_count()%NUM_ROWS > 0)
				{
					$num_pages++;
				}
				$template -> assign_block_vars("venue_list.pagination", array(
					"PAGE_NUM" => $page_num,
					"PAGE_PREV" => ($page_num-1),
					"PAGE_NEXT" => ($page_num+1),
					"PAGE_LAST" => $num_pages));
				if($page_num > 1)
				{
					$template -> assign_block_vars("venue_list.pagination.prev_link", array());
				}
				if($page_num < $num_pages)
				{
					$template -> assign_block_vars("venue_list.pagination.next_link", array());
				}
				for($i=1;$i<=$num_pages;$i++)
				{
					$template -> assign_block_vars("venue_list.pagination.page", array(
						"PAGE_NUM" => $i));
					if($i != $page_num)
					{
						$template -> assign_block_vars("venue_list.pagination.page.page_link", array());
					}
				}
			}
			if(count($venueList) > 0)
			{
				foreach($venueList as $key => $value)
				{
					$template -> assign_block_vars("venue_list.venue", array(
						"VENUE_ID" => $value->get_venue_id(),
						"VENUE_NAME" => $value->get_venue_name(),
						"VENUE_ADDRESS" => $value->get_venue_address(),
						"VENUE_CITY" => $value->get_venue_city(),
						"VENUE_STATE" => $value->get_venue_state(),
						"VENUE_ZIP" => $value->get_venue_zip(),
						"VENUE_URL" => $value->get_venue_url(),
						"VENUE_PHONE" => $value->get_venue_phone(),
						"VENUE_EMAIL" => $value->get_venue_email(),
						"VENUE_COMMENTS" => $value->get_venue_comments()));
				}
			}

			$stationObj = new StationObj();
			$stationlist = $stationObj -> listStation();
			$template -> assign_block_vars("station_list", array());
			if(count($stationlist) > 0)
			{
				foreach($stationlist as $key => $value)
				{
					$selected = "";
					if($_GET['station_id'] == $value->get_station_id())
					{
						$selected = " SELECTED";
					}
					$template -> assign_block_vars("venue_list.station", array(
						"STATION_ID" => $value->get_station_id(),
						"STATION_LETTERS" => $value->get_station_letters(),
						"STATION_CITY" => $value->get_station_city(),
						"STATION_STATE" => $value->get_station_state(),
						"STATION_CONTACT" => $value->get_station_contact(),
						"STATION_EMAIL" => $value->get_station_email(),
						"STATION_URL" => $value->get_station_url(),
						"STATION_NETWORK_LETTERS" => $value->get_station_network_letters(),
						"SELECTED" => $selected));
				}
			}
			
			$stateObj = new CityObj();
			$stateList = $stateObj->listState();
			foreach($stateList AS $key => $value)
			{
				$select = "";
				if($_GET['city_state'] == $value->get_city_st())
				{
					$select = " SELECTED";
				}
				$template -> assign_block_vars("venue_list.state", array(
						"CITY_STATE" => $value->get_city_st(),
						"CITY_STATE_SELECT" => $select));
			}
			$template -> assign_var_from_handle('VENUE_ADMIN_PAGE', 'venueadminpage');
		}
		/******************************************************/
		/*  Venue View/Edit Page
		/******************************************************/
		elseif($_GET['function'] == 'venueview' || $_GET['function'] == 'venueedit' || $_GET['function'] == 'venueadd')
		{
			if($_POST['post_form'] == 'true' && ($_POST['venueadd'] == 'true' || $_POST['venue_id'] != ''))
			{
				$venueObj = new VenueObj();
				$venueObj->set_venue_id($_POST['venue_id']);
				$venueObj->set_venue_name($_POST['venue_name']);
				$venueObj->set_venue_address($_POST['venue_address']);
				$venueObj->set_venue_city($_POST['venue_city']);
				$venueObj->set_venue_state($_POST['venue_state']);
				$venueObj->set_venue_zip($_POST['venue_zip']);
				$venueObj->set_venue_url($_POST['venue_url']);
				$venueObj->set_venue_phone($_POST['venue_phone']);
				$venueObj->set_venue_email($_POST['venue_email']);
				$venueObj->set_venue_comments($_POST['venue_comments']);
				$venueMsg = $venueObj->updateVenue();
				
				$template -> assign_block_vars("venue_msg", array(
					"VENUE_MSG" => $venueMsg));
			}
			else
			{
				if($_GET['function'] == 'venueadd')
				{
					$template -> assign_block_vars("venue_view", array(
						"DISABLED" => " DISABLED"));
					$template -> assign_block_vars("venue_view.edit", array());
					$template -> assign_block_vars("venue_view.add", array());
				}
				elseif($_GET['venue_id'] == '' && $_POST['venue_id'] == '')
				{
					header("Location: index.php?action=venue&function=venuelist");
				}
				else
				{	
					$venueObj = new VenueObj();
					$venueObj->set_venue_id($_GET['venue_id']);
					$venueObj->getVenue();
					if($venueObj->get_venue_name() == '')
					{
						$template -> assign_block_vars("page_error",array());
						$template -> assign_block_vars("page_error.error_msg", array(
							"ERROR_MSG" => "No venue found with the provided id.  Please try again."));
					}
					else
					{
						$template -> assign_block_vars("venue_view", array(
							"VENUE_ID" => $venueObj->get_venue_id(),
							"VENUE_NAME" => $venueObj->get_venue_name(),
							"VENUE_ADDRESS" => $venueObj->get_venue_address(),
							"VENUE_CITY" => $venueObj->get_venue_city(),
							"VENUE_STATE" => $venueObj->get_venue_state(),
							"VENUE_ZIP" => $venueObj->get_venue_zip(),
							"VENUE_URL" => $venueObj->get_venue_url(),
							"VENUE_PHONE" => $venueObj->get_venue_phone(),
							"VENUE_EMAIL" => $venueObj->get_venue_email(),
							"VENUE_COMMENTS" => $venueObj->get_venue_comments()));
							
						if($_GET['function'] == 'venueedit') 
						{
							$template -> assign_block_vars("venue_view.edit", array());
							$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=venue&function=venueview&venue_id='.$_GET['venue_id'].'">Venue View</a>';
						}
						else
						{
							$template -> assign_block_vars("venue_view.view", array());
						}
					}
				}
			}
			$template -> assign_var_from_handle('VENUE_ADMIN_PAGE', 'venueadminpage');
		}
		/******************************************************/
		/*  Venue Import(from file)
		/******************************************************/
		elseif($_GET['function'] == 'venueimport')
		{
			if($_POST['post_form'] == 'true' && $_POST['venueimport'] == 'true')
			{
				include(INCLUDE_PATH.'dataImport.php');
		
				$venueMsg = ImportFile("venue");
				$template -> assign_block_vars("venue_msg", array(
					"VENUE_MSG" => $venueMsg));

			}
			else
			{
				$template -> assign_block_vars("venue_import", array());
			}
			$template -> assign_var_from_handle('VENUE_ADMIN_PAGE', 'venueadminpage');
		}
		/******************************************************/
		/*  User Del(Deactivate)
		/******************************************************/
		elseif($_GET['function'] == 'venuedel')
		{
			if($_GET['venue_id'] == '')
			{
				$template -> assign_block_vars("page_error",array());
				$template -> assign_block_vars("page_error.error_msg", array(
					"ERROR_MSG" => "No venue provided.  Please try again."));
			}
			else
			{
				$venueObj = new VenueObj();
				$venueObj->set_venue_id($_GET['venue_id']);
				$venueMsg = $venueObj->delVenue();
				$template -> assign_block_vars("venue_msg", array(
					"VENUE_MSG" => $venueMsg));
			}
			$template -> assign_var_from_handle('VENUE_ADMIN_PAGE', 'venueadminpage');
		}
	} 
	else
	{
		$template -> assign_block_vars("page_error",array());
		$template -> assign_block_vars("page_error.permission", array());
	}
?>