1: <?php
2: /**
3: * The Turba_Driver:: class provides a common abstracted interface to the
4: * various directory search drivers. It includes functions for searching,
5: * adding, removing, and modifying directory entries.
6: *
7: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
8: *
9: * See the enclosed file LICENSE for license information (ASL). If you did
10: * did not receive this file, see http://www.horde.org/licenses/apache.
11: *
12: * @author Chuck Hagenbuch <chuck@horde.org>
13: * @author Jon Parise <jon@csh.rit.edu>
14: * @author Michael J. Rubinsky <mrubinsk@horde.org>
15: * @category Horde
16: * @license http://www.horde.org/licenses/apache ASL
17: * @package Turba
18: */
19: class Turba_Driver_Share extends Turba_Driver
20: {
21: /**
22: * Horde_Share object for this source.
23: *
24: * @var Horde_Share
25: */
26: protected $_share;
27:
28: /**
29: * Underlying driver object for this source.
30: *
31: * @var Turba_Driver
32: */
33: protected $_driver;
34:
35: /**
36: * Constructor
37: *
38: * @param string $name The source name
39: * @param array $params The parameter array describing the source
40: *
41: * @return Turba_Driver
42: */
43: public function __construct($name = '', array $params = array())
44: {
45: parent::__construct($name, $params);
46: $this->_share = $this->_params['config']['params']['share'];
47: $this->_driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($this->_params['config']);
48: $this->_driver->setContactOwner($this->_getContactOwner());
49: $this->_driver->setSourceName($name);
50: }
51:
52: /**
53: * Checks if this backend has a certain capability.
54: *
55: * @param string $capability The capability to check for.
56: *
57: * @return boolean Supported or not.
58: */
59: public function hasCapability($capability)
60: {
61: return $this->_driver->hasCapability($capability);
62: }
63:
64: /**
65: * Checks if the current user has the requested permissions on this
66: * address book.
67: *
68: * @param integer $perm The permission to check for.
69: *
70: * @return boolean True if the user has permission, otherwise false.
71: */
72: public function hasPermission($perm)
73: {
74: return $this->_share->hasPermission($GLOBALS['registry']->getAuth(), $perm);
75: }
76:
77: /**
78: * Return the name of this address book.
79: *
80: * @string Address book name
81: */
82: public function getName()
83: {
84: $share_parts = explode(':', $this->_share->getName());
85: return array_pop($share_parts);
86: }
87:
88: /**
89: * Return the owner to use when searching or creating contacts in
90: * this address book.
91: *
92: * @return string TODO
93: * @throws Turba_Exception
94: */
95: protected function _getContactOwner()
96: {
97: $params = @unserialize($this->_share->get('params'));
98: if (!empty($params['name'])) {
99: return $params['name'];
100: }
101:
102: throw new Turba_Exception(_("Unable to find contact owner."));
103: }
104:
105: /**
106: * Searches the address book with the given criteria and returns a
107: * filtered list of results. If the criteria parameter is an empty array,
108: * all records will be returned.
109: *
110: * @param array $criteria Array containing the search criteria.
111: * @param array $fields List of fields to return.
112: * @param array $blobFields Array of fields containing binary data.
113: *
114: * @return array Hash containing the search results.
115: * @throws Turba_Exception
116: */
117: protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
118: {
119: return $this->_driver->_search($criteria, $fields, $blobFields, $count_only);
120: }
121:
122: /**
123: * Searches the current address book for duplicate entries.
124: *
125: * Duplicates are determined by comparing email and name or last name and
126: * first name values.
127: *
128: * @return array A hash with the following format:
129: * <code>
130: * array('name' => array('John Doe' => Turba_List, ...), ...)
131: * </code>
132: * @throws Turba_Exception
133: */
134: public function searchDuplicates()
135: {
136: return $this->_driver->searchDuplicates();
137: }
138:
139: /**
140: * Reads the given data from the address book and returns the results.
141: *
142: * @param string $key The primary key field to use.
143: * @param mixed $ids The ids of the contacts to load.
144: * @param string $owner Only return contacts owned by this user.
145: * @param array $fields List of fields to return.
146: * @param array $blobFields Array of fields containing binary data.
147: *
148: * @return array Hash containing the search results.
149: * @throws Turba_Exception
150: */
151: protected function _read($key, $ids, $owner, array $fields,
152: array $blobFields = array())
153: {
154: return $this->_driver->_read($key, $ids, $owner, $fields, $blobFields);
155: }
156:
157: /**
158: * Adds the specified object to the SQL database.
159: *
160: * @param array $attributes
161: * @param array $blob_fields
162: */
163: protected function _add(array $attributes, array $blob_fields = array())
164: {
165: $this->_driver->_add($attributes, $blob_fields);
166: }
167:
168: /**
169: * TODO
170: */
171: protected function _canAdd()
172: {
173: return $this->_driver->canAdd();
174: }
175:
176: /**
177: * Deletes the specified object from the SQL database.
178: *
179: * TODO
180: */
181: protected function _delete($object_key, $object_id)
182: {
183: $this->_driver->_delete($object_key, $object_id);
184: }
185:
186: /**
187: * Deletes all contacts from a specific address book.
188: *
189: * @param string $sourceName The source to delete all contacts from.
190: *
191: * @throws Turba_Exception
192: */
193: protected function _deleteAll($sourceName = null)
194: {
195: if (is_null($sourceName)) {
196: $sourceName = $this->getContactOwner();
197: }
198: $this->_driver->_deleteAll($sourceName);
199: }
200:
201: /**
202: * Saves the specified object in the SQL database.
203: *
204: * @param Turba_Object $object The object to save
205: *
206: * @return string The object id, possibly updated.
207: * @throws Turba_Exception
208: */
209: protected function _save(Turba_Object $object)
210: {
211: return $this->_driver->_save($object);
212: }
213:
214: /**
215: * Remove all data for a specific user.
216: *
217: * @param string $user The user to remove all data for.
218: */
219: public function removeUserData($user)
220: {
221: // Make sure we are being called by an admin.
222: if (!$GLOBALS['registry']->isAdmin()) {
223: throw new Horde_Exception_PermissionDenied(_("Permission denied"));
224: }
225: $this->_deleteAll();
226: $GLOBALS['turba_shares']->removeShare($this->_share);
227: unset($this->_share);
228: }
229:
230: /**
231: * @param array $attributes
232: */
233: protected function _makeKey(array $attributes)
234: {
235: return $this->_driver->_makeKey($attributes);
236: }
237:
238: /**
239: * @param Horde_Date $start The starting date.
240: * @param Horde_Date $end The ending date.
241: * @param string $field The address book field containing the
242: * timeObject information (birthday,
243: * anniversary).
244: *
245: * @return array The list of timeobjects
246: */
247: public function getTimeObjectTurbaList(Horde_Date $start, Horde_Date $end, $field)
248: {
249: return $this->_driver->getTimeObjectTurbaList($start, $end, $field);
250: }
251:
252: }
253: