2005-12-19 05:23:14 +01:00
|
|
|
<?php
|
2006-08-26 18:32:29 +02:00
|
|
|
/**
|
|
|
|
* TimeSheet - business object
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @package timesheet
|
2009-03-23 10:53:03 +01:00
|
|
|
* @copyright (c) 2005-9 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
2006-08-26 18:32:29 +02:00
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2008-10-07 10:57:09 +02:00
|
|
|
* @version $Id$
|
2006-08-26 18:32:29 +02:00
|
|
|
*/
|
2005-12-19 05:23:14 +01:00
|
|
|
|
|
|
|
if (!defined('TIMESHEET_APP'))
|
|
|
|
{
|
|
|
|
define('TIMESHEET_APP','timesheet');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Business object of the TimeSheet
|
|
|
|
*
|
|
|
|
* Uses eTemplate's so_sql as storage object (Table: egw_timesheet).
|
2008-10-15 18:37:23 +02:00
|
|
|
*
|
|
|
|
* @todo Implement sorting&filtering by and searching of custom fields
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
2009-03-23 16:23:45 +01:00
|
|
|
class timesheet_bo extends so_sql_cf
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2007-03-08 12:22:57 +01:00
|
|
|
/**
|
|
|
|
* Timesheets config data
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $config_data = array();
|
2007-07-10 09:34:30 +02:00
|
|
|
/**
|
|
|
|
* Should we show a quantity sum, makes only sense if we sum up identical units (can be used to sum up negative (over-)time)
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $quantity_sum=false;
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Timestaps that need to be adjusted to user-time on reading or saving
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var array
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
var $timestamps = array(
|
|
|
|
'ts_start','ts_modified'
|
|
|
|
);
|
|
|
|
/**
|
2008-10-07 10:57:09 +02:00
|
|
|
* Offset in secconds between user and server-time, it need to be add to a server-time to get the user-time
|
2006-08-26 18:32:29 +02:00
|
|
|
* or substracted from a user-time to get the server-time
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var int
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
var $tz_offset_s;
|
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Current time as timestamp in user-time
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var int
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
var $now;
|
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Start of today in user-time
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var int
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
var $today;
|
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Filter for search limiting the date-range
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var array
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
var $date_filters = array( // Start: year,month,day,week, End: year,month,day,week
|
|
|
|
'Today' => array(0,0,0,0, 0,0,1,0),
|
|
|
|
'Yesterday' => array(0,0,-1,0, 0,0,0,0),
|
|
|
|
'This week' => array(0,0,0,0, 0,0,0,1),
|
|
|
|
'Last week' => array(0,0,0,-1, 0,0,0,0),
|
|
|
|
'This month' => array(0,0,0,0, 0,1,0,0),
|
|
|
|
'Last month' => array(0,-1,0,0, 0,0,0,0),
|
|
|
|
'2 month ago' => array(0,-2,0,0, 0,-1,0,0),
|
|
|
|
'This year' => array(0,0,0,0, 1,0,0,0),
|
|
|
|
'Last year' => array(-1,0,0,0, 0,0,0,0),
|
|
|
|
'2 years ago' => array(-2,0,0,0, -1,0,0,0),
|
|
|
|
'3 years ago' => array(-3,0,0,0, -2,0,0,0),
|
2006-01-11 06:17:15 +01:00
|
|
|
);
|
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Grants: $GLOBALS['egw']->acl->get_grants(TIMESHEET_APP);
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var array
|
2008-10-07 10:57:09 +02:00
|
|
|
*/
|
2006-03-21 14:52:49 +01:00
|
|
|
var $grants;
|
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Sums of the last search in keys duration and price
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var array
|
2006-03-21 14:52:49 +01:00
|
|
|
*/
|
|
|
|
var $summary;
|
2006-03-27 14:20:06 +02:00
|
|
|
/**
|
2006-08-26 18:32:29 +02:00
|
|
|
* Array with boolean values in keys 'day', 'week' or 'month', for the sums to return in the search
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2006-08-26 18:32:29 +02:00
|
|
|
* @var array
|
2006-03-27 14:20:06 +02:00
|
|
|
*/
|
|
|
|
var $show_sums;
|
2009-03-23 10:53:03 +01:00
|
|
|
/**
|
|
|
|
* Array with custom fileds
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-03-25 22:06:54 +02:00
|
|
|
var $customfields=array();
|
2009-03-23 10:53:03 +01:00
|
|
|
/**
|
|
|
|
* Array with status label
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $status_labels = array();
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2008-10-15 18:37:23 +02:00
|
|
|
/**
|
|
|
|
* Name of the timesheet table storing custom fields
|
|
|
|
*/
|
|
|
|
const EXTRA_TABLE = 'egw_timesheet_extra';
|
|
|
|
|
2008-10-07 10:57:09 +02:00
|
|
|
function __construct()
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2009-03-23 16:23:45 +01:00
|
|
|
parent::__construct(TIMESHEET_APP,'egw_timesheet',self::EXTRA_TABLE,'','ts_extra_name','ts_extra_value','ts_id');
|
2005-12-19 05:23:14 +01:00
|
|
|
|
2008-03-09 08:33:42 +01:00
|
|
|
$this->config_data = config::read(TIMESHEET_APP);
|
2007-07-10 09:34:30 +02:00
|
|
|
$this->quantity_sum = $this->config_data['quantity_sum'] == 'true';
|
2009-03-23 10:53:03 +01:00
|
|
|
if($this->config_data['status_labels']) $this->status_labels =& $this->config_data['status_labels'];
|
2005-12-19 05:23:14 +01:00
|
|
|
|
|
|
|
$this->tz_offset_s = $GLOBALS['egw']->datetime->tz_offset;
|
|
|
|
$this->now = time() + $this->tz_offset_s; // time() is server-time and we need a user-time
|
|
|
|
$this->today = mktime(0,0,0,date('m',$this->now),date('d',$this->now),date('Y',$this->now));
|
|
|
|
|
2008-10-07 10:57:09 +02:00
|
|
|
// save us in $GLOBALS['timesheet_bo'] for ExecMethod used in hooks
|
|
|
|
if (!is_object($GLOBALS['timesheet_bo']))
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2008-10-07 10:57:09 +02:00
|
|
|
$GLOBALS['timesheet_bo'] =& $this;
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
$this->grants = $GLOBALS['egw']->acl->get_grants(TIMESHEET_APP);
|
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
|
|
|
* get list of specified grants as uid => Username pairs
|
|
|
|
*
|
|
|
|
* @param int $required=EGW_ACL_READ
|
|
|
|
* @return array with uid => Username pairs
|
|
|
|
*/
|
|
|
|
function grant_list($required=EGW_ACL_READ)
|
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
foreach($this->grants as $uid => $grant)
|
|
|
|
{
|
|
|
|
if ($grant & $required)
|
|
|
|
{
|
|
|
|
$result[$uid] = $GLOBALS['egw']->common->grab_owner_name($uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
natcasesort($result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
|
|
|
* checks if the user has enough rights for a certain operation
|
|
|
|
*
|
|
|
|
* Rights are given via owner grants or role based acl
|
|
|
|
*
|
|
|
|
* @param int $required EGW_ACL_READ, EGW_ACL_WRITE, EGW_ACL_ADD, EGW_ACL_DELETE, EGW_ACL_BUDGET, EGW_ACL_EDIT_BUDGET
|
|
|
|
* @param array/int $data=null project or project-id to use, default the project in $this->data
|
2006-06-24 18:06:11 +02:00
|
|
|
* @return boolean true if the rights are ok, null if not found, false if no rights
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
function check_acl($required,$data=null)
|
|
|
|
{
|
2008-10-15 18:37:23 +02:00
|
|
|
//error_log(__METHOD__."($required,".array2string($data).")");
|
|
|
|
if (is_null($data) || (int)$data == $this->data['ts_id'])
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
|
|
|
$data =& $this->data;
|
|
|
|
}
|
|
|
|
if (!is_array($data))
|
|
|
|
{
|
|
|
|
$save_data = $this->data;
|
2009-03-23 16:23:45 +01:00
|
|
|
$data = $this->read($data,true);
|
2005-12-19 05:23:14 +01:00
|
|
|
$this->data = $save_data;
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2006-06-24 18:06:11 +02:00
|
|
|
if (!$data) return null; // entry not found
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
$rights = $this->grants[$data['ts_owner']];
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
return $data && !!($rights & $required);
|
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2008-10-15 18:37:23 +02:00
|
|
|
/**
|
|
|
|
* return SQL implementing filtering by date
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param int &$start
|
|
|
|
* @param int &$end_param
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-03-27 14:20:06 +02:00
|
|
|
function date_filter($name,&$start,&$end_param)
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2006-03-27 14:20:06 +02:00
|
|
|
$end = $end_param;
|
|
|
|
|
2006-03-21 23:18:29 +01:00
|
|
|
if ($name == 'custom' && $start)
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2006-03-21 23:18:29 +01:00
|
|
|
if ($end)
|
|
|
|
{
|
|
|
|
$end += 24*60*60;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$end = $start + 8*24*60*60;
|
|
|
|
}
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
2006-03-21 23:18:29 +01:00
|
|
|
else
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2006-03-21 23:18:29 +01:00
|
|
|
if (!isset($this->date_filters[$name]))
|
|
|
|
{
|
|
|
|
return '1=1';
|
|
|
|
}
|
|
|
|
$year = (int) date('Y',$this->today);
|
|
|
|
$month = (int) date('m',$this->today);
|
|
|
|
$day = (int) date('d',$this->today);
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2006-03-21 23:18:29 +01:00
|
|
|
list($syear,$smonth,$sday,$sweek,$eyear,$emonth,$eday,$eweek) = $this->date_filters[$name];
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2006-03-21 23:18:29 +01:00
|
|
|
if ($syear || $eyear)
|
|
|
|
{
|
|
|
|
$start = mktime(0,0,0,1,1,$syear+$year);
|
|
|
|
$end = mktime(0,0,0,1,1,$eyear+$year);
|
|
|
|
}
|
|
|
|
elseif ($smonth || $emonth)
|
|
|
|
{
|
|
|
|
$start = mktime(0,0,0,$smonth+$month,1,$year);
|
|
|
|
$end = mktime(0,0,0,$emonth+$month,1,$year);
|
|
|
|
}
|
|
|
|
elseif ($sday || $eday)
|
|
|
|
{
|
|
|
|
$start = mktime(0,0,0,$month,$sday+$day,$year);
|
|
|
|
$end = mktime(0,0,0,$month,$eday+$day,$year);
|
|
|
|
}
|
|
|
|
elseif ($sweek || $eweek)
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2006-03-21 23:18:29 +01:00
|
|
|
$wday = (int) date('w',$this->today); // 0=sun, ..., 6=sat
|
|
|
|
switch($GLOBALS['egw_info']['user']['preferences']['calendar']['weekdaystarts'])
|
|
|
|
{
|
|
|
|
case 'Sunday':
|
|
|
|
$weekstart = $this->today - $wday * 24*60*60;
|
|
|
|
break;
|
|
|
|
case 'Saturday':
|
|
|
|
$weekstart = $this->today - (6-$wday) * 24*60*60;
|
|
|
|
break;
|
|
|
|
case 'Moday':
|
|
|
|
default:
|
|
|
|
$weekstart = $this->today - ($wday ? $wday-1 : 6) * 24*60*60;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$start = $weekstart + $sweek*7*24*60*60;
|
|
|
|
$end = $weekstart + $eweek*7*24*60*60;
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
2006-03-27 14:20:06 +02:00
|
|
|
$end_param = $end - 24*60*60;
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
//echo "<p align='right'>date_filter($name,$start,$end) today=".date('l, Y-m-d H:i',$this->today)." ==> ".date('l, Y-m-d H:i:s',$start)." <= date < ".date('l, Y-m-d H:i:s',$end)."</p>\n";
|
2006-03-27 14:20:06 +02:00
|
|
|
// convert start + end from user to servertime for the filter
|
|
|
|
return '('.($start-$this->tz_offset_s).' <= ts_start AND ts_start < '.($end-$this->tz_offset_s).')';
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search the timesheet
|
|
|
|
*
|
|
|
|
* reimplemented to limit result to users we have grants from
|
|
|
|
*
|
|
|
|
* @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
|
|
|
|
* @param boolean/string $only_keys=true True returns only keys, False returns all cols. comma seperated list of keys to return
|
|
|
|
* @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
|
|
|
|
* @param string/array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
|
|
|
|
* @param string $wildcard='' appended befor and after each criteria
|
|
|
|
* @param boolean $empty=false False=empty criteria are ignored in query, True=empty have to be empty in row
|
|
|
|
* @param string $op='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
|
|
|
|
* @param mixed $start=false if != false, return only maxmatch rows begining with start, or array($start,$num)
|
|
|
|
* @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
|
2008-10-07 10:57:09 +02:00
|
|
|
* @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or
|
2005-12-19 05:23:14 +01:00
|
|
|
* "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
|
|
|
|
* @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
|
2006-03-21 14:52:49 +01:00
|
|
|
* @param boolean $only_summary=false If true only return the sums as array with keys duration and price, default false
|
2005-12-19 05:23:14 +01:00
|
|
|
* @return array of matching rows (the row is an array of the cols) or False
|
|
|
|
*/
|
2006-03-21 14:52:49 +01:00
|
|
|
function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false,$only_summary=false)
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2008-10-20 21:43:26 +02:00
|
|
|
//error_log(__METHOD__."(".print_r($criteria,true).",'$only_keys','$order_by',".print_r($extra_cols,true).",'$wildcard','$empty','$op','$start',".print_r($filter,true).",'$join')");
|
2008-03-13 13:47:03 +01:00
|
|
|
//echo "<p>".__METHOD__."(".print_r($criteria,true).",'$only_keys','$order_by',".print_r($extra_cols,true).",'$wildcard','$empty','$op','$start',".print_r($filter,true).",'$join')</p>\n";
|
2006-04-05 18:23:44 +02:00
|
|
|
// postgres can't round from double precission, only from numeric ;-)
|
|
|
|
$total_sql = $this->db->Type != 'pgsql' ? "round(ts_quantity*ts_unitprice,2)" : "round(cast(ts_quantity*ts_unitprice AS numeric),2)";
|
2005-12-19 05:23:14 +01:00
|
|
|
|
2006-08-26 18:32:29 +02:00
|
|
|
if (!is_array($extra_cols))
|
2006-04-05 18:23:44 +02:00
|
|
|
{
|
2006-08-26 18:32:29 +02:00
|
|
|
$extra_cols = $extra_cols ? explode(',',$extra_cols) : array();
|
2006-04-05 18:23:44 +02:00
|
|
|
}
|
2006-08-26 18:32:29 +02:00
|
|
|
$extra_cols[] = $total_sql.' AS ts_total';
|
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
if (!isset($filter['ts_owner']) || !count($filter['ts_owner']))
|
|
|
|
{
|
|
|
|
$filter['ts_owner'] = array_keys($this->grants);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!is_array($filter['ts_owner'])) $filter['ts_owner'] = array($filter['ts_owner']);
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
foreach($filter['ts_owner'] as $key => $owner)
|
|
|
|
{
|
|
|
|
if (!isset($this->grants[$owner]))
|
|
|
|
{
|
|
|
|
unset($filter['ts_owner'][$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!count($filter['ts_owner']))
|
|
|
|
{
|
|
|
|
$this->total = 0;
|
2006-03-21 14:52:49 +01:00
|
|
|
$this->summary = array();
|
2005-12-19 05:23:14 +01:00
|
|
|
return array();
|
|
|
|
}
|
2007-07-10 09:34:30 +02:00
|
|
|
$this->summary = parent::search($criteria,"SUM(ts_duration) AS duration,SUM($total_sql) AS price".
|
|
|
|
($this->quantity_sum ? ",SUM(ts_quantity) AS quantity" : ''),
|
2005-12-19 05:23:14 +01:00
|
|
|
'','',$wildcard,$empty,$op,false,$filter,$join);
|
|
|
|
$this->summary = $this->summary[0];
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2006-03-21 14:52:49 +01:00
|
|
|
if ($only_summary) return $this->summary;
|
2005-12-19 05:23:14 +01:00
|
|
|
|
2007-05-09 10:21:26 +02:00
|
|
|
if ($this->show_sums && strpos($order_by,'ts_start') !== false && // sums only make sense if ordered by ts_start
|
2006-04-05 18:23:44 +02:00
|
|
|
$this->db->capabilities['union'] && ($from_unixtime_ts_start = $this->db->from_unixtime('ts_start')))
|
2006-03-27 00:11:58 +02:00
|
|
|
{
|
2006-04-05 18:23:44 +02:00
|
|
|
$sum_sql = array(
|
|
|
|
'year' => $this->db->date_format($from_unixtime_ts_start,'%Y'),
|
|
|
|
'month' => $this->db->date_format($from_unixtime_ts_start,'%Y%m'),
|
|
|
|
'week' => $this->db->date_format($from_unixtime_ts_start,$GLOBALS['egw_info']['user']['preferences']['calendar']['weekdaystarts'] == 'Sunday' ? '%X%V' : '%x%v'),
|
|
|
|
'day' => $this->db->date_format($from_unixtime_ts_start,'%Y-%m-%d'),
|
|
|
|
);
|
|
|
|
foreach($this->show_sums as $type)
|
|
|
|
{
|
|
|
|
$extra_cols[] = $sum_sql[$type].' AS ts_'.$type;
|
|
|
|
$extra_cols[] = '0 AS is_sum_'.$type;
|
|
|
|
$sum_extra_cols[] = str_replace('ts_start','MIN(ts_start)',$sum_sql[$type]); // as we dont group by ts_start
|
|
|
|
$sum_extra_cols[$type] = '0 AS is_sum_'.$type;
|
|
|
|
}
|
2006-03-27 00:11:58 +02:00
|
|
|
// regular entries
|
|
|
|
parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,'UNION',$filter,$join,$need_full_no_count);
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2006-04-05 18:23:44 +02:00
|
|
|
$sort = substr($order_by,8);
|
|
|
|
$union_order = array();
|
|
|
|
$sum_ts_id = array('year' => -3,'month' => -2,'week' => -1,'day' => 0);
|
|
|
|
foreach($this->show_sums as $type)
|
2006-03-27 14:20:06 +02:00
|
|
|
{
|
2006-04-05 18:23:44 +02:00
|
|
|
$union_order[] = 'ts_'.$type . ' ' . $sort;
|
|
|
|
$union_order[] = 'is_sum_'.$type;
|
|
|
|
$sum_extra_cols[$type]{0} = '1';
|
|
|
|
// the $type sum
|
2007-07-10 09:34:30 +02:00
|
|
|
parent::search($criteria,$sum_ts_id[$type].",'','','',MIN(ts_start),SUM(ts_duration) AS ts_duration,".
|
2009-04-01 22:02:33 +02:00
|
|
|
($this->quantity_sum ? "SUM(ts_quantity) AS ts_quantity" : '0').",0,NULL,0,0,0,0,0,SUM($total_sql) AS ts_total",
|
2006-04-05 18:23:44 +02:00
|
|
|
'GROUP BY '.$sum_sql[$type],$sum_extra_cols,$wildcard,$empty,$op,'UNION',$filter,$join,$need_full_no_count);
|
|
|
|
$sum_extra_cols[$type]{0} = '0';
|
2006-03-27 14:20:06 +02:00
|
|
|
}
|
2006-09-20 15:31:41 +02:00
|
|
|
$union_order[] = 'ts_start '.$sort;
|
2006-04-05 18:23:44 +02:00
|
|
|
return parent::search('','',implode(',',$union_order),'','',false,'',$start);
|
2006-03-27 00:11:58 +02:00
|
|
|
}
|
2005-12-19 05:23:14 +01:00
|
|
|
return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read a timesheet entry
|
|
|
|
*
|
|
|
|
* @param int $ts_id
|
|
|
|
* @param boolean $ignore_acl=false should the acl be checked
|
2006-06-24 18:06:11 +02:00
|
|
|
* @return array/boolean array with timesheet entry, null if timesheet not found or false if no rights
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
2009-03-23 16:23:45 +01:00
|
|
|
function read($ts_id,$ignore_acl=false)
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2008-10-20 21:43:26 +02:00
|
|
|
//error_log(__METHOD__."($ts_id,$ignore_acl) ".function_backtrace());
|
2008-10-15 18:37:23 +02:00
|
|
|
if (!(int)$ts_id || (int)$ts_id != $this->data['ts_id'] && !parent::read($ts_id))
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2008-10-15 18:37:23 +02:00
|
|
|
return null; // entry not found
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
2008-10-15 18:37:23 +02:00
|
|
|
if (!$ignore_acl && !($ret = $this->check_acl(EGW_ACL_READ)))
|
|
|
|
{
|
|
|
|
return false; // no read rights
|
|
|
|
}
|
2005-12-19 05:23:14 +01:00
|
|
|
return $this->data;
|
|
|
|
}
|
2007-03-08 12:22:57 +01:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
|
|
|
* saves a timesheet entry
|
|
|
|
*
|
|
|
|
* reimplemented to notify the link-class
|
|
|
|
*
|
|
|
|
* @param array $keys if given $keys are copied to data before saveing => allows a save as
|
|
|
|
* @param boolean $touch_modified=true should modification date+user be set, default yes
|
|
|
|
* @param boolean $ignore_acl=false should the acl be checked, returns true if no edit-rigts
|
|
|
|
* @return int 0 on success and errno != 0 else
|
|
|
|
*/
|
|
|
|
function save($keys=null,$touch_modified=true,$ignore_acl=false)
|
|
|
|
{
|
|
|
|
if ($keys) $this->data_merge($keys);
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
if (!$ignore_acl && $this->data['ts_id'] && !$this->check_acl(EGW_ACL_EDIT))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($touch_modified)
|
|
|
|
{
|
|
|
|
$this->data['ts_modifier'] = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
$this->data['ts_modified'] = $this->now;
|
|
|
|
}
|
2006-01-11 06:17:15 +01:00
|
|
|
if (!($err = parent::save()))
|
2005-12-19 05:23:14 +01:00
|
|
|
{
|
2008-10-15 18:37:23 +02:00
|
|
|
// notify the link-class about the update, as other apps may be subscribt to it
|
|
|
|
egw_link::notify_update(TIMESHEET_APP,$this->data['ts_id'],$this->data);
|
2007-03-08 12:22:57 +01:00
|
|
|
}
|
2008-10-15 18:37:23 +02:00
|
|
|
return $err;
|
2007-03-08 12:22:57 +01:00
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
|
|
|
* deletes a timesheet entry identified by $keys or the loaded one, reimplemented to notify the link class (unlink)
|
|
|
|
*
|
|
|
|
* @param array $keys if given array with col => value pairs to characterise the rows to delete
|
|
|
|
* @param boolean $ignore_acl=false should the acl be checked, returns false if no delete-rigts
|
|
|
|
* @return int affected rows, should be 1 if ok, 0 if an error
|
|
|
|
*/
|
|
|
|
function delete($keys=null,$ignore_acl=false)
|
|
|
|
{
|
2006-01-11 06:17:15 +01:00
|
|
|
if (!is_array($keys) && (int) $keys)
|
|
|
|
{
|
|
|
|
$keys = array('ts_id' => (int) $keys);
|
|
|
|
}
|
|
|
|
$ts_id = is_null($keys) ? $this->data['ts_id'] : $keys['ts_id'];
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
if (!$this->check_acl(EGW_ACL_DELETE,$ts_id))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (($ret = parent::delete($keys)) && $ts_id)
|
|
|
|
{
|
|
|
|
// delete all links to timesheet entry $ts_id
|
2008-03-09 08:33:42 +01:00
|
|
|
egw_link::unlink(0,TIMESHEET_APP,$ts_id);
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2009-03-23 10:53:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set a status for timesheet entry identified by $keys
|
|
|
|
*
|
|
|
|
* @param array $keys if given array with col => value pairs to characterise the rows to delete
|
|
|
|
* @param boolean $status
|
|
|
|
* @return int affected rows, should be 1 if ok, 0 if an error
|
|
|
|
*/
|
|
|
|
function set_status($keys=null,$status)
|
|
|
|
{
|
|
|
|
$ret = true;
|
|
|
|
if (!is_array($keys) && (int) $keys)
|
|
|
|
{
|
|
|
|
$keys = array('ts_id' => (int) $keys);
|
|
|
|
}
|
|
|
|
$ts_id = is_null($keys) ? $this->data['ts_id'] : $keys['ts_id'];
|
|
|
|
|
|
|
|
if (!$this->check_acl(EGW_ACL_EDIT,$ts_id))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-23 16:23:45 +01:00
|
|
|
$this->read($keys,true);
|
2009-03-23 10:53:03 +01:00
|
|
|
$this->data['ts_status'] = $status;
|
|
|
|
if ($this->save($ts_id)!=0) $ret = false;
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
|
|
|
* changes the data from the db-format to your work-format
|
|
|
|
*
|
|
|
|
* reimplemented to adjust the timezone of the timestamps (adding $this->tz_offset_s to get user-time)
|
|
|
|
* Please note, we do NOT call the method of the parent so_sql !!!
|
|
|
|
*
|
|
|
|
* @param array $data if given works on that array and returns result, else works on internal data-array
|
|
|
|
* @return array with changed data
|
|
|
|
*/
|
|
|
|
function db2data($data=null)
|
|
|
|
{
|
|
|
|
if (!is_array($data))
|
|
|
|
{
|
|
|
|
$data = &$this->data;
|
|
|
|
}
|
|
|
|
foreach($this->timestamps as $name)
|
|
|
|
{
|
|
|
|
if (isset($data[$name]) && $data[$name]) $data[$name] += $this->tz_offset_s;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* changes the data from your work-format to the db-format
|
|
|
|
*
|
|
|
|
* reimplemented to adjust the timezone of the timestamps (subtraction $this->tz_offset_s to get server-time)
|
|
|
|
* Please note, we do NOT call the method of the parent so_sql !!!
|
|
|
|
*
|
|
|
|
* @param array $data if given works on that array and returns result, else works on internal data-array
|
|
|
|
* @return array with changed data
|
|
|
|
*/
|
|
|
|
function data2db($data=null)
|
|
|
|
{
|
|
|
|
if ($intern = !is_array($data))
|
|
|
|
{
|
|
|
|
$data = &$this->data;
|
|
|
|
}
|
|
|
|
foreach($this->timestamps as $name)
|
|
|
|
{
|
|
|
|
if (isset($data[$name]) && $data[$name]) $data[$name] -= $this->tz_offset_s;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2006-03-21 14:52:49 +01:00
|
|
|
/**
|
|
|
|
* Get the time- and pricesum for the given timesheet entries
|
|
|
|
*
|
|
|
|
* @param array $ids array of timesheet id's
|
|
|
|
* @return array with keys time and price
|
|
|
|
*/
|
|
|
|
function sum($ids)
|
|
|
|
{
|
|
|
|
return $this->search(array('ts_id'=>$ids),true,'','','',false,'AND',false,null,'',false,true);
|
|
|
|
}
|
2008-10-07 10:57:09 +02:00
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
2008-03-09 08:33:42 +01:00
|
|
|
* get title for a timesheet entry identified by $entry
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2005-12-19 05:23:14 +01:00
|
|
|
* Is called as hook to participate in the linking
|
|
|
|
*
|
|
|
|
* @param int/array $entry int ts_id or array with timesheet entry
|
2007-01-12 02:27:37 +01:00
|
|
|
* @return string/boolean string with title, null if timesheet not found, false if no perms to view it
|
2005-12-19 05:23:14 +01:00
|
|
|
*/
|
|
|
|
function link_title( $entry )
|
|
|
|
{
|
|
|
|
if (!is_array($entry))
|
|
|
|
{
|
2009-03-23 16:23:45 +01:00
|
|
|
$entry = $this->read( $entry,false,false);
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
if (!$entry)
|
|
|
|
{
|
2006-06-24 18:06:11 +02:00
|
|
|
return $entry;
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
$format = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'];
|
|
|
|
if (date('H:i',$entry['ts_start']) != '00:00') // dont show 00:00 time, as it means date only
|
|
|
|
{
|
|
|
|
$format .= ' '.($GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == 12 ? 'h:i a' : 'H:i');
|
|
|
|
}
|
|
|
|
return date($format,$entry['ts_start']).': '.$entry['ts_title'];
|
|
|
|
}
|
|
|
|
|
2008-03-09 08:33:42 +01:00
|
|
|
/**
|
|
|
|
* get title for multiple timesheet entries identified by $ids
|
2008-10-07 10:57:09 +02:00
|
|
|
*
|
2008-03-09 08:33:42 +01:00
|
|
|
* Is called as hook to participate in the linking
|
|
|
|
*
|
|
|
|
* @param array $ids array with ts_id's
|
|
|
|
* @return array with titles, see link_title
|
|
|
|
*/
|
|
|
|
function link_titles( array $ids )
|
|
|
|
{
|
|
|
|
$titles = array();
|
|
|
|
if (($entries = $this->search(array('ts_id' => $ids),'ts_id,ts_title,ts_start')))
|
|
|
|
{
|
|
|
|
foreach($entries as $entry)
|
|
|
|
{
|
|
|
|
$titles[$entry['ts_id']] = $this->link_title($entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we assume all not returned entries are not readable by the user, as we notify egw_link about all deletes
|
|
|
|
foreach($ids as $id)
|
|
|
|
{
|
|
|
|
if (!isset($titles[$id]))
|
|
|
|
{
|
|
|
|
$titles[$id] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $titles;
|
|
|
|
}
|
|
|
|
|
2005-12-19 05:23:14 +01:00
|
|
|
/**
|
|
|
|
* query timesheet for entries matching $pattern
|
|
|
|
*
|
|
|
|
* Is called as hook to participate in the linking
|
|
|
|
*
|
|
|
|
* @param string $pattern pattern to search
|
|
|
|
* @return array with ts_id - title pairs of the matching entries
|
|
|
|
*/
|
|
|
|
function link_query( $pattern )
|
|
|
|
{
|
|
|
|
$criteria = array();
|
|
|
|
foreach(array('ts_project','ts_title','ts_description') as $col)
|
|
|
|
{
|
|
|
|
$criteria[$col] = $pattern;
|
|
|
|
}
|
|
|
|
$result = array();
|
|
|
|
foreach((array) $this->search($criteria,false,'','','%',false,'OR') as $ts )
|
|
|
|
{
|
2007-04-17 15:33:23 +02:00
|
|
|
if ($ts) $result[$ts['ts_id']] = $this->link_title($ts);
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-10-07 10:57:09 +02:00
|
|
|
* Check access to the projects file store
|
2005-12-19 05:23:14 +01:00
|
|
|
*
|
2008-10-07 11:00:19 +02:00
|
|
|
* @param int $id id of entry
|
2008-10-07 10:57:09 +02:00
|
|
|
* @param int $check EGW_ACL_READ for read and EGW_ACL_EDIT for write or delete access
|
|
|
|
* @return boolean true if access is granted or false otherwise
|
2006-08-26 18:32:29 +02:00
|
|
|
*/
|
2008-10-07 10:57:09 +02:00
|
|
|
function file_access($id,$check,$rel_path)
|
2006-08-26 18:32:29 +02:00
|
|
|
{
|
2008-10-07 10:57:09 +02:00
|
|
|
return $this->check_acl($check,$id);
|
2006-08-26 18:32:29 +02:00
|
|
|
}
|
2007-03-08 12:22:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* updates the project titles in the timesheet application (called whenever a project name is changed in the project manager)
|
|
|
|
*
|
|
|
|
* Todo: implement via notification
|
|
|
|
*
|
|
|
|
* @param string $oldtitle => the origin title of the project
|
|
|
|
* @param string $newtitle => the new title of the project
|
|
|
|
* @return boolean true for success, false for invalid parameters
|
|
|
|
*/
|
|
|
|
function update_ts_project($oldtitle='', $newtitle='')
|
|
|
|
{
|
2008-10-07 10:57:09 +02:00
|
|
|
if(strlen($oldtitle) > 0 && strlen($newtitle) > 0)
|
2008-03-13 13:47:03 +01:00
|
|
|
{
|
|
|
|
$this->db->update('egw_timesheet',array(
|
|
|
|
'ts_project' => $newtitle,
|
|
|
|
'ts_title' => $newtitle,
|
|
|
|
),array(
|
|
|
|
'ts_project' => $oldtitle,
|
|
|
|
),__LINE__,__FILE__,TIMESHEET_APP);
|
2007-03-08 12:22:57 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns array with relation link_id and ts_id (necessary for project-selection)
|
|
|
|
*
|
|
|
|
* @param int $pm_id ID of selected project
|
|
|
|
* @return array containing link_id and ts_id
|
2008-10-07 10:57:09 +02:00
|
|
|
*/
|
|
|
|
function get_ts_links($pm_id=0)
|
2008-03-13 13:47:03 +01:00
|
|
|
{
|
|
|
|
if($pm_id && isset($GLOBALS['egw_info']['user']['apps']['projectmanager']))
|
2008-10-07 10:57:09 +02:00
|
|
|
{
|
2008-10-15 18:37:23 +02:00
|
|
|
$pm_ids = ExecMethod('projectmanager.projectmanager_bo.children',$pm_id);
|
2008-03-13 13:47:03 +01:00
|
|
|
$pm_ids[] = $pm_id;
|
|
|
|
$links = solink::get_links('projectmanager',$pm_ids,'timesheet'); // solink::get_links not egw_links::get_links!
|
|
|
|
if ($links)
|
|
|
|
{
|
|
|
|
$links = array_unique(call_user_func_array('array_merge',$links));
|
2007-03-08 12:22:57 +01:00
|
|
|
}
|
2008-03-13 13:47:03 +01:00
|
|
|
return $links;
|
|
|
|
}
|
|
|
|
return array();
|
2007-03-08 12:22:57 +01:00
|
|
|
}
|
2005-12-19 05:23:14 +01:00
|
|
|
}
|