1: <?php
2:
3: require_once dirname(__FILE__) . '/sql.php';
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Folks_Friends_shared extends Folks_Friends_sql {
18:
19: 20: 21: 22: 23:
24: private $_shares;
25:
26: 27: 28: 29: 30: 31:
32: protected $_capabilities = array('groups_add' => true);
33:
34: 35: 36: 37: 38: 39: 40:
41: public function getGroupOwner($group)
42: {
43: $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
44:
45: try {
46: $share = $GLOBALS['folks_shares']->getShareById($group);
47: } catch (Horde_Share_Exception $e) {
48: }
49:
50: return $share->get('owner');
51: }
52:
53: 54: 55:
56: protected function _getGroups()
57: {
58: $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
59: $groups = $GLOBALS['folks_shares']->listShares($this->_user, array('perm' => Horde_Perms::READ));
60:
61: $list = array();
62: foreach ($groups as $group) {
63: $list[$group->getId()] = $group->get('name');
64: }
65:
66: return $list;
67: }
68:
69: 70: 71: 72: 73:
74: public function renameGroup($group, $name)
75: {
76: if (empty($name)) {
77: return PEAR::raiseError(_("A group names cannot be empty"));
78: }
79:
80: $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
81: $share = $GLOBALS['folks_shares']->getShareById($group);
82:
83:
84: if (!$GLOBALS['registry']->getAuth() ||
85: ($GLOBALS['registry']->getAuth() != $share->get('owner') &&
86: !$GLOBALS['registry']->isAdmin(array('permission' => 'folks:admin')))) {
87: return PEAR::raiseError("You can rename only your own groups.");
88: }
89:
90: $share->set('name', $name);
91: $result = $share->save();
92: if ($result instanceof PEAR_Error) {
93: return $result;
94: }
95:
96: $this->_cache->expire('folksGroups' . $this->_user);
97:
98: return true;
99: }
100:
101: 102: 103: 104: 105:
106: public function removeGroup($group)
107: {
108: $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
109:
110: $share = $GLOBALS['folks_shares']->getShareById($group);
111: if ($share instanceof PEAR_Error) {
112: return $share;
113: }
114:
115:
116: if (!$GLOBALS['registry']->getAuth() ||
117: ($GLOBALS['registry']->getAuth() != $share->get('owner') &&
118: !$GLOBALS['registry']->isAdmin(array('permission' => 'folks:admin')))) {
119: return PEAR::raiseError("You can delete only your own groups.");
120: }
121:
122: $query = 'DELETE FROM ' . $this->_params['friends']
123: . ' WHERE user_uid = ' . $share->getShareOb()->getWriteDb()->quote($this->_user)
124: . ' AND group_id = ' . $share->getShareOb()->getWriteDb()->quote($share->getId());
125:
126: $result = $share->getShareOb()->getWriteDb()->exec($query);
127: if ($result instanceof PEAR_Error) {
128: return $result;
129: }
130:
131: $result = $GLOBALS['folks_shares']->removeShare($share);
132: if ($result instanceof PEAR_Error) {
133: return $result;
134: }
135:
136: $this->_cache->expire('folksGroups' . $this->_user);
137: $this->_cache->expire('folksFriends' . $this->_user . $group);
138:
139: return true;
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function addGroup($name)
149: {
150: if (empty($name)) {
151: return PEAR::raiseError(_("A group names cannot be empty"));
152: }
153:
154: $groups = $this->getGroups();
155: if (in_array($name, $groups)) {
156: return PEAR::raiseError(sprintf(_("You already have a group named \"%s\"."), $name));
157: }
158:
159: $GLOBALS['folks_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
160:
161: $share = $GLOBALS['folks_shares']->newShare(strval(new Horde_Support_Uuid()), $name);
162: $result = $GLOBALS['folks_shares']->addShare($share);
163:
164: return $share->getId();
165: }
166: }
167: