<?php
include_once($staticpath.'classes/InputButton.php');
$submitButton = new InputButton;

if(!isset($_REQUEST['table']))
	$_REQUEST['table'] = 0;

if($_REQUEST['values'] && $_POST['values'])
{
	foreach($_REQUEST['values'] as $id=>$columns)
	{
		if($columns['DEFAULT_CODE'] == 'Y')
			db_execute("UPDATE ATTENDANCE_CODES_SETTINGS SET DEFAULT_CODE=NULL WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND TABLE_NAME='".$_REQUEST['table']."'");

		if($id!='new')
		{
			$sql = "UPDATE ATTENDANCE_CODES_SETTINGS SET ";
							
			foreach($columns as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			db_execute($sql);
		}
		else
		{
			$sql = "INSERT INTO ATTENDANCE_CODES_SETTINGS ";

			$fields = 'ID,SCHOOL_ID,SCHOOL_YEAR,TABLE_NAME,';
			$values = db_seq_nextval('ATTENDANCE_CODES_SETTINGS_SEQ').",'".get_school_id()."','".get_school_year()."','".$_REQUEST['table']."',";

			$go = 0;
			foreach($columns as $column=>$value)
			{
				if(isset($value) && $value!='')
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					if($column!='DEFAULT_CODE' && $column!='TYPE' && $column!='STATE_CODE')
						$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
			
			if($go)
				db_execute($sql);
		}
	}
	unset($_REQUEST['values']);
	unset($_SESSION['_REQUEST_vars']['values']);
}

if($_REQUEST['categories'] && $_POST['categories'])
{
	foreach($_REQUEST['categories'] as $id=>$columns)
	{
		if($id!='new')
		{
			$sql = "UPDATE ATTENDANCE_CODE_CATS_SETTINGS SET ";
							
			foreach($columns as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			db_execute($sql);
		}
	}
	unset($_REQUEST['categories']);
	unset($_SESSION['_REQUEST_vars']['categories']);
}

if($_REQUEST['new_category_title'])
{
	db_execute("INSERT INTO ATTENDANCE_CODE_CATS_SETTINGS (ID,SCHOOL_YEAR,SCHOOL_ID,TITLE) values(".db_seq_nextval('ATTENDANCE_CODE_CATS_SETTINGS_SEQ').",'".get_school_year()."','".get_school_id()."','".$_REQUEST['new_category_title']."')");
	$id = db_seq_currval('ATTENDANCE_CODE_CATS_SETTINGS_SEQ');
	
	$_REQUEST['table'] = $id;
	unset($_REQUEST['new_category_title']);
	unset($_SESSION['_REQUEST_vars']['new_category_title']);
}

DisplayHeader(".:".ProgramTitle().":.");

if($_REQUEST['servicefunction']=='remove')
{
	if($_REQUEST['id'])
	{
		if(DeletePrompt(_('this attendance code')))
		{
			db_execute("DELETE FROM ATTENDANCE_CODES_SETTINGS WHERE ID='$_REQUEST[id]'");
			unset($_REQUEST['servicefunction']);
		}
	}
	elseif($_REQUEST['table'])
	{
		if(DeletePrompt(_('category')))
		{
			db_execute("DELETE FROM ATTENDANCE_CODES_SETTINGS WHERE TABLE_NAME='$_REQUEST[table]'");
			db_execute("DELETE FROM ATTENDANCE_CODE_CATS_SETTINGS WHERE ID='$_REQUEST[table]'");
			unset($_REQUEST['servicefunction']);
			$_REQUEST['table'] = '0';
		}
	}
}

if($_REQUEST['servicefunction']!='remove')
{
	if($_REQUEST['table']!=='new')
	{
		$QI = db_execute("SELECT ID,TITLE,SHORT_NAME,TYPE,DEFAULT_CODE,STATE_CODE,IS_DAILY_CODE,SORT_ORDER FROM ATTENDANCE_CODES_SETTINGS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' AND TABLE_NAME='".$_REQUEST['table']."' ORDER BY SORT_ORDER,TITLE");
		$attendance_codes_array = db_request($QI,array('TITLE'=>'_makeTextInput','SHORT_NAME'=>'_makeTextInput','SORT_ORDER'=>'_makeTextInput','TYPE'=>'_makeSelectInput','STATE_CODE'=>'_makeSelectInput','DEFAULT_CODE'=>'_makeCheckBoxInput','IS_DAILY_CODE'=>'_makeCheckboxInput'));
	}
	
	$columns = array('TITLE'=>_('Code of Attendance'),
			 'SHORT_NAME'=>_('Short Name'),
			 'STATE_CODE'=>_('Attendance Code Status'),
			 'SORT_ORDER'=>_('Sort Order'),
			 'DEFAULT_CODE'=>_('Code of Attendance by Default'),
			 'IS_DAILY_CODE'=>_('Is it a Daily Absence Code?'),
			 'TYPE'=>_('Permission to Mark Attendance'));
	if($_REQUEST['table']!='0')
		unset($columns['STATE_CODE']);
	$link['add']['html'] = array('TITLE'=>_makeTextInput('','TITLE'),'SHORT_NAME'=>_makeTextInput('','SHORT_NAME'),'SORT_ORDER'=>_makeTextInput('','SORT_ORDER'),'TYPE'=>_makeSelectInput('','TYPE'),'DEFAULT_CODE'=>_makeCheckBoxInput('','DEFAULT_CODE'),'STATE_CODE'=>_makeSelectInput('','STATE_CODE'),'IS_DAILY_CODE'=>_makeCheckboxInput('','IS_DAILY_CODE'));
	$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove";
	$link['remove']['variables'] = array('id'=>'ID');
	
	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=update&table=$_REQUEST[table] method=POST>";
	DisplayHeader('',$submitButton->createSButton(_('Save')));

	$tabs = array(array('title'=>_('Attendance'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&table=0"));
	$categories_array = db_request(db_execute("SELECT ID,TITLE FROM ATTENDANCE_CODE_CATS_SETTINGS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."'"));
	foreach($categories_array as $category)
		$tabs[] = array('title'=>$category['TITLE'],'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&table=".$category['ID']);

	if($_REQUEST['table']==='new')
		$tabs[] = array('title'=>button('white_add'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&table=new");
	else
		$tabs[] = array('title'=>button('add'),'link'=>"modloader.php?servicemod=$_REQUEST[servicemod]&table=new");

	echo '<BR>';

	if($_REQUEST['table']!=='new')
	{
		if(count($attendance_codes_array)==0)
		{
			$_SYSTEM['selected_tab'] = "modloader.php?servicemod=$_REQUEST[servicemod]&table=$_REQUEST[table]";
			echo PopTable('header',$tabs);
			Gridoutput($attendance_codes_array,$columns,'','',$link,array(),array('download'=>false,'search'=>false));
			if($_REQUEST['table']!=0)
				echo '<CENTER>'.button('remove',_('Delete this category'),"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove&table=$_REQUEST[table]");
			echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
			echo PopTable('footer');
		}
		else
		{
			$options = array('search'=>false);
			$options['middle'] = WrapTabs($tabs,"modloader.php?servicemod=$_REQUEST[servicemod]&table=$_REQUEST[table]");
//			echo '<CENTER>'.WrapTabs($tabs,"modloader.php?servicemod=$_REQUEST[servicemod]&table=$_REQUEST[table]").'</CENTER>';
			Gridoutput($attendance_codes_array,$columns,_('Attendance Codes'),_('Amount of Attendance Codes'),$link,array(),$options);
			echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
		}
	}
	else
	{
		$_SYSTEM['selected_tab'] = "modloader.php?servicemod=$_REQUEST[servicemod]&table=$_REQUEST[table]";

		$options = array('search'=>false);
		$options['middle'] = WrapTabs($tabs,"modloader.php?servicemod=$_REQUEST[servicemod]&table=$_REQUEST[table]");
		$categories_array = db_request(db_execute("SELECT ID,TITLE FROM ATTENDANCE_CODE_CATS_SETTINGS WHERE SCHOOL_YEAR='".get_school_year()."' AND SCHOOL_ID='".get_school_id()."' ORDER BY TITLE"),array('TITLE'=>'_makeCategoryTitle'));
		$columns = array('TITLE'=>_('Category Title'));
		$link = array();
		$link['add']['html'] = array('TITLE'=>'<INPUT type=text name=new_category_title>');
		$link['remove']['link'] = "modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=remove";
		$link['remove']['variables'] = array('table'=>'ID');
		Gridoutput($categories_array,$columns,'','',$link,array(),$options);
		echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';

		/*
		echo PopTable('header',$tabs);
		echo '<CENTER>'._('New Category Title').' <INPUT type=text name=new_category_title></CENTER>';
		echo '<BR><CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
		echo PopTable('footer');
		*/
	}
	echo '</FORM>';
}

function _makeCategoryTitle($value,$name)
{	global $THIS_array;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';

	return TextInput($value,'categories['.$id.']['.$name.']','',$addition);
}

function _makeTextInput($value,$name)
{	global $THIS_array;
	
	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';
	
	if($name=='SHORT_NAME' || $name=='SORT_ORDER')
		$addition = 'size=5 maxlength=10';
	
	return TextInput($value,'values['.$id.']['.$name.']','',$addition);
}

function _makeSelectInput($value,$name)
{	global $THIS_array;
	
	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';
	
	if($name=='TYPE')
		$options = array('official'=>_('Administration Only'),'teacher'=>_('Teacher & Administration'));
	elseif($name='STATE_CODE')
		$options = array('P'=>_('Present'),'A'=>_('Absent'),'H'=>_('Half'));
	
	return SelectInput($value,'values['.$id.']['.$name.']','',$options,false);
}

function _makeCheckBoxInput($value,$name)
{	global $THIS_array,$attendance_codes_array;

	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
	{
		$id = 'new';
		$new = true;
		if(is_array($attendance_codes_array)==0)
		$value = 'Y';
	}
	
	return CheckBoxInput($value,'values['.$id.']['.$name.']','','',$new);
}
?>