1: <?php
2: /**
3: * The hook that updates the Horde history information once data gets
4: * synchronized with the Kolab backend.
5: *
6: * PHP version 5
7: *
8: * @category Kolab
9: * @package Kolab_Storage
10: * @author Gunnar Wrobel <wrobel@pardus.de>
11: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
12: * @link http://pear.horde.org/index.php?package=Kolab_Storage
13: */
14:
15: /**
16: * The hook that updates the Horde history information once data gets
17: * synchronized with the Kolab backend.
18: *
19: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
20: *
21: * See the enclosed file COPYING for license information (LGPL). If you
22: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
23: *
24: * @since Horde_Kolab_Storage 1.1.0
25: *
26: * @category Kolab
27: * @package Kolab_Storage
28: * @author Gunnar Wrobel <wrobel@pardus.de>
29: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
30: * @link http://pear.horde.org/index.php?package=Kolab_Storage
31: */
32: class Horde_Kolab_Storage_Data_Query_History_Base
33: implements Horde_Kolab_Storage_Data_Query_History
34: {
35: /**
36: * The queriable data.
37: *
38: * @var Horde_Kolab_Storage_Data
39: */
40: protected $data;
41:
42: /**
43: * The history handler.
44: *
45: * @var Horde_History
46: */
47: protected $history;
48:
49: /**
50: * Constructor.
51: *
52: * @param Horde_Kolab_Storage_Data $data The queriable data.
53: * @param array $params Additional parameters.
54: */
55: public function __construct(Horde_Kolab_Storage_Data $data,
56: $params)
57: {
58: $this->data = $data;
59: $this->history = $params['factory']->createHistory($data->getAuth());
60: }
61:
62: /**
63: * Synchronize the preferences information with the information from the
64: * backend.
65: *
66: * @param array $params Additional parameters.
67: *
68: * @return NULL
69: */
70: public function synchronize($params = array())
71: {
72: $stamp = $this->data->getStamp();
73: if (isset($params['changes'])) {
74: foreach ($params['changes'][Horde_Kolab_Storage_Folder_Stamp::ADDED] as $bid => $object) {
75: $this->_updateLog($object['uid'], $bid, $stamp);
76: }
77: foreach ($params['changes'][Horde_Kolab_Storage_Folder_Stamp::DELETED] as $bid => $object) {
78: $this->history->log(
79: $object, array('action' => 'delete', 'bid' => $bid, 'stamp' => $stamp)
80: );
81: }
82: } else {
83: foreach ($this->data->getObjectToBackend() as $object => $bid) {
84: $this->_updateLog($object, $bid, $stamp);
85: }
86: }
87: }
88:
89: /**
90: * Update the history log for an object.
91: *
92: * @param string $object The object ID.
93: * @param string $bid The backend ID of
94: * the object.
95: * @param Horde_Kolab_Storage_Folder_Stamp $stamp The folder stamp.
96: *
97: * @return NULL
98: */
99: private function _updateLog($object, $bid, $stamp)
100: {
101: $log = $this->history->getHistory($object);
102: if (count($log) == 0) {
103: $this->history->log(
104: $object, array('action' => 'add', 'bid' => $bid, 'stamp' => $stamp)
105: );
106: } else {
107: $last = array('ts' => 0);
108: foreach ($log as $entry) {
109: if ($entry['ts'] > $last['ts']) {
110: $last = $entry;
111: }
112: }
113: if (!isset($last['bid']) || $last['bid'] != $bid
114: || (isset($last['stamp']) && $last['stamp']->isReset($stamp))) {
115: $this->history->log(
116: $object, array('action' => 'modify', 'bid' => $bid, 'stamp' => $stamp)
117: );
118: }
119: }
120: }
121: }