1 | <?php |
---|
2 | /** |
---|
3 | * Officeshots.org - Test your office documents in different applications |
---|
4 | * Copyright (C) 2009 Stichting Lone Wolves |
---|
5 | * Written by Sander Marechal <s.marechal@jejik.com> |
---|
6 | * |
---|
7 | * This program is free software: you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU Affero General Public License as |
---|
9 | * published by the Free Software Foundation, either version 3 of the |
---|
10 | * License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU Affero General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU Affero General Public License |
---|
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | */ |
---|
20 | |
---|
21 | /** |
---|
22 | * The galleries controller |
---|
23 | */ |
---|
24 | class GalleriesController extends AppController |
---|
25 | { |
---|
26 | /** @var array The components this controller uses */ |
---|
27 | public $components = array('AuthCert'); |
---|
28 | |
---|
29 | /** The controller helpers */ |
---|
30 | public $helpers = array('Html', 'Form', 'Javascript', 'RequestModel', 'JobModel', 'ValidatorModel'); |
---|
31 | |
---|
32 | /** The models this controller uses */ |
---|
33 | public $uses = array('Gallery', 'Request', 'User'); |
---|
34 | |
---|
35 | /** |
---|
36 | * Set the auth permissions for this controller |
---|
37 | * @return void |
---|
38 | */ |
---|
39 | public function beforeFilter() |
---|
40 | { |
---|
41 | parent::beforeFilter(); |
---|
42 | $this->AuthCert->allow('index', 'view'); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Check if the current user has write access to the gallery |
---|
47 | * @param string $slug The unique gallery slug |
---|
48 | */ |
---|
49 | private function _checkAccess($slug) |
---|
50 | { |
---|
51 | $gallery = $this->Gallery->find('first', array( |
---|
52 | 'conditions' => array('Gallery.slug' => $slug), |
---|
53 | 'recursive' => -1, |
---|
54 | )); |
---|
55 | |
---|
56 | // Gallery does not exist |
---|
57 | if (empty($gallery)) { |
---|
58 | return false; |
---|
59 | } |
---|
60 | |
---|
61 | // User owns the gallery |
---|
62 | if ($gallery['Gallery']['user_id'] == $this->AuthCert->user('id')) { |
---|
63 | return true; |
---|
64 | } |
---|
65 | |
---|
66 | // Gallery is assigned to a group the user is a member of |
---|
67 | if (!empty($gallery['Gallery']['group_id'])) { |
---|
68 | return $this->User->Group->has_member($this->AuthCert->user('id'), $gallery['Gallery']['group_id']); |
---|
69 | } |
---|
70 | |
---|
71 | return false; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Sort an array of Galleries threaded, like find('threaded') would do |
---|
76 | */ |
---|
77 | private function _sortThreaded($results) |
---|
78 | { |
---|
79 | $return = $idMap = array(); |
---|
80 | $ids = Set::extract($results, '{n}.Gallery.id'); |
---|
81 | |
---|
82 | foreach ($results as $result) { |
---|
83 | $result['children'] = array(); |
---|
84 | $id = $result['Gallery']['id']; |
---|
85 | $parentId = $result['Gallery']['parent_id']; |
---|
86 | if (isset($idMap[$id]['children'])) { |
---|
87 | $idMap[$id] = array_merge($result, (array)$idMap[$id]); |
---|
88 | } else { |
---|
89 | $idMap[$id] = array_merge($result, array('children' => array())); |
---|
90 | } |
---|
91 | if (!$parentId || !in_array($parentId, $ids)) { |
---|
92 | $return[] =& $idMap[$id]; |
---|
93 | } else { |
---|
94 | $idMap[$parentId]['children'][] =& $idMap[$id]; |
---|
95 | } |
---|
96 | } |
---|
97 | if (count($return) > 1) { |
---|
98 | $ids = array_unique(Set::extract('/Gallery/parent_id', $return)); |
---|
99 | if (count($ids) > 1) { |
---|
100 | $root = $return[0]['Gallery']['parent_id']; |
---|
101 | foreach ($return as $key => $value) { |
---|
102 | if ($value['Gallery']['parent_id'] != $root) { |
---|
103 | unset($return[$key]); |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | return $return; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Show a list of all galleries |
---|
113 | */ |
---|
114 | public function index() |
---|
115 | { |
---|
116 | $this->Gallery->contain(array('User', 'Request')); |
---|
117 | $galleries = $this->paginate('Gallery', array('Gallery.parent_id' => null)); |
---|
118 | |
---|
119 | foreach ($galleries as &$gallery) { |
---|
120 | $gallery['Gallery']['num_documents'] = $this->Gallery->requestCount($gallery['Gallery']['id'], true); |
---|
121 | } |
---|
122 | |
---|
123 | $this->set('galleries', $galleries); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * View a gallery |
---|
128 | * @param string $slug The gallery slug |
---|
129 | */ |
---|
130 | public function view($slug = null) |
---|
131 | { |
---|
132 | if (!$slug) { |
---|
133 | $this->Session->setFlash(__('Invalid Gallery.', true)); |
---|
134 | $this->redirect(array('action'=>'index')); |
---|
135 | } |
---|
136 | |
---|
137 | // Get the gallery |
---|
138 | $gallery = $this->Gallery->find('first', array( |
---|
139 | 'conditions' => array('Gallery.slug' => $slug), |
---|
140 | 'contain' => array( |
---|
141 | 'Request', |
---|
142 | 'Request.Validator' => array('order' => 'Validator.name ASC'), |
---|
143 | 'Request.Job' => array('order' => array( |
---|
144 | 'Application.name ASC', |
---|
145 | 'Job.version ASC', |
---|
146 | 'Platform.name ASC', |
---|
147 | 'Format.name ASC', |
---|
148 | )), |
---|
149 | 'Request.Job.Application', |
---|
150 | 'Request.Job.Format', |
---|
151 | 'Request.Job.Platform', |
---|
152 | 'Request.Job.Result', |
---|
153 | 'Request.Job.Result.Validator', |
---|
154 | ), |
---|
155 | )); |
---|
156 | |
---|
157 | // Get the children of this gallery |
---|
158 | // TODO: Do this without recursion below Request because it generates ridiculous amounts of |
---|
159 | // duplicate queries that slow the system *a lot* |
---|
160 | $children = $this->Gallery->find('all', array( |
---|
161 | 'conditions' => array( |
---|
162 | 'Gallery.lft >' => $gallery['Gallery']['lft'], |
---|
163 | 'Gallery.rght <' => $gallery['Gallery']['rght'], |
---|
164 | ), |
---|
165 | 'contain' => array('Request'), |
---|
166 | )); |
---|
167 | |
---|
168 | foreach ($children as &$child) { |
---|
169 | foreach ($child['Request'] as &$request) { |
---|
170 | $request['Validator'] = $this->Request->Validator->find('all', array( |
---|
171 | 'conditions' => array('Validator.parent_id' => $request['id']), |
---|
172 | 'order' => 'Validator.name ASC', |
---|
173 | 'recursive' => -1, |
---|
174 | )); |
---|
175 | |
---|
176 | $request['Job'] = $this->Request->Job->find('all', array( |
---|
177 | 'conditions' => array('Job.request_id' => $request['id']), |
---|
178 | 'contain' => array( |
---|
179 | 'Application', |
---|
180 | 'Format', |
---|
181 | 'Platform', |
---|
182 | 'Result', |
---|
183 | 'Result.Validator' |
---|
184 | ), |
---|
185 | 'order' => array( |
---|
186 | 'Application.name ASC', |
---|
187 | 'Job.version ASC', |
---|
188 | 'Platform.name ASC', |
---|
189 | 'Format.name ASC', |
---|
190 | ), |
---|
191 | )); |
---|
192 | |
---|
193 | // Fix array layout to match a regular query layout |
---|
194 | foreach ($request['Job'] as &$job) { |
---|
195 | foreach ($job['Job'] as $attr => $value) { |
---|
196 | $job[$attr] = $value; |
---|
197 | } |
---|
198 | unset($job['Job']); |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | $gallery['children'] = $this->_sortThreaded($children); |
---|
204 | |
---|
205 | // Get the path to this gallery |
---|
206 | $path = $this->Gallery->getpath($gallery['Gallery']['id']); |
---|
207 | array_pop($path); |
---|
208 | |
---|
209 | // Check access |
---|
210 | $access = $this->_checkAccess($slug); |
---|
211 | $this->set(compact('gallery', 'path', 'access')); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Add a new gallery |
---|
216 | */ |
---|
217 | public function add() |
---|
218 | { |
---|
219 | if (!empty($this->data)) { |
---|
220 | $this->data['Gallery']['user_id'] = $this->AuthCert->user('id'); |
---|
221 | |
---|
222 | $this->Gallery->create(); |
---|
223 | if ($this->Gallery->save($this->data)) { |
---|
224 | $this->Session->setFlash(__('The Gallery has been saved', true)); |
---|
225 | $gallery = $this->Gallery->read('slug', $this->Gallery->id); |
---|
226 | $this->redirect(array('action' => 'view', $gallery['Gallery']['slug'])); |
---|
227 | } else { |
---|
228 | $this->Session->setFlash(__('The Gallery could not be saved. Please, try again.', true)); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | $groups = $this->User->find('first', array( |
---|
233 | 'contain' => array('Group'), |
---|
234 | 'conditions' => array('User.id' => $this->AuthCert->user('id')), |
---|
235 | )); |
---|
236 | |
---|
237 | $groups = array_combine( |
---|
238 | Set::extract('/Group/id', $groups), |
---|
239 | Set::extract('/Group/name', $groups) |
---|
240 | ); |
---|
241 | |
---|
242 | $this->set(compact('groups')); |
---|
243 | $this->render('edit'); |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * Edit a gallery |
---|
248 | * @param string $slug the unique gallery slug |
---|
249 | */ |
---|
250 | public function edit($slug = null) |
---|
251 | { |
---|
252 | if (!empty($this->data)) { |
---|
253 | $slug = $this->Gallery->read('slug', $this->data['Gallery']['id']); |
---|
254 | } |
---|
255 | |
---|
256 | if (!$slug && empty($this->data)) { |
---|
257 | $this->Session->setFlash(__('Invalid Gallery', true)); |
---|
258 | $this->redirect(array('action' => 'index')); |
---|
259 | } |
---|
260 | |
---|
261 | if (!$this->_checkAccess($slug)) { |
---|
262 | $this->Session->setFlash(__('You are not allowed to edit this gallery', true)); |
---|
263 | $this->redirect(array('action' => 'view', $slug)); |
---|
264 | } |
---|
265 | |
---|
266 | if (!empty($this->data)) { |
---|
267 | if ($this->Gallery->save($this->data)) { |
---|
268 | $this->Session->setFlash(__('The Gallery has been saved', true)); |
---|
269 | $this->redirect(array('action' => 'view', $this->data['Gallery']['slug'])); |
---|
270 | } else { |
---|
271 | $this->Session->setFlash(__('The Gallery could not be saved. Please, try again.', true)); |
---|
272 | } |
---|
273 | } else { |
---|
274 | $this->data = $this->Gallery->find('first', array( |
---|
275 | 'conditions' => array('Gallery.slug' => $slug), |
---|
276 | 'recursive' => -1, |
---|
277 | )); |
---|
278 | } |
---|
279 | |
---|
280 | $groups = $this->User->find('first', array( |
---|
281 | 'contain' => array('Group'), |
---|
282 | 'conditions' => array('User.id' => $this->AuthCert->user('id')), |
---|
283 | )); |
---|
284 | |
---|
285 | $groups = array_combine( |
---|
286 | Set::extract('/Group/id', $groups), |
---|
287 | Set::extract('/Group/name', $groups) |
---|
288 | ); |
---|
289 | |
---|
290 | $this->set(compact('groups')); |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * Delete a gallery |
---|
295 | * @param string $slug the unique gallery slug |
---|
296 | */ |
---|
297 | public function delete($slug = null) |
---|
298 | { |
---|
299 | if (!$slug) { |
---|
300 | $this->Session->setFlash(__('Invalid Gallery', true)); |
---|
301 | $this->redirect(array('action'=>'index')); |
---|
302 | } |
---|
303 | |
---|
304 | if (!$this->_checkAccess($slug)) { |
---|
305 | $this->Session->setFlash(__('You are not allowed to delete this gallery', true)); |
---|
306 | $this->redirect(array('action' => 'view', $slug)); |
---|
307 | } |
---|
308 | |
---|
309 | $gallery = $this->Gallery->find('first', array( |
---|
310 | 'recursive' => -1, |
---|
311 | 'conditions' => array('Gallery.slug' => $slug), |
---|
312 | )); |
---|
313 | |
---|
314 | if ($this->Gallery->del($gallery['Gallery']['id'])) { |
---|
315 | $this->Session->setFlash(__('Gallery deleted', true)); |
---|
316 | $this->redirect(array('action'=>'index')); |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * Add a document to the gallery |
---|
322 | * @param string $slug the unique gallery slug |
---|
323 | */ |
---|
324 | public function add_document($slug) |
---|
325 | { |
---|
326 | if (!$this->_checkAccess($slug)) { |
---|
327 | $this->Session->setFlash(__('You are not allowed to edit this gallery', true)); |
---|
328 | $this->redirect(array('action' => 'view', $slug)); |
---|
329 | } |
---|
330 | |
---|
331 | if (!empty($this->data)) { |
---|
332 | $gallery = $this->Gallery->find('first', array( |
---|
333 | 'conditions' => array('Gallery.slug' => $slug), |
---|
334 | 'recursive' => -1, |
---|
335 | )); |
---|
336 | |
---|
337 | if (!$this->Gallery->addRequest(array_shift($this->data['Gallery']['requests']), $gallery['Gallery']['id'])) { |
---|
338 | $this->Session->setFlash(__('The document could not be added.', true)); |
---|
339 | } |
---|
340 | |
---|
341 | $this->redirect(array('action' => 'view', $slug)); |
---|
342 | } |
---|
343 | |
---|
344 | $tmp_requests = $this->Request->find('all', array( |
---|
345 | 'conditions' => array('Request.user_id' => $this->AuthCert->user('id')), |
---|
346 | 'order' => 'Request.created DESC', |
---|
347 | 'recursive' => -1, |
---|
348 | )); |
---|
349 | |
---|
350 | $requests = array(); |
---|
351 | foreach ($tmp_requests as $request) { |
---|
352 | $requests[$request['Request']['id']] = $request['Request']['created'] . ': ' |
---|
353 | . $request['Request']['filename'] |
---|
354 | . ' (' . $request['Request']['result_count'] |
---|
355 | . '/' . $request['Request']['job_count'] . ')'; |
---|
356 | } |
---|
357 | |
---|
358 | $this->set(compact('slug', 'requests')); |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | * Remove a document from a gallery |
---|
363 | * @param string $slug the unique gallery slug |
---|
364 | * @param string $request_id The request ID |
---|
365 | */ |
---|
366 | public function remove_document($slug, $request_id) |
---|
367 | { |
---|
368 | if (!$this->_checkAccess($slug)) { |
---|
369 | $this->Session->setFlash(__('You are not allowed to edit this gallery', true)); |
---|
370 | $this->redirect(array('action' => 'view', $slug)); |
---|
371 | } |
---|
372 | |
---|
373 | $gallery = $this->Gallery->find('first', array( |
---|
374 | 'conditions' => array('Gallery.slug' => $slug), |
---|
375 | 'recursive' => -1, |
---|
376 | )); |
---|
377 | |
---|
378 | if (!$this->Gallery->removeRequest($request_id, $gallery['Gallery']['id'])) { |
---|
379 | $this->Session->setFlash(__('The document could not be removed.', true)); |
---|
380 | } |
---|
381 | |
---|
382 | $this->redirect(array('action' => 'view', $slug)); |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | ?> |
---|