1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Horde_Kolab_Storage_List_Query_List_Cache
29: implements Horde_Kolab_Storage_List_Query_List
30: {
31:
32: const TYPES = 'TYPES';
33:
34:
35: const BY_TYPE = 'BY_TYPE';
36:
37:
38: const FOLDERS = 'FOLDERS';
39:
40:
41: const OWNERS = 'OWNERS';
42:
43:
44: const PERSONAL_DEFAULTS = 'PERSONAL_DEFAULTS';
45:
46:
47: const DEFAULTS = 'DEFAULTS';
48:
49: 50: 51: 52: 53:
54: private $_list;
55:
56: 57: 58: 59: 60:
61: private $_list_cache;
62:
63: 64: 65: 66: 67:
68: private $_factory;
69:
70: 71: 72: 73: 74: 75:
76: public function __construct(Horde_Kolab_Storage_List $list,
77: $params)
78: {
79: $this->_list = $list;
80: $this->_list_cache = $params['cache'];
81: $this->_factory = $params['factory'];
82: }
83:
84: 85: 86: 87: 88: 89: 90:
91: private function _init($query)
92: {
93: if (!$this->_list_cache->hasQuery($query)) {
94: $this->synchronize();
95: }
96: }
97:
98: 99: 100: 101: 102: 103:
104: public function listTypes()
105: {
106: $this->_init(self::TYPES);
107: return $this->_list_cache->getQuery(self::TYPES);
108: }
109:
110: 111: 112: 113: 114: 115:
116: public function listFolderTypeAnnotations()
117: {
118: if (!$this->_list_cache->hasFolderTypes()) {
119: $this->synchronize();
120: }
121: $result = array();
122: $list = $this->_list_cache->getFolderTypes();
123: foreach ($list as $folder => $annotation) {
124: $result[$folder] = $this->_factory->createFolderType($annotation);
125: }
126: return $result;
127: }
128:
129: 130: 131: 132: 133: 134: 135:
136: public function listByType($type)
137: {
138: $this->_init(self::BY_TYPE);
139: $by_type = $this->_list_cache->getQuery(self::BY_TYPE);
140: if (isset($by_type[$type])) {
141: return array_keys($by_type[$type]);
142: } else {
143: return array();
144: }
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: public function dataByType($type)
155: {
156: $this->_init(self::BY_TYPE);
157: $data_by_type = $this->_list_cache->getQuery(self::BY_TYPE);
158: if (isset($data_by_type[$type])) {
159: return $data_by_type[$type];
160: } else {
161: return array();
162: }
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function folderData($folder)
173: {
174: $this->_init(self::FOLDERS);
175: $folders = $this->_list_cache->getQuery(self::FOLDERS);
176: if (isset($folders[$folder])) {
177: return $folders[$folder];
178: } else {
179: throw new Horde_Kolab_Storage_Exception(
180: sprintf('Folder %s does not exist!', $folder)
181: );
182: }
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function listOwners()
192: {
193: $this->_init(self::OWNERS);
194: return $this->_list_cache->getQuery(self::OWNERS);
195: }
196:
197: 198: 199: 200: 201: 202:
203: public function listPersonalDefaults()
204: {
205: $this->_init(self::PERSONAL_DEFAULTS);
206: return $this->_list_cache->getQuery(self::PERSONAL_DEFAULTS);
207: }
208:
209: 210: 211: 212: 213: 214: 215:
216: public function listDefaults()
217: {
218: $this->_init(self::DEFAULTS);
219: return $this->_list_cache->getQuery(self::DEFAULTS);
220: }
221:
222: 223: 224: 225: 226: 227: 228:
229: public function getDefault($type)
230: {
231: $this->_init(self::PERSONAL_DEFAULTS);
232: $defaults = $this->_list_cache->getQuery(self::PERSONAL_DEFAULTS);
233: if (isset($defaults[$type])) {
234: return $defaults[$type];
235: } else {
236: return false;
237: }
238: }
239:
240: 241: 242: 243: 244: 245: 246: 247:
248: public function getForeignDefault($owner, $type)
249: {
250: $this->_init(self::DEFAULTS);
251: $defaults = $this->_list_cache->getQuery(self::DEFAULTS);
252: if (isset($defaults[$owner][$type])) {
253: return $defaults[$owner][$type];
254: } else {
255: return false;
256: }
257: }
258:
259: 260: 261: 262: 263: 264: 265: 266:
267: public function createFolder($folder, $type = null)
268: {
269: $this->synchronize();
270: }
271:
272: 273: 274: 275: 276: 277: 278:
279: public function deleteFolder($folder)
280: {
281: $this->synchronize();
282: }
283:
284: 285: 286: 287: 288: 289: 290: 291:
292: public function renameFolder($old, $new)
293: {
294: $this->synchronize();
295: }
296:
297: 298: 299: 300: 301:
302: public function getStamp()
303: {
304: return $this->_list->getStamp();
305: }
306:
307: 308: 309: 310: 311: 312: 313:
314: public function synchronize($params = array())
315: {
316: $namespace = $this->_list->getNamespace();
317: $annotations = $this->listFolderTypeAnnotations();
318: $mail_type = $this->_factory->createFolderType('mail');
319:
320: $folders = array();
321: $owners = array();
322: $types = array();
323: $by_type = array();
324: $personal_defaults = array();
325: $defaults = array();
326:
327: foreach ($this->_list->listFolders() as $folder) {
328: if (!isset($annotations[$folder])) {
329: $type = $mail_type;
330: } else {
331: $type = $annotations[$folder];
332: }
333: $folder_type = $type->getType();
334: $owner = $namespace->getOwner($folder);
335:
336: $owners[$folder] = $owner;
337: $folders[$folder] = array(
338: 'type' => $folder_type,
339: 'default' => $type->isDefault(),
340: 'namespace' => $namespace->matchNamespace($folder)->getType(),
341: 'prefix' => $namespace->matchNamespace($folder)->getName(),
342: 'owner' => $owner,
343: 'name' => $namespace->getTitle($folder),
344: 'subpath' => $namespace->getSubpath($folder),
345: 'parent' => $namespace->getParent($folder),
346: 'delimiter' => $namespace->matchNamespace($folder)->getDelimiter(),
347: );
348:
349: $types[$folder] = $folders[$folder]['type'];
350: $by_type[$folder_type][$folder] = array(
351: 'default' => $folders[$folder]['parent'],
352: 'owner' => $folders[$folder]['owner'],
353: 'name' => $folders[$folder]['name'],
354: 'prefix' => $folders[$folder]['prefix'],
355: 'parent' => $folders[$folder]['parent'],
356: 'delimiter' => $folders[$folder]['delimiter'],
357: 'subpath' => $folders[$folder]['subpath'],
358: 'folder' => $folder,
359: );
360:
361: if ($folders[$folder]['default']) {
362: if (!isset($defaults[$owner][$folder_type])) {
363: $defaults[$owner][$folder_type] = $folder;
364: } else {
365: throw new Horde_Kolab_Storage_Exception(
366: sprintf(
367: 'Both folders %s and %s are marked as default folder of type %s!',
368: $defaults[$owner][$folder_type],
369: $folder,
370: $folder_type
371: )
372: );
373: }
374: if ($folders[$folder]['namespace']
375: == Horde_Kolab_Storage_Folder_Namespace::PERSONAL) {
376: if (!isset($personal_defaults[$folder_type])) {
377: $personal_defaults[$folder_type] = $folder;
378: } else {
379: throw new Horde_Kolab_Storage_Exception(
380: sprintf(
381: 'Both folders %s and %s are marked as default folder of type %s!',
382: $personal_defaults[$folder_type],
383: $folder,
384: $folder_type
385: )
386: );
387: }
388: }
389: }
390: }
391:
392: $this->_list_cache->setQuery(self::FOLDERS, $folders);
393: $this->_list_cache->setQuery(self::OWNERS, $owners);
394: $this->_list_cache->setQuery(self::TYPES, $types);
395: $this->_list_cache->setQuery(self::BY_TYPE, $by_type);
396: $this->_list_cache->setQuery(self::DEFAULTS, $defaults);
397: $this->_list_cache->setQuery(
398: self::PERSONAL_DEFAULTS, $personal_defaults
399: );
400: }
401: }