new search method plus documentation

This commit is contained in:
Ralf Becker 2006-08-11 20:50:36 +00:00
parent e19d53e870
commit 3d831bf7d7

View File

@ -1,32 +1,46 @@
<?php
/**************************************************************************\
* eGroupWare API - Record history logging *
* This file written by Joseph Engo <jengo@phpgroupware.org> *
* Copyright (C) 2001 Joseph Engo *
* -------------------------------------------------------------------------*
* This library is part of the eGroupWare API *
* http://www.egroupware.org/api *
* ------------------------------------------------------------------------ *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, *
* or any later version. *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
\**************************************************************************/
/**
* API - Record history logging
*
* This class extends a backend class (at them moment SQL or LDAP) and implements some
* caching on to top of the backend functions. The cache is share for all instances of
* the accounts class and for LDAP it is persistent through the whole session, for SQL
* it's only on a per request basis.
*
* @link http://www.egroupware.org
* @author Joseph Engo <jengo@phpgroupware.org>
* @copyright 2001 by Joseph Engo <jengo@phpgroupware.org>
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> new DB-methods and search
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage db
* @access public
* @version $Id$
*/
// $Id$
class historylog
{
/**
* Record history logging service
*
* This class need to be instanciated for EACH app, which wishes to use it!
*/
class historylog
{
var $db;
var $table = 'egw_history_log';
/**
* App.name this class is instanciated for / working on
*
* @var string
*/
var $appname;
/**
* 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 int
*/
var $tz_offset_s;
var $template;
var $nextmatchs;
var $types = array(
@ -36,6 +50,12 @@
);
var $alternate_handlers = array();
/**
* Constructor
*
* @param string $appname app name this instance operates on
* @return historylog
*/
function historylog($appname='')
{
if (!$appname)
@ -53,26 +73,41 @@
$this->db = clone($GLOBALS['egw']->db);
}
$this->db->set_app('phpgwapi');
if (!is_object($GLOBALS['egw']->datetime))
{
$GLOBALS['egw']->datetime =& CreateObject('phpgwapi.datetime');
}
$this->tz_offset_s = $GLOBALS['egw']->datetime->tz_offset;
}
/**
* Delete the history-log of one or multiple records of $this->appname
*
* @param int/array $record_id one or more id's of $this->appname, or null to delete ALL records of $this->appname
* @return int number of deleted records/rows (0 is not necessaryly an error, it can just mean there's no record!)
*/
function delete($record_id)
{
$where = array('history_appname' => $record_id);
if (is_array($record_id) || is_numeric($record_id))
{
$where = array(
'history_record_id' => $record_id,
'history_appname' => $this->appname,
);
}
else
{
$where = array('history_appname' => $record_id);
$where['history_record_id'] = $record_id;
}
$this->db->delete($this->table,$where,__LINE__,__FILE__);
return $this->db->affected_rows();
}
/**
* Add a history record, if $new_value != $old_value
*
* @param string $status 2 letter code: eg. $this->types: C=Created, D=Deleted, E=Edited
* @param int $record_id it of the record in $this->appname (set by the constructor)
* @param string $new_value new value
* @param string $old_value old value
*/
function add($status,$record_id,$new_value,$old_value)
{
if ($new_value != $old_value)
@ -89,8 +124,59 @@
}
}
// array $filter_out
function return_array($filter_out,$only_show,$_orderby = '',$sort = '', $record_id)
/**
* Search history-log
*
* @param array/int $filter array with filters, or int record_id
* @param string $order='history_id' sorting after history_id is identical to history_timestamp
* @param string $sort='ASC'
* @return array of arrays with keys id, record_id, appname, owner (account_id), status, new_value, old_value,
* timestamp (Y-m-d H:i:s in servertime), user_ts (timestamp in user-time)
*/
function search($filter,$order='history_id',$sort='DESC')
{
if (!is_array($filter)) $filter = (int)$filter ? array('history_record_id' => $filter) : array();
if (!$_orderby || !preg_match('/^[a-z0-9_]+$/i',$_orderby) || !preg_match('/^(asc|desc)?$/i',$sort))
{
$orderby = 'ORDER BY history_id DESC';
}
else
{
$orderby = "ORDER BY $_orderby $sort";
}
foreach($filter as $col => $value)
{
if (substr($col,0,8) != 'history_')
{
$filter['history_'.$col] = $value;
unset($filter[$col]);
}
}
if (!isset($filter['history_appname'])) $filter['history_appname'] = $this->appname;
$this->db->select($this->table,'*',$filter,__LINE__,__FILE__,false,$orderby);
$rows = array();
while(($row = $this->db->row(true,'history_')))
{
$row['user_ts'] = $this->db->from_timestamp($row['timestamp']) + 3600 * $GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'];
$rows[] = $row;
}
return $rows;
}
/**
* return history-log for one record of $this->appname
*
* @deprecated use search
* @param array $filter_out stati to NOT show
* @param array $only_show stati to show
* @param string $_orderby column name to order, default history_timestamp,history_id
* @param string $sort ASC,DESC
* @param int $record_id id of the record in $this->appname (set by the constructor)
* @return array of arrays with keys id, record_id, owner (account_lid!), status, new_value, old_value, datetime (timestamp in servertime)
*/
function return_array($filter_out,$only_show,$_orderby,$sort, $record_id)
{
if (!$_orderby || !preg_match('/^[a-z0-9_]+$/i',$_orderby) || !preg_match('/^(asc|desc)?$/i',$sort))
@ -139,7 +225,17 @@
return $return_values;
}
function return_html($filter_out,$orderby = '',$sort = '', $record_id)
/**
* Creates html to show the history-log of one record
*
* @deprecated use eg. the historylog_widget of eTemplate or your own UI
* @param array $filter_out see stati to NOT show
* @param string $orderby column-name to order by
* @param string $sort ASC, DESC
* @param int $record_id id of the record in $this->appname (set by the constructor)
* @return string the html
*/
function return_html($filter_out,$orderby,$sort, $record_id)
{
$this->template =& CreateObject('phpgwapi.Template',EGW_TEMPLATE_DIR);
$this->nextmatchs =& CreateObject('phpgwapi.nextmatchs');
@ -201,4 +297,4 @@
}
return $this->template->fp('out','list');
}
}
}