Reworked so_sql class to:

- use new egw_time::server2user() and egw_time::user2server() to do
  timezone conversation of registered timestamps
- added $timestamp_type parameter to constructor, to automatic convert
  all timestamps on read (or search) to given type, default NULL leaves
  them unchanged --> will allow in future to work completly with
  egw_time objects
This commit is contained in:
Ralf Becker 2009-10-08 16:18:28 +00:00
parent 3ef01bc589
commit 200a6d61d5
2 changed files with 39 additions and 28 deletions

View File

@ -59,7 +59,7 @@ class so_sql
/** /**
* total number of entries of last search with start != false * total number of entries of last search with start != false
* *
* @var int/boolean * @var int|boolean
*/ */
var $total = false; var $total = false;
/** /**
@ -114,17 +114,29 @@ class so_sql
* @var array * @var array
*/ */
var $timestamps = array(); var $timestamps = array();
/**
* Type of timestamps returned by this class (read and search methods), default null means leave them unchanged
*
* Possible values:
* - 'ts'|'integer' convert every timestamp to an integer unix timestamp
* - 'string' convert every timestamp to a 'Y-m-d H:i:s' string
* - 'object' convert every timestamp to a egw_time object
*
* @var string
*/
public $timestamp_type;
/** /**
* Offset in secconds between user and server-time, it need to be add to a server-time to get the user-time * 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 * or substracted from a user-time to get the server-time
* *
* @var int * @var int
* @deprecated use egw_time methods instead, as the offset between user and server time is only valid for current time
*/ */
var $tz_offset_s; var $tz_offset_s;
/** /**
* Current time as timestamp in user-time * Current time in user timezone
* *
* @var int * @var int|string|DateTime format depends on $this->timestamp_type
*/ */
var $now; var $now;
/** /**
@ -147,10 +159,11 @@ class so_sql
* @param string $colum_prefix='' column prefix to automatic remove from the column-name, if the column name starts with it * @param string $colum_prefix='' column prefix to automatic remove from the column-name, if the column name starts with it
* @param boolean $no_clone=false can we avoid to clone the db-object, default no * @param boolean $no_clone=false can we avoid to clone the db-object, default no
* new code using appnames and foreach(select(...,$app) can set it to avoid an extra instance of the db object * new code using appnames and foreach(select(...,$app) can set it to avoid an extra instance of the db object
* @param string $timestamp_type=null default null=leave them as is, 'ts'|'integer' use integer unix timestamps, 'object' use egw_time objects
* *
* @return so_sql * @return so_sql
*/ */
function __construct($app='',$table='',$db=null,$column_prefix='',$no_clone=false) function __construct($app='',$table='',$db=null,$column_prefix='',$no_clone=false,$timestamp_type=null)
{ {
if ($no_clone) if ($no_clone)
{ {
@ -177,8 +190,19 @@ class so_sql
echo "<p>so_sql('$app','$table')</p>\n"; echo "<p>so_sql('$app','$table')</p>\n";
_debug_array($this); _debug_array($this);
} }
$this->tz_offset_s = $GLOBALS['egw']->datetime->tz_offset; // set timestampt type and current time
$this->now = time() + $this->tz_offset_s; // time() is server-time and we need a user-time switch(($this->timestamp_type = $timestamp_type))
{
case 'object':
$this->now = new egw_time('now');
break;
case 'string':
$this->now = egw_time::to('now',egw_time::DATABASE);
break;
default:
$this->now = egw_time::to('now','ts');
}
$this->tz_offset_s = egw_time::tz_offset_s();
} }
/** /**
@ -319,20 +343,13 @@ class so_sql
{ {
$data = &$this->data; $data = &$this->data;
} }
if ($this->tz_offset_s && $this->timestamps) if ($this->timestamps)
{ {
foreach($this->timestamps as $name) foreach($this->timestamps as $name)
{ {
if (isset($data[$name]) && $data[$name]) if (isset($data[$name]) && $data[$name])
{ {
if (is_numeric($data[$name])) $data[$name] = egw_time::server2user($data[$name],$this->timestamp_type);
{
$data[$name] += $this->tz_offset_s;
}
elseif (($ts = strtotime($data[$name])) !== false)
{
$data[$name] = date('Y-m-d H:i:s',$ts + $this->tz_offset_s);
}
} }
} }
} }
@ -366,20 +383,13 @@ class so_sql
{ {
$data = &$this->data; $data = &$this->data;
} }
if ($this->tz_offset_s && $this->timestamps) if ($this->timestamps)
{ {
foreach($this->timestamps as $name) foreach($this->timestamps as $name)
{ {
if (isset($data[$name]) && $data[$name]) if (isset($data[$name]) && $data[$name])
{ {
if (is_numeric($data[$name])) // we check for numeric, as timestamps are allowed (get converted by egw_db::quote) $data[$name] = egw_time::user2server($data[$name],$this->timestamp_type);
{
$data[$name] -= $this->tz_offset_s;
}
elseif (($ts = strtotime($data[$name])) !== false)
{
$data[$name] = date('Y-m-d H:i:s',$ts - $this->tz_offset_s);
}
} }
} }
} }
@ -565,7 +575,7 @@ class so_sql
{ {
continue; // no need to write that (unset) column continue; // no need to write that (unset) column
} }
$data[$db_col] = (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; $data[$db_col] = !is_object($this->data[$col]) && (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col];
} }
// allow to add direct sql updates, eg. "etag=etag+1" with int keys // allow to add direct sql updates, eg. "etag=etag+1" with int keys
if (is_array($keys) && isset($keys[0])) if (is_array($keys) && isset($keys[0]))

View File

@ -111,15 +111,16 @@ class so_sql_cf extends so_sql
* @param string $extra_id='_id' column name for cf id column (will be prefixed with colum prefix, if starting with _) * @param string $extra_id='_id' column name for cf id column (will be prefixed with colum prefix, if starting with _)
* @param egw_db $db=null database object, if not the one in $GLOBALS['egw']->db should be used, eg. for an other database * @param egw_db $db=null database object, if not the one in $GLOBALS['egw']->db should be used, eg. for an other database
* @param boolean $no_clone=true can we avoid to clone the db-object, default yes (different from so_sql!) * @param boolean $no_clone=true can we avoid to clone the db-object, default yes (different from so_sql!)
* @param boolean $allow_multiple_values=false should we allow AND store multiple values (1:N relations)
* new code using appnames and foreach(select(...,$app) can set it to avoid an extra instance of the db object * new code using appnames and foreach(select(...,$app) can set it to avoid an extra instance of the db object
* @param boolean $allow_multiple_values=false should we allow AND store multiple values (1:N relations)
* @param string $timestamp_type=null default null=leave them as is, 'ts'|'integer' use integer unix timestamps, 'object' use egw_time objects
*/ */
function __construct($app,$table,$extra_table,$column_prefix='', function __construct($app,$table,$extra_table,$column_prefix='',
$extra_key='_name',$extra_value='_value',$extra_id='_id', $extra_key='_name',$extra_value='_value',$extra_id='_id',
$db=null,$no_clone=true,$allow_multiple_values=false) $db=null,$no_clone=true,$allow_multiple_values=false,$timestamp_type=null)
{ {
// calling the so_sql constructor // calling the so_sql constructor
parent::__construct($app,$table,$db,$column_prefix,$no_clone); parent::__construct($app,$table,$db,$column_prefix,$no_clone,$timestamp_type);
$this->allow_multiple_values = $allow_multiple_values; $this->allow_multiple_values = $allow_multiple_values;
$this->extra_table = $extra_table; $this->extra_table = $extra_table;