<?php

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

// THIS PROGRAM RELIES ON fckEditor
//	- editor/dialog/fck_template.php
echo GetBaseJavaFunc();
//die('llllll');
$item = _('Template title');
//print_r($item);
DisplayHeader(".:".ProgramTitle().":.");

if($_REQUEST['servicefunction']=='update' && AllowEdit() && !$_REQUEST['new_title'])
{

//die("********");
	$array = db_request(db_execute("SELECT HEADER,FOOTER,TITLE,SCHOOL_ID,DEFAULT_PROGRAM,PAGE_BREAKS,DEFAULT_PROGRAM_FOR_TEACHERS FROM LH_TEMPLATES WHERE ID='".$_REQUEST['id']."'"));
//print_r($array);
//die('00000');
	$title_extra = '';
	if($_REQUEST['values']['title'])
		$title_extra .= ", TITLE='".str_replace("\'","''",$_REQUEST['values']['title'])."'";
	if(isset($_REQUEST['values']['orientation']))
		$title_extra .= ", ORIENTATION='".str_replace("\'","''",$_REQUEST['values']['orientation'])."'";
	if(isset($_REQUEST['values']['school_id']))
	{
		$title_extra .= ", SCHOOL_ID='".str_replace("\'","''",$_REQUEST['values']['school_id'])."'";
		$array[1]['SCHOOL_ID'] = $_REQUEST['values']['school_id'];
	}


//die('jjjjjjj');
	if(isset($_REQUEST['values']['default_program']))
	{
		$title_extra .= ", DEFAULT_PROGRAM='".str_replace("\'","''",$_REQUEST['values']['default_program'])."'";

		if($array[1]['SCHOOL_ID']!='0')
			$default_array = db_request(db_execute("SELECT TITLE FROM LH_TEMPLATES WHERE DEFAULT_PROGRAM='".$_REQUEST['values']['default_program']."' AND ID!='".$_REQUEST['id']."' AND SCHOOL_ID='".$array[1]['SCHOOL_ID']."'"));	
		else
			$default_array = db_request(db_execute("SELECT TITLE FROM LH_TEMPLATES WHERE DEFAULT_PROGRAM='".$_REQUEST['values']['default_program']."' AND ID!='".$_REQUEST['id']."' AND SCHOOL_ID='0'"));	
	}

	if(isset($_REQUEST['values']['default_program_for_teachers']))
	{
		$title_extra .= ", DEFAULT_PROGRAM_FOR_TEACHERS='".str_replace("\'","''",$_REQUEST['values']['default_program_for_teachers'])."'";

		if($array[1]['SCHOOL_ID']!='0')
			$default_teachers_array = db_request(db_execute("SELECT TITLE FROM LH_TEMPLATES WHERE DEFAULT_PROGRAM_FOR_TEACHERS='".$_REQUEST['values']['default_program_for_teachers']."' AND ID!='".$_REQUEST['id']."' AND SCHOOL_ID='".$array[1]['SCHOOL_ID']."'"));	
		else
			$default_teachers_array = db_request(db_execute("SELECT TITLE FROM LH_TEMPLATES WHERE DEFAULT_PROGRAM_FOR_TEACHERS='".$_REQUEST['values']['default_program_for_teachers']."' AND ID!='".$_REQUEST['id']."' AND SCHOOL_ID='0'"));	
	}
	if(isset($_REQUEST['values']['page_breaks_options']))
		$title_extra .= ", PAGE_BREAKS='".str_replace("\'","''",$_REQUEST['values']['page_breaks_options'])."'";
	
	if($default_array)
	{
		$text1 = _('Confirm Default');
		$text2 = $default_array[1]['TITLE'];
	}
	if($default_teachers_array)
	{
		$text1 = _('Confirm Default for Teachers, Parents & Students');
		$text2 = $default_teachers_array[1]['TITLE'];
	}
	
	if((!isset($_REQUEST['values']['default_program']) && !isset($_REQUEST['values']['default_program_for_teachers'])) || (!count($default_array) && !count($default_teachers_array)) || (Prompt(_($text1),sprintf(_('A different template, %s, is already set as the default for %s. Are you sure that you want to overwrite this default?'),$text2,ProgramTitle($_REQUEST['values']['default_program'])))))
	{
		if(isset($_REQUEST['values']['default_program']))
			db_execute("UPDATE LH_TEMPLATES SET DEFAULT_PROGRAM=NULL WHERE DEFAULT_PROGRAM='".$_REQUEST['values']['default_program']."' AND SCHOOL_ID='".$array[1]['SCHOOL_ID']."' AND ID!='".$_REQUEST['id']."'");
			
		if(isset($_REQUEST['values']['default_program_for_teachers']))
			db_execute("UPDATE LH_TEMPLATES SET DEFAULT_PROGRAM_FOR_TEACHERS=NULL WHERE DEFAULT_PROGRAM_FOR_TEACHERS='".$_REQUEST['values']['default_program_for_teachers']."' AND SCHOOL_ID='".$array[1]['SCHOOL_ID']."' AND ID!='".$_REQUEST['id']."'");
			
		$sql = "UPDATE LH_TEMPLATES SET HEADER='".str_replace("\'","''",$_REQUEST['header'])."',FOOTER='".str_replace("\'","''",$_REQUEST['footer'])."'".$title_extra." WHERE ID='".$_REQUEST['id']."'";
		db_execute($sql);
		unset($_REQUEST['header']);
		unset($_REQUEST['footer']);
		unset($_REQUEST['servicefunction']);
		unset($_REQUEST['values']);
		unset($_SESSION['_REQUEST_vars']['header']);
		unset($_SESSION['_REQUEST_vars']['footer']);
		unset($_SESSION['_REQUEST_vars']['servicefunction']);
		unset($_SESSION['_REQUEST_vars']['values']);
	}
}

if($_REQUEST['new_title'] && AllowEdit())
{
	$id = db_request(db_execute("SELECT ".db_seq_nextval('LH_TEMPLATES_SEQ')." AS ID".FROM_DUAL));
	$id = $id[1]['ID'];
	db_execute("INSERT INTO LH_TEMPLATES (ID,TITLE,SCHOOL_ID) values('$id','".str_replace("\'","''",$_REQUEST['new_title'])."','".get_school_id()."')");

	$_REQUEST['id'] = $id;
	unset($_REQUEST['new_title']);
	unset($_SESSION['_REQUEST_vars']['new_title']);
	unset($_REQUEST['servicefunction']);
	unset($_SESSION['_REQUEST_vars']['servicefunction']);
}

if($_REQUEST['servicefunction']=='delete' && AllowEdit())
{
	if(DeletePrompt(_('Letterhead Template')))
	{
		db_execute("DELETE FROM LH_TEMPLATES WHERE ID='".$_REQUEST['id']."'");
		unset($_REQUEST['servicefunction']);
		unset($_REQUEST['id']);
		unset($_SESSION['_REQUEST_vars']['servicefunction']);
		unset($_SESSION['_REQUEST_vars']['id']);
	}
}

if(!$_REQUEST['servicefunction'])
{
	if($_SYSTEM['direction'])
		$arrow_right = 'arrow_left';
	else
		$arrow_right = 'arrow_right';

	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=update&id=$_REQUEST[id] method=POST>";
	DisplayHeader('<DIV style="width:800;padding-left:10px;">'._('To create a letterhead template use the formatting palette above the text area. Letterhead templates have two sections: a header which appears above the reports content and a footer which appears below the content.').'<BR><BR>'._('If you want to add students data into your letterhead templates, you have to click the Insert Field button ').'<IMG SRC=modules/mod_students/assets/insert.gif>'._(' in the bottom right of the formatting palette and choose any field.').'</DIV>',$submitButton->createSButton(_('Save')));
	echo '<BR>';
	echo '<TABLE width=100%><TR><TD valign=top width=200>';
	echo '<TABLE border=0 cellpadding=0 cellspacing=0 width=100%>';

	$schools = substr(str_replace(",","','",get_user('SCHOOLS')),2,-2);
	if($schools)
		$schools_extra .= "WHERE SCHOOL_ID IN ('0',$schools)";
	else
		$schools_extra = '';

	$letterhead_array = db_request(db_execute("SELECT ID,TITLE FROM LH_TEMPLATES ".$schools_extra." ORDER BY TITLE"));
	echo '<TR><TD colspan=3 style="border-width:1; border-style: none none solid none;"><b><small>'._('Letterhead Templates').'</small></b></TD></TR>';
	if(count($letterhead_array))
	{
		foreach($letterhead_array as $template)
		{
			if($template['ID']==$_REQUEST['id'])
			{
				echo '<TR id=selected_tr onmouseover="" onmouseout="" bgcolor="#'.Preferences('HIGHLIGHT').'" style="color:white;"><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('remove','',"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=delete&id=$template[ID]",20).'</TD><TD '.$style.' onclick="document.location.href=\'modloader.php?servicemod='.$_REQUEST['servicemod'].'&id='.$template['ID'].'\';">';
				$this_title = $template['TITLE'];
			}
			else
				echo '<TR onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="background-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('remove','',"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=delete&id=$template[ID]",20).'</TD><TD'.$style.' onclick="document.location.href=\'modloader.php?servicemod='.$_REQUEST['servicemod'].'&id='.$template['ID'].'\';">';
			echo '<A style="cursor:pointer;"><small>'.$template['TITLE'].' &nbsp; </small></A>';
			echo '</TD>';
			echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=images/'.$arrow_right.'.gif></A></TD>';
			echo '</TR>';
			$style = ' style="border-width:1; border-style: dashed none none none;"';
		}
	}
	if(!count($letterhead_array) || !$_REQUEST['id'])
		echo '<TR id=selected_tr><TD height=0></TD></TR>';

	echo '<TR id=new_tr onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="background-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD'.$style.'>';
	echo '<A style="cursor:pointer;" onclick=\'document.getElementById("selected_tr").onmouseover="this.style.backgroundColor=\"#'.Preferences('HIGHLIGHT').'\"; this.style.color=\"white\";"; document.getElementById("selected_tr").onmouseout="this.style.cssText=\"background-color:transparent; color:black;\";"; document.getElementById("selected_tr").style.cssText="background-color:transparent; color:black;"; document.getElementById("new_id_div").style.display="block";document.getElementById("new_title_input").focus();document.getElementById("main_div").style.display="none";document.getElementById("new_tr").onmouseover="";document.getElementById("new_tr").onmouseout="";this.onclick="";\'><small> '._('Add a Letterhead Template').' &nbsp;<BR><DIV id=new_id_div style="display:none;">'._('Title').' <INPUT type=text id=new_title_input name=new_title></DIV> </small></A>';
	echo '</TD>';
	echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=images/'.$arrow_right.'.gif></A></TD>';
	echo '</TR>';
	
	echo '</TABLE>';
	echo '</TD><TD width=75%>';
	echo '<DIV id=main_div>';
	if($_REQUEST['id'])
	{
		$array = db_request(db_execute("SELECT HEADER,FOOTER,TITLE,SCHOOL_ID,DEFAULT_PROGRAM,ORIENTATION,PAGE_BREAKS,DEFAULT_PROGRAM_FOR_TEACHERS FROM LH_TEMPLATES WHERE ID='".$_REQUEST['id']."'"));
		include_once($staticpath."js/fckeditor/fckeditor.php");
		
		if($array[1]['ORIENTATION']=='landscape')
			$width = 950;
		else
			$width = 700;

		PopTable('header',false,'width='.$width,0);
		echo '<TABLE>';
		echo '<TR><TD><b>'._($item).':</b></TD><TD>'.TextInput($array[1]['TITLE'],'values[title]').'</TD></TR>';
		
		$schools = substr(str_replace(",","','",get_user('SCHOOLS')),2,-2);
		$schools_arr[0] = _('All Schools');
		$choose_schools = false;
		if($schools)
		{	
			$schools = explode(',',str_replace("'",'',$schools));
			foreach($schools as $school)
				$schools_arr[$school] = get_school($school);
			if(count($schools_arr)>1)
				$choose_schools = true;
		}
		else
		{
			$schools_array = db_request(db_execute("SELECT ID,TITLE FROM SCHOOLS"));
			if(count($schools_array)>1)
			{
				$choose_schools = true;
				foreach($schools_array as $school)
					$schools_arr[$school['ID']] = $school['TITLE'];
			}
		}
		
		if($choose_schools)
			echo '<TR><TD><b>'._('School').':</b></TD><TD>'.SelectInput($array[1]['SCHOOL_ID'],'values[school_id]',null,$schools_arr,false,null,'force').'</TD></TR>';
		$template_programs = array('mod_grades/GrdPrintStudentReportCards.php'=>_('Report Cards'),'mod_evaluation/BncGetStudReportCard.php'=>_('Benchmark Report Cards'),'mod_grades/GrdStudentPrintGC.php'=>_('Transcripts'),'mod_grades/GrdBookReportWorkspace.php'=>_('Gradebook Progress Reports'),'mod_students/StuLetterWorkspace.php'=>_('Print Letters & Send Emails'),'mod_curriculum/ScdPRStudSchedWorkspace.php'=>_('Print Schedules'),'mod_curriculum/ScdPRStudReqWorkspace.php'=>_('Print Requests'),'mod_curriculum/ScdPReportStudClassList.php'=>_('Print Class Lists'),'mod_billing/BilStatReport.php'=>_('Print Statements'));
		echo '<TR><TD><b>'._('Default for').':</b></TD><TD>'.SelectInput($array[1]['DEFAULT_PROGRAM'],'values[default_program]',null,$template_programs,_('No Programs'),null,'force').'</TD></TR>';
		echo '<TR><TD><b>'._('Default for Teachers, Parents & Students').':</b></TD><TD>'.SelectInput($array[1]['DEFAULT_PROGRAM_FOR_TEACHERS'],'values[default_program_for_teachers]',null,$template_programs,_('No Programs'),null,'force').'</TD></TR>';

		$orientations = array('portrait'=>_('Portrait'),'landscape'=>_('Landscape'));
		echo '<TR><TD><b>'._('Page Orientation').':</b></TD><TD>'.SelectInput($array[1]['ORIENTATION'],'values[orientation]',null,$orientations,_('Auto'),null,'force').'</TD></TR>';

		$pageBreaksOptions = array(0 =>_("Include Hard Page Breaks between Students"), 1 =>_("Don't Include Hard Page Breaks"));
		echo '<TR><TD><b>'._('Include Hard Page Breaks between Students').':</b></TD><TD>'.SelectInput($array[1]['PAGE_BREAKS'],'values[page_breaks_options]',null,$pageBreaksOptions,false,null,'force').'</TD></TR>';

		echo '</TABLE><BR>';
		echo '<b>'._('Header').': </b>';
		$oFCKeditor = new FCKeditor('header',null,800);
		$oFCKeditor->BasePath = 'js/fckeditor/';
		$oFCKeditor->ToolbarSet = 'Letterhead';
		$oFCKeditor->Value = $array[1]['HEADER'];
		echo $oFCKeditor->Create();
		echo '<b><BR><BR>'._('Footer').':</b>';
	
		$oFCKeditor = new FCKeditor('footer',null,800);
		$oFCKeditor->BasePath = 'js/fckeditor/';
		$oFCKeditor->Value = $array[1]['FOOTER'];
		$oFCKeditor->ToolbarSet = 'Letterhead';
		echo $oFCKeditor->Create();
		echo '<CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
		PopTable('footer');
	}
	echo '</DIV>';
	echo '</TD></TR></TABLE>';
	echo '</FORM>';
}
?>