fixed some problems with the history functions

This commit is contained in:
Stefan Becker 2009-09-13 10:48:40 +00:00
parent 9259494a02
commit 46f8530437
3 changed files with 16 additions and 139 deletions

View File

@ -158,25 +158,9 @@ class timesheet_bo extends so_sql_cf
*
* @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',
);
var $field2history = array();
/**
* Translate field-name to 2-char history status
* setting field-name from DB to history status
*
* @var array
*/
@ -246,6 +230,11 @@ class timesheet_bo extends so_sql_cf
$GLOBALS['timesheet_bo'] =& $this;
}
$this->grants = $GLOBALS['egw']->acl->get_grants(TIMESHEET_APP);
//set fields for tracking
$this->field2history = array_keys($this->db_cols);
$this->field2history = array_diff(array_combine($this->field2history,$this->field2history),
array('ts_modified'));
$this->field2history = $this->field2history +array('customfields' => '#c'); //add custom fields for history
}
/**
@ -818,118 +807,6 @@ 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

@ -13,7 +13,7 @@
/**
* Timesheet - tracking object for the tracker
*/
class timesheet_tracking extends timesheet_bo
class timesheet_tracking extends bo_tracking
{
/**
* Application we are tracking (required!)
@ -65,12 +65,14 @@ class timesheet_tracking extends timesheet_bo
* @param timesheet_bo $botimesheet
* @return timesheet_tracking
*/
function __construct(&$botimesheet)
function __construct($bo)
{
parent::__construct(); // calling the constructor of the extended class
$this->timesheet =& $botimesheet;
$this->field2history =& $botimesheet->field2history;
$this->bo = $bo;
$this->field2history = $this->bo->field2history;
}
/**

View File

@ -309,17 +309,15 @@ class timesheet_ui extends timesheet_bo
'id' => $this->data['ts_id'],
'app' => 'timesheet',
'status-widgets' => array(
'Sta' => $this->status_labels,
'Mod' => 'select-account',
'Dur' => 'date-time',
'Cat' => 'select-cat',
'ts_status' => $this->status_labels,
'ts_modifier' => 'select-account',
'cat_id' => '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'];