1: <?php
2: /**
3: * The basic handler for accessing 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 basic handler for accessing folder lists from Kolab storage.
16: *
17: * Copyright 2004-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_Base
29: implements Horde_Kolab_Storage_List, Horde_Kolab_Storage_List_Query
30: {
31: /** The folder type annotation */
32: const ANNOTATION_FOLDER_TYPE = '/shared/vendor/kolab/folder-type';
33:
34: /**
35: * The driver for accessing the Kolab storage system.
36: *
37: * @var Horde_Kolab_Storage_Driver
38: */
39: private $_driver;
40:
41: /**
42: * The factory for generating additional resources.
43: *
44: * @var Horde_Kolab_Storage_Factory
45: */
46: private $_factory;
47:
48: /**
49: * The list of registered queries.
50: *
51: * @var array
52: */
53: private $_queries = array();
54:
55: /**
56: * Constructor.
57: *
58: * @param Horde_Kolab_Storage_Driver $driver The primary connection driver.
59: * @param Horde_Kolab_Storage_Factory $factory The factory.
60: */
61: public function __construct(Horde_Kolab_Storage_Driver $driver,
62: Horde_Kolab_Storage_Factory $factory)
63: {
64: $this->_driver = $driver;
65: $this->_factory = $factory;
66: }
67:
68: /**
69: * Return the list driver.
70: *
71: * @return Horde_Kolab_Storage_Driver The driver.
72: */
73: public function getDriver()
74: {
75: return $this->_driver;
76: }
77:
78: /**
79: * Return the ID of the underlying connection.
80: *
81: * @return string The connection ID.
82: */
83: public function getId()
84: {
85: return $this->_driver->getId();
86: }
87:
88: /**
89: * Return the connection parameters.
90: *
91: * @return array The connection parameters.
92: */
93: public function getIdParameters()
94: {
95: return $this->_driver->getParameters();
96: }
97:
98: /**
99: * Create a new folder.
100: *
101: * @param string $folder The path of the folder to create.
102: * @param string $type An optional type for the folder.
103: *
104: * @return NULL
105: */
106: public function createFolder($folder, $type = null)
107: {
108: $this->_driver->create($folder);
109: if ($type) {
110: $this->_driver->setAnnotation(
111: $folder, self::ANNOTATION_FOLDER_TYPE, $type
112: );
113: }
114: foreach ($this->_queries as $name => $query) {
115: $query->createFolder($folder, $type);
116: }
117: }
118:
119: /**
120: * Delete a folder.
121: *
122: * WARNING: Do not use this call in case there is still data present in the
123: * folder. You are required to empty any data set *before* removing the
124: * folder. Otherwise there is no guarantee you can adhere to that Kolab
125: * specification that might require the triggering of remote systems to
126: * inform them about the removal of the folder.
127: *
128: * @param string $folder The path of the folder to delete.
129: *
130: * @return NULL
131: */
132: public function deleteFolder($folder)
133: {
134: $this->_driver->delete($folder);
135: foreach ($this->_queries as $name => $query) {
136: $query->deleteFolder($folder);
137: }
138: }
139:
140: /**
141: * Rename a folder.
142: *
143: * @param string $old The old path of the folder.
144: * @param string $new The new path of the folder.
145: *
146: * @return NULL
147: */
148: public function renameFolder($old, $new)
149: {
150: $this->_driver->rename($old, $new);
151: foreach ($this->_queries as $name => $query) {
152: $query->renameFolder($old, $new);
153: }
154: }
155:
156: /**
157: * Returns a representation for the requested folder.
158: *
159: * @param string $folder The path of the folder to return.
160: *
161: * @return Horde_Kolab_Storage_Folder The folder representation.
162: */
163: public function getFolder($folder)
164: {
165: return $this->_factory->createFolder(
166: $this,
167: $folder
168: );
169: }
170:
171: /**
172: * Mark the specified folder as the default folder of this type.
173: *
174: * @param string $folder The path of the folder to mark as default.
175: *
176: * @return NULL
177: */
178: public function setDefault($folder)
179: {
180: /* $result = array(); */
181: /* $namespace = $this->getNamespace(); */
182: /* $list = $this->listFolderTypes(); */
183: /* foreach ($list as $name => $annotation) { */
184: /* $result[$name] = $this->_factory->createFolderType($annotation); */
185: /* } */
186: /* if (!isset($result[$folder])) { */
187: /* $type = 'mail'; */
188: /* } else { */
189: /* $type = $result[$folder]->getType(); */
190: /* } */
191: /* foreach ($result as $name => $annotation) { */
192: /* if ($annotation->getType() == $type */
193: /* && $annotation->isDefault() */
194: /* && ($namespace->matchNamespace($name)->getType() */
195: /* == Horde_Kolab_Storage_Folder_Namespace::PERSONAL)) { */
196: /* $this->_driver->setAnnotation( */
197: /* $name, */
198: /* self::ANNOTATION_FOLDER_TYPE, */
199: /* $type */
200: /* ); */
201: /* } */
202: /* } */
203: /* $this->_driver->setAnnotation( */
204: /* $folder, */
205: /* self::ANNOTATION_FOLDER_TYPE, */
206: /* $type . '.default' */
207: /* ); */
208: }
209:
210: /**
211: * Returns the list of folders visible to the current user.
212: *
213: * @return array The list of folders, represented as a list of strings.
214: */
215: public function listFolders()
216: {
217: return $this->_driver->listFolders();
218: }
219:
220: /**
221: * Returns the folder type annotation as associative array.
222: *
223: * @return array The list folder types with the folder names as key and the
224: * folder type as values.
225: */
226: public function listFolderTypes()
227: {
228: return $this->_driver->listAnnotation(
229: self::ANNOTATION_FOLDER_TYPE
230: );
231: }
232:
233: /**
234: * Returns the namespace for the list.
235: *
236: * @return Horde_Kolab_Storage_Folder_Namespace The namespace handler.
237: */
238: public function getNamespace()
239: {
240: return $this->_driver->getNamespace();
241: }
242:
243: /**
244: * Return the last sync stamp.
245: *
246: * @return string The stamp.
247: */
248: public function getStamp()
249: {
250: return pack('Nn', time(), mt_rand());
251: }
252:
253: /**
254: * Synchronize the list information with the information from the backend.
255: *
256: * @param array $params Additional parameters.
257: *
258: * @return NULL
259: */
260: public function synchronize($params = array())
261: {
262: foreach ($this->_queries as $name => $query) {
263: $query->synchronize($params);
264: }
265: }
266:
267: /**
268: * Register a query to be updated if the underlying data changes.
269: *
270: * @param string $name The query name.
271: * @param Horde_Kolab_Storage_Query $query The query to register.
272: *
273: * @return NULL
274: */
275: public function registerQuery($name, Horde_Kolab_Storage_Query $query)
276: {
277: if (!$query instanceOf Horde_Kolab_Storage_List_Query) {
278: throw new Horde_Kolab_Storage_Exception(
279: 'The provided query is no list query.'
280: );
281: }
282: $this->_queries[$name] = $query;
283: }
284:
285: /**
286: * Return a registered query.
287: *
288: * @param string $name The query name.
289: *
290: * @return Horde_Kolab_Storage_Query The requested query.
291: *
292: * @throws Horde_Kolab_Storage_Exception In case the requested query does
293: * not exist.
294: */
295: public function getQuery($name = null)
296: {
297: if ($name === null) {
298: $name = self::QUERY_BASE;
299: }
300: if (isset($this->_queries[$name])) {
301: return $this->_queries[$name];
302: } else {
303: throw new Horde_Kolab_Storage_Exception('No such query!');
304: }
305: }
306: }