Location: PHPKode > projects > Web-NoteTaker > notetaker/include/time.php
<?php
// NoteTaker
// Created and Coded by Bo Ahlberg
// Copyright 2008 by Bo Ahlberg ( bahlberg at mac.com )
// This code is licensed "AS-IS", No warranty is expressed or implied as to the
// suitability of this software for any purpose. Further, this code is licensed
//	under the:
//    Creative Commons Attribution-Share Alike 3.0 United States License
// This means you are free to:
//		to Share - to copy, distribute, display, and perorm work
//	  to remix - to make derivative works
//  Under the Following Conditions:
//		Attribution - You must attribute the work in the manner 
//    		specified by the author or licensor (but not in any way 
//    		that suggests that they endorse you or your use of the work).
//    Share Alike. If you alter, transform, or build upon this work, 
//				you may distribute the resulting work only under the same, 
//				similar or a compatible license.
//	For any reuse or distribution, you must make clear to others the license 
//		terms of this work. The best way to do this is with a link to this web page.
//	Any of the above conditions can be waived if you get permission from the 
//		copyright holder.
//	Apart from the remix rights granted under this license, nothing in this 
//		license impairs or restricts the author's moral rights.
//

$timeMenu = array( 'New'        => 'new', 
                   'Edit'       => 'edit', 
                   'List'       => 'list' );

function timeMain(  )
{
	$mode = (array_key_exists( 'mode', $_REQUEST ))? $_REQUEST['mode'] : "default";
	$id   = (array_key_exists( 'id',   $_REQUEST ))? $_REQUEST['id']   : 0;
	$mnu  = makeRow( makeCell( 5, makeTimeMenu( ) ) );
	$bdy  = "";
	switch( $mode )
	{
		case 'post' :
			$bdy.= processTimeInput( $id );
			$bdy.= listTime( $id );
			break;
		case 'edit' :
			$bdy.= editTimeRecord( $id );
			$bdy.= listTime( $id );
			break;
		case 'new' :
			$bdy.= newTimeRecord( );
			break;
		default:
			$bdy.= listTime( $id );
			break;
	}
	return makeTable( 0, $mnu.makeRow( makeCell( 1, $bdy ) ) );
}

function makeTimeMenu( )
{	global $timeMenu;
	$form = "";
	foreach( $timeMenu as $index => $item )
	{
		$class = ( $item == $mode )? "ActiveItem" : "InactiveItem" ;
		$form.= makeCell( 1, makeActionLink( 'time', $item, '', '', $index ), $class );
	}
	$ret = makeTable( 0, makeRow( $form, 'pageMenubanner' ) );

	return $ret;
}

function listTime( $id )
{
	$ret = "";
	$rows = getTimeRecords( $id );
	$hdr = makeRow(	makeCell( 1, "Project", 'ActiveItem' )
								 .makeCell( 1, "Task",    'ActiveItem' )
								 .makeCell( 1, "Date",    'ActiveItem' )
								 .makeCell( 1, "Hours",   'ActiveItem' ) );

	$count = 1;
	foreach( $rows as $row )
	{
		$ret.= listTimeEntry( $row, $count );
		$count++;
	}
	return makeTable( 0, $hdr.$ret );
}

function listTimeEntry( $row, $count )
{ global $proplist;
	$ret = "";
	$class = ( ($count%2) == 0 )? 'kanbanEven' : 'kanbanOdd' ;
	if ( is_array( $row ) )
	{
		$ret.= makeCell( 1, $proplist['proj'][$row['projID']]['projName'], $class );
		$ret.= makeCell( 1, $proplist['task'][$row['taskID']]['taskName'], $class );
		$ret.= makeCell( 1, $row['tcDate'], $class  );
		$ret.= makeCell( 1, $row['tcHours'], $class );
		$lwr = makeCell( 4, "<B>Notes:</B><br>".$row['tcNotes'], $class );
	}
	else
	{
		$ret.= makeCell( 4, "No Time Entries", $class );
	}
	return makeRow( $ret );
}

function makeTimeEntry( $row )
{
	$hdr = makeRow(	makeCell( 1, "Project", 'ActiveItem' )
								 .makeCell( 1, "Task",    'ActiveItem' )
								 .makeCell( 1, "Date",    'ActiveItem' )
								 .makeCell( 1, "Hours",   'ActiveItem' ) );

	$ret = "";
	if ( is_array( $row ) )
	{
		$ret.= makeCell( 1, makeUserSelect( 'projID', 'proj',  $row['projID'], 'Project'  ));
		$ret.= makeCell( 1, makeUserSelect( 'taskID', 'task',  $row['taskID'], 'Task'     ));
		$ret.= makeCell( 1, makeUserDate(   'date',   'Date',  $row['tcDate']  ) );
		$ret.= makeCell( 1, makeUserInput(  'text',   'hours', '2', $row['tcHours'] ) );
		$lwr = makeCell( 4, makeUserNote(   'notes',  'Notes',  5,      80,  $row['tcNotes'] ) );
	}
	else
	{
		$ret.= makeCell( 1, makeUserSelect( 'projID', 'proj',  "", 'Project'  ));
		$ret.= makeCell( 1, makeUserSelect( 'taskID', 'task',  "", 'Task'     ));
		$ret.= makeCell( 1, makeUserDate(   'date',   'Date',  ""  ) );
		$ret.= makeCell( 1, makeUserInput(  'text',   'hours', '2',  "" ) );
		$lwr = makeCell( 4, makeUserNote(   'notes',  'Notes',  5,    80,  "" ) );
	}
	return makeTable( 0, $hdr.makeRow( $ret ).makeRow( $lwr ) );
}

function getTimeRecords( $id )
{	global $session;
	
	$query = "SELECT * FROM timecards WHERE userName = '".$session->username."'";
	
	$row[1] = "No Time Entries";
	$result = processQuery( $query );
	if ( $result )
	{
		$count = 1;
		while ( $rec = mysql_fetch_array( $result, MYSQL_ASSOC ) )
		{
			$row[$count] = $rec;
			$count++;
		}
	}
	else
		Message( 9, "Query Failed: ".$query );
	//Message( 2, $query );
	return $row;
}

function processTimeInput( $id )
{	global $session;
	// input fields are: proj, task, date, hours, notes 
	if ( $id == 0 )
	{	// new
		$query = "INSERT INTO timecards( projID, taskID, userName, tcDate, tcHours, tcNotes)";
		$query.=       " VALUES( ";
		$query.= ($_POST['projID'] != "" )? $_POST['projID'] : 1;
		$query.=       ", ";
		$query.= ($_POST['taskID'] != "" )? $_POST['taskID'] : 1;
		$query.=       ", '".$session->username."' ";
		$query.=       ", ";
		$query.= ($_POST['month'] != "" )? "date('".$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day']."' )" : date('2008-1-1');
		$query.=       ", ";
		$query.= ($_POST['hours'] != "" )? $_POST['hours'] : 1;
		$query.=       ", ";
		$query.= ($_POST['notes'] != "" )? "'".$_POST['notes']."'" : "";
		$query.= ")";
	}
	else
	{	// update
	}
	return makeTable( 0,makeRow( makeCell( 1, $query ) ) );
}

function editTimeRecord( $id )
{
	return makeTable( 0, makeRow( makeCell( 1, "Edit Time Record (".$id.")" ) ) );
}

function newTimeRecord(  )
{	global $session;
	
	$form.= makeRow( makeCell( $count, "New Timecard: ", 'kanbanTitle' ) );
	$count = 0;
	$row = "";
	$form.= makeTimeEntry( "" );
	
	$ret.= makeTable( 100, makeForm( 'time', 'post', $id, $prop, '', $form ) );

	return makeTable( 0, makeRow( makeCell( 1, $ret ) ) );
}

?>
Return current item: Web-NoteTaker