1: <?php
2: /**
3: * The basis for Kolab storage access.
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 basis for Kolab storage access.
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: abstract class Horde_Kolab_Storage_Base
29: implements Horde_Kolab_Storage
30: {
31: /**
32: * The master Kolab storage system.
33: *
34: * @var Horde_Kolab_Storage_Driver
35: */
36: private $_master;
37:
38: /**
39: * The query handler.
40: *
41: * @var Horde_Kolab_Storage_QuerySet
42: */
43: private $_query_set;
44:
45: /**
46: * The factory for generating additional resources.
47: *
48: * @var Horde_Kolab_Storage_Factory
49: */
50: private $_factory;
51:
52: /**
53: * Additional parameters.
54: *
55: * @var array
56: */
57: private $_params;
58:
59: /**
60: * List instances.
61: *
62: * @var array
63: */
64: private $_lists;
65:
66: /**
67: * Data instances.
68: *
69: * @var array
70: */
71: private $_data;
72:
73: /**
74: * Constructor.
75: *
76: * @param Horde_Kolab_Storage_Driver $master The primary connection
77: * driver.
78: * @param Horde_Kolab_Storage_QuerySet $query_set The query handler.
79: * @param Horde_Kolab_Storage_Factory $factory The factory.
80: * @param array $params Additional parameters.
81: * <pre>
82: * - system [array]: A list of credentials for system users.
83: * Each entry has the system user type as key and
84: * another array as value. The latter needs to provide
85: * both 'username' and 'password' for the system user.
86: * A key of '' indicates the default system user.
87: * </pre>
88: */
89: public function __construct(Horde_Kolab_Storage_Driver $master,
90: Horde_Kolab_Storage_QuerySet $query_set,
91: Horde_Kolab_Storage_Factory $factory,
92: array $params = array())
93: {
94: $this->_master = $master;
95: $this->_query_set = $query_set;
96: $this->_factory = $factory;
97: $this->_params = $params;
98: }
99:
100: /**
101: * Get a folder list object for a "system" user.
102: *
103: * @param string $type The type of system user.
104: *
105: * @return Horde_Kolab_Storage_List The handler for the list of folders
106: * present in the Kolab backend.
107: */
108: public function getSystemList($type)
109: {
110: if (!isset($this->_params['system'][$type])) {
111: if (!isset($this->_params['system'][''])) {
112: throw new Horde_Kolab_Storage_Exception(
113: 'No system users are available!'
114: );
115: } else {
116: $params = $this->_params['system'][''];
117: }
118: } else {
119: $params = $this->_params['system'][$type];
120: }
121:
122: return $this->getList(
123: $this->_factory->createDriver(array('params' => $params))
124: );
125: }
126:
127: /**
128: * Get the folder list object.
129: *
130: * @params Horde_Kolab_Storage_Driver $driver Optional driver as backend
131: * for the list.
132: *
133: * @return Horde_Kolab_Storage_List The handler for the list of folders
134: * present in the Kolab backend.
135: */
136: public function getList(Horde_Kolab_Storage_Driver $driver = null)
137: {
138: if ($driver === null) {
139: $driver = $this->_master;
140: }
141: if (!isset($this->_lists[$driver->getId()])) {
142: $list = $this->_createList($driver, $this->_factory);
143: if (isset($this->_params['logger'])) {
144: $list = new Horde_Kolab_Storage_List_Decorator_Log(
145: $list, $this->_params['logger']
146: );
147: }
148: $this->_query_set->addListQuerySet($list);
149: $this->_lists[$driver->getId()] = $list;
150: }
151: return $this->_lists[$driver->getId()];
152: }
153:
154: /**
155: * Create the folder list object.
156: *
157: * @param Horde_Kolab_Storage_Driver $master The primary connection driver.
158: * @param Horde_Kolab_Storage_Factory $factory The factory.
159: *
160: * @return Horde_Kolab_Storage_List The handler for the list of folders
161: * present in the Kolab backend.
162: */
163: abstract protected function _createList(
164: Horde_Kolab_Storage_Driver $master,
165: Horde_Kolab_Storage_Factory $factory
166: );
167:
168: /**
169: * Get a Folder object.
170: *
171: * @param string $folder The folder name.
172: *
173: * @return Horde_Kolab_Storage_Folder The Kolab folder object.
174: */
175: public function getFolder($folder)
176: {
177: return $this->getList()->getFolder($folder);
178: }
179:
180: /**
181: * Return a data handler for accessing data in the specified folder.
182: *
183: * @param mixed $folder The name of the folder or an instance
184: * representing the folder.
185: * @param string $object_type The type of data we want to access in the
186: * folder.
187: * @param int $data_version Format version of the object data.
188: *
189: * @return Horde_Kolab_Data The data object.
190: */
191: public function getData($folder, $object_type = null, $data_version = 1)
192: {
193: if ($folder instanceOf Horde_Kolab_Storage_Folder) {
194: $folder_key = $folder->getPath();
195: } else {
196: $folder_key = $folder;
197: }
198: $key = join(
199: '@',
200: array(
201: $data_version,
202: $object_type,
203: $folder_key,
204: $this->_master->getId()
205: )
206: );
207: if (!isset($this->_data[$key])) {
208: if (!$folder instanceOf Horde_Kolab_Storage_Folder) {
209: $folder = $this->getList()->getFolder($folder);
210: }
211: $this->_data[$key] = $this->_createData(
212: $folder,
213: $this->_master,
214: $this->_factory,
215: $object_type,
216: $data_version
217: );
218: if (isset($this->_params['logger'])) {
219: $this->_data[$key] = new Horde_Kolab_Storage_Data_Decorator_Log(
220: $this->_data[$key], $this->_params['logger']
221: );
222: }
223: $this->_query_set->addDataQuerySet($this->_data[$key]);
224: }
225: return $this->_data[$key];
226: }
227:
228: /**
229: * Return a data handler for accessing data in the specified folder.
230: *
231: * @param mixed $folder The name of the folder or
232: * an instance representing
233: * the folder.
234: * @param Horde_Kolab_Storage_Driver $master The primary connection
235: * driver.
236: * @param Horde_Kolab_Storage_Factory $factory The factory.
237: * @param string $object_type The type of data we want
238: * to access in the folder.
239: * @param int $data_version Format version of the
240: * object data.
241: *
242: * @return Horde_Kolab_Data The data object.
243: */
244: abstract protected function _createData(
245: $folder,
246: Horde_Kolab_Storage_Driver $master,
247: Horde_Kolab_Storage_Factory $factory,
248: $object_type = null,
249: $data_version = 1
250: );
251: }
252:
253: