1: <?php
2: /**
3: * Cached access to the preferences data.
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: * Cached access to the preferences data.
16: *
17: * Copyright 2011-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: * @since Horde_Kolab_Storage 1.1.0
23: *
24: * @category Kolab
25: * @package Kolab_Storage
26: * @author Gunnar Wrobel <wrobel@pardus.de>
27: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
28: * @link http://pear.horde.org/index.php?package=Kolab_Storage
29: */
30: class Horde_Kolab_Storage_Data_Query_Preferences_Cache
31: implements Horde_Kolab_Storage_Data_Query_Preferences
32: {
33: /** The preferences query data */
34: const PREFS = 'PREFS';
35:
36: /**
37: * The data cache.
38: *
39: * @var Horde_Kolab_Storage_Cache_Data
40: */
41: private $_data_cache;
42:
43: /**
44: * The queriable data.
45: *
46: * @var Horde_Kolab_Storage_Data
47: */
48: private $_data;
49:
50: /**
51: * The cached preference mapping.
52: *
53: * @var array
54: */
55: private $_mapping;
56:
57: /**
58: * Constructor.
59: *
60: * @param Horde_Kolab_Storage_Data $data The queriable data.
61: * @param array $params Additional parameters.
62: */
63: public function __construct(Horde_Kolab_Storage_Data $data,
64: $params)
65: {
66: $this->_data = $data;
67: $this->_data_cache = $params['cache'];
68: }
69:
70: /**
71: * Ensure we have the query data.
72: *
73: * @return NULL
74: */
75: private function _init()
76: {
77: if ($this->_mapping !== null) {
78: return;
79: }
80: if ($this->_data_cache->hasQuery(self::PREFS)) {
81: $this->_mapping = $this->_data_cache->getQuery(self::PREFS);
82: } else {
83: $this->synchronize();
84: }
85: }
86:
87: /**
88: * Return the preferences for the specified application.
89: *
90: * @param string $application The application.
91: *
92: * @return array The preferences.
93: */
94: public function getApplicationPreferences($application)
95: {
96: $this->_init();
97: if (isset($this->_mapping[$application])) {
98: return $this->_data->getObject($this->_mapping[$application]);
99: } else {
100: throw new Horde_Kolab_Storage_Exception(
101: sprintf(
102: 'No preferences for application %s available',
103: $application
104: )
105: );
106: }
107: }
108:
109: /**
110: * Return the applications for which preferences exist in the backend.
111: *
112: * @param string $application The application.
113: *
114: * @return array The applications.
115: */
116: public function getApplications()
117: {
118: $this->_init();
119: return array_keys($this->_mapping);
120: }
121:
122: /**
123: * Synchronize the preferences information with the information from the
124: * backend.
125: *
126: * @param array $params Additional parameters.
127: *
128: * @return NULL
129: */
130: public function synchronize($params = array())
131: {
132: $this->_mapping = array();
133: foreach ($this->_data->getObjects() as $id => $data) {
134: $this->_mapping[$data['application']] = $id;
135: }
136: $this->_data_cache->setQuery(self::PREFS, $this->_mapping);
137: }
138: }