<?php

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

$_SESSION['orientation'] = 'landscape';
if($_REQUEST['day_values'] && $_POST['day_values'])
{
	foreach($_REQUEST['day_values'] as $id=>$values)
	{
		if($_REQUEST['day_values'][$id]['START_DATE'] && $_REQUEST['month_values'][$id]['START_DATE'] && $_REQUEST['year_values'][$id]['START_DATE'])
			$_REQUEST['values'][$id]['START_DATE'] = $_REQUEST['year_values'][$id]['START_DATE'].'-'.$_REQUEST['month_values'][$id]['START_DATE'].'-'.$_REQUEST['day_values'][$id]['START_DATE'];
		elseif(isset($_REQUEST['day_values'][$id]['START_DATE']) && isset($_REQUEST['month_values'][$id]['START_DATE']) && isset($_REQUEST['year_values'][$id]['START_DATE']))
			$_REQUEST['values'][$id]['START_DATE'] = '';
			
		if($_REQUEST['day_values'][$id]['END_DATE'] && $_REQUEST['month_values'][$id]['END_DATE'] && $_REQUEST['year_values'][$id]['END_DATE'])
			$_REQUEST['values'][$id]['END_DATE'] = $_REQUEST['year_values'][$id]['END_DATE'].'-'.$_REQUEST['month_values'][$id]['END_DATE'].'-'.$_REQUEST['day_values'][$id]['END_DATE'];
		elseif(isset($_REQUEST['day_values'][$id]['END_DATE']) && isset($_REQUEST['month_values'][$id]['END_DATE']) && isset($_REQUEST['year_values'][$id]['END_DATE']))
			$_REQUEST['values'][$id]['END_DATE'] = '';
	}
	if(!$_POST['values'])
		$_POST['values'] = $_REQUEST['values'];
}

$profiles_array = db_request(db_execute("SELECT ID,TITLE FROM USER_PROFILES"));
$profiles_array[(count($profiles_array)+1)] = array('ID'=>0,'TITLE'=>_('Other Users'));
if((($_REQUEST['profiles'] && $_POST['profiles']) || ($_REQUEST['values'] && $_POST['values'])) && AllowEdit())
{
	$notes_array = db_request(db_execute("SELECT ID FROM PORTAL_MESSAGES"));

	foreach($notes_array as $note_id)
	{
		$note_id = $note_id['ID'];
		if(count($_REQUEST['profiles'][$note_id]))
		{
			foreach($profiles_array as $profile)
			{
				$profile_id = $profile['ID'];
		
				if($_REQUEST['profiles'][$note_id][$profile_id])
					$_REQUEST['values'][$note_id]['PUBLISHED_PROFILES'] .= $profile_id.',';
			}
			$_REQUEST['values'][$note_id]['PUBLISHED_PROFILES'] = ','.$_REQUEST['values'][$note_id]['PUBLISHED_PROFILES'];
		}
		else
			$_REQUEST['values'][$note_id]['PUBLISHED_PROFILES'] = '';
	}
}

if($_REQUEST['values'] && $_POST['values'] && AllowEdit())
{
	foreach($_REQUEST['values'] as $id=>$columns)
	{		
		if($id!='new')
		{
			$sql = "UPDATE PORTAL_MESSAGES SET ";
							
			foreach($columns as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			db_execute($sql);
		}
		else
		{
			if(is_array($_REQUEST['profiles']['new']))
			{
				foreach($profiles_array as $profile)
				{
					$profile_id = $profile['ID'];
			
					if($_REQUEST['profiles']['new'][$profile_id])
						$_REQUEST['values']['new']['PUBLISHED_PROFILES'] .= $profile_id.',';
				}
				$columns['PUBLISHED_PROFILES'] = ','.$_REQUEST['values']['new']['PUBLISHED_PROFILES'];
			}
			else
				$_REQUEST['values']['new']['PUBLISHED_PROFILES'] = '';

			$sql = "INSERT INTO PORTAL_MESSAGES ";

			$fields = 'ID,PUBLISHED_DATE,PUBLISHED_USER,';
			$values = db_seq_nextval('PORTAL_MESSAGES_SEQ').",'".get_db_date()."','".get_user('USER_ID')."',";

			$go = 0;
			foreach($columns as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$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']);
	unset($_REQUEST['profiles']);
	unset($_SESSION['_REQUEST_vars']['profiles']);
}

if($_REQUEST['schools'] && $_POST['schools'] && AllowEdit())
{
    foreach($_REQUEST['schools'] as $id=>$columns)
	{
		$sql = "UPDATE PORTAL_MESSAGES SET ";

		foreach($columns as $column=>$value)
		{
			$sql .= $column."='".str_replace("\'","''",$value)."',";
		}
		$sql = substr($sql,0,-1) . " WHERE ID='$id'";
		db_execute($sql);
	}
}

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

if($_REQUEST['servicefunction']=='remove' && AllowEdit())
{
	if(DeletePrompt(_('message')))
	{
		db_execute("DELETE FROM PORTAL_MESSAGES WHERE ID='$_REQUEST[id]'");
		unset($_REQUEST['servicefunction']);
	}
}

if($_REQUEST['servicefunction']!='remove')
{
	$sql = "SELECT ID,SORT_ORDER,TITLE,CONTENT,START_DATE,END_DATE,PUBLISHED_PROFILES,SCHOOL_ID FROM PORTAL_MESSAGES ORDER BY PUBLISHED_DATE";
	$QI = db_execute($sql);
	$notes_array = db_request($QI,array('TITLE'=>'_makeTextInput','CONTENT'=>'_makeContentInput','SORT_ORDER'=>'_makeTextInput','START_DATE'=>'_makePublishing','SCHOOL_ID'=>'_makeSchools'));

	$columns = array('TITLE'=>_('Title'),'CONTENT'=>_('Note'),'SORT_ORDER'=>_('Sort Order'),'START_DATE'=>_('Publishing Options'));
    if(!get_user('SCHOOLS'))
        $columns['SCHOOL_ID']=_('Schools');
	//,'START_TIME'=>'Start Time','END_TIME'=>'End Time'
	$link['add']['html'] = array('TITLE'=>_makeTextInput('','TITLE'),'CONTENT'=>_makeContentInput('','CONTENT'),'SHORT_NAME'=>_makeTextInput('','SHORT_NAME'),'SORT_ORDER'=>_makeTextInput('','SORT_ORDER'),'START_DATE'=>_makePublishing('','START_DATE'),'SCHOOL_ID'=>_makeSchools(get_school_id(),'SCHOOL_ID',false));
	$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 method=POST>";
	DisplayHeader('',$submitButton->createSButton(_('Save')));
	Gridoutput($notes_array,$columns,_('Messages'),_('Amount of Messages'),$link);
	echo '<CENTER>'.$submitButton->createSButton(_('Save')).'</CENTER>';
	echo '</FORM>';
}

function _makeSchools($value,$name,$div=true)
{
    global $THIS_array;
    $options = array(get_school_id()=>get_school(get_school_id()),'0'=>_('All Schools'));

//	foreach($options as $key=>$opt)
//{
//	echo $key." - ".$opt."<br>";
//}	

    return SelectInput($value,'schools['.$THIS_array['ID'].']['.$name.']','',$options,false,'',$div,'', false);
}

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

function _makeContentInput($value,$name)
{	global $THIS_array;
	
	if($THIS_array['ID'])
		$id = $THIS_array['ID'];
	else
		$id = 'new';

	return TextareaInput($value,'values['.$id.']['.$name.']','','rows=8');
}

function _makePublishing($value,$name)
{	global $THIS_array,$profiles_array;

    $columns = 4;

	if($THIS_array['ID'])
	{
		$id = $THIS_array['ID'];
		$div = 'force';
	}
	else
		$id = 'new';

	$return = '<TABLE class=GridOutput_fieldint><TR><TD align=center><fieldset><legend align=center><font color=gray><b>'._('Visible Period of Time').':</b></font></legend><TABLE width=100% cellpadding="0" cellspacing="0"><TR><TD>'._('From').'</TD><TD align=left>';
	//$return .= DateInput($value,'values['.$id.']['.$name.']','',$div).'</TD><TD width=1%>'._(' & ').'</TD></TR><TR><TD>';
	$return .= DateInput($value,'values['.$id.']['.$name.']','',$div).'</TD><TD>'._('To ').'</TD><TD align=left>';
	$return .= DateInput($THIS_array['END_DATE'],'values['.$id.'][END_DATE]','',$div).'</TD></TR></TABLE></fieldset></TD></TR>';
	$return .= '<TR><TD colspan=4>';
	
	//$exceptions_array = db_request(db_execute("SELECT EDITOR,VIEWER,PROFILE_ID FROM PROFILE_SETTINGS WHERE MODNAME='mod_savedinfo/SIRunLink.php?id=$THIS_array[ID]'"),array(),array('PROFILE_ID'));

	$return .= '<fieldset><legend><b>'._('Visible To').': </b></legend><TABLE border=0 cellspacing=0 cellpadding=0 class=GridOutput_fieldint>';
	foreach($profiles_array as $profile)
	{
		$i++;
		$return .= '<TD><INPUT type=checkbox name=profiles['.$id.']['.$profile['ID'].'] value=Y'.(strpos($THIS_array['PUBLISHED_PROFILES'],','.$profile['ID'].',')!==false?' CHECKED':'').'> '.$profile['TITLE'].'</TD>';
		if($i%$columns==0 && $i!=count($profile))
			$return .= '</TR><TR>';
	}
	for(;$i%$columns!=0;$i++)
		$return .= '<TD></TD>';
	$return .= '</TR>';
	//<TR><TD colspan=5><B><A HREF=#>Schools: ...</A></B></TD></TR></TABLE>';
	$return .= '</TABLE></fieldset>';
	$return .= '</TD></TR></TABLE>';
	return $return;
}
?>