fix for timesheet edit tabs problem; tabs showed, even if set to readonly; backport of history tracking for timesheet from s.becker

This commit is contained in:
Klaus Leithoff 2009-09-24 12:16:00 +00:00
commit 7d03dbc33f
6 changed files with 396 additions and 16 deletions

View File

@ -1151,6 +1151,9 @@ class etemplate extends boetemplate
}
else
{
// if textarea is readonly, but form_name is already used by an other widget, dont use it
// browser would only send the content of the readonly (and therefore unchanged) field
if ($readonly && self::$request->isset_to_process($form_name)) $form_name = '';
$html .= html::textarea($form_name,$value,
$options.html::formatOptions($cell_options,'ROWS,COLS'));
}

View File

@ -41,6 +41,12 @@ class timesheet_bo extends so_sql_cf
*
* @var array
*/
var $user;
/**
* current user
*
* @var int
*/
var $timestamps = array(
'ts_start','ts_modified'
);
@ -115,6 +121,62 @@ class timesheet_bo extends so_sql_cf
/**
* Name of the timesheet table storing custom fields
*/
var $historylog;
/**
* Instance of the timesheet_tracking object
*
* @var timesheet_tracking
*/
var $field2label = array(
'ts_project' => 'Project',
'ts_title' => 'Title',
'cat_id' => 'Category',
'ts_description' => 'Description',
'ts_start' => 'Start',
'ts_duration' => 'Duration',
'ts_quantity' => 'Quantity',
'ts_unitprice' => 'Unitprice',
'ts_owner' => 'Owner',
'ts_modifier' => 'Modifier',
'ts_status' => 'Status',
'pm_id' => 'Projectid',
// pseudo fields used in edit
//'link_to' => 'Attachments & Links',
'customfields' => 'Custom fields',
);
/**
* Translates field / acl-names to labels
*
* @var array
*/
var $field2history = array(
'ts_project' => 'Pro',
'ts_title' => 'Tit',
'cat_id' => 'Cat',
'ts_description' => 'Des',
'ts_start' => 'Sta',
'ts_duration' => 'Dur',
'ts_quantity' => 'Qua',
'ts_unitprice' => 'Uni',
'ts_owner' => 'Own',
'ts_modifier' => 'Mid',
'ts_status' => 'Sta',
'pm_id' => 'Pri',
// pseudo fields used in edit
//'link_to' => 'Att',
'customfields' => '#c',
);
/**
* Translate field-name to 2-char history status
*
* @var array
*/
var $tracking;
/**
* Names of all config vars
*
* @var array
*/
const EXTRA_TABLE = 'egw_timesheet_extra';
function __construct()
@ -409,12 +471,54 @@ class timesheet_bo extends so_sql_cf
{
$this->data['ts_modifier'] = $GLOBALS['egw_info']['user']['account_id'];
$this->data['ts_modified'] = $this->now;
$this->user = $this->data['ts_modifier'];
}
// check if we have a real modification
// read the old record
$new =& $this->data;
unset($this->data);
$this->read($new['ts_id']);
$old =& $this->data;
$this->data =& $new;
$changed[] = array();
if (isset($old)) foreach($old as $name => $value)
{
if (isset($new[$name]) && $new[$name] != $value) $changed[] = $name;
}
if (!$changed)
{
return false;
}
if (!is_object($this->tracking))
{
$this->tracking = new timesheet_tracking($this);
$this->tracking->html_content_allow = true;
}
if ($this->customfields)
{
$data_custom = $old_custom = array();
foreach($this->customfields as $name => $custom)
{
if (isset($this->data['#'.$name]) && (string)$this->data['#'.$name]!=='') $data_custom[] = $custom['label'].': '.$this->data['#'.$name];
if (isset($old['#'.$name]) && (string)$old['#'.$name]!=='') $old_custom[] = $custom['label'].': '.$old['#'.$name];
}
$this->data['customfields'] = implode("\n",$data_custom);
$old['customfields'] = implode("\n",$old_custom);
}
if (!$this->tracking->track($this->data,$old,$this->user))
{
return implode(', ',$this->tracking->errors);
}
if (!($err = parent::save()))
{
// 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);
}
return $err;
}
@ -665,4 +769,118 @@ class timesheet_bo extends so_sql_cf
}
return array();
}
/**
* Tracks the changes in one entry $data, by comparing it with the last version in $old
*
* @param array $data current entry
* @param array $old=null old/last state of the entry or null for a new entry
* @param int $user=null user who made the changes, default to current user
* @param boolean $deleted=null can be set to true to let the tracking know the item got deleted or undelted
* @param array $changed_fields=null changed fields from ealier call to $this->changed_fields($data,$old), to not compute it again
* @return int|boolean false on error, integer number of changes logged or true for new entries ($old == null)
*/
public function track(array $data,array $old=null,$user=null,$deleted=null,array $changed_fields=null)
{
$this->user = !is_null($user) ? $user : $GLOBALS['egw_info']['user']['account_id'];
$changes = true;
if ($old && $this->field2history)
{
$changes = $this->save_history($data,$old,$deleted,$changed_fields);
}
return $changes;
}
/**
* Save changes to the history log
*
* @internal use only track($data,$old)
* @param array $data current entry
* @param array $old=null old/last state of the entry or null for a new entry
* @param boolean $deleted=null can be set to true to let the tracking know the item got deleted or undelted
* @param array $changed_fields=null changed fields from ealier call to $this->changed_fields($data,$old), to not compute it again
* @return int number of log-entries made
*/
protected function save_history(array $data,array $old=null,$deleted=null,array $changed_fields=null)
{
if (is_null($changed_fields))
{
$changed_fields = self::changed_fields($data,$old);
}
if (!$changed_fields) return 0;
if (!is_object($this->historylog) || $this->historylog->user != $this->user)
{
$this->historylog = new historylog($this->app,$this->user);
}
foreach($changed_fields as $name)
{
$status = $this->field2history[$name];
if (is_array($status)) // 1:N relation --> remove common rows
{
self::compact_1_N_relation($data[$name],$status);
self::compact_1_N_relation($old[$name],$status);
$added = array_diff($data[$name],$old[$name]);
$removed = array_diff($old[$name],$data[$name]);
$n = max(array(count($added),count($removed)));
for($i = 0; $i < $n; ++$i)
{
$this->historylog->add($name,$data[$this->id_field],$added[$i],$removed[$i]);
}
}
else
{
$this->historylog->add($status,$data[$this->id_field],
is_array($data[$name]) ? implode(',',$data[$name]) : $data[$name],
is_array($old[$name]) ? implode(',',$old[$name]) : $old[$name]);
}
}
return count($changed_fields);
}
/**
* Compute changes between new and old data
*
* Can be used to check if saving the data is really necessary or user just pressed save
*
* @param array $data
* @param array $old=null
* @return array of keys with different values in $data and $old
*/
public function changed_fields(array $data,array $old=null)
{
if (is_null($old)) return array_keys($data);
$changed_fields = array();
foreach($this->field2history as $name => $status)
{
if (is_array($status)) // 1:N relation
{
self::compact_1_N_relation($data[$name],$status);
self::compact_1_N_relation($old[$name],$status);
}
if ($old[$name] != $data[$name] && !(!$old[$name] && !$data[$name]))
{
// normalize arrays, we do NOT care for the order of multiselections
if (is_array($data[$name]) || is_array($old[$name]))
{
if (!is_array($data[$name])) $data[$name] = explode(',',$data[$name]);
if (!is_array($old[$name])) $old[$name] = explode(',',$old[$name]);
if (count($data[$name]) == count($old[$name]))
{
sort($data[$name]);
sort($old[$name]);
if ($data[$name] == $old[$name]) continue;
}
}
$changed_fields[] = $name;
}
}
return $changed_fields;
}
}

View File

@ -0,0 +1,129 @@
<?php
/**
* Timesheet - history and notifications
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package tracker
* @copyright (c) 2006-8 by Ralf Becker <RalfBecker-AT-stylite.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id: class.timesheet_tracking.inc.php 26515 2009-03-24 11:50:16Z leithoff $
*/
/**
* Timesheet - tracking object for the tracker
*/
class timesheet_tracking extends timesheet_bo
{
/**
* Application we are tracking (required!)
*
* @var string
*/
var $app = 'timesheet';
/**
* Name of the id-field, used as id in the history log (required!)
*
* @var string
*/
var $id_field = 'ts_id';
/**
* Name of the field with the creator id, if the creator of an entry should be notified
*
* @var string
*/
var $creator_field = 'ts_owner';
/**
* Name of the field with the id(s) of assinged users, if they should be notified
*
* @var string
*/
var $assigned_field = 'ts_assigned';
/**
* Translate field-name to 2-char history status
*
* @var array
*/
var $field2history = array();
/**
* Should the user (passed to the track method or current user if not passed) be used as sender or get_config('sender')
*
* @var boolean
*/
var $prefer_user_as_sender = false;
/**
* Instance of the timesheet_bo class calling us
*
* @access private
* @var timesheet_bo
*/
var $timesheet;
/**
* Constructor
*
* @param timesheet_bo $botimesheet
* @return timesheet_tracking
*/
function __construct(&$botimesheet)
{
parent::__construct(); // calling the constructor of the extended class
$this->timesheet =& $botimesheet;
$this->field2history =& $botimesheet->field2history;
}
/**
* Get a notification-config value
*
* @param string $what
* - 'copy' array of email addresses notifications should be copied too, can depend on $data
* - 'lang' string lang code for copy mail
* - 'sender' string send email address
* @param array $data current entry
* @param array $old=null old/last state of the entry or null for a new entry
* @return mixed
*/
function get_config($name,$data,$old=null)
{
$timesheet = $data['ts_id'];
//$config = $this->timesheet->notification[$timesheet][$name] ? $this->timesheet->notification[$timesheet][$name] : $this->$timesheet->notification[0][$name];
//no nitify configert (ToDo)
return $config;
}
/**
* Get the subject for a given entry, reimplementation for get_subject in bo_tracking
*
* Default implementation uses the link-title
*
* @param array $data
* @param array $old
* @return string
*/
function get_subject($data,$old)
{
return '#'.$data['ts_id'].' - '.$data['ts_title'];
}
/**
* Get the modified / new message (1. line of mail body) for a given entry, can be reimplemented
*
* @param array $data
* @param array $old
* @return string
*/
function get_message($data,$old)
{
if (!$data['ts_modified'] || !$old)
{
return lang('New timesheet submitted by %1 at %2',
$GLOBALS['egw']->common->grab_owner_name($data['ts_creator']),
$this->datetime($data['ts_created']-$this->tracker->tz_offset_s));
}
return lang('Timesheet modified by %1 at %2',
$data['ts_modifier'] ? $GLOBALS['egw']->common->grab_owner_name($data['ts_modifier']) : lang('Timesheet'),
$this->datetime($data['ts_modified']-$this->timesheet->tz_offset_s));
}
}

View File

@ -58,9 +58,7 @@ class timesheet_ui extends timesheet_bo
function edit($content = null,$view = false)
{
$tabs = 'general|notes|links|customfields';
$etpl =& new etemplate('timesheet.edit');
$etpl = new etemplate('timesheet.edit');
if (!is_array($content))
{
if ($view || (int)$_GET['ts_id'])
@ -110,7 +108,7 @@ class timesheet_ui extends timesheet_bo
$view = $content['view'];
$referer = $content['referer'];
$this->data = $content;
foreach(array('button','view','referer',$tabs,'start_time') as $key)
foreach(array('button','view','referer','tabs','start_time') as $key)
{
unset($this->data[$key]);
}
@ -238,7 +236,7 @@ class timesheet_ui extends timesheet_bo
$content = array_merge($this->data,array(
'msg' => $msg,
'view' => $view,
$tabs => $content[$tabs],
'tabs' => $content['tabs'],
'link_to' => array(
'to_id' => $this->data['ts_id'] ? $this->data['ts_id'] : $content['link_to']['to_id'],
'to_app' => TIMESHEET_APP,
@ -296,6 +294,21 @@ class timesheet_ui extends timesheet_bo
{
$preserv['ts_project'] = $preserv['ts_project_blur'];
}
$content['history'] = array(
'id' => $this->data['ts_id'],
'app' => 'timesheet',
'status-widgets' => array(
'Sta' => $this->status_labels,
'Mod' => 'select-account',
'Dur' => 'date-time',
'Cat' => 'select-cat',
),
);
foreach($this->field2history as $field => $status)
{
$sel_options['status'][$status] = $this->field2label[$field];
}
// the actual title-blur is either the preserved title blur (if we are called from infolog entry),
// or the preserved project-blur comming from the current selected project
$content['ts_title_blur'] = $preserv['ts_title_blur'] ? $preserv['ts_title_blur'] : $preserv['ts_project_blur'];
@ -307,6 +320,7 @@ class timesheet_ui extends timesheet_bo
'button[save_new]' => $view,
'button[apply]' => $view,
);
if ($view)
{
foreach(array_merge(array_keys($this->data),array('pm_id','pl_id','link_to')) as $key)
@ -320,6 +334,8 @@ class timesheet_ui extends timesheet_bo
{
$readonlys['ts_owner'] = true;
}
$sel_options['ts_owner'] = $edit_grants;
$sel_options['ts_status'] = $this->status_labels;
$GLOBALS['egw_info']['flags']['app_header'] = lang('timesheet').' - '.
($view ? lang('View') : ($this->data['ts_id'] ? lang('Edit') : lang('Add')));
@ -329,17 +345,14 @@ class timesheet_ui extends timesheet_bo
$etpl->set_cell_attribute('pm_id','disabled',true);
$etpl->set_cell_attribute('pl_id','disabled',true);
}
if($this->ts_viewtype == 'short')
{
$content['ts_viewtype'] = $readonlys[$tabs]['notes'] = true;
$content['ts_viewtype'] = $readonlys['tabs']['notes'] = true;
}
if (!$this->customfields) $readonlys[$tabs]['customfields'] = true; // suppress tab if there are not customfields
if (!$this->customfields) $readonlys['tabs']['customfields'] = true; // suppress tab if there are not customfields
if (!$this->data['ts_id']) $readonlys['tabs']['history'] = true; //suppress history for the first loading without ID
return $etpl->exec(TIMESHEET_APP.'.timesheet_ui.edit',$content,array(
'ts_owner' => $edit_grants,
'ts_status' => $this->status_labels,
),$readonlys,$preserv,2);
return $etpl->exec(TIMESHEET_APP.'.timesheet_ui.edit',$content,$sel_options,$readonlys,$preserv,2);
}
/**
@ -714,7 +727,6 @@ class timesheet_ui extends timesheet_bo
'ts_status' => $this->status_labels,
);
$content['nm']['no_status'] = !$sel_options['ts_status'];
$status =array();
$sel_options['action'] ['delete']= lang('Delete Timesheet');
foreach ($this->status_labels as $status_id => $label_status)

View File

@ -2,7 +2,7 @@
/**
* eGroupWare - eTemplates for Application timesheet
* http://www.egroupware.org
* generated by soetemplate::dump4setup() 2009-04-01 15:00
* generated by soetemplate::dump4setup() 2009-09-24 13:50
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package timesheet
@ -36,6 +36,10 @@ $templ_data[] = array('name' => 'timesheet.edit','template' => '','lang' => '','
.fullWidth input { widht: 100%; }
.fullWidth textarea { widht: 100%; }','modified' => '1237891824',);
$templ_data[] = array('name' => 'timesheet.edit','template' => '','lang' => '','group' => '0','version' => '1.7.003','data' => 'a:1:{i:0;a:6:{s:4:"type";s:4:"grid";s:4:"data";a:8:{i:0;a:9:{s:2:"c2";s:2:"th";s:2:"c3";s:3:"row";s:1:"A";s:3:"100";s:2:"c6";s:3:"row";s:2:"h6";s:14:",!@ts_modified";s:2:"c4";s:3:"row";s:2:"h2";s:2:"28";s:2:"h1";s:6:",!@msg";s:2:"h4";s:13:",@ts_viewtype";}i:1;a:2:{s:1:"A";a:5:{s:4:"type";s:5:"label";s:4:"span";s:13:"all,redItalic";s:4:"name";s:3:"msg";s:7:"no_lang";s:1:"1";s:5:"align";s:6:"center";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:2;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"size";s:11:",,,ts_owner";s:5:"label";s:4:"User";}s:1:"B";a:4:{s:4:"type";s:6:"select";s:4:"name";s:8:"ts_owner";s:4:"span";s:3:"all";s:7:"no_lang";s:1:"1";}}i:3;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"size";s:13:",,,ts_project";s:5:"label";s:7:"Project";}s:1:"B";a:7:{s:4:"type";s:4:"grid";s:4:"size";s:7:",,,,1,1";s:4:"span";s:3:"all";s:4:"data";a:3:{i:0;a:2:{s:2:"h1";s:21:",@pm_integration=none";s:2:"h2";s:21:",@pm_integration=full";}i:1;a:1:{s:1:"A";a:6:{s:4:"type";s:21:"projectmanager-select";s:4:"size";s:4:"None";s:4:"name";s:5:"pm_id";s:4:"span";s:13:"all,fullWidth";s:4:"help";s:16:"Select a project";s:8:"onchange";i:1;}}i:2;a:1:{s:1:"A";a:5:{s:4:"type";s:4:"text";s:4:"name";s:10:"ts_project";s:4:"blur";s:16:"@ts_project_blur";s:4:"size";s:5:"65,80";s:4:"span";s:10:",fullWidth";}}}s:4:"rows";i:2;s:4:"cols";i:1;s:7:"options";a:2:{i:4;s:1:"1";i:5;s:1:"1";}}}i:4;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"size";s:14:",,ts_unitprice";s:5:"label";s:9:"Unitprice";}s:1:"B";a:5:{s:4:"type";s:4:"grid";s:4:"span";s:3:"all";s:4:"data";a:2:{i:0;a:1:{s:1:"A";s:21:",@pm_integration=none";}i:1;a:2:{s:1:"A";a:4:{s:4:"type";s:24:"projectmanager-pricelist";s:4:"name";s:5:"pl_id";s:4:"size";s:4:"None";s:8:"onchange";s:209:"this.form[\'exec[ts_unitprice]\'].value=this.options[this.selectedIndex].text.lastIndexOf(\'(\') < 0 ? \'\' : this.options[this.selectedIndex].text.slice(this.options[this.selectedIndex].text.lastIndexOf(\'(\')+1,-1);";}s:1:"B";a:3:{s:4:"type";s:5:"float";s:4:"name";s:12:"ts_unitprice";s:4:"span";s:3:"all";}}}s:4:"rows";i:1;s:4:"cols";i:2;}}i:5;a:2:{s:1:"A";a:4:{s:4:"type";s:3:"tab";s:5:"label";s:41:"General|Notes|Links|Custom Fields|History";s:4:"name";s:45:"tabs=general|notes|links|customfields|history";s:4:"span";s:3:"all";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:6;a:2:{s:1:"A";a:2:{s:4:"type";s:5:"label";s:5:"label";s:13:"Last modified";}s:1:"B";a:4:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"2";i:1;a:3:{s:4:"type";s:9:"date-time";s:4:"name";s:11:"ts_modified";s:8:"readonly";s:1:"1";}i:2;a:4:{s:4:"type";s:14:"select-account";s:4:"name";s:11:"ts_modifier";s:5:"label";s:2:"by";s:8:"readonly";s:1:"1";}}}i:7;a:2:{s:1:"A";a:5:{s:4:"type";s:4:"hbox";s:4:"size";s:5:"2,0,0";s:4:"span";s:1:"2";i:1;a:8:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"6";i:1;a:4:{s:4:"type";s:6:"button";s:5:"label";s:4:"Edit";s:4:"name";s:12:"button[edit]";s:4:"help";s:15:"Edit this entry";}i:2;a:4:{s:4:"type";s:6:"button";s:4:"name";s:16:"button[save_new]";s:5:"label";s:10:"Save & New";s:4:"help";s:34:"Saves this entry and add a new one";}i:3;a:4:{s:4:"type";s:6:"button";s:4:"name";s:12:"button[save]";s:5:"label";s:4:"Save";s:4:"help";s:22:"Saves the changes made";}i:4;a:4:{s:4:"type";s:6:"button";s:4:"name";s:13:"button[apply]";s:5:"label";s:5:"Apply";s:4:"help";s:24:"Applies the changes made";}i:5;a:5:{s:4:"type";s:6:"button";s:4:"name";s:14:"button[cancel]";s:5:"label";s:6:"Cancel";s:4:"help";s:44:"closes the window without saving the changes";s:7:"onclick";s:15:"window.close();";}i:6;a:2:{s:4:"type";s:4:"html";s:4:"name";s:2:"js";}}i:2;a:6:{s:4:"type";s:6:"button";s:5:"label";s:6:"Delete";s:5:"align";s:5:"right";s:4:"name";s:14:"button[delete]";s:4:"help";s:17:"Delete this entry";s:7:"onclick";s:36:"return confirm(\'Delete this entry\');";}}s:1:"B";a:1:{s:4:"type";s:5:"label";}}}s:4:"rows";i:7;s:4:"cols";i:2;s:4:"size";s:4:"100%";s:7:"options";a:1:{i:0;s:4:"100%";}}}','size' => '100%','style' => '.fullWidth select { widht: 100%; }
.fullWidth input { widht: 100%; }
.fullWidth textarea { widht: 100%; }','modified' => '1253785365',);
$templ_data[] = array('name' => 'timesheet.edit.customfields','template' => '','lang' => '','group' => '0','version' => '','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:2:{i:0;a:0:{}i:1;a:1:{s:1:"A";a:1:{s:4:"type";s:12:"customfields";}}}s:4:"rows";i:1;s:4:"cols";i:1;}}','size' => '','style' => '','modified' => '1163173930',);
$templ_data[] = array('name' => 'timesheet.edit.customfields','template' => '','lang' => '','group' => '0','version' => '1.5.001','data' => 'a:1:{i:0;a:6:{s:4:"type";s:4:"grid";s:4:"data";a:2:{i:0;a:0:{}i:1;a:1:{s:1:"A";a:1:{s:4:"type";s:12:"customfields";}}}s:4:"rows";i:1;s:4:"cols";i:1;s:4:"size";s:17:"100%,150,,,,,auto";s:7:"options";a:3:{i:0;s:4:"100%";i:1;s:3:"150";i:6;s:4:"auto";}}}','size' => '100%,150,,,,,auto','style' => '','modified' => '1163173930',);

View File

@ -101,7 +101,19 @@
</rows>
</grid>
</template>
<template id="timesheet.edit" template="" lang="" group="0" version="1.7.001">
<template id="timesheet.edit.history" template="" lang="" group="0" version="1.7.001">
<grid width="100%" height="250" overflow="auto">
<columns>
<column/>
</columns>
<rows>
<row valign="top">
<historylog id="history"/>
</row>
</rows>
</grid>
</template>
<template id="timesheet.edit" template="" lang="" group="0" version="1.7.003">
<grid width="100%">
<columns>
<column width="100"/>
@ -150,18 +162,20 @@
</grid>
</row>
<row>
<tabbox id="general|notes|links|customfields" span="all">
<tabbox id="tabs" span="all">
<tabs>
<tab label="General" statustext=""/>
<tab label="Notes" statustext=""/>
<tab label="Links" statustext=""/>
<tab label="Custom Fields" statustext=""/>
<tab label="History" statustext=""/>
</tabs>
<tabpanels>
<template id="timesheet.edit.general"/>
<template id="timesheet.edit.notes"/>
<template id="timesheet.edit.links"/>
<template id="timesheet.edit.customfields"/>
<template id="timesheet.edit.history"/>
</tabpanels>
</tabbox>
</row>