<?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
*/
 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 

function tep_get_events_output_tree($parent_id = '0',&$content,$default,$event_category_id=0) {
    global $languages_id,$selected_name,$login_id,$login_groups_type;

    if (!is_array($events_category_tree_array)) $events_category_tree_array = array();
    //if ( (sizeof($events_category_tree_array) < 1) && ($exclude != '0') ) $events_category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP,'type'=>'C','spacing'=>$spacing);

	/*if ($include_itself) {
	$events_category_query = tep_db_query("select ecd.events_categories_name from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ecd.language_id = '" . (int)$languages_id . "' and ecd.events_categories_id = '" . (int)$parent_id . "'");
	$events_category = tep_db_fetch_array($category_query);
	$events_category_tree_array[] = array('id' => $parent_id, 'text' => $events_category['events_categories_name'],'type'=>'C','spacing'=>$spacing);
	
	
	   
	}*/
	// filte to particular instructor
	$login_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY){
		$login_option=" and e.events_instructor=" . $login_id;
	}
	$event_where = '';
	if($event_category_id>0)$event_where = " and ec.events_categories_id='" . $event_category_id . "'";
	// iterate with nested category
	$events_categories_sql = "select ec.events_categories_id, ecd.events_categories_name, ec.parent_id from " . TABLE_EVENTS_CATEGORIES . " ec, " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ec.events_categories_id = ecd.events_categories_id and ecd.language_id = '" . (int)$languages_id . "' and ec.parent_id = '" . (int)$parent_id . "'" . $event_where . " order by ec.sort_order, ecd.events_categories_name";
	//echo $parent_id . ":" . $events_categories_sql;exit;
	$events_categories_query = tep_db_query($events_categories_sql);
	while ($events_categories = tep_db_fetch_array($events_categories_query)) {
		$content.= '<optgroup style="color:#0000FF" label="' . write_events_category_space($events_categories['events_categories_id']) . tep_output_string($events_categories['events_categories_name'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '">';
		// select the list of events of particular category
		$events_query=tep_db_query("select ed.events_name,ed.events_id from " . TABLE_EVENTS . ' e, ' . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ecd where ecd.events_categories_id='" . $events_categories['events_categories_id'] . "' " . $login_option . " and ed.language_id = " . (int)$languages_id . EVENTS_CONDITION_STATUS. " and e.events_id=ecd.events_id and e.events_id=ed.events_id order by ed.events_name");
		while ($events=tep_db_fetch_array($events_query))
		{
			$content .= '<option style="color:#000000" value="' . tep_output_string($events['events_id']) . '"';
			if ($default == $events['events_id']){
				$selected_name=$events['events_name'];
				$content .= ' SELECTED';
			}
			$content .= '>' . tep_output_string($events['events_name'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
		}    
		tep_get_events_output_tree($events_categories['events_categories_id'], $content,$default);
		$content.='</optgroup>';
	}

}
	function write_events_category_space($category_id){
	    global $languages_id,$selected_name,$login_id,$login_groups_type;
		$sql = "select parent_id,ec.events_categories_id,events_categories_name from " . TABLE_EVENTS_CATEGORIES . " ec," . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ec.events_categories_id=ecd.events_categories_id and ecd.language_id = '" . (int)$languages_id . "' and ec.events_categories_id=" . $category_id;
		$query = tep_db_query($sql);
		$result = tep_db_fetch_array($query);
		if($result['parent_id']>0){
			return "&nbsp;&nbsp;" . write_events_category_space($result['parent_id']);
		}else{
			return "";
		}
	}
	// Output a form pull down menu
	function tep_draw_event_select_menu($name, $values, $default = '', $parameters = '', $required = false,$initial_category_id=0,$need_all=true,$initial_parent_id=0) {
		global $selected_name;
		$field = '<select name="' . tep_output_string($name) . '"';
	
		if (tep_not_null($parameters)) $field .= ' ' . $parameters;
		$field.=' class="inputNormal" onBlur="javascript:toggle_focus(this,1);" onFocus="javascript:toggle_focus(this,2);"';
	
		$field .= '>';
		if($need_all=='true')
		$field .= '<option style="color:#000000" value="-1">' . TEXT_ALL_EVENTS . '</option>';
		else if($need_all=='1')
		$field .= '<option style="color:#000000" value="-1">' . TEXT_SELECT . '</option>';
	
		if (empty($default) && isset($GLOBALS[$name])) $default = stripslashes($GLOBALS[$name]);
	
		/*for ($i=0, $n=sizeof($values); $i<$n; $i++) {
			if ($values[$i]['type']=='C')
				$color="#0000FF";
			else
				$color="#000000";

			$padding=$values[$i]['spacing']*10;
			$field .= '<option style="color:' . $color . ';" value="' . tep_output_string($values[$i]['id']) . '"';
			if ($default == $values[$i]['id'] && $values[$i]['type']=='E'){
				$selected_name=str_replace('&nbsp;',"",$values[$i]['text']);
				$field .= ' SELECTED';
			}
			$field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
		}*/
		$content="";
		tep_get_events_output_tree($initial_parent_id,$content,$default,$initial_category_id);
		$field.=$content;
	$field .= '</select>';
	
	if ($required == true) $field .= TEXT_FIELD_REQUIRED;
	
	return $field;
	}
	// get the list of category and events in a tree view
	function tep_get_events_display_tree_grouped($parent_id = '0', $spacing = 0, $exclude = '', $events_category_tree_array = '', $include_itself = false) {
		global $languages_id,$login_id,$login_groups_type;
	
		$login_option="";
		if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY){
			$login_option=" and e.events_instructor=" . $login_id;
		}
		if (!is_array($events_category_tree_array)) $events_category_tree_array = array();
		//if ( (sizeof($events_category_tree_array) < 1) && ($exclude != '0') ) $events_category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP,'type'=>'C','spacing'=>$spacing);
	
		if ($include_itself) {
		  $events_category_query = tep_db_query("select ecd.events_categories_name from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd where ecd.language_id = '" . (int)$languages_id . "' and ecd.events_categories_id = '" . (int)$parent_id . "'");
		  $events_category = tep_db_fetch_array($category_query);
		  $events_category_tree_array[] = array('id' => $parent_id, 'text' => $events_category['events_categories_name'],'type'=>'C','spacing'=>$spacing);
		  
	
  
		}
		$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.events_categories_id = ecd.events_categories_id and ecd.language_id = '" . (int)$languages_id . "' and ec.parent_id = '" . (int)$parent_id . "' order by ec.sort_order, ecd.events_categories_name");
		while ($events_categories = tep_db_fetch_array($events_categories_query)) {
		  if ($exclude != $events_categories['events_categories_id']) $events_category_tree_array[] = array('id' => $events_categories['events_categories_id'], 'text' =>$events_categories['events_categories_name'],'type'=>'C','spacing'=>$spacing);
		  $events_category_tree_array = tep_get_events_display_tree_grouped($events_categories['events_categories_id'], $spacing+1, $exclude, $events_category_tree_array);
		}
	
		$events_query=tep_db_query("select ed.events_name,ed.events_id from " . TABLE_EVENTS . ' e, ' . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ecd where ecd.events_categories_id='" . (int)$parent_id . "' " . $login_option . " and e.events_id=ecd.events_id and e.events_id=ed.events_id order by ed.events_name");
		while ($events=tep_db_fetch_array($events_query))
		{
			$events_category_tree_array[] = array('id' => $events['events_id'], 'text' =>$events['events_name'],'type'=>'E','spacing'=>$spacing);
		}
		return $events_category_tree_array;
	}
	function tep_get_events_array_single($first=''){
		global $languages_id;
		$event_array=array();
		$event_query=tep_db_query("SELECT e.events_id,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id=ed.events_id " . EVENTS_CONDITION_STATUS . " and language_id='" . $languages_id. "' order by ed.events_name");
		while($event_result=tep_db_fetch_array($event_query)){
			$event_array[]=array('id'=>$event_result['events_id'],'text'=>$event_result['events_name']);
		}
		return $event_array;
	} 
	
	function tep_get_events_instructor_array_single($first=''){
		global $languages_id,$login_groups_type,$login_id;
		$event_array=array();
		$events_option="";
		if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";
		$event_query=tep_db_query("SELECT e.events_id,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id=ed.events_id " . EVENTS_CONDITION_STATUS . $events_option . " and language_id='" . $languages_id. "' order by ed.events_name");
		while($event_result=tep_db_fetch_array($event_query)){
			$event_array[]=array('id'=>$event_result['events_id'],'text'=>$event_result['events_name']);
		}
		return $event_array;
	} 
?>
