1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: class Whups_Query_Manager
17: {
18: 19: 20: 21: 22:
23: protected $_shareManager;
24:
25: 26: 27: 28: 29:
30: public function __construct()
31: {
32: $this->_shareManager =
33: $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43:
44: public function getQuery($queryId)
45: {
46: try {
47: $share = $this->_shareManager->getShareById($queryId);
48: } catch (Horde_Exception_NotFound $e) {
49: throw new Whups_Exception($e);
50: }
51: return $this->_getQuery($share);
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61:
62: public function getQueryBySlug($slug)
63: {
64: try {
65: $shares = $this->_shareManager->listShares(
66: $GLOBALS['registry']->getAuth(),
67: array('perm' => Horde_Perms::READ,
68: 'attributes' => array('slug' => $slug)));
69: } catch (Horde_Share_Exception $e) {
70: throw new Whups_Exception($e);
71: }
72: if (!count($shares)) {
73: return;
74: }
75:
76: return $this->_getQuery(reset($shares));
77: }
78:
79: 80: 81: 82: 83: 84: 85:
86: protected function _getQuery(Horde_Share_Object $share)
87: {
88: $queryDetails = $GLOBALS['whups_driver']->getQuery($share->getId());
89: $queryDetails['query_id'] = $share->getId();
90: $queryDetails['query_name'] = $share->get('name');
91: $queryDetails['query_slug'] = $share->get('slug');
92:
93: return new Whups_Query($this, $queryDetails);
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
106: public function hasPermission($queryId, $userid, $permission, $creator = null)
107: {
108: try {
109: $share = $this->_shareManager->getShareById($queryId);
110: } catch (Horde_Exception_NotFound $e) {
111:
112: return true;
113: }
114: return $share->hasPermission($userid, $permission, $creator);
115: }
116:
117: 118: 119:
120: public function listQueries($user, $return_slugs = false)
121: {
122: try {
123: $shares = $this->_shareManager->listShares($user);
124: } catch (Horde_Share_Exception $e) {
125: throw new Whups_Exception($e);
126: }
127:
128: $queries = array();
129: foreach ($shares as $share) {
130: $queries[$share->getId()] = $return_slugs
131: ? array('name' => $share->get('name'),
132: 'slug' => $share->get('slug'))
133: : $share->get('name');
134: }
135:
136: return $queries;
137: }
138:
139: 140:
141: public function newQuery()
142: {
143: return new Whups_Query($this);
144: }
145:
146: 147: 148: 149:
150: public function save(Whups_Query $query)
151: {
152: if ($query->id) {
153:
154:
155: try {
156: $share = $this->_shareManager->getShareById($query->id);
157: } catch (Horde_Exception_NotFound $e) {
158:
159:
160: throw new Whups_Exception($e);
161: }
162: if ($share->get('name') != $query->name ||
163: $share->get('slug') != $query->slug) {
164: $share->set('name', $query->name);
165: $share->set('slug', $query->slug);
166: $share->save();
167: }
168: } else {
169:
170: $share = $this->_shareManager->newShare($GLOBALS['registry']->getAuth(), (string)new Horde_Support_Uuid(), $query->name);
171: $share->set('slug', $query->slug);
172: try {
173: $this->_shareManager->addShare($share);
174: } catch (Horde_Share_Exception $e) {
175: throw new Whups_Exception($e);
176: }
177: $query->id = $share->getId();
178: }
179:
180:
181: $GLOBALS['whups_driver']->saveQuery($query);
182: }
183:
184: 185: 186:
187: public function delete(Whups_Query $query)
188: {
189: if (!$query->id) {
190:
191: return;
192: }
193:
194: try {
195: $share = $this->_shareManager->getShareById($query->id);
196: $this->_shareManager->removeShare($share);
197: } catch (Exception $e) {
198: throw new Whups_Exception($e);
199: }
200: $GLOBALS['whups_driver']->deleteQuery($query->id);
201: }
202:
203: }