1: <?php
2: /**
3: * The cache decorator for folder lists from Kolab storage.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Storage
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_Storage
12: */
13:
14: /**
15: * The cache decorator for folder lists from Kolab storage.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Kolab
23: * @package Kolab_Storage
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Kolab_Storage
27: */
28: class Horde_Kolab_Storage_List_Decorator_Cache
29: implements Horde_Kolab_Storage_List, Horde_Kolab_Storage_List_Query
30: {
31: /**
32: * Decorated list handler.
33: *
34: * @var Horde_Kolab_Storage_List
35: */
36: private $_list;
37:
38: /**
39: * The list cache.
40: *
41: * @var Horde_Kolab_Storage_Cache_List
42: */
43: private $_list_cache;
44:
45: /**
46: * Has the cache already been loaded and validated?
47: *
48: * @var boolean
49: */
50: private $_init = false;
51:
52: /**
53: * Constructor.
54: *
55: * @param Horde_Kolab_Storage_List $list The original list handler.
56: * @param Horde_Kolab_Storage_Cache_List $cache The cache storing data for
57: * this decorator.
58: */
59: public function __construct(Horde_Kolab_Storage_List $list,
60: Horde_Kolab_Storage_Cache_List $cache)
61: {
62: $this->_list = $list;
63: $this->_list_cache = $cache;
64: }
65:
66: /**
67: * Return the list driver.
68: *
69: * @return Horde_Kolab_Storage_Driver The driver.
70: */
71: public function getDriver()
72: {
73: return $this->_list->getDriver();
74: }
75:
76: /**
77: * Return the ID of the underlying connection.
78: *
79: * @return string The connection ID.
80: */
81: public function getId()
82: {
83: return $this->_list->getId();
84: }
85:
86: /**
87: * Return the connection parameters.
88: *
89: * @return array The connection parameters.
90: */
91: public function getIdParameters()
92: {
93: return $this->_list->getIdParameters();
94: }
95:
96: /**
97: * Check if the cache has been initialized.
98: *
99: * @return NULL
100: */
101: private function _isInitialized()
102: {
103: return ($this->_init || $this->_list_cache->isInitialized());
104: }
105:
106: /**
107: * Check if the cache has been initialized at all and synchronize it if not.
108: *
109: * @return NULL
110: */
111: private function _init()
112: {
113: if (!$this->_isInitialized()) {
114: $this->synchronize();
115: }
116: }
117:
118: /**
119: * Create a new folder.
120: *
121: * @param string $folder The path of the folder to create.
122: * @param string $type An optional type for the folder.
123: *
124: * @return NULL
125: */
126: public function createFolder($folder, $type = null)
127: {
128: if ($this->_isInitialized()) {
129: $folders = $this->listFolders();
130: $types = $this->listFolderTypes();
131: $folders[] = $folder;
132: if (!empty($type)) {
133: $types[$folder] = $type;
134: }
135: $this->_list_cache->store($folders, $types);
136: }
137: $result = $this->_list->createFolder($folder, $type);
138: $this->_list_cache->save();
139: return $result;
140: }
141:
142: /**
143: * Delete a folder.
144: *
145: * @param string $folder The path of the folder to delete.
146: *
147: * @return NULL
148: */
149: public function deleteFolder($folder)
150: {
151: if ($this->_isInitialized()) {
152: $folders = $this->listFolders();
153: $types = $this->listFolderTypes();
154: $folders = array_diff($folders, array($folder));
155: if (isset($types[$folder])) {
156: unset($types[$folder]);
157: }
158: $this->_list_cache->store($folders, $types);
159: }
160: $result = $this->_list->deleteFolder($folder);
161: $this->_list_cache->save();
162: return $result;
163: }
164:
165: /**
166: * Rename a folder.
167: *
168: * @param string $old The old path of the folder.
169: * @param string $new The new path of the folder.
170: *
171: * @return NULL
172: */
173: public function renameFolder($old, $new)
174: {
175: if ($this->_isInitialized()) {
176: $folders = $this->listFolders();
177: $types = $this->listFolderTypes();
178: $folders = array_diff($folders, array($old));
179: $folders[] = $new;
180: if (isset($types[$old])) {
181: $types[$new] = $types[$old];
182: unset($types[$old]);
183: }
184: $this->_list_cache->store($folders, $types);
185: }
186: $result = $this->_list->renameFolder($old, $new);
187: $this->_list_cache->save();
188: return $result;
189: }
190:
191: /**
192: * Returns a representation for the requested folder.
193: *
194: * @param string $folder The path of the folder to return.
195: *
196: * @return Horde_Kolab_Storage_Folder The folder representation.
197: */
198: public function getFolder($folder)
199: {
200: $this->_init();
201: return $this->_list->getFolder($folder);
202: }
203:
204: /**
205: * Mark the specified folder as the default folder of this type.
206: *
207: * @param string $folder The path of the folder to mark as default.
208: *
209: * @return NULL
210: */
211: public function setDefault($folder)
212: {
213: $this->_list->setDefault($folder);
214: }
215:
216: /**
217: * Returns the list of folders visible to the current user.
218: *
219: * @return array The list of folders, represented as a list of strings.
220: */
221: public function listFolders()
222: {
223: $this->_init();
224: return $this->_list_cache->getFolders();
225: }
226:
227: /**
228: * Returns the folder type annotation as associative array.
229: *
230: * @return array The list folder types with the folder names as key and the
231: * folder type as values.
232: */
233: public function listFolderTypes()
234: {
235: $this->_init();
236: return $this->_list_cache->getFolderTypes();
237: }
238:
239: /**
240: * Returns the namespace for the list.
241: *
242: * @return Horde_Kolab_Storage_Folder_Namespace The namespace handler.
243: */
244: public function getNamespace()
245: {
246: $this->_init();
247: return unserialize($this->_list_cache->getNamespace());
248: }
249:
250: /**
251: * Return the last sync stamp.
252: *
253: * @return string The stamp.
254: */
255: public function getStamp()
256: {
257: $this->_init();
258: return $this->_list_cache->getStamp();
259: }
260:
261: /**
262: * Synchronize the list information with the information from the backend.
263: *
264: * @param array $params Additional parameters.
265: *
266: * @return NULL
267: */
268: public function synchronize($params = array())
269: {
270: $this->_store(
271: $this->_list->listFolders(),
272: $this->_list->listFolderTypes()
273: );
274: }
275:
276: /**
277: * Store updated information in the list cache.
278: *
279: * @param array|NULL $folders The list of folders.
280: * @param array|NULL $types The list of types.
281: *
282: * @return NULL
283: */
284: private function _store($folders, $types)
285: {
286: $this->_list_cache->store($folders, $types);
287:
288: if (!$this->_list_cache->hasNamespace()) {
289: $this->_list_cache->setNamespace(
290: serialize($this->_list->getNamespace())
291: );
292: }
293:
294: $this->_list->synchronize();
295:
296: $this->_list_cache->save();
297:
298: $this->_init = true;
299: }
300:
301: /**
302: * Register a query to be updated if the underlying data changes.
303: *
304: * @param string $name The query name.
305: * @param Horde_Kolab_Storage_Query $query The query to register.
306: *
307: * @return NULL
308: */
309: public function registerQuery($name, Horde_Kolab_Storage_Query $query)
310: {
311: $this->_list->registerQuery($name, $query);
312: }
313:
314: /**
315: * Return a registered query.
316: *
317: * @param string $name The query name.
318: *
319: * @return Horde_Kolab_Storage_Query The requested query.
320: *
321: * @throws Horde_Kolab_Storage_Exception In case the requested query does
322: * not exist.
323: */
324: public function getQuery($name = null)
325: {
326: return $this->_list->getQuery($name);
327: }
328: }