<?php
/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require('includes/application_top.php');
	require(DIR_WS_CLASSES . 'split_page_results_report.php');
	require(DIR_WS_FUNCTIONS . 'events_tree.php');
	define(BOX_WIDTH,'125');
	
	$command=(isset($HTTP_GET_VARS["command"])?$HTTP_GET_VARS["command"]:'');
	$command=(isset($HTTP_GET_VARS["command"])?$HTTP_GET_VARS["command"]:'');
	$date=(isset($HTTP_POST_VARS["cbo_date"])?tep_convert_date_raw($HTTP_POST_VARS["cbo_date"]):getServerDate(false));
	$view_id=(isset($HTTP_POST_VARS["cbo_view"])?$HTTP_POST_VARS["cbo_view"]:'1');
	$instructor_id=(isset($HTTP_POST_VARS["cbo_instructor"])?$HTTP_POST_VARS["cbo_instructor"]:'-1');
	$location_id=(isset($HTTP_POST_VARS["cbo_location"])?$HTTP_POST_VARS["cbo_location"]:'-1');
	$category_id=(isset($HTTP_POST_VARS["cbo_category"])?$HTTP_POST_VARS["cbo_category"]:'0');
	//get category list
	$category_array=tep_get_event_category_tree();

	//View Array
	$view_array = array(array("id"=>1,"text"=>TEXT_DAY),array("id"=>2,"text"=>TEXT_WEEK),array("id"=>3,"text"=>TEXT_MONTH));

	// get location names
	$location_array = array();
	$sql = "select distinct location_id, location_name from events,events_locations where location_id=events_location order by location_name";
	$sql_result = tep_db_query($sql);
	$location_array[]=array("id"=>-1,"text"=>TEXT_ALL_LOCATIONS);
	while($row=tep_db_fetch_array($sql_result)) {
		$location_array[]=array("id"=>$row["location_id"],"text"=>$row["location_name"]);
	}
	mysql_free_result($sql_result);

	$instructor_array = array();
	if ($login_groups_type!=TEXT_INSTRUCTOR_ENTRY){
		// get instructor names			
		$sql = "select a.admin_id admin_id, admin_firstname, admin_lastname  from events_instructors ei, admin a where ei.admin_id=a.admin_id order by admin_firstname,admin_lastname";
		$sql_result = tep_db_query($sql);
		$num_rows = tep_db_num_rows($sql_result);
		$instructor_array[]=array("id"=>-1,"text"=>TEXT_ALL_INSTRUCTORS);
		while($row=tep_db_fetch_array($sql_result)) {
			$instructor_array[]=array("id"=>$row["admin_id"],"text"=>$row["admin_firstname"]." ".$row["admin_lastname"]);
		}
		mysql_free_result($sql_result);
	}

	//ajax start
 if($command=='get_progress_list'){
   // create the sql for report
	$cat_id=(isset($HTTP_GET_VARS["cat_id"])?$HTTP_GET_VARS["cat_id"]:0);
	$page=(isset($HTTP_GET_VARS["page"])?$HTTP_GET_VARS["page"]:1);
	$event_id=(isset($HTTP_GET_VARS["event_id"])?$HTTP_GET_VARS["event_id"]:0);
	$where="";
	echo "Progress_list^";
	if ($cat_id!=0){
		$where.=" and ec.events_categories_id='" . $cat_id . "'";
	}
	$date_begin = $date;
	$date_end = $date;
	$days= date('t',strtotime($date));	
	if($view_id==2) {
		$sql =  "select date_add('$date', interval -(dayofweek('$date')-1) day) begin, date_add('$date', interval (7-dayofweek('$date')) day) end";	
	} else if($view_id==3) {
		$sql =  "select date_add('$date', interval -(dayofmonth('$date')-1) day) begin, date_add('$date', interval ('$days'-dayofmonth('$date')) day) end";	
	}
	if($view_id==2 || $view_id==3) {
		$sql_result = tep_db_query($sql);
		$row = mysql_fetch_assoc($sql_result);
		$date_begin = $row["begin"];
		$date_end = $row["end"];
	}
	$event_where = "";
	if($instructor_id>0)$event_where .= " and e.events_instructor='" . (int)$instructor_id . "' ";
	if($location_id>0)$event_where .= " and e.events_location='" . (int)$location_id . "' ";
	if($event_id>0)$event_where .= " and e.events_id='" . (int)$event_id . "' ";
	$events_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
	$events_sql="SELECT distinct e.events_id,ed.events_name,e.events_location,l.location_name,e.events_instructor,concat(a.admin_firstname,' ', a.admin_lastname) as instructor_name,ecd.events_categories_id,ecd.events_categories_name, " . 
					" time_format(e.events_start_time,'%h-%i %p') as start_time,time_format(e.events_end_time,'%h-%i %p') as end_time,es.start_date from " . 
					TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ec, ". TABLE_EVENTS_SESSIONS." es," . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS . " e LEFT JOIN " . TABLE_EVENTS_LOCATIONS . " l on e.events_location=l.location_id  
					LEFT JOIN admin a on a.admin_id=e.events_instructor where es.events_id=e.events_id and e.events_id=ec.events_id and ecd.events_categories_id=ec.events_categories_id
					and e.events_id=ed.events_id and es.start_date >='" . $date_begin . "' and es.start_date <='" . $date_end . "' " . $where . $events_option . $event_where . " and ed.language_id='" . (int)$languages_id . "' and ecd.language_id='". (int)$languages_id . "' and (e.events_status=1 or e.events_status=3) order by ecd.events_categories_name,ed.events_name,es.start_date";
	
	//echo $events_sql;
	$query_split = new splitPageResultsReport($page, REPORT_MAX_ROWS_PAGE, $events_sql, $query_split_numrows);
	$events_query=tep_db_query($events_sql);
   ?>
   		<table border="0" width="100%" cellspacing="0" cellpadding="2">
   			<tr>
					<td>
					  <table border="0" width="100%" cellspacing="0" cellpadding="2">
							<tr>
							<?php if($query_split_numrows>0){?>
								<td class="smallText" align="left">
									<?php echo $query_split->display_links($query_split_numrows, REPORT_MAX_ROWS_PAGE, REPORT_MAX_LINKS_PAGE, $page,tep_get_report_params()); ?>&nbsp;
								</td>
							<?php 
								}
								else{
									echo '<tr><td class="main" align="center">' . TEXT_NO_RECORDS_FOUND . '</td></tr>';
									exit;
								}
							?>
							</tr>
						</table>
						</td>
					</tr>
			    <?php
					$prev_cat_id=0;
					$icnt=1;
					$prev_event_id=0;
				   if (tep_db_num_rows($events_query)>0){
						while($row=tep_db_fetch_array($events_query)){
						  if ($prev_cat_id!=$row["events_categories_id"]) { // output category
			      ?>
							<tr height="50">
								<td class="tableHeading"><?php echo $row["events_categories_name"];?></td>
							</tr>
					<?php $prev_cat_id=$row["events_categories_id"];
						}
						if ($prev_event_id!=$row["events_id"]) { // output category
					 ?>
							<tr>
								<td class="dataTableContent"><b><?php echo $icnt . ". " . $row["events_name"]; ?></b></td>
							</tr>
							<tr>
								<td>
									<table border="0" cellpadding="2" cellspacing="0" width="100%">
									<tr class='dataTableHeadingTitleRow'>
									 <TD class='dataTableHeadingTitleContent' ><?php echo TEXT_DATE;?></TD>
									 <TD class='dataTableHeadingTitleContent'><?php echo TEXT_LOCATION;?></TD>
									 <TD class='dataTableHeadingTitleContent'><?php echo TEXT_INSTRUCTOR;?></TD>
									 <TD class='dataTableHeadingTitleContent'><?php echo TEXT_TIME;?></TD>
									</tr>
									</table>
								</td>
							</tr> 
						<?php $prev_event_id=$row['events_id'];
						$icnt++;
							} 
						?>
								<tr>
								 <td>
								  <table border="0" cellpadding="0" cellspacing="0" width="100%"> 
									  <TR class='dataTableRow' height="20">
										  <TD width="155" class='dataTableContent'><?php echo format_date($row['start_date']);?></TD>
										  <TD width="250" class='dataTableContent'><?php echo $row['location_name'];?></TD>
										  <TD width="300" class='dataTableContent'><?php echo $row['instructor_name'];?></TD>
										  <TD width="144" class='dataTableContent'><?php echo $row['start_time'] . ' - ' . $row['end_time'];?></TD>
									  </TR>
								 </table>
								</td>
							</tr>
					<?php   
				 } //while $row   
				}else { ?>
				<tr>
					<td colspan="4" align="center" class="main"><?php echo TEXT_NO_RECORDS_FOUND;?></td>
				</tr> 
			<?php 	} ?>
		</table>
    <?php
    exit; 
   }else if($command=='get_event_list'){
		$cat_id=(isset($HTTP_GET_VARS["cat_id"])?$HTTP_GET_VARS["cat_id"]:0);
		$event_id=(isset($HTTP_GET_VARS["event_id"])?$HTTP_GET_VARS["event_id"]:0);
		$all_events_array=array();
		$all_events_array = tep_get_events_array_single();
		$parent_sql = "select parent_id from " . TABLE_EVENTS_CATEGORIES . " where events_categories_id=" . $cat_id;
		$parent_query = tep_db_query($parent_sql);
		$parent_result = tep_db_fetch_array($parent_query);
		if($parent_result['parent_id']=="")$parent_result['parent_id']=0;
		echo "Events^";
		if(sizeof($all_events_array)<=0) echo 'Events are not Available';
		else echo TEXT_EVENTS . '&nbsp;' . tep_draw_event_select_menu('sel_event',$all_events_array,$event_id,' id="sel_event"',false,$cat_id,true,$parent_result['parent_id']);
		exit;
   }
   //ajax end
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript" src="includes/date-picker.js"></script>
<script language="JavaScript">
	
	function get_list(page){
		id=document.f.cbo_category.options[document.f.cbo_category.selectedIndex].value;
		id_event=0;
		if(document.getElementById("sel_event"))
			id_event=document.getElementById("sel_event").options[document.getElementById("sel_event").selectedIndex].value;
		if(!page) page=1;
		command="<?php echo tep_href_link(FILENAME_EVENTS_PROGRESS);?>?command=get_progress_list&cat_id="+id+"&page="+page+"&event_id="+id_event;
		do_post_command("f",command);
		document.getElementById("put_result").innerHTML="Loading...";
	}
	function get_event_list(id){
		id_event=0;
		if(document.getElementById("sel_event"))
			id_event=document.getElementById("sel_event").options[document.getElementById("sel_event").selectedIndex].value;
		command="<?php echo tep_href_link(FILENAME_EVENTS_PROGRESS);?>?command=get_event_list&cat_id="+id+"&event_id="+id_event;
		do_post_command("f",command);
	}
	function do_result(result){
		var res = result.toString();
		res = res.split("^");
		if(res[0]=="Events"){
			element=document.getElementById("put_events");
		}else{
			command="<?php echo tep_href_link(FILENAME_EVENTS_PROGRESS);?>?command=get_event_list&cat_id="+id+"&event_id="+id_event;
			do_post_command("f",command);
			element=document.getElementById("put_result");
		}
		if (element) element.innerHTML=res[1];
	}
	function nav_page(cnt){
		get_list(cnt);
	}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
		<!-- body_text //-->	  
		<td width=100% align=left valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
			<tr height="10">
				<!--<td class="pageHeading"><?php //echo HEADING_EVENTS_PROGRESS;?></td>!-->
			</tr>
			<tr>
				<td class="pageHeading"></td>
			</tr>
			<?php 
				echo tep_draw_form("f",FILENAME_EVENTS_PROGRESS);
			?>
			<tr >
				<td>
					<table cellspacing="2" cellpadding="2" class="searchArea" width="100%" border="0" height="35">
					<tr>
						<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
						<td nowrap="nowrap" colspan="2">
							<?php echo TEXT_DATE . '&nbsp;' . tep_draw_input_field("cbo_date",format_date($date),' size="10"');?>
			                <a href="javascript:show_calendar('f.cbo_date',null,null,'<?php echo $date_format;?>');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
							</a>&nbsp;&nbsp;
							<?php echo TEXT_VIEW . '&nbsp;' . tep_draw_pull_down_menu("cbo_view",$view_array,$view_id,'');?>
							<?php echo "&nbsp;&nbsp;" . TEXT_INSTRUCTORS . '&nbsp;' . tep_draw_pull_down_menu("cbo_instructor",$instructor_array,$instructor_id,'');?>
							<?php echo "&nbsp;&nbsp;" . TEXT_LOCATIONS . '&nbsp;' . tep_draw_pull_down_menu("cbo_location",$location_array,$location_id,'');?>
						</td>
					</tr>
					<tr>
						<td nowrap="nowrap" width="30%">
						<?php 
							echo TEXT_CATEGORY . '&nbsp;' . tep_draw_pull_down_menu("cbo_category",$category_array,$category_id,' onChange="javascript:get_event_list(this.value);"');
							echo "&nbsp;&nbsp;";
						?>
						</td>
						<td id="put_events" style="display:''" width="30%"></td>
						<td nowrap align="right" width="40%"><?php echo tep_image_button('button_report_search.gif', IMAGE_SEARCH_DETAILS,' onClick="javascript:get_list();"');?></td>
					</tr>
				</table>

				</td>
			</tr>			
			</form>
			<tr>
			<tr ><td class="cell_bg_report_header">&nbsp;</td></tr>
				<TD>
					<?php echo tep_draw_separator('pixel_trans.gif',1,5); ?>
				</TD>
			</tr>
			<tr>
				<td id="put_result"></td>
			</tr>						
	</table>
	</td>
</tr>
</table>
<script language="javascript">get_list();</script>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php');?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
