<?php

/*

 osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License 
*/
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
	$template_html=$MASTER_BOX_HTML;
	function tep_show_events_category($counter) {
    	global $events_foo, $events_categories_string, $events_display_id, $events_aa,$template_html,$BOX_CONTENT_ICON,$INITIAL_WIDTH,$languages_id;
		if ($events_foo[$counter]['parent'] == 0) {
			$ecPath_new = 'ecPath=' . $counter;
			$cid=$counter;
		} else {
			$ecPath_new = 'ecPath=' . $events_foo[$counter]['path'];
			$cid=$events_foo[$counter]['path'];
		}
		//check the single event
		$server_date=getServerDate();
			if($cid>0){
				$get_events_query = tep_db_query("select ec2.events_id  " .
										" from " . TABLE_EVENTS . " e , " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec2 where ed.events_id = e.events_id " .
										" and ed.events_id=ec2.events_id and ec2.events_categories_id='" . $cid . "' " . EVENTS_CONDITION_STATUS . " and ed.language_id = '" . (int)$languages_id . "'");
				$count_event=0;	
				$single_event_id="";
				if(tep_db_num_rows($get_events_query)>0){
					while($get_events_count=tep_db_fetch_array($get_events_query)){						
					 $check_event_id=$get_events_count['events_id'];
					 $find_sessions_query=tep_db_query("SELECT s.events_id,s.start_date,s.sessions_id from " . TABLE_EVENTS_SESSIONS . " s ".
														" where "." s.start_date>'" .  $server_date . "' " ." and s.events_id=".$check_event_id." and s.sessions_type in('R','S') order by s.start_date limit 6");
					  if(tep_db_num_rows($find_sessions_query)>0){
					 	$count_event++;
					 	$single_event_id=$check_event_id;
					  }
					}					
				}				
			}			
		//check the single event
		
		$replace_string=$template_html;
		$replace_array["SPACE_WIDTH"]=($events_foo[$counter]['level']+1)*10;
		$replace_array["SPACER"]=$replace_array["SPACE_WIDTH"];
		$replace_array["INITIAL_WIDTH"]=$INITIAL_WIDTH;
		if (strtolower(EVENTS_VIEW_STYLE)=="calendar") {
			$navi_view=(isset($current_view)?strtolower($current_view):strtolower(EVENTS_CALENDAR_STYLE));
			if ($navi_view=="month"){
				$replace_array["LINK_1"]= tep_href_link(FILENAME_EVENTS_MONTH_VIEW, $ecPath_new);
			} else if ($navi_view=="week"){
				$replace_array["LINK_1"] = tep_href_link(FILENAME_EVENTS_WEEK_VIEW, $ecPath_new);
			} else {
				$replace_array["LINK_1"] = tep_href_link(FILENAME_EVENTS_DAY_VIEW, $ecPath_new);
			}
		} else if (strtolower(EVENTS_VIEW_STYLE)=="view1") {
			$replace_array["LINK_1"] = tep_href_link(FILENAME_EVENTS_VIEW1, $ecPath_new);
		} else if(strtolower(EVENTS_VIEW_STYLE)=="view2") {
			$replace_array["LINK_1"] = tep_href_link(FILENAME_EVENTS_VIEW2, $ecPath_new);
		} else if(strtolower(EVENTS_VIEW_STYLE)=="view3") {
			$replace_array["LINK_1"] = tep_href_link(FILENAME_EVENTS_VIEW1, $ecPath_new);
		} else {
			$replace_array["LINK_1"] = tep_href_link(FILENAME_DEFAULT, $ecPath_new);
		}
		if($count_event==1) $replace_array["LINK_1"] = tep_href_link(FILENAME_SESSION_DETAILS, $ecPath_new.'&events_id='.$single_event_id);
		$replace_array["NAME"]=$events_foo[$counter]['name'];
		$replace_array["ICON"]=$BOX_CONTENT_ICON;
		
		if(sizeof($events_display_id)>0)$selected[0] = $events_display_id[sizeof($events_display_id)-1];
		else $selected[0] = "";
		if ( $events_display_id && (in_array($counter, $selected)) ) {
			$replace_array["CLASS"]='infoBoxNavigationSelect';
		} else {
			if ($events_foo[$counter]['parent'] != 0) {
				  $replace_array["CLASS"]="infoBoxNavigationSub";
			} else {
				$replace_array["CLASS"]="infoBoxNavigation";
			}
		}
		reset($replace_array);
		while(list($key,)=each($replace_array)){
			$replace_string=str_replace("{{" . $key . "}}",$replace_array[$key],$replace_string);
		}
		$events_categories_string.=$replace_string;
		if ($events_foo[$counter]['next_id']) {
			tep_show_events_category($events_foo[$counter]['next_id']);
		}
	}
if(HIDE_FROM_BACKEND_MENU_EVENTS=='false'){
?>
<!-- categories //-->
<tr>
	<td>
	<table cellpadding="0" cellspacing="0" width="100%" border="0">
		<tr>
        	<td>
			<?php
				if(!defined('BOX_HEADING_EVENTS_CATEGORIES'))define('BOX_HEADING_EVENTS_CATEGORIES', 'Events');
				$events_aa = 0;
				$info_box_contents = array();
				$info_box_contents[] = array('align' => 'left',
									'text'  => BOX_HEADING_EVENTS_CATEGORIES 
									   );
				new infoBoxHeading($info_box_contents, true, true);
				$events_categories_string = '';
				
				$events_categories_query = tep_db_query("select ec.events_categories_id, ecd.events_categories_name, ec.parent_id from " . TABLE_EVENTS_CATEGORIES . " ec, " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ec.parent_id = '0' and ec.events_categories_id = ecd.events_categories_id and ecd.language_id='" . $languages_id ."' order by sort_order, ecd.events_categories_name");
				while ($events_categories = tep_db_fetch_array($events_categories_query))  {
					$events_foo[$events_categories['events_categories_id']] = array(
													'name' => $events_categories['events_categories_name'],
													'parent' => $events_categories['parent_id'],
													'level' => 0,
													'path' => $events_categories['events_categories_id'],
													'next_id' => false
												   );
					
					if (isset($events_prev_id)) {
						$events_foo[$events_prev_id]['next_id'] = $events_categories['events_categories_id'];
					}
					
					
					$events_prev_id = $events_categories['events_categories_id'];
					
					if (!isset($events_first_element)) {
						$events_first_element = $events_categories['events_categories_id'];
					}
				}
	
				//------------------------
				if ($ecPath) {
					$events_new_path = '';
					$events_display_id = split('_', $ecPath);
					reset($events_display_id);
					while (list($events_key, $events_value) = each($events_display_id)) {
						unset($events_prev_id);
						unset($events_first_id);
						$events_categories_query = tep_db_query("select ec.events_categories_id, ecd.events_categories_name, ec.parent_id from " . TABLE_EVENTS_CATEGORIES . " ec, " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ec.parent_id = '" . $events_value . "' and ec.events_categories_id = ecd.events_categories_id and ecd.language_id='" . $languages_id ."' order by sort_order, ecd.events_categories_name");
						$events_category_check = tep_db_num_rows($events_categories_query);
						if ($events_category_check > 0) {
							$events_new_path .= $events_value;
							while ($row = tep_db_fetch_array($events_categories_query)) {
								$events_foo[$row['events_categories_id']] = array(
																  'name' => $row['events_categories_name'],
																  'parent' => $row['parent_id'],
																  'level' => $events_key+1,
																  'path' => $events_new_path . '_' . $row['events_categories_id'],
																  'next_id' => false
																 );
								
								if (isset($events_prev_id)) {
									$events_foo[$events_prev_id]['next_id'] = $row['events_categories_id'];
								}
								
								$events_prev_id = $row['events_categories_id'];
								
								if (!isset($events_first_id)) {
									$events_first_id = $row['events_categories_id'];
								}
								
								$events_last_id = $row['events_categories_id'];
							}
							$events_foo[$events_last_id]['next_id'] = $events_foo[$events_value]['next_id'];
							$events_foo[$events_value]['next_id'] = $events_first_id;
							$events_new_path .= '_';
						} else {
							break;
						}
					}
				}
				tep_show_events_category($events_first_element);
				
				$info_box_contents = array();
				$info_box_contents[] = array(	'align' => 'left',
										   		'text'  => '<table border="0" cellpadding="0" cellspacing="0" width="100%">' . $events_categories_string . '</table>'
											);
				new infoBox($info_box_contents);
				
				$info_box_contents = array();
				$info_box_contents[] = array(	'align' => 'left',
												'text'  => tep_draw_separator('pixel_trans.gif', '100%', '1')
                              				);
				new infoboxFooter($info_box_contents, true, true);
?>
			</td>
		</tr>
	</table>
	</td>
</Tr>				  
<?php } ?>
<!-- categories_eof //-->