1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class Horde_Group_Sql extends Horde_Group_Base
17: {
18: 19: 20: 21: 22:
23: protected $_db;
24:
25: 26: 27:
28: public function __construct($params)
29: {
30: if (!isset($params['db'])) {
31: throw new Horde_Group_Exception('The \'db\' parameter is missing.');
32: }
33: $this->_db = $params['db'];
34: }
35:
36: 37: 38: 39: 40:
41: public function readOnly()
42: {
43: return false;
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53: 54:
55: public function create($name, $email = null)
56: {
57: try {
58: return $this->_db->insert(
59: 'INSERT INTO horde_groups (group_name, group_email, group_parents) VALUES (?, ?, ?)',
60: array($name, $email, ''));
61: } catch (Horde_Db_Exception $e) {
62: throw new Horde_Group_Exception($e);
63: }
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73:
74: public function rename($gid, $name)
75: {
76: try {
77: return $this->_db->update(
78: 'UPDATE horde_groups SET group_name = ? WHERE group_uid = ?',
79: array($name, $gid));
80: } catch (Horde_Db_Exception $e) {
81: throw new Horde_Group_Exception($e);
82: }
83: }
84:
85: 86: 87: 88: 89: 90: 91:
92: public function remove($gid)
93: {
94: try {
95: $this->_db->beginDbTransaction();
96: $this->_db->delete(
97: 'DELETE FROM horde_groups_members WHERE group_uid = ?',
98: array($gid));
99: $this->_db->delete(
100: 'DELETE FROM horde_groups WHERE group_uid = ?',
101: array($gid));
102: $this->_db->commitDbTransaction();
103: } catch (Horde_Db_Exception $e) {
104: throw new Horde_Group_Exception($e);
105: }
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115:
116: public function exists($gid)
117: {
118: try {
119: return (bool)$this->_db->selectValue(
120: 'SELECT 1 FROM horde_groups WHERE group_uid = ?',
121: array($gid));
122: } catch (Horde_Db_Exception $e) {
123: throw new Horde_Group_Exception($e);
124: }
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: public function getName($gid)
136: {
137: try {
138: return $this->_db->selectValue(
139: 'SELECT group_name FROM horde_groups WHERE group_uid = ?',
140: array($gid));
141: } catch (Horde_Db_Exception $e) {
142: throw new Horde_Group_Exception($e);
143: }
144: }
145:
146: 147: 148: 149: 150: 151: 152: 153: 154:
155: public function getData($gid)
156: {
157: try {
158: $result = $this->_db->selectOne(
159: 'SELECT * FROM horde_groups WHERE group_uid = ?',
160: array($gid));
161: if (!$result) {
162: throw new Horde_Exception_NotFound('Group with the ID ' . $gid . ' not found');
163: }
164: } catch (Horde_Db_Exception $e) {
165: throw new Horde_Group_Exception($e);
166: }
167: $data = array();
168: foreach ($result as $attribute => $value) {
169: $data[preg_replace('/^group_/', '', $attribute)] = $value;
170: }
171: return $data;
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
185: public function setData($gid, $attribute, $value = null)
186: {
187: $attributes = is_array($attribute)
188: ? $attribute
189: : array($attribute => $value);
190: $updates = array();
191: foreach ($attributes as $attribute => $value) {
192: $updates[] = $this->_db->quoteColumnName('group_' . $attribute)
193: . ' = ' . $this->_db->quote($value);
194: }
195: try {
196: $this->_db->execute('UPDATE horde_groups SET ' . implode(', ', $updates) . ' WHERE group_uid = ?',
197: array($gid));
198: } catch (Horde_Db_Exception $e) {
199: throw new Horde_Group_Exception($e);
200: }
201: }
202:
203: 204: 205: 206: 207: 208: 209: 210: 211:
212: public function listAll($member = null)
213: {
214: if (!is_null($member)) {
215: return $this->listGroups($member);
216: }
217:
218: try {
219: return $this->_db->selectAssoc('SELECT group_uid, group_name FROM horde_groups');
220: } catch (Horde_Db_Exception $e) {
221: throw new Horde_Group_Exception($e);
222: }
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232:
233: public function listUsers($gid)
234: {
235: try {
236: return $this->_db->selectValues(
237: 'SELECT user_uid FROM horde_groups_members WHERE group_uid = ? ORDER BY user_uid ASC',
238: array($gid));
239: } catch (Horde_Db_Exception $e) {
240: throw new Horde_Group_Exception($e);
241: }
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251:
252: public function listGroups($user)
253: {
254: try {
255: return $this->_db->selectAssoc(
256: 'SELECT g.group_uid AS group_uid, g.group_name AS group_name FROM horde_groups g, horde_groups_members m WHERE m.user_uid = ? AND g.group_uid = m.group_uid ORDER BY g.group_name',
257: array($user));
258: } catch (Horde_Db_Exception $e) {
259: throw new Horde_Group_Exception($e);
260: }
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270:
271: public function addUser($gid, $user)
272: {
273: try {
274: $this->_db->insert(
275: 'INSERT INTO horde_groups_members (group_uid, user_uid) VALUES (?, ?)', array($gid, $user));
276: } catch (Horde_Db_Exception $e) {
277: throw new Horde_Group_Exception($e);
278: }
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288:
289: public function removeUser($gid, $user)
290: {
291: try {
292: $this->_db->delete(
293: 'DELETE FROM horde_groups_members WHERE group_uid = ? AND user_uid = ?',
294: array($gid, $user));
295: } catch (Horde_Db_Exception $e) {
296: throw new Horde_Group_Exception($e);
297: }
298: }
299:
300: 301: 302: 303: 304: 305: 306: 307: 308:
309: public function search($name)
310: {
311: try {
312: return $this->_db->selectAssoc(
313: 'SELECT group_uid, group_name FROM horde_groups WHERE group_name LIKE ?',
314: array('%' . $name . '%'));
315: } catch (Horde_Db_Exception $e) {
316: throw new Horde_Group_Exception($e);
317: }
318: }
319: }
320: