1: <?php
2: /**
3: * Adds a set of uncached queries to the list handlers.
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: * Adds a set of uncached queries to the list handlers.
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: * @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: abstract class Horde_Kolab_Storage_QuerySet_Base
29: implements Horde_Kolab_Storage_QuerySet
30: {
31: /** Query set identifiers */
32: const BASIC = 'basic';
33: const HORDE = 'horde';
34:
35: /**
36: * The factory for generating additional resources.
37: *
38: * @var Horde_Kolab_Storage_Factory
39: */
40: private $_factory;
41:
42: /**
43: * The list of query types to add to lists.
44: *
45: * @var array
46: */
47: private $_list_queries = array();
48:
49: /**
50: * The list of query types to add to data handlers.
51: *
52: * @var array
53: */
54: private $_data_queries = array();
55:
56: /**
57: * The query class map. Override in extending classes.
58: *
59: * @var array
60: */
61: protected $_class_map = array();
62:
63: /**
64: * Predefined query sets.
65: *
66: * @var array
67: */
68: private $_list_query_sets = array(
69: self::BASIC => array(
70: Horde_Kolab_Storage_List::QUERY_BASE,
71: Horde_Kolab_Storage_List::QUERY_ACL
72: ),
73: self::HORDE => array(
74: Horde_Kolab_Storage_List::QUERY_BASE,
75: Horde_Kolab_Storage_List::QUERY_ACL,
76: Horde_Kolab_Storage_List::QUERY_SHARE
77: )
78: );
79:
80: /**
81: * Predefined query sets.
82: *
83: * @var array
84: */
85: private $_data_query_sets = array(
86: self::HORDE => array(
87: Horde_Kolab_Storage_Data::QUERY_PREFS => 'h-prefs',
88: Horde_Kolab_Storage_Data::QUERY_HISTORY => true,
89: )
90: );
91:
92: /**
93: * Constructor.
94: *
95: * @param Horde_Kolab_Storage_Factory $factory The factory.
96: * @param array $params Optional parameters.
97: * <pre>
98: * - list: Array of list query settings
99: * - queryset [string]: One of the predefined query set
100: * ("basic" or "horde")
101: * - myset [string]: A list of query types that should be selected.
102: * - classmap [array] : An array of "type" => "class" mappings.
103: * </pre>
104: */
105: public function __construct(Horde_Kolab_Storage_Factory $factory,
106: array $params = array())
107: {
108: $this->_factory = $factory;
109:
110: if (isset($params['list']['classmap'])) {
111: $this->_class_map = array_merge(
112: $this->_class_map, $params['list']['classmap']
113: );
114: }
115: if (isset($params['list']['queryset'])) {
116: if (isset($this->_list_query_sets[$params['list']['queryset']])) {
117: $this->_list_queries = $this->_list_query_sets[$params['list']['queryset']];
118: } else {
119: throw new Horde_Kolab_Storage_Exception(
120: sprintf(
121: 'List query set %s not supported!',
122: $params['list']['queryset']
123: )
124: );
125: }
126: }
127: if (isset($params['list']['myset'])) {
128: $this->_list_queries = array_merge($this->_list_queries, $params['list']['myset']);
129: }
130: if (empty($this->_list_queries)) {
131: $this->_list_queries = $this->_list_query_sets[self::BASIC];
132: }
133:
134: if (isset($params['data']['queryset'])) {
135: if (isset($this->_data_query_sets[$params['data']['queryset']])) {
136: $this->_data_queries = $this->_data_query_sets[$params['data']['queryset']];
137: } else {
138: throw new Horde_Kolab_Storage_Exception(
139: sprintf(
140: 'Data query set %s not supported!',
141: $params['data']['queryset']
142: )
143: );
144: }
145: }
146: if (isset($params['data']['myset'])) {
147: $this->_data_queries = array_merge($this->_data_queries, $params['data']['myset']);
148: }
149: }
150:
151: /**
152: * Add the set of list queries.
153: *
154: * @param Horde_Kolab_Storage_List $list The list.
155: * @param array $params Additional query parameters.
156: *
157: * @return NULL
158: */
159: public function addListQuerySet(Horde_Kolab_Storage_List $list, $params = array())
160: {
161: foreach ($this->_list_queries as $query) {
162: $this->_addListQuery($list, $query, $params);
163: }
164: }
165:
166: /**
167: * Add a list query.
168: *
169: * @param Horde_Kolab_Storage_List $list The list.
170: * @param string $type The query type.
171: * @param array $params Additional query parameters.
172: *
173: * @return NULL
174: */
175: private function _addListQuery(Horde_Kolab_Storage_List $list, $type, $params = array())
176: {
177: if (isset($this->_class_map[$type])) {
178: $params = array_merge(
179: $this->_getListQueryParameters($list),
180: $params
181: );
182: $list->registerQuery(
183: $type,
184: $this->_createListQuery(
185: $this->_class_map[$type], $list, $params
186: )
187: );
188: } else {
189: throw new Horde_Kolab_Storage_Exception(
190: sprintf('Query type %s not supported!', $type)
191: );
192: }
193: }
194:
195: /**
196: * Fetch any additional parameters required when creating list queries.
197: *
198: * @param Horde_Kolab_Storage_List $list The list.
199: *
200: * @return array The parameters for list queries.
201: */
202: abstract protected function _getListQueryParameters(
203: Horde_Kolab_Storage_List $list
204: );
205:
206: /**
207: * Create the specified list query type.
208: *
209: * @param string $name The query name.
210: * @param Horde_Kolab_Storage_List $list The list that should be queried.
211: * @param array $params Additional parameters provided
212: * to the query constructor.
213: *
214: * @return Horde_Kolab_Storage_Query A query handler.
215: *
216: * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported.
217: */
218: private function _createListQuery($name, Horde_Kolab_Storage_List $list, $params = array())
219: {
220: return $this->_createQuery($name, $list, $params);
221: }
222:
223: /**
224: * Add the set of data queries.
225: *
226: * @since Horde_Kolab_Storage 1.1.0
227: *
228: * @param Horde_Kolab_Storage_Data $data The data.
229: * @param array $params Additional query parameters.
230: *
231: * @return NULL
232: */
233: public function addDataQuerySet(Horde_Kolab_Storage_Data $data, $params = array())
234: {
235: foreach ($this->_data_queries as $query => $type) {
236: if ($type === true || $type == $data->getType()) {
237: $this->_addDataQuery($data, $query, $params);
238: }
239: }
240: }
241:
242: /**
243: * Add a data query.
244: *
245: * @param Horde_Kolab_Storage_Data $data The data.
246: * @param string $type The query type.
247: * @param array $params Additional query parameters.
248: *
249: * @return NULL
250: */
251: private function _addDataQuery(Horde_Kolab_Storage_Data $data, $type, $params = array())
252: {
253: if (isset($this->_class_map[$type])) {
254: $params = array_merge(
255: $this->_getDataQueryParameters($data),
256: $params
257: );
258: $data->registerQuery(
259: $type,
260: $this->_createDataQuery(
261: $this->_class_map[$type], $data, $params
262: )
263: );
264: } else {
265: throw new Horde_Kolab_Storage_Exception(
266: sprintf('Query type %s not supported!', $type)
267: );
268: }
269: }
270:
271: /**
272: * Fetch any additional parameters required when creating data queries.
273: *
274: * @param Horde_Kolab_Storage_Data $data The data.
275: *
276: * @return array The parameters for data queries.
277: */
278: abstract protected function _getDataQueryParameters(
279: Horde_Kolab_Storage_Data $data
280: );
281:
282: /**
283: * Create the specified data query type.
284: *
285: * @param string $name The query name.
286: * @param Horde_Kolab_Storage_Data $data The data that should be queried.
287: * @param array $params Additional parameters provided
288: * to the query constructor.
289: *
290: * @return Horde_Kolab_Storage_Query A query handler.
291: *
292: * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported.
293: */
294: private function _createDataQuery($name, Horde_Kolab_Storage_Data $data, $params = array())
295: {
296: return $this->_createQuery($name, $data, $params);
297: }
298:
299: /**
300: * Create the specified query type.
301: *
302: * @param string $name The query name.
303: * @param mixed $data The data that should be queried.
304: * @param array $params Additional parameters provided
305: * to the query constructor.
306: *
307: * @return Horde_Kolab_Storage_Query A query handler.
308: *
309: * @throws Horde_Kolab_Storage_Exception In case the requested query is not supported.
310: */
311: private function _createQuery($name, $data, $params = array())
312: {
313: if (class_exists($name)) {
314: $constructor_params = array_merge(
315: array('factory' => $this->_factory), $params
316: );
317: $query = new $name($data, $constructor_params);
318: } else {
319: throw new Horde_Kolab_Storage_Exception(sprintf('No such query "%s"!', $name));
320: }
321: return $query;
322: }
323:
324: }
325:
326: