<?php

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

Widgets('document_template');
Widgets('all');

function SelectOneInput_1($name, $options)
{
	$result = '';
	
	if(is_array($options) && count($options))
	{
		foreach($options as $k=>$v)
		{
			$result .="<input type=radio id='{$name}[{$k}]' name='{$name}' value='{$k}' /><label for='{$name}[{$k}]'>{$v}</label><br />";	
		}
	}
	
	return $result;
}

$html_field_name = 'trg';

echo "<style>.hilite{background-color:".Preferences('ROLLOVER_HIGHLIGHT').";}</style>";
echo "<script>";
echo "function hilite_checked(cb, row){if(cb.checked){row.className+=' hilite'}else{row.className=row.className.replace(/hilite/g,'')};}; function check_clicked(row){var cb=row.firstChild.firstChild; cb.checked=!cb.checked; hilite_checked(cb, row); }; function CheckAll_hilite(form,value,name_like){for(i=0;i<form.elements.length;i++){	if(form.elements[i].type=='checkbox' && form.elements[i].name!='controller' && form.elements[i].name.substr(0,name_like.length)==name_like){hilite_checked(form.elements[i], form.elements[i].parentNode.parentNode);}}}";
echo "function checkAll(form,value,name_like){if(value==true)checked = true;else checked = false;for(i=0;i<form.elements.length;i++){if(form.elements[i].type=='checkbox' && form.elements[i].name!='controller' && form.elements[i].name.substr(0,name_like.length)==name_like)form.elements[i].checked = checked;}}";
echo "</script>";

//--List all triggers
echo "<FORM name=letters_form action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=save method=POST>"; 	

$buttons = '<input type=submit name="btn_delete" value="Delete Selected" onclick="return confirm(\''._('Are you sure?').'\')" /> &nbsp; <INPUT TYPE=button value="'._('Add Trigger').'" onclick="document.getElementById(\'add_trigger_form\').style.display=\'block\'">'.' &nbsp; <INPUT style="width:100px;" type=submit value="'._('Save').'" name="btn_save">';

$bgcolor = Preferences('COLOR');

$trigger_events = array(
		'1'=>_('Student Added'),
		'2'=>_('Student Updated'),
		//'3'=>_('Student Deleted'),
		'4'=>_('Attendance Updated'),
		'5'=>_('Schedule Added'),
		'6'=>_('Schedule Updated'),
		'7'=>_('Referral Added'),
		'8'=>_('Referral Updated'),
);

$trigger_actions = array(
		'1'=>_('Add a letter to the letter queue'),
		'2'=>_('Send an Email')
);

$templates = array();
$result = db_request(db_execute("SELECT ID, TITLE FROM STUDENT_EMAILS"));
if(count($result))
{
	foreach($result as $row)
	{
		$templates[$row['ID']] = $row['TITLE'];	
	}	
}

//-- Handle insert
if(isset($_REQUEST['btn_addtrigger']))
{
	$trigger_id = db_request(db_execute("SELECT ".db_seq_nextval('STUDENT_EMAILS_PROCESSING_SEQ').' as NEXTVAL'.FROM_DUAL));
	$trigger_id = $trigger_id[1]['NEXTVAL'];
	$trigger_event = $_REQUEST['trigger_event'];
	$trigger_action = $_REQUEST['trigger_action'];
	$template_id = $_REQUEST['template_id'];

	if($trigger_id && $trigger_event && $trigger_action && $template_id)
	{
		db_execute("INSERT INTO STUDENT_EMAILS_PROCESSING(trigger_id, trigger_event,trigger_action,template_id) VALUES('$trigger_id','$trigger_event','$trigger_action','$template_id')");
	}
}

//-- Handle update
if(isset($_REQUEST['btn_save']))
{
	if($_REQUEST['values'] && $_POST['values'])
	{
		foreach($_REQUEST['values'] as $column=>$ids)
		{	
			switch($column)
			{
				case "event": $col = "TRIGGER_EVENT"; break;	
				case "action": $col = "TRIGGER_ACTION"; break;
				case "template": $col = "TEMPLATE_ID"; break;
			}

			foreach($ids as $id=>$value)
			{
				$sql = "UPDATE STUDENT_EMAILS_PROCESSING SET ";
				$sql .= $col."='".str_replace("\'","''",$value)."',";
				$sql = substr($sql,0,-1) . " WHERE TRIGGER_ID='$id'";
				db_execute($sql);
			}
		}
	}
}

//-- Handle delete
if(isset($_REQUEST['btn_delete']))
{
	if(isset($_REQUEST[$html_field_name]) && is_array($_REQUEST[$html_field_name]) && count($_REQUEST[$html_field_name]))
	{
		$ids = array();
		foreach($_REQUEST[$html_field_name] as $k=>$v)
			$ids[] = $k;
		$ids = implode("','", $ids);
		db_execute("DELETE FROM STUDENT_EMAILS_PROCESSING WHERE trigger_id IN ('$ids')");
	}
}

DisplayHeader('', $buttons);

echo "<br />";
$add_form_style = isset($_REQUEST['cb_stayopened']) ? "" : "display:none;";
$cb_stayopened_checked = isset($_REQUEST['cb_stayopened']) ? "checked" : "";
echo "<div id='add_trigger_form' style='$add_form_style'>";
PopTable('header', _('Add Letter Trigger'));
echo "<table cellspacing=0 cellpadding=3>
		<tbody>
			<tr>
				<td valign=top>Event</td>
				<td>".SelectOneInput_1('trigger_event', $trigger_events)."</td>
			</tr>
			<tr bgcolor=$bgcolor>
				<td valign=top>Action</td>
				<td>".SelectOneInput_1('trigger_action', $trigger_actions)."</td>
			</tr>
			<tr>
				<td>Letter Template</td>
				<td>".SelectInput(0, 'template_id', '', $templates, false)."</td>
			</tr>
			<tr><td>&nbsp;</td></tr>
			<tr>
				<td colspan=\"2\"><input type=\"checkbox\" $cb_stayopened_checked  name=\"cb_stayopened\" value=\"Y\" id=\"cb_stayopened\" /><label for=\"cb_stayopened\">Stay opened</label></td>
			</tr>
			<tr>
				<td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"btn_addtrigger\" value=\"Add Trigger\" /></td>
			</tr>
		</tbody>
	</table>";
	
PopTable('footer');
echo "</div>";

function _makeChooseCheckbox1($value,$title='')
{	global $THIS_array, $html_field_name;
	
	$student_id_field="TRIGGER_ID";
	
	return "<INPUT onchange='hl_cb(this)' type=checkbox name=".$html_field_name."[".$THIS_array[$student_id_field]."] value=Y>";
}

function _makeEventDropdown($value, $title="")
{
	global $trigger_events, $THIS_array;
	
	$result = SelectInput($value, "values[event][".$THIS_array['TRIGGER_ID']."]", "", $trigger_events, false);
	
	return $result;
}

function _makeTriggerAction($value, $title='')
{
	global $trigger_actions, $THIS_array;
	
	$result = SelectInput($value, "values[action][".$THIS_array['TRIGGER_ID']."]", "", $trigger_actions, false);
	
	return $result;	
}

function _makeLetterTemplate($value, $title='')
{
	global $templates, $THIS_array;
	
	$result = SelectInput($value, "values[template][".$THIS_array['TRIGGER_ID']."]", "", $templates, false);
	
	return $result;
}

$functions = array(
	"CHECKBOX"=>'_makeChooseCheckbox1', 
	'TRIGGER_EVENT'=>"_makeEventDropdown", 
	"TRIGGER_ACTION"=>"_makeTriggerAction", 
	"TEMPLATE_ID"=>"_makeLetterTemplate");

$sections_array = db_request(db_execute("SELECT '' as CHECKBOX, TRIGGER_ID,TRIGGER_EVENT,TRIGGER_ACTION,TEMPLATE_ID FROM STUDENT_EMAILS_PROCESSING"), $functions); //,$functions,$group);

$columns = array(
	'CHECKBOX'=>'</A><INPUT type=checkbox value=Y name=controller onclick="checkAll(this.form,this.checked,\''.$html_field_name.'\');CheckAll_hilite(this.form,this.form.controller.checked,\''.$html_field_name.'\');"><A>',
	'TRIGGER_EVENT'=>_('Trigger Event'), 
	'TRIGGER_ACTION'=>_('Trigger Action'), 
	'TEMPLATE_ID'=>_('Template')
);

//$addition['onclick']="check_clicked(this);";

Gridoutput($sections_array, $columns, '', '', false, false, $addition);

echo "</FORM>";

?>