<?php
/*
 * Copyright (C) 2007, M2X
 *
 * Authors: Jean-Paul Saman
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

// create a new instance of the timesheet class
$obj = new CTimesheet();
$msg = '';	// reset the message string

// bind the informations (variables) retrieved via post to the timesheet object
if (!$obj->bind( $_POST )) {
	$AppUI->setMsg( $obj->getError(), UI_MSG_ERROR );
	$AppUI->redirect();
}

// detect if a deleete operation has to be processed
$period = dPgetParam($_POST, 'period', 0);
$stat = dPgetParam($_POST, 'stat', 0);
$del = dPgetParam($_POST, 'del', 0);

$obj->timesheet_id = $_POST["timesheet_id"];
$obj->timesheet_status = $_POST["timesheet_status"];
$obj->timesheet_period = $_POST["timesheet_period"];
$obj->timesheet_date = $_POST["timesheet_date"];
$obj->timesheet_creator = $_POST["timesheet_creator"];
$obj->timesheet_worked = $_POST["timesheet_worked"];

$AppUI->setMsg( 'Timesheet' );
if ($del) {
	// check if there are dependencies on this object
	if (!$obj->canDelete( $msg )) {
		$AppUI->setMsg( $msg, UI_MSG_ERROR );
		$AppUI->redirect();
	}
	// see how easy it is to run database commands with the object oriented architecture !
	// simply delete a quote from db and have detailed error or success report
	if ($msg = $obj->delete()) {
		$AppUI->setMsg( $msg, UI_MSG_ERROR );
		$AppUI->redirect();
	} else {
		$AppUI->setMsg( "Timesheet deleted", UI_MSG_ALERT);
		$AppUI->redirect( "m=timesheet" );
	}
} 
else if ($stat) {
	if ($msg = $obj->change_status($obj->timesheet_status)) {
		$AppUI->setMsg( $msg, UI_MSG_ERROR );
		$AppUI->redirect();
	} else {
		$AppUI->setMsg( "Timesheet status updated", UI_MSG_ALERT);
		$AppUI->redirect( "m=timesheet&a=addedit&timesheet_id=".$obj->timesheet_id );
	}
}
else if ($period) {
	if ($msg = $obj->change_period($obj->timesheet_period)) {
		$AppUI->setMsg( $msg, UI_MSG_ERROR );
		$AppUI->redirect();
	} else {
		$AppUI->setMsg( "Timesheet period changed", UI_MSG_ALERT);
		$AppUI->redirect( "m=timesheet&a=addedit&timesheet_id=".$obj->timesheet_id );
	}
}
else {
	// simply store the added/edited quote in database via the store method of the timesheet 
	// child class of the CDpObject provided ba the dPFramework
	if (($msg = $obj->store())) {
		$AppUI->setMsg( $msg, UI_MSG_ERROR );
	} else {
		$isNotNew = @$_POST['timesheet_id'];
		$AppUI->setMsg( $isNotNew ? 'Timesheet updated' : 'Timesheet inserted', UI_MSG_OK);
	}
        $AppUI->redirect("m=timesheet" );
}
?>