<?php

if($_REQUEST['servicemod']=='mod_discipline/DisSearchIncident.php')
	".:".ProgramTitle().":.";

if(get_user('PROFILE')=='admin' && ($_REQUEST['servicemod']!='mod_discipline/DisSearchIncident.php' || AllowUse('mod_discipline/DisIncidentList.php')))
{
	if($_REQUEST['servicemod'] == 'mod_discipline/DisAdvancedReportWizard.php' || $_REQUEST['incident_search_servicefunction']=='search')
	{ 
		if($_SESSION['incident_id'])
		{
			unset($_SESSION['incident_id']);
			if(!$_REQUEST['CREATEPDF'] && Preferences('MENU')=='Side')
				echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';
		}
		$tmp_REQUEST = $_REQUEST;
		unset($tmp_REQUEST['customised_view']);
		unset($tmp_REQUEST['incident_search_servicefunction']);
			
		if(!$_REQUEST['CREATEPDF'])
		{
			if(!$_REQUEST['incident_search_servicefunction'])
			{
				$_REQUEST['incident_search_servicefunction'] = 'search';
				$unset_search_servicefunction = true;
			}
			
			$tabcolor = 'CCCCCC';
			$textcolor = '000000';
			$header .= '<TD width=10></TD><TD>'.DrawTab(_('Search'),SetRequestOptions($tmp_REQUEST).'&amp;incident_search_servicefunction=search',$tabcolor,$textcolor,'_circle').'</TD>';
			$header .= '<TD width=10></TD><TD><A HREF='.SetRequestOptions($tmp_REQUEST).'&amp;incident_search_servicefunction=list'.' style="text-decoration:none;font-weight:bold;color:#666666;" onmouseover="this.style.color=\'#000000\';" onmouseout="this.style.color=\'#666666\';">'._('Incidents List').'</A></TD></TD>';
			
			if(!$addition['force_search'])
				echo TabsHeader($header);
			if($unset_search_servicefunction===true)
				unset($_REQUEST['incident_search_servicefunction']);
		}
		echo '<BR>';
		if($addition['incident_type'])
			$user = ucwords($addition['incident_type']);
		else
			$user = _('Incident');
		PopTable('header',_('Find an ').$user);
		// all this is copied from mod_students/Search
		echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=$_REQUEST[servicefunction]&search_servicefunction=list&incident_search_servicefunction=list&selected_menu=$_REQUEST[selected_menu]&link_menu=$_REQUEST[link_menu]&next_servicemod=$_REQUEST[next_servicemod]".$addition['action']." id=incident_search_form method=POST>";
		echo '<TABLE width=100%><TR><TD valign=top>';
		echo '<DIV id=menu_visiblesearch>';

		echo '<BR><TABLE><TR><TD align='.ALIGN_RIGHT.'></TD><TD></TD><TD width="100px;">&nbsp;</TD></TR></TABLE>';
		
		/*
		if(!$addition['hide_default_search'])
		{
			echo '<BR><TABLE><TR><TD align='.ALIGN_RIGHT.'>'._('Incident').'</TD><TD><INPUT type=text name=smart_name size=40></TD></TR></TABLE>';
			$search_options_title = _('More Search Options');
		}
		else
			$search_options_title = _('Search Options');
		if($_SYSTEM['direction']=='RTL')
			echo '<BR><TABLE><TR><TD align='.ALIGN_RIGHT.' width=15></TD><TD><IMG SRC=assets/arrow_left.gif height=15></TD><TD><A HREF=# onclick=\'document.getElementById("menu_hiddensearch").style.display="inline";document.getElementById("menu_visiblesearch").style.display="none";\'><font color=gray><small><B>'.$search_options_title.'</B></small></font></A></TD></TR></TABLE>';
		else
			echo '<BR><TABLE><TR><TD align='.ALIGN_RIGHT.' width=15></TD><TD><IMG SRC=assets/arrow_right.gif height=15></TD><TD><A HREF=# onclick=\'document.getElementById("menu_hiddensearch").style.display="inline";document.getElementById("menu_visiblesearch").style.display="none";\'><font color=gray><small><B>'.$search_options_title.'</B></small></font></A></TD></TR></TABLE></DIV>';
		*/
			
		echo '</DIV>';

		echo '<DIV id=menu_hiddensearch style="display:none;">';
		//Search('student_fields',$addition);
		if($addition['search'])
			echo $addition['search'];
		echo '<CENTER><INPUT type=submit value="'._('Search').'"></CENTER>';
		echo '</DIV>';
		if($addition['second_col'])
			echo '</TD><TD valign=top>'.$addition['second_col'];
		else
			echo '</TD><TD valign=top>';
		echo '<TABLE><TR><TD width=100> &nbsp; </TD><TD><BR>';

		if(get_user('PROFILE')=='admin' && (substr_count(get_user('SCHOOLS'),',')>2 || (substr_count(get_user('SCHOOLS'),',')==0 && count(db_request(db_execute("SELECT ID FROM SCHOOLS")))>1)))
			echo '<INPUT type=checkbox name=_search_all_schools id=input_search_all_schools value=Y'.(Preferences('DEFAULT_ALL_SCHOOLS')=='Y'?' CHECKED':'').'><font color=black><label for="input_search_all_schools">'._('Search All Schools').'</label></font><BR>';
		
		echo '</TD></TR></TABLE>';
		echo '<TABLE><TR><TD width=75></TD><TD>';
		if(!$addition['search_button_title'])
			$addition['search_button_title'] = _('Search');
		echo Buttons($addition['search_button_title'],_('Reset'));
		echo '</TD></TR></TABLE>';
		echo '</TD></TR></TABLE>';
		echo '</FORM>';
		PopTable('footer');
	}

	if(($_REQUEST['incident_search_servicefunction']=='list' || !$_REQUEST['incident_search_servicefunction']) && $_REQUEST['servicemod'] != 'mod_discipline/DisAdvancedReportWizard.php')
	{
		if(!$_REQUEST['next_servicemod'] || $_REQUEST['next_servicemod']=='mod_discipline/DisSearchIncident.php')
			$_REQUEST['next_servicemod'] = 'mod_discipline/DisIncidentList.php';

		if(!$addition['functions'])
			$addition['functions'] = array();
		//$addition['functions'] += array('PROFILE_ID'=>'_getProfile');
		
		$incidentsTitles = db_request(db_execute(db_limit("SELECT * FROM STUDENT_DISCIPLINE_INCIDENTS WHERE
			SCHOOL_YEAR ='".get_school_year()."'",1)));
		
		$grid_columns['ID'] = 'ID';

		if ($incidentsTitles) {
			foreach($incidentsTitles[1] as $name => $field)
			{	
				$columnNames .= $name.' AS '.$name.',';
				if(preg_match('/CFIELD_/', $name))
				{
					$fieldTitle = db_request(db_execute("SELECT TITLE AS TITLE FROM DISCIPLINE_INCIDENT_FIELDS WHERE ID=".substr($name,7)));
					$grid_columns[$name] = $fieldTitle[1]['TITLE'];
					$addition['functions'][$name] = '_make';
				}
			}
		}
		
		
		$addition['SELECT_ONLY'] = substr($columnNames,0,-1); 
		$incident_array = get_incident_list($addition);
		
		$tmp_REQUEST = $_REQUEST;
		unset($tmp_REQUEST['customised_view']);
		unset($tmp_REQUEST['search_servicefunction']);
		if($_REQUEST['incident_search_servicefunction']=='list')
		{
			unset($_REQUEST['incident_search_servicefunction']);
			$_REQUEST['incident_search_servicefunction'] = 'list';
		}
		
		//$header = '<TD width=10></TD><TD><A HREF='.SetRequestOptions($tmp_REQUEST).'&amp;incident_search_servicefunction=search'.' style="text-decoration:none;font-weight:bold;color:#666666;" onmouseover="this.style.color=\'#000000\';" onmouseout="this.style.color=\'#666666\';">'._('Search Screen').'</A></TD></TD>';
		$tabcolor = 'CCCCCC';
		$textcolor = '000000';
//		$header .= '<TD width=10></TD><TD>'.DrawTab(_('Incidents List'),SetRequestOptions($tmp_REQUEST).'&amp;incident_search_servicefunction=list',$tabcolor,$textcolor,'_circle').'</TD>';
		
		if(!$_REQUEST['CREATEPDF'] && $addition['tabs_header']!==false)
			echo TabsHeader($header);
		if($_SYSTEM['SearchTerms'])
			DisplayHeader(str_replace('<BR>','<BR> &nbsp;',substr($_SYSTEM['SearchTerms'],0,-4)));
		if(!$addition['columns_before'])
			$addition['columns_before'] = array();
		if(!$addition['columns_after'])
			$addition['columns_after'] = array();
		
		$columns = $addition['columns_before'] + $grid_columns + $addition['columns_after'];
		if($addition['link']['ID']!==false)
		{
			$link['ID']['link'] = "modloader.php?servicemod=$_REQUEST[next_servicemod]";
			$link['ID']['variables'] = array('incident_id'=>'ID','GridOutputCount'=>'index');
		}
		Gridoutput($incident_array,$columns,_('Incidents'),_('Amount of Incidents'),$link,$addition['LO_group'],$addition['options']);
	}
}

?>
