egroupware/phpgwapi/inc/class.contenthistory.inc.php

219 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2008-03-21 18:15:02 +01:00
/**
* eGW API - content history class
*
2008-03-21 18:15:02 +01:00
* @link http://www.egroupware.org
* @author Lars Kneschke [lkneschke@linux-at-work.de]
* @copyright Lars Kneschke 2005
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @version $Id$
*/
2008-03-21 18:15:02 +01:00
/**
* class to maintain history of content
*
* This class contains all logic of the egw content history.
*/
class contenthistory
{
/**
2008-03-21 18:15:02 +01:00
* Name of the content-history table
*/
const TABLE = 'egw_api_content_history';
/**
* @var egw_db
*/
private $db;
function __construct()
{
$this->db = $GLOBALS['egw']->db;
}
2008-03-21 18:15:02 +01:00
/**
* mark mapping as expired
*
* mark a mapping from externel to internal id as expired, when
* a egw entry gets deleted
*
* @param string $_appName the appname example: infolog_notes
* @param int $_id the internal egwapp content id
* @return boolean
2008-03-21 18:15:02 +01:00
*/
function expireMapping($_appName, $_id)
{
2008-03-21 18:15:02 +01:00
return !!$this->db->update('egw_contentmap',array (
'map_expired' => 1,
),array (
'map_guid' => $GLOBALS['egw']->common->generate_uid($_appName, $_id),
),__LINE__,__FILE__,'syncml');
2008-03-21 18:15:02 +01:00
}
2008-03-21 18:15:02 +01:00
/**
* get the timestamp for action
*
* find which content changed since $_ts for application $_appName
*
* @param string$_appName the appname example: infolog_notes
* @param string $_action can be modify, add or delete
* @param string $_ts timestamp where to start searching from
* @param array $readableItems (optional) readable items of current user
* @return array containing contentIds with changes
2008-03-21 18:15:02 +01:00
*/
function getHistory($_appName, $_action, $_ts, $readableItems = false)
2008-03-21 18:15:02 +01:00
{
$where = array('sync_appname' => $_appName);
$ts = $this->db->to_timestamp($_ts);
$idList = array();
2008-03-21 18:15:02 +01:00
switch($_action)
{
case 'modify':
$where[] = "sync_modified > '".$ts."' AND sync_deleted IS NULL";
2008-03-21 18:15:02 +01:00
break;
case 'delete':
$where[] = "sync_deleted > '".$ts."'";
2008-03-21 18:15:02 +01:00
break;
case 'add':
$where[] = "sync_added > '".$ts."' AND sync_deleted IS NULL AND sync_modified IS NULL";
2008-03-21 18:15:02 +01:00
break;
default:
// no valid $_action set
return array();
}
if (is_array($readableItems))
{
foreach ($readableItems as $id)
{
$where['sync_contentid'] = $id;
if (!$this->db->select(self::TABLE,'sync_contentid',$where,__LINE__,__FILE__))
{
$idList[] = $id;
}
}
}
else
{
foreach ($this->db->select(self::TABLE,'sync_contentid',$where,__LINE__,__FILE__) as $row)
{
$idList[] = $row['sync_contentid'];
}
}
return $idList;
2008-03-21 18:15:02 +01:00
}
2008-03-21 18:15:02 +01:00
/**
* when got a entry last added/modified/deleted
*
* @param $_guid string the global uid of the entry
* @param $_action string can be add, delete or modify
* @return string the last timestamp
*/
function getTSforAction($_appName, $_id, $_action)
2008-03-21 18:15:02 +01:00
{
switch($_action)
2005-06-19 20:07:06 +02:00
{
2008-03-21 18:15:02 +01:00
case 'add':
$col = 'sync_added';
break;
case 'delete':
$col = 'sync_deleted';
break;
case 'modify':
$col = 'sync_modified';
break;
default:
return false;
2005-06-19 20:07:06 +02:00
}
2008-03-21 18:15:02 +01:00
$where = array (
'sync_appname' => $_appName,
'sync_contentid' => $_id,
2008-03-21 18:15:02 +01:00
);
2005-06-19 20:07:06 +02:00
if (($ts = $this->db->select(self::TABLE,$col,$where,__LINE__,__FILE__)->fetchColumn()))
{
2008-03-21 18:15:02 +01:00
$ts = $this->db->from_timestamp($ts);
}
2008-03-21 18:15:02 +01:00
return $ts;
}
2008-03-21 18:15:02 +01:00
/**
* update a timestamp for action
*
* @param string $_appName the appname example: infolog_notes
* @param int $_id the app internal content id
* @param string $_action can be modify, add or delete
* @param string $_ts timestamp where to start searching from
2008-03-21 18:15:02 +01:00
* @return boolean returns allways true
*/
function updateTimeStamp($_appName, $_id, $_action, $_ts)
{
$newData = array (
'sync_appname' => $_appName,
'sync_contentid' => $_id,
'sync_added' => $this->db->to_timestamp($_ts),
'sync_changedby' => $GLOBALS['egw_info']['user']['account_id'],
);
switch($_action)
{
2008-03-21 18:15:02 +01:00
case 'add':
$this->db->insert(self::TABLE,$newData,array(),__LINE__,__FILE__);
break;
2008-03-21 18:15:02 +01:00
case 'modify':
case 'delete':
// first check that this entry got ever added to database already
$where = array (
'sync_appname' => $_appName,
'sync_contentid' => $_id,
);
if (!$this->db->select(self::TABLE,'sync_contentid',$where,__LINE__,__FILE__)->fetchColumn())
{
2008-03-21 18:15:02 +01:00
$this->db->insert(self::TABLE,$newData,array(),__LINE__,__FILE__);
}
2008-03-21 18:15:02 +01:00
// now update the time stamp
$newData = array (
'sync_changedby' => $GLOBALS['egw_info']['user']['account_id'],
$_action == 'delete' ? 'sync_deleted' : 'sync_modified' => $this->db->to_timestamp($_ts),
2008-03-21 18:15:02 +01:00
);
$this->db->update(self::TABLE, $newData, $where,__LINE__,__FILE__);
break;
}
2008-03-21 18:15:02 +01:00
return true;
}
2010-06-29 15:52:56 +02:00
/**
* get the timestamp of last change for appname
*
* find which content changed since $_ts for application $_appName
*
* @param string$_appName the appname example: infolog_notes
*
* @return timestamp of last change for this application
*/
function getLastChange($_appName)
{
$max = 0;
$where = array('sync_appname' => $_appName);
foreach (array('modified','deleted','added') as $action)
{
if (($ts = $this->db->select(self::TABLE,'MAX(sync_'.$action.')',$where,__LINE__,__FILE__)->fetchColumn()))
{
$ts = $this->db->from_timestamp($ts);
if ($ts > $max) $max = $ts;
}
}
return $max;
}
2008-03-21 18:15:02 +01:00
}