From 15c845e0de9ad71383d213c6e54c5fa6079e38ab Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Tue, 7 Mar 2006 22:43:08 +0000 Subject: [PATCH] fixed timezone handling in addressbook: - db stores now server-time as everywhere in eGW - contacts-class in the api and bocontacts in addressbook take and deliver user-time - xmlrpc gives user-time as the other apps too --- addressbook/inc/class.bocontacts.inc.php | 63 +++++++++++++++++++++++- addressbook/inc/class.socontacts.inc.php | 43 ++++++++++++++-- phpgwapi/inc/class.contacts_sql.inc.php | 38 +++++++++----- 3 files changed, 127 insertions(+), 17 deletions(-) diff --git a/addressbook/inc/class.bocontacts.inc.php b/addressbook/inc/class.bocontacts.inc.php index 64fc5078d6..6a61ebec2b 100755 --- a/addressbook/inc/class.bocontacts.inc.php +++ b/addressbook/inc/class.bocontacts.inc.php @@ -34,11 +34,30 @@ class bocontacts extends socontacts */ var $user; + /** + * @var int $tz_offset_s offset in secconds between user and server-time, + * it need to be add to a server-time to get the user-time or substracted from a user-time to get the server-time + */ + var $tz_offset_s; + + /** + * @var int $now_su actual user (!) time + */ + var $now_su; + + /** + * @var array $timestamps timestamps + */ + var $timestamps = array('last_mod'); + function bocontacts($contact_app='addressbook') { $this->socontacts($contact_app); $this->grants = $GLOBALS['egw']->acl->get_grants($contact_app); $this->user = $GLOBALS['egw_info']['user']['account_id']; + $this->tz_offset_s = 3600 * $GLOBALS['egw_info']['user']['preferences']['common']['tz_offset']; + $this->now_su = time() + $this->tz_offset_s; + /* foreach(array( 'so' => $appname. 'soadb', ) as $my => $app_class) @@ -54,6 +73,48 @@ class bocontacts extends socontacts } + /** + * changes the data from the db-format to your work-format + * + * it gets called everytime when data is read from the db + * This function needs to be reimplemented in the derived class + * + * @param array $data + */ + function db2data($data) + { + // convert timestamps from server-time in the db to user-time + foreach($this->timestamps as $name) + { + if(isset($data[$name])) + { + $data[$name] += $this->tz_offset_s; + } + } + return $data; + } + + /** + * changes the data from your work-format to the db-format + * + * It gets called everytime when data gets writen into db or on keys for db-searches + * this needs to be reimplemented in the derived class + * + * @param array $data + */ + function data2db($data) + { + // convert timestamps from user-time to server-time in the db + foreach($this->timestamps as $name) + { + if(isset($data[$name])) + { + $data[$name] -= $this->tz_offset_s; + } + } + return $data; + } + /** * deletes contact in db * @@ -133,7 +194,7 @@ class bocontacts extends socontacts // convert categories $contact['cat_id'] = $contact['cat_id'] ? implode(',',$contact['cat_id']) : ''; // last modified - $contact['last_mod'] = time(); + $contact['last_mod'] = $this->now_su; // only owner can set access status $contact['access'] = $contact['owner'] == $this->user ? ($contact['private'] ? 'private': 'public') : $contact['access']; // create fullname diff --git a/addressbook/inc/class.socontacts.inc.php b/addressbook/inc/class.socontacts.inc.php index e2102ef393..39b642fef4 100755 --- a/addressbook/inc/class.socontacts.inc.php +++ b/addressbook/inc/class.socontacts.inc.php @@ -76,6 +76,36 @@ class socontacts if (!$this->customfields) $this->customfields = array(); } + /** + * changes the data from the db-format to your work-format + * + * it gets called everytime when data is read from the db + * This function needs to be reimplemented in the derived class + * + * @param array $data + */ + function db2data($data) + { + // do the necessare changes here + + return $data; + } + + /** + * changes the data from your work-format to the db-format + * + * It gets called everytime when data gets writen into db or on keys for db-searches + * this needs to be reimplemented in the derived class + * + * @param array $data + */ + function data2db($data) + { + // do the necessary changes here + + return $data; + } + /** * deletes contact entry including custom fields * @@ -102,7 +132,7 @@ class socontacts function save(&$contact) { // save mainfields - $this->somain->data = $contact; + $this->somain->data = $this->data2db($contact); $error_nr = $this->somain->save(); $contact['id'] = $this->somain->data['id']; if($error_nr) return $error_nr_main; @@ -133,7 +163,10 @@ class socontacts function read($contact_id) { // read main data - $contact = $this->somain->read($contact_id); + if (!($contact = $this->somain->read($contact_id))) + { + return $contact; + } // read customfilds $keys = array( @@ -145,7 +178,7 @@ class socontacts { $contact['#'.$field[$this->extra_key]] = $field[$this->extra_value]; } - return $contact; + return $this->db2data($contact); } /** @@ -334,6 +367,10 @@ class socontacts } } } + foreach($result as $num => $contact) + { + $result[$num] = $this->db2data($contact); + } return $need_full_no_count ? count($result) : $result; } diff --git a/phpgwapi/inc/class.contacts_sql.inc.php b/phpgwapi/inc/class.contacts_sql.inc.php index 83fe272435..0fd9681666 100644 --- a/phpgwapi/inc/class.contacts_sql.inc.php +++ b/phpgwapi/inc/class.contacts_sql.inc.php @@ -31,6 +31,9 @@ * Syntax: CreateObject('phpgwapi.contacts'); * Example1: $contacts = CreateObject('phpgwapi.contacts'); * + * The contacts class now uses user-time in all param- and return-values. + * The time stored in the DB is in server-time, as in all other eGW apps. + * * Last Editor: $Author$ * @class contacts_ * @abstract Contact Management System @@ -47,6 +50,12 @@ var $std_table='egw_addressbook'; var $ext_table='egw_addressbook_extra'; + /** + * @var int $tz_offset_s offset in secconds between user and server-time, + * it need to be add to a server-time to get the user-time or substracted from a user-time to get the server-time + */ + var $tz_offset_s; + var $account_id = 0; var $total_records = 0; var $grants = ''; @@ -167,6 +176,11 @@ 'parcel' => lang('Parcel'), 'postal' => lang('Postal') ); + if (!is_object($GLOBALS['egw']->datetime)) + { + $GLOBALS['egw']->datetime =& CreateObject('phpgwapi.datetime'); + } + $this->tz_offset_s = $GLOBALS['egw']->datetime->tz_offset; } /* send this the id and whatever fields you want to see */ @@ -402,18 +416,15 @@ echo "
DEBUG - $ordermethod"; } - if($lastmod >= 0 && $fwhere) + if($lastmod >= 0) { - $fwhere .= " AND last_mod > ".(int)$lastmod.' '; - } - elseif($lastmod >= 0) - { - $fwhere = " WHERE last_mod > ".(int)$lastmod.' '; - } + if (!$fwhere) $fwhere = ' WHERE '; + $fwhere .= " AND last_mod > ".(int)($lastmod - $this->tz_offset_s).' '; - if ($DEBUG && $last_mod_filter && $fwhere) - { - echo "
DEBUG - last_mod_filter added to fwhere: $fwhere"; + if ($DEBUG) + { + echo "
DEBUG - last_mod_filter added to fwhere: $fwhere"; + } } $filtermethod = ''; @@ -551,7 +562,7 @@ $return_fields[$i]['owner'] = $this->db->f('owner'); $return_fields[$i]['access'] = $this->db->f('access'); $return_fields[$i]['cat_id'] = $this->db->f('cat_id'); - $return_fields[$i]['last_mod'] = $this->db->f('last_mod'); + $return_fields[$i]['last_mod'] = $this->db->f('last_mod')+$this->tz_offset_s; $return_fields[$i]['rights'] = (int)$this->grants[$this->db->f('owner')]; if(@is_array($stock_fieldnames)) @@ -599,7 +610,7 @@ //this is added here so it is never tainted $this->stock_contact_fields['last_mod'] = 'last_mod'; - $stock_fields['last_mod'] = $GLOBALS['egw']->datetime->gmtnow; + $stock_fields['last_mod'] = time(); $data = array( 'owner' => $owner, @@ -608,6 +619,7 @@ 'tid' => $fields['tid'], ); if (isset($fields['lid'])) $data['lid'] = $fields['lid']; + $this->db->insert($this->std_table,array_merge($data,$stock_fields),False,__LINE__,__FILE__); @@ -682,7 +694,7 @@ if (count($stock_fields)) { - $stock_fields['last_mod'] = $GLOBALS['egw']->datetime->gmtnow; + $stock_fields['last_mod'] = time(); $this->db->update($this->std_table,$stock_fields,array('id'=>$id),__LINE__,__FILE__); } if (is_array($extra_fields))