1: <?php
2: /**
3: * Read-only Turba_Driver implementation for creating a Horde_Group based
4: * address book.
5: *
6: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file LICENSE for license information (ASL). If you did
9: * did not receive this file, see http://www.horde.org/licenses/apache.
10: *
11: * @author Michael J. Rubinsky <mrubinsk@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/apache ASL
14: * @package Turba
15: */
16: class Turba_Driver_Group extends Turba_Driver
17: {
18: /**
19: * Constructor function.
20: *
21: * @param array $params Array of parameters for this driver.
22: * Basically, just passes the group id.
23: *
24: */
25: public function __construct($name = '', $params)
26: {
27: $this->_gid = $params['gid'];
28: }
29:
30: /**
31: * Checks if the current user has the requested permissions on this
32: * source. This source is always read only.
33: *
34: * @param integer $perm The permission to check for.
35: *
36: * @return boolean True if the user has permission, otherwise false.
37: */
38: public function hasPermission($perm)
39: {
40: switch ($perm) {
41: case Horde_Perms::EDIT:
42: case Horde_Perms::DELETE:
43: return false;
44:
45: default:
46: return true;
47: }
48: }
49:
50: /**
51: * Searches the group list with the given criteria and returns a
52: * filtered list of results. If the criteria parameter is an empty array,
53: * all records will be returned.
54: *
55: * This method 'borrowed' from the favorites driver.
56: *
57: * @param array $criteria Array containing the search criteria.
58: * @param array $fields List of fields to return.
59: * @param array $blobFields A list of fields that contain binary data.
60: *
61: * @return array Hash containing the search results.
62: * @throws Turba_Exception
63: */
64: protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
65: {
66: $results = array();
67:
68: foreach ($this->_getAddressBook() as $key => $contact) {
69: $found = !isset($criteria['OR']);
70: foreach ($criteria as $op => $vals) {
71: if ($op == 'AND') {
72: foreach ($vals as $val) {
73: if (isset($contact[$val['field']])) {
74: switch ($val['op']) {
75: case 'LIKE':
76: if (stristr($contact[$val['field']], $val['test']) === false) {
77: continue 4;
78: }
79: $found = true;
80: break;
81: }
82: }
83: }
84: } elseif ($op == 'OR') {
85: foreach ($vals as $val) {
86: if (isset($contact[$val['field']])) {
87: switch ($val['op']) {
88: case 'LIKE':
89: if (empty($val['test']) ||
90: stristr($contact[$val['field']], $val['test']) !== false) {
91: $found = true;
92: break 3;
93: }
94: }
95: }
96: }
97: }
98: }
99: if ($found) {
100: $results[$key] = $contact;
101: }
102: }
103:
104: return $count_only ? count($results) : $results;
105: }
106:
107: /**
108: * Reads the given data from the address book and returns the results.
109: *
110: * @param string $key The primary key field to use.
111: * @param mixed $ids The ids of the contacts to load.
112: * @param string $owner Only return contacts owned by this user.
113: * @param array $fields List of fields to return.
114: * @param array $blobFields Array of fields containing binary data.
115: *
116: * @return array Hash containing the search results.
117: * @throws Turba_Exception
118: */
119: protected function _read($key, $ids, $owner, array $fields,
120: array $blobFields = array())
121: {
122: $book = $this->_getAddressBook();
123: $results = array();
124: if (!is_array($ids)) {
125: $ids = array($ids);
126: }
127: foreach ($ids as $id) {
128: if (isset($book[$id])) {
129: $results[] = $book[$id];
130: }
131: }
132:
133: return $results;
134: }
135:
136: /**
137: * TODO
138: */
139: protected function _getAddressBook()
140: {
141: $groups = $GLOBALS['injector']->getInstance('Horde_Group');
142: $members = $groups->listUsers($this->_gid);
143: $addressbook = array();
144: foreach ($members as $member) {
145: $identity = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($member);
146: $name = $identity->getValue('fullname');
147: $email = $identity->getValue('from_addr');
148: // We use the email as the key since we could have multiple users
149: // with the same fullname, so no email = no entry in address book.
150: if (!empty($email)) {
151: $addressbook[$email] = array(
152: 'name' => ((!empty($name) ? $name : $member)),
153: 'email' => $identity->getValue('from_addr')
154: );
155: }
156: }
157:
158: return $addressbook;
159: }
160:
161: }
162: