Commit 3f6e6356 authored by Jean-Paul Saman's avatar Jean-Paul Saman

Update and cleanup timesheet.class.php

parent 78011656
<?php
/*
* Copyright (C) 2007-2008, M2X
* Copyright (C) 2007-2014, M2X
*
* Authors: Jean-Paul Saman
*
......@@ -19,76 +19,82 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module timesheet
// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework
/**
* @package dotProject
* @package web2project
* @subpackage modules
* @version $Revision: 1.0 $
* @version $Revision: 2.0 $
*/
// include the powerful parent class that we want to extend for timesheet
// use the dPFramework for easy inclusion of this class here
require_once( $AppUI->getSystemClass('dp') );
if (!defined('W2P_BASE_DIR')) {
die('You should not access this file directly.');
}
/**
* The Timesheet Class
*/
class CTimesheet extends CDpObject {
class CTimesheet extends w2p_Core_BaseObject {
// link variables to the timesheet object (according to the existing columns in the database table timesheet)
//use null for a new object, so the database automatically assigns an unique id by 'not null'-functionality
var $timesheet_id = null;
var $timesheet_period = null;
var $timesheet_status = 0;
var $timesheet_date = null;
var $timesheet_creator = null;
var $timesheet_worked = null;
public $timesheet_id = null;
public $timesheet_period = null;
public $timesheet_status = 0;
public $timesheet_date = null;
public $timesheet_creator = null;
public $timesheet_worked = null;
// the constructor of the CTimesheet class, always combined with the table name and the unique key of the table
function CTimesheet() {
$this->CDpObject( 'timesheet', 'timesheet_id' );
public function __construct() {
parent::__construct('timesheet', 'timesheet_id');
$this->timesheet_id=$_POST["timesheet_id"];
}
function check() {
if ( $this->timesheet_id == "0" ) {
$this->init();
public function isValid() {
$baseErrorMsg = get_class($this) . '::store-check failed -';
if ($this->timesheet_id == 0) {
$this->_error['timesheet_id'] = $baseErrorMsg . 'timesheet id not set';
}
return NULL;
return (count($this->_error)) ? false : true;
}
public function loadFull() {
// loadFull information for this timesheet
}
function init() {
$q = new DBQuery();
protected function init() {
$q = $this->_getQuery();
$q->addTable('timesheet');
$q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked',
$_POST['timesheet_period'].','.$_POST['timesheet_status'].','.
$_POST['timesheet_date'].','.$_POST['timesheet_creator'].','.
$_POST['timesheet_worked'], true);
if (!$q->exec()) {
return dberror();
if (!($q->exec())) {
$this->_error['init-timesheet'] = db_error();
return false;
}
// TODO: move this to a postCreate hook
$this->timesheet_id = db_insert_id();
return true;
}
function project_purge()
protected function project_purge()
{
$q = new DBQuery();
$q = $this->_getQuery();
$q->setDelete('timesheet_project');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec())
return db_error();
$q->addWhere('timesheet_id = '. $this->timesheet_id);
if (!($q->exec())) {
$this->_error['project-purge-timesheet'] = db_error();
return false;
}
return true;
}
function project_store()
protected function project_store()
{
// Get the current timesheet period
$period = new CDate();
$period->setDate($_POST['timesheet_period'],DATE_FORMAT_UNIXTIME);
$period = new w2p_UtilitiesDate($_POST['timesheet_period']);
//$period->setDate($_POST['timesheet_period'],DATE_FORMAT_UNIXTIME);
$q = new DBQuery();
$q = $this->_getQuery();
$q->addQuery('project_id');
$q->addQuery('project_name');
$q->addQuery('project_status');
......@@ -96,7 +102,7 @@ class CTimesheet extends CDpObject {
$q->addOrder('project_name ASC');
$projects = $q->LoadList();
foreach ($projects as $row) {
$t = new DBQuery;
$t = $this->_getQuery();
$t->addQuery('task_id');
$t->addQuery('task_name');
$t->addQuery('task_owner');
......@@ -126,7 +132,7 @@ class CTimesheet extends CDpObject {
foreach ($log as $logitem) {
if (intval($logitem['task_log_date']))
{
$taskDate = new CDate($logitem['task_log_date']);
$taskDate = new w2p_Utilities_Date($logitem['task_log_date']);
if ($period->GetMonth() == $taskDate->GetMonth()) {
$amount = $amount + $logitem["task_log_hours"];
}
......@@ -134,83 +140,126 @@ class CTimesheet extends CDpObject {
}
}
}
$sheet = new DBQuery();
$sheet = $this->_getQuery();
$sheet->addTable('timesheet_project');
$sheet->addInsert('timesheet_id,timesheet_project,timesheet_project_amount',
$this->timesheet_id.','.$row["project_id"].','.$amount, true);
if (!$sheet->exec()) {
return db_error();
if (!($q->exec())) {
$this->_error['project-purge-timesheet'] = db_error();
return false;
}
}
return true;
}
function store() {
if ($this->timesheet_id != 0) {
$this->_action='updated';
$q = new DBQuery();
public function store($unused = null) {
$this->clearErrors();
if ($this->timesheet_id != 0 && $this->canEdit()) {
$this->_action='updated'; // FIXME: still needed??
$q = $this->_getQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_worked', $this->timesheet_worked);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) {
return db_error();
if (!($q->exec())) {
$this->_error['update-timesheet'] = db_error();
return false;
}
return true;
}
else {
$q = new DBQuery;
else if ($this->canCreate()) {
$q = $this->_getQuery();
$q->addTable('timesheet');
$q->addInsert('timesheet_period,timesheet_status,timesheet_date,timesheet_creator,timesheet_worked',
$this->timesheet_period.','.$this->timesheet_status.','.
$this->timesheet_date.','.$this->timesheet_creator.',' .
$this->timesheet_worked, true);
if (!$q->exec())
return db_error();
if (!($q->exec())) {
$this->_error['store-timesheet'] = db_error();
return false;
}
return true;
}
return false;
}
// overload the delete method of the parent class for adaptation for timesheet's needs
function delete() {
$q = new DBQuery();
$q->setDelete('timesheet_project');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec()) {
return db_error();
public function delete($unused = null) {
$this->clearErrors();
if ($this->canDelete()) {
$q = $this->_getQuery();
$q->setDelete('timesheet_project');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!($q->exec())) {
$this->_error['delete-timesheet-project'] = db_error();
return false;
}
$q->clear();
$q->setDelete('timesheet');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!($q->exec())) {
$this->_error['delete-timesheet'] = db_error();
return false;
}
return true;
}
$q->clear();
$q->setDelete('timesheet');
$q->addWhere('timesheet_id = '. $this->timesheet_id );
if (!$q->exec()) {
return db_error();
} else {
return NULL;
return false;
}
public function change_status() {
$this->clearErrors();
if ($this->canEdit()) {
$this->_action='updated';
$q = $this->_getQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_status', $this->timesheet_status);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!($q->exec())) {
$this->_error['change-status-timesheet'] = db_error();
return false;
}
return true;
}
return false;
}
function change_status()
{
$msg = $this->check();
$this->_action='updated';
$q = new DBQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_status', $this->timesheet_status);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!$q->exec()) {
return db_error();
public function change_period($period) {
$result = false;
$this->clearErrors();
if ($this->canEdit()) {
$this->_action='updated';
$q = $this->_getQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_period', $period);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
if (!($q->exec())) {
$this->_error['change-period-timesheet'] = db_error();
return $result;
}
$result = true;
$purge_result = $this->project_purge();
$store_result = $this->project_store();
$result = $result && $purge_result && $store_result;
}
return $result;
}
function change_period($period)
{
$msg = $this->check();
$this->_action='updated';
$q = new DBQuery();
$q->addTable('timesheet');
$q->addUpdate('timesheet_period', $period);
$q->addWhere('timesheet_id = ' . $this->timesheet_id);
public function get_config() {
// Read values from table
$config = null;
$q = $this->_getQuery();
$q->addQuery('*');
$q->addTable('timesheet_config');
if (!$q->exec()) {
return db_error();
$AppUI->setMsg(db_error(), UI_MSG_ERROR);
return $config;
}
$this->project_purge();
$this->project_store();
while ($row = $q->fetchRow()) {
$config[$row['timesheet_config']] = $row['timesheet_value'];
}
$q->clear();
return $config;
}
}
?>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment