Location: PHPKode > projects > MyOrgBook > myorgbook/ManageTask.php
<?php
/*
Purpose: Manage (add, update, delete, complete) tasks

$Header: /cvsroot/myorgbook/unstable/ManageTask.php,v 1.27 2003/10/19 14:24:41 mik3 Exp $
$Log: ManageTask.php,v $
Revision 1.27  2003/10/19 14:24:41  mik3
Timezone stuff

Revision 1.26  2003/10/04 19:01:55  mik3
Made timeformat an users choice (24h / am/pm)

Revision 1.25  2003/09/09 15:15:41  mik3
Bugfix in show reminder code

Revision 1.24  2003/09/07 16:08:46  mik3
Refined reminder function

Revision 1.23  2003/08/24 11:22:26  mik3
Simplified date input/output (to GNU standard)

Revision 1.22  2003/08/24 08:03:58  mik3
Made little icons which show the priority of a task in the managetask screen

Revision 1.21  2003/08/17 20:15:56  mik3
Changed the default prio for a new task to medium instead of low

Revision 1.20  2003/08/13 18:12:56  mik3
Changed the way feedback behaves

Revision 1.19  2003/08/09 15:06:29  mik3
.

Revision 1.18  2003/08/09 06:28:08  mik3
Bugfix

Revision 1.17  2003/08/09 05:28:38  mik3
Clearified use of reminder (put 'days' in the template)

Revision 1.16  2003/08/07 18:46:32  mik3
bugfix

Revision 1.15  2003/08/02 13:44:38  mik3
Layout changes, language changes, theme support changes. Iow version 2.5

Revision 1.14  2003/07/06 19:35:59  mik3
Bugfix

Revision 1.13  2003/07/05 21:31:59  mik3
bugfix in statistical SQL query

Revision 1.12  2003/07/05 14:20:38  mik3
Added statistical logging

Revision 1.11  2003/07/04 15:51:49  mik3
Added reminder functionality

Revision 1.10  2003/06/20 22:04:22  mik3
Changed how language works, fixed bugs, made small other enhancements.......

*/

include ('inc/session.inc');
include ('inc/calendar.inc'); 
include ('inc/functions.inc');
include ('inc/template.inc');

// CHecking for external error's
If (isset($_REQUEST['Info'])) $Info = $_REQUEST['Info'];
If (isset($_REQUEST['Error'])) $Error = $_REQUEST['Error'];

// Calculating difference between user timezone and GMT
$diff_hours = round($_SESSION["TimeZone"]/100);
$diff_minutes = substr($_SESSION["TimeZone"],0,1) . substr($_SESSION["TimeZone"],3,2);
$diff = ($diff_hours * 3600) + ($diff_minutes * 60);

// Reporting a task finished
// Breaking the date so we can use it in the form
if (!isset($_REQUEST['MyDate'])) {
	$MyDate = mktime();
} else{
	$MyDate = $_REQUEST['MyDate'];
}

If (isset($_REQUEST['Action'])) {

If ($_REQUEST['Action'] == "Done") {
	//execute query
	$result = $db->Execute("UPDATE myorgbook_tasks SET taskdone = 1 WHERE taskid =" . $_REQUEST['taskid']);
	// check if query is succesfull
	if ($result == false) die(msg('Query Failed'));
}

// Reporting a finished task unfinished
If ($_REQUEST['Action'] == "NotDone") {
	//execute query
	$result = $db->Execute("UPDATE myorgbook_tasks SET taskdone = 0 WHERE taskid =" . $_REQUEST['taskid']);
	// check if query is succesfull
	if ($result == false) die(msg('Query Failed'));
}

// The actual Deleting of a Task
If ($_REQUEST['Action'] == "Delete") {
	// execute delete query
	$result = $db->Execute("DELETE FROM myorgbook_tasks WHERE taskid =" . $_REQUEST['taskid']);
	// check if query is succesfull
	if ($result == false) die(msg('Query Failed'));
}

// The actual adding of task, a date is used so the page returns to the date where the user was
If ($_REQUEST['Action'] == "Add") {

	// Checking for title
	if ($_REQUEST['ttitle'] == "") {
		// Something is missing
		$AnError = msg('The title is a required field!');
	} else {
		$date = $_REQUEST['Task_Date'];
		$time = $_REQUEST['Task_Time'];
		$date = strtotime($date . " " . $time);
		// Setting to GMT
		$date = $date - $diff;

		if ($_REQUEST['Reminder_Date'] != '') {
			$reminder_date = strtotime($_REQUEST['Reminder_Date'] . " " . $_REQUEST['Reminder_Hours'] . ":00");
			// Setting to GMT
			$reminder_date = $reminder_date + $diff;
			$query = "INSERT INTO myorgbook_tasks (uemail, ttitle, task, print, date, reminder_date) VALUES ('" . $_SESSION["Email"] . "',"
			. $db->qstr(StripHTML($_REQUEST['ttitle']),get_magic_quotes_gpc()) . ","
			. $db->qstr(StripHTML($_REQUEST['task']),get_magic_quotes_gpc()) . ",'" . $_REQUEST['print'] . "', $date, $reminder_date)";
		} else {
			$reminder_date = '';
			$query = "INSERT INTO myorgbook_tasks (uemail, ttitle, task, print, date) VALUES ('" . $_SESSION["Email"] . "',"
			. $db->qstr(StripHTML($_REQUEST['ttitle']),get_magic_quotes_gpc()) . ","
			. $db->qstr(StripHTML($_REQUEST['task']),get_magic_quotes_gpc()) . ",'" . $_REQUEST['print'] . "', $date)";
		}
		// executing the query
 		$result = $db->Execute($query);
 		// check if query is succesfull and show the success
 		if ($result == false) die(msg('Query Failed'));
		$Timestamp = strtotime ("now");
		$db->Execute("INSERT INTO myorgbook_stat_tasks (timestamp,username) VALUES ('$Timestamp','" . $_SESSION["Email"] . "')");
		header("Location: http://".$_SERVER['SERVER_NAME']."/".substr($_SERVER['PHP_SELF'], 1, strrpos($_SERVER['PHP_SELF'],"/")) . "ManageTask.php?MyDate=" . $MyDate . "&Info=" . msg('The task was successfully added'));
		exit;
	}
}


If ($_REQUEST['Action'] == "Update") {

	// Checking for title
	if ($_REQUEST['ttitle'] == "") {
		// Something is missing
		$AnError = msg('The title is a required field!');
	} else {
		$date = $_REQUEST['Task_Date'];
		$time = $_REQUEST['Task_Time'];
		$date = strtotime($date . " " . $time);
		// Setting to GMT
		$date = $date - $diff;

		if ($_REQUEST['Reminder_Date'] != '') {
			$reminder_date = strtotime($_REQUEST['Reminder_Date'] . " " . $_REQUEST['Reminder_Hours'] . ":00");
			// Setting to GMT
			$reminder_date = $reminder_date + $diff;
		} else {
			$reminder_date = '-1';
		}
		// Assembling update query
		$query = "UPDATE myorgbook_tasks SET
		ttitle =" . $db->qstr(StripHTML($_REQUEST['ttitle']),get_magic_quotes_gpc()) . ",
		task =" . $db->qstr(StripHTML($_REQUEST['task']),get_magic_quotes_gpc()) . ",
		print = '" . $_REQUEST['print'] . "', date = $date, reminder_date = '$reminder_date' WHERE taskid =" . $_REQUEST['taskid'];
		// executing the query
		$result = $db->Execute($query);
 		// check if query is succesfull and show the success
 		if ($result == false) die(msg('Query Failed'));
		header("Location: http://".$_SERVER['SERVER_NAME']."/".substr($_SERVER['PHP_SELF'], 1, strrpos($_SERVER['PHP_SELF'],"/")) . "ManageTask.php?MyDate=" . $MyDate . "&Info=" . msg('The task is successfully updated'));
		exit;
	}
}

// Retrieving data needed to show details of task
If ($_REQUEST['Action'] == "Details") {
	// Defining query
	$query = "SELECT * FROM myorgbook_tasks WHERE uemail = '" . $_SESSION["Email"] . "' AND taskid =" . $_REQUEST['taskid'];
	$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
	// executing query
	$result = $db->Execute($query);
	// Getting results or if query failed generate an error message
	if ($result == false) die(msg('Query Failed'));
	$r = $result->fields;

	$task 	= $r["task"];
	$ttitle = $r["ttitle"];
	$taskid = $r["taskid"];
	$MyDate = $r["date"];
	$print  = $r["print"];
	$reminder_date = $r["reminder_date"];
}

} else {
	$task 	= '';
	$ttitle = '';
	$print  = 'medium';
}
	
$MonthNames = msg('MonthNames');
$month_number = date("n",$MyDate);
$month = $MonthNames[$month_number];
$year = date("Y",$MyDate);
$numberofdays = date("t",$MyDate);
$month_day = date("j",$MyDate);

// Option filler function for reminder
function option_select($select, $t_first, $t_last){
	$returnvar = "";
	for($t=$t_first; $t <= $t_last; $t++){
		if ($_SESSION["TimeFormat"] == 1) {
			// 24h
			$TF = "h";
			$tt = $t;
			$tv = $t;
		} else {
			// am/pm nevermind the algebra, just to ensure both give the same value while
			// looking good for the user.
			if ($t < 12) {
				$TF = " am";
				$tt = $t + 1;
				$tv = $t + 1;
			} else {
				$tt = $t - 11;
				$tv = $t + 1;
				if ($tv == 24) $tv = 0;
				$TF = " pm";
			}
		}
  		if ($select == $t){
			$returnvar .= "<option value=$tv selected>" . $tt . $TF . "</option>";
		}else{
			$returnvar .= "<option value=$tv>" . $tt . $TF . "</option>";
		}
	}
return $returnvar;
}


// Setup the template
$MyPage = New Template("Templates/$Theme");
$MyPage->set_file(array("MyHeaderHandle" => "header.tpl",
		"MyFileHandle" => "ManageTasks.tpl",
		"MyError" => "error.tpl",
		"MyInfo" => "info.tpl",
		"MyTableElementHandle_high" => "ManageTasks_table_element_high.tpl",
		"MyTableElementHandle_medium" => "ManageTasks_table_element_medium.tpl",
		"MyTableElementHandle_low" => "ManageTasks_table_element_low.tpl"));

// Checking for error
if ($Error != "") {
	$MyPage->set_var("error_text",$Error);
	$MyPage->parse("Error_Element","MyError");
}
// Checking for info
if ($Info != "") {
	$MyPage->set_var("info_text",$Info);
	$MyPage->parse("Info_Element","MyInfo");
}
// Setting the variables
include ('inc/Templateheader.inc');

// parsing header
$MyPage->parse("header","MyHeaderHandle",true);

// Setting up Calendar
$cal = new MainCalendar;
$cal->setStartDay(1);
$cal->dayNames = msg('DayCharNames');
$cal->monthNames = msg('MonthNames');

// Setting up the works to retrieve the tasks that day
$databasedate = date("Y-m-d", $MyDate);
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$date1 = strtotime("$databasedate 00:00");
$date2 = strtotime("$databasedate 23:59");
$result = $db->Execute("SELECT * FROM myorgbook_tasks WHERE uemail = '" . $_SESSION["Email"] . "' AND date BETWEEN '$date1' AND '$date2' ORDER BY 'date'");
if ($result == false) die(msg('Query Failed'));
$num = $result->RecordCount();

// setting up general form stuff (static and refreshto things)
If ((isset($_REQUEST['Action'])) && ($_REQUEST['Action'] == "Details")) {
	$MainRefreshName = $_SERVER['PHP_SELF'] . "?Action=Update&taskid=$taskid&MyDate=$MyDate";
	$MainFormTitle = msg('Update Task');
} else {
	$MainRefreshName = $_SERVER['PHP_SELF'] . "?Action=Add&MyDate=$MyDate";
	$MainFormTitle = msg('Add Task');
}

// Setting up an error message (if needed)
if ($AnError != "") $MyPage->set_var("Error","<b>" . $AnError . "</b><br><br>");

$MyPage->set_var("MainRefreshFormName",$MainRefreshName);
$MyPage->set_var("MainFormTitle",$MainFormTitle);
$MyPage->set_var("Date:",msg('Date:'));
$MyPage->set_var("Time:",msg('Time:'));
$MyPage->set_var("Task Title:",msg('Task Title:'));
$MyPage->set_var("Priority:",msg('Priority:'));
$MyPage->set_var("Task:",msg('Task:'));
$MyPage->set_var("check message length",msg('check message length'));
$MyPage->set_var("Today's Tasks",msg('Today\'s Tasks'));
$MyPage->set_var("Delete",msg('Delete'));
$MyPage->set_var("Details",msg('Details'));
$MyPage->set_var("date",$month_day . " " . $month . " " . $year);
$MyPage->set_var("Report",msg('Report'));
$MyPage->set_var("Timestamp",$MyDate);
$MyPage->set_var("Reminder:",msg('Reminder:'));
$MyPage->set_var("Back",msg('Back'));
$MyPage->set_var("Add Task",msg('Add Task'));
$MyPage->set_var("NegLang",$_SESSION["Language"]);

// setting up right side menu
$MyPage->set_var("Calendar",msg('Calendar'));

// setting up select boxes
// date
// Setting to Users Timezone
$MyDate = $MyDate + $diff;
$MyPage->set_var("Task_Date",date("j F Y",$MyDate));
$MyPage->set_var("MyDate",$MyDate);
// time
if ($_SESSION["TimeFormat"] == 1) {
	//24h
	$MyPage->set_var("Task_Time",date("G:i",$MyDate));
} else {
	// am/pm
	$MyPage->set_var("Task_Time",date("g:i a",$MyDate));
}

//reminder Stuff
// If task is not remind.
if ($reminder_date == '-1' || $reminder_date == '') {
	$reminder_date = $reminder_date + $diff;
	$reminder_option_selects = option_select(0,0,23);
	$MyPage->set_var("Reminder_Hours",$reminder_option_selects);
	$MyPage->set_var("Reminder_Date",'');
} else {
	$reminder_hour_selected = date("G",$reminder_date);
	$reminder_option_selects = option_select($reminder_hour_selected,0,23);
	$MyPage->set_var("Reminder_Hours", $reminder_option_selects);
	$MyPage->set_var("Reminder_Date",date("j F Y",$reminder_date));
}

//Setting up dynamic info
$MyPage->set_var("calendar",$cal->getMonthView($month_number, $year));
$MyPage->set_var("text_ttitle", $ttitle);
$MyPage->set_var("Select_" . $print,"selected");
$MyPage->set_var("text_task",$task);

if ($num == "0") {
        $MyPage->set_var("Table_Elements",$lang_main_nothing);
        $MyPage->pparse("output","MyFileHandle");
	exit;
} else {
	while (!$result->EOF) {
		$r = $result->fields;
		$ttitle = $r["ttitle"];
		$taskid = $r["taskid"];
		$date = $r["date"];
		// Setting to users Timezone
		$date = $date + $diff;
		if ($_SESSION["TimeFormat"] == 1) {
			// 24h
			$time = date("G:i",$date);
		} else {
			// am/pm
			$time = date("g:i a",$date);
		}
		if ($r["taskdone"] == 1) {
			$time = "<s>$time</s>";
			$ttitle = "<s>$ttitle</s>";
			$MyPage->set_var("Task_Done","NotDone");
			$MyPage->set_var("lang_text_done",msg('Not Done'));
		} else {
			$MyPage->set_var("Task_Done","Done");
			$MyPage->set_var("lang_text_done",msg('Done'));
		}
		$MyPage->set_var("te_time",$time);
		$MyPage->set_var("te_ttitle",$ttitle);
		$MyPage->set_var("te_taskid",$taskid);
		$MyPage->parse("Table_Elements","MyTableElementHandle_" . $r["print"],true);
		$result->MoveNext();
	}
}
// parsing output
$MyPage->pparse("output","MyFileHandle");
?>
Return current item: MyOrgBook