root/trunk/src/org.routamc.photostream/handler/list.php

Revision 5267, 16.6 kB (checked in by bergie, 4 years ago)

CSS and breadcrumb improvements

Line 
1 <?php
2 /**
3  * Created on 2006-Oct-Thu
4  * @package org.routamc.photostream
5  * @copyright The Midgard Project, http://www.midgard-project.org
6  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7  *
8  */
9 class org_routamc_photostream_handler_list extends midcom_baseclasses_components_handler
10 {
11     /**
12      * Simple default constructor.
13      */
14     function org_routamc_photostream_handler_list()
15     {
16         parent::midcom_baseclasses_components_handler();
17     }
18
19     /**
20      * Resolve username or person GUID to a midcom_db_person object
21      *
22      * @param string $username Username or GUID
23      * @return midcom_db_person Matching person or null
24      */
25     function _resolve_user($username)
26     {
27         $qb = midcom_db_person::new_query_builder();
28         $qb->add_constraint('username', '=', $username);
29         $users = $qb->execute();
30         if (count($users) > 0)
31         {
32             return $users[0];
33         }
34
35         if (mgd_is_guid($username))
36         {
37             // Try resolving as GUID as well
38             $user = new midcom_db_person($username);
39             return $user;
40         }
41
42         return null;
43     }
44
45     /**
46      * Prepare a paged query builder for listing photos
47      */
48     function &_prepare_photo_qb()
49     {
50         $qb = new org_openpsa_qbpager('org_routamc_photostream_photo_dba', 'org_routamc_photostream_photo');
51         $qb->results_per_page = $this->_config->get('photos_per_page');
52         $qb->add_constraint('node', '=', $this->_topic->id);
53         $this->_request_data['qb'] =& $qb;
54         return $qb;
55     }
56
57     function _prepare_ajax_controllers()
58     {
59         // Initiate AJAX controllers for all photos
60         $this->_request_data['controllers'] = array();
61         foreach ($this->_request_data['photos'] as $photo)
62         {
63             $this->_request_data['controllers'][$photo->id] =& midcom_helper_datamanager2_controller::create('ajax');
64             $this->_request_data['controllers'][$photo->id]->schemadb =& $this->_request_data['schemadb'];
65             $this->_request_data['controllers'][$photo->id]->set_storage($photo);
66             $this->_request_data['controllers'][$photo->id]->process_ajax();
67         }
68     }
69
70     /**
71      * The handler for displaying a photographer's photostream
72      * @param mixed $handler_id the array key from the requestarray
73      * @param array $args the arguments given to the handler
74      */
75     function _handler_photostream_list($handler_id, $args, &$data)
76     {
77         if ($handler_id == 'photostream_list')
78         {
79             $data['user'] = $this->_resolve_user($args[0]);
80             if (!$data['user'])
81             {
82                 return false;
83             }
84
85             $data['view_title'] = sprintf($this->_l10n->get('photos of %s'), $data['user']->name);
86             $data['user_url'] = $args[0];
87         }
88         else
89         {
90             $data['view_title'] = $this->_l10n->get('all photos');
91             $data['user_url'] = 'all';
92         }
93
94         // List photos
95         $qb =& $this->_prepare_photo_qb();
96
97         if ($handler_id == 'photostream_list')
98         {
99             // Limit list of photos to the user
100             $qb->add_constraint('photographer', '=', $data['user']->id);
101         }
102
103         $qb->add_order('taken', 'DESC');
104         $data['photos'] = $qb->execute();
105
106         // Make photos AJAX-editable
107         $this->_prepare_ajax_controllers();
108
109         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
110         $this->_update_breadcrumb_line($handler_id);
111
112         return true;
113     }
114
115     function _show_photostream_list($handler_id, &$data)
116     {
117         $this->_show_photostream($handler_id, &$data);
118     }
119     
120     /**
121      * The handler for displaying a photographer's photostream
122      * @param mixed $handler_id the array key from the requestarray
123      * @param array $args the arguments given to the handler
124      */
125     function _handler_photostream_latest($handler_id, $args, &$data)
126     {
127         if ($handler_id == 'photostream_latest')
128         {
129             $data['user'] = $this->_resolve_user($args[0]);
130             if (!$data['user'])
131             {
132                 return false;
133             }
134
135             $data['view_title'] = sprintf($this->_l10n->get('latest photos of %s'), $data['user']->name);
136             $data['user_url'] = $args[0];
137             $data['limit'] = $args[1];
138         }
139         else
140         {
141             $data['view_title'] = $this->_l10n->get('latest photos');
142             $data['user_url'] = 'all';
143             $data['limit'] = $args[0];
144         }
145
146         // List photos
147         $qb = org_routamc_photostream_photo_dba::new_query_builder();
148         $qb->add_constraint('node', '=', $this->_topic->id);
149
150         if ($handler_id == 'photostream_latest')
151         {
152             // Limit list of photos to the user
153             $qb->add_constraint('photographer', '=', $data['user']->id);
154         }
155
156         $qb->add_order('taken', 'DESC');
157         $qb->set_limit($data['limit']);
158         
159         $data['photos'] = $qb->execute();
160
161         // Make photos AJAX-editable
162         $this->_prepare_ajax_controllers();
163
164         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
165         $this->_update_breadcrumb_line($handler_id);
166
167         return true;
168     }
169
170     function _show_photostream_latest($handler_id, &$data)
171     {
172         $this->_show_photostream($handler_id, &$data);
173     }
174
175     /**
176      * The handler for displaying photos in time window
177      * @param mixed $handler_id the array key from the requestarray
178      * @param array $args the arguments given to the handler
179      * @todo 1.7 support
180      */
181     function _handler_photostream_between($handler_id, $args, &$data)
182     {
183         // TODO: Check format as YYYY-MM-DD via regexp   
184         $data['from_time'] = @strtotime($args[0]);
185         $data['to_time'] = @strtotime($args[1]);
186         if (   !$data['from_time']
187             || !$data['to_time'])
188         {
189             return false;
190         }
191     
192         $data['view_title'] = sprintf($this->_l10n->get('photos from %s - %s'), strftime('%x', $data['from_time']), strftime('%x', $data['to_time']));
193         $qb =& $this->_prepare_photo_qb();
194         $qb->add_constraint('taken', '>=', $data['from_time']);
195         $qb->add_constraint('taken', '<=', $data['to_time']);
196         $data['photos'] = $qb->execute();
197
198         // Make photos AJAX-editable
199         $this->_prepare_ajax_controllers();
200
201         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
202         $this->_update_breadcrumb_line($handler_id);
203
204         return true;
205     }
206
207     function _show_photostream_between($handler_id, &$data)
208     {
209         $this->_show_photostream($handler_id, &$data);
210     }
211
212     /**
213      * The handler for displaying photos by upload batch
214      * @param mixed $handler_id the array key from the requestarray
215      * @param array $args the arguments given to the handler
216      * @todo 1.7 support
217      */
218     function _handler_photostream_batch($handler_id, $args, &$data)
219     {
220         $data['view_title'] = sprintf($this->_l10n->get('photos in batch %s'), $args[0]);
221         $qb =& $this->_prepare_photo_qb();
222         if (version_compare(mgd_version(), '1.8.0alpha1', '>='))
223         {
224             $qb->add_constraint('parameter.domain', '=', 'org.routamc.photostream');
225             $qb->add_constraint('parameter.name', '=', 'batch_number');
226             $qb->add_constraint('parameter.value', '=', $args[0]);
227             $data['photos'] = $qb->execute();
228         }
229         else
230         {
231             // FIXME: This is Midgard 1.7 compatibility patch
232             $photos = $qb->execute();
233             $data['photos'] = array();
234             foreach ($photos as $photo)
235             {
236                 if ($photo->parameter('org.routamc.photostream', 'batch_number') == $args[0])
237                 {
238                     $data['photos'][] = $photo;
239                 }
240             }
241         }
242
243         // Make photos AJAX-editable
244         $this->_prepare_ajax_controllers();
245
246         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
247         $this->_update_breadcrumb_line($handler_id);
248
249         return true;
250     }
251
252     function _show_photostream_batch($handler_id, &$data)
253     {
254         $this->_show_photostream($handler_id, &$data);
255     }
256
257     /**
258      * The handler for displaying photos by tag
259      * @param mixed $handler_id the array key from the requestarray
260      * @param array $args the arguments given to the handler
261      */
262     function _handler_photostream_tags($handler_id, $args, &$data)
263     {
264         if ($handler_id == 'photostream_tag')
265         {
266             $data['user'] = $this->_resolve_user($args[0]);
267             if (!$data['user'])
268             {
269                 return false;
270             }
271
272             $data['view_title'] = sprintf($this->_l10n->get('photo tags of %s'), $data['user']->name);
273             $data['user_url'] = $args[0];
274             $data['tags'] = net_nemein_tag_handler::get_tags_by_class('org_routamc_photostream_photo_dba', $data['user']);
275         }
276         else
277         {
278             $data['view_title'] = $this->_l10n->get('photo tags');
279             $data['user_url'] = 'all';
280             $data['tags'] = net_nemein_tag_handler::get_tags_by_class('org_routamc_photostream_photo_dba');
281         }
282
283         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
284         $this->_update_breadcrumb_line($handler_id);
285         
286         return true;
287     }
288     
289     function _show_photostream_tags($handler_id, &$data)
290     {
291         midcom_show_style('show_photostream_tags');
292     }
293
294     /**
295      * The handler for displaying photos by tag
296      * @param mixed $handler_id the array key from the requestarray
297      * @param array $args the arguments given to the handler
298      */
299     function _handler_photostream_tag($handler_id, $args, &$data)
300     {
301         if ($handler_id == 'photostream_tag')
302         {
303             $data['tag'] = $args[1];
304             $data['user'] = $this->_resolve_user($args[0]);
305             if (!$data['user'])
306             {
307                 return false;
308             }
309
310             $data['view_title'] = sprintf($this->_l10n->get('photos of %s tagged with %s'), $data['user']->name, $data['tag']);
311             $data['user_url'] = $args[0];
312         }
313         else
314         {
315             $data['tag'] = $args[0];
316             $data['view_title'] = sprintf($this->_l10n->get('photos tagged with %s'), $data['tag']);
317             $data['user_url'] = 'all';
318         }
319         $data['photos'] = array();
320
321         // Get photo GUIDs from tags
322         // TODO: Use MidgardCollector for this
323         $qb = net_nemein_tag_link_dba::new_query_builder();
324         $qb->add_constraint('tag.tag', '=', $data['tag']);
325         $qb->begin_group('OR');
326             $qb->add_constraint('fromClass', '=', 'org_routamc_photostream_photo_dba');
327             $qb->add_constraint('fromClass', '=', 'org_routamc_photostream_photo');
328         $qb->end_group();
329         //mgd_debug_start();
330         $tags = $qb->execute();
331         //mgd_debug_stop();
332
333         if (count($tags) > 0)
334         {
335             // List photos
336             $qb =& $this->_prepare_photo_qb();
337
338             $qb->begin_group('OR');
339             foreach ($tags as $tag)
340             {
341                 if (class_exists('midgard_query_builder'))
342                 {
343                     // 1.8 allows us to do this sanely
344                     $qb->add_constraint('guid', '=', $tag->fromGuid);
345                 }
346                 else
347                 {
348                     // 1.7 much less so...
349                     $photo = new org_routamc_photostream_photo_dba($tag->fromGuid);
350                     if (!$photo)
351                     {
352                         continue;
353                     }
354                     $qb->add_constraint('id', '=', $photo->id);
355                 }
356             }
357             $qb->end_group();
358
359             if ($handler_id == 'photostream_tag')
360             {
361                 // Limit list of photos to the user
362                 $qb->add_constraint('photographer', '=', $data['user']->id);
363             }
364
365             $qb->add_order('taken', 'DESC');
366             $data['photos'] = $qb->execute();
367         }
368
369         // Make photos AJAX-editable
370         $this->_prepare_ajax_controllers();
371
372         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
373         $this->_update_breadcrumb_line($handler_id);
374
375         return true;
376     }
377
378     function _show_photostream_tag($handler_id, &$data)
379     {
380         $this->_show_photostream($handler_id, &$data);
381     }
382
383     /**
384      * The handler for displaying photos rated with specific rating
385      * @param mixed $handler_id the array key from the requestarray
386      * @param array $args the arguments given to the handler
387      */
388     function _handler_photostream_rated($handler_id, $args, &$data)
389     {
390         if ($handler_id == 'photostream_rated')
391         {
392             $data['rating'] = $args[1];
393             $data['user'] = $this->_resolve_user($args[0]);
394             if (!$data['user'])
395             {
396                 return false;
397             }
398
399             $data['view_title'] = sprintf($this->_l10n->get('photos of %s rated as %s'), $data['user']->name, $data['rating']);
400             $data['user_url'] = $args[0];
401         }
402         else
403         {
404             $data['rating'] = $args[0];
405             $data['view_title'] = sprintf($this->_l10n->get('all photos rated as %s'), $data['rating']);
406             $data['user_url'] = 'all';
407         }
408
409         if (!is_numeric($data['rating']))
410         {
411             return false;
412         }
413
414         // List photos
415         $qb =& $this->_prepare_photo_qb();
416
417         // TODO: We should support "this or better" here too
418         $qb->add_constraint('rating', '=', $data['rating']);
419
420         if ($handler_id == 'photostream_rated')
421         {
422             // Limit list of photos to the user
423             $qb->add_constraint('photographer', '=', $data['user']->id);
424         }
425
426         $qb->add_order('taken', 'DESC');
427         $data['photos'] = $qb->execute();
428
429         // Make photos AJAX-editable
430         $this->_prepare_ajax_controllers();
431
432         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
433         $this->_update_breadcrumb_line($handler_id);
434
435         return true;
436     }
437
438     function _show_photostream_rated($handler_id, &$data)
439     {
440         $this->_show_photostream($handler_id, &$data);
441     }
442
443     /**
444      * Display a list of photos. This method is used by several of the request
445      * switches.
446      */
447     function _show_photostream($handler_id, &$data)
448     {
449         midcom_show_style('show_photostream_header');
450
451         foreach ($data['photos'] as $photo)
452         {
453             $data['photo'] = $photo;
454
455             $data['photo_view'] = $data['controllers'][$photo->id]->get_content_html();
456             $data['datamanager'] =& $data['controllers'][$photo->id]->datamanager;
457
458             midcom_show_style('show_photostream_item');
459         }
460
461         midcom_show_style('show_photostream_footer');
462     }
463
464     /**
465      * Helper, updates the context so that we get a complete breadcrum line towards the current
466      * location.
467      *
468      */
469     function _update_breadcrumb_line($handler_id)
470     {
471         $tmp = Array();
472
473         switch ($handler_id)
474         {
475             case 'photostream_list_all':
476             case 'photostream_list':
477                 $tmp[] = Array
478                 (
479                     MIDCOM_NAV_URL => "list/{$this->_request_data['user_url']}/",
480                     MIDCOM_NAV_NAME => $this->_request_data['view_title'],
481                 );
482                 break;
483             case 'photostream_tags_all':
484             case 'photostream_tags':
485                 $tmp[] = Array
486                 (
487                     MIDCOM_NAV_URL => "tag/{$this->_request_data['user_url']}/",
488                     MIDCOM_NAV_NAME => $this->_request_data['view_title'],
489                 );
490                 break;
491             case 'photostream_tag_all':
492             case 'photostream_tag':
493                 $tmp[] = Array
494                 (
495                     MIDCOM_NAV_URL => "tag/{$this->_request_data['user_url']}/",
496                     MIDCOM_NAV_NAME => $this->_l10n->get('photo tags'),
497                 );
498                 $tmp[] = Array
499                 (
500                     MIDCOM_NAV_URL => "tag/{$this->_request_data['user_url']}/{$this->_request_data['tag']}",
501                     MIDCOM_NAV_NAME => $this->_request_data['view_title'],
502                 );
503                 break;
504             case 'photostream_rated_all':
505             case 'photostream_rated':
506                 $tmp[] = Array
507                 (
508                     MIDCOM_NAV_URL => "rated/{$this->_request_data['user_url']}/{$this->_request_data['rating']}",
509                     MIDCOM_NAV_NAME => $this->_request_data['view_title'],
510                 );
511                 break;
512         }
513
514         $_MIDCOM->set_custom_context_data('midcom.helper.nav.breadcrumb', $tmp);
515     }
516 }
517 ?>
Note: See TracBrowser for help on using the browser.