root/branches/MidCOM_2_8/org.routamc.photostream/viewer.php

Revision 11036, 14.8 kB (checked in by rambo, 3 years ago)

Started work on feed creation, the viewer now autogenerates some
request switches it considers likely to be usefull and passes them on
to a dispatcher that will then call corresponding handler to see if we
can get an array of photos.

Making an actual feed from those with feedcreator is next up.

Line 
1 <?php
2 /**
3  * @package org.routamc.photostream
4  * @author The Midgard Project, http://www.midgard-project.org
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 /**
10  * This is the class that defines which url's should be handled by this module.
11  *
12  * @package org.routamc.photostream
13  */
14
15 class org_routamc_photostream_viewer extends midcom_baseclasses_components_request
16 {
17     function org_routamc_photostream_viewer($topic, $config)
18     {
19         parent::midcom_baseclasses_components_request($topic, $config);
20     }
21
22     /**
23      * Initialize the request switch and the content topic.
24      *
25      * @access protected
26      */
27     function _on_initialize()
28     {
29         // *** Prepare the request switch ***
30
31         // Handle /config
32         $this->_request_switch['config'] = array
33         (
34             'handler' => array
35             (
36                 'midcom_core_handler_configdm',
37                 'configdm'
38             ),
39             'schemadb' => 'file:/org/routamc/photostream/config/schemadb_config.inc',
40             'schema' => 'config',
41             'fixed_args' => array
42             (
43                 'config'
44             ),
45         );
46
47         // Handle /upload
48         $this->_request_switch['upload'] = array
49         (
50             'handler' => array
51             (
52                 'org_routamc_photostream_handler_upload',
53                 'upload'
54             ),
55             'fixed_args' => array
56             (
57                 'upload'
58             ),
59         );
60
61         // Handle /latest/all/<n>
62         $this->_request_switch['photostream_latest_all'] = array
63         (
64             'handler' => array
65             (
66                 'org_routamc_photostream_handler_list',
67                 'photostream_latest'
68             ),
69             'fixed_args' => array
70             (
71                 'latest',
72                 'all',
73             ),
74             'variable_args' => 1,
75         );
76
77         // Handle /latest/<username>/<n>
78         $this->_request_switch['photostream_latest'] = array
79         (
80             'handler' => array
81             (
82                 'org_routamc_photostream_handler_list',
83                 'photostream_latest'
84             ),
85             'fixed_args' => array
86             (
87                 'latest',
88             ),
89             'variable_args' => 2,
90         );
91
92         // Handle /between/all/<from>/<to>
93         $this->_request_switch['photostream_between_all'] = array
94         (
95             'handler' => array
96             (
97                 'org_routamc_photostream_handler_list',
98                 'photostream_between'
99             ),
100             'fixed_args' => array
101             (
102                 'between',
103                 'all',
104             ),
105             'variable_args' => 2,
106         );
107
108         // Handle /between/<username>/<from>/<to>
109         $this->_request_switch['photostream_between'] = array
110         (
111             'handler' => array
112             (
113                 'org_routamc_photostream_handler_list',
114                 'photostream_between'
115             ),
116             'fixed_args' => array
117             (
118                 'between',
119             ),
120             'variable_args' => 3,
121         );
122
123         // Handle /tag/all/<tag>
124         $this->_request_switch['photostream_tag_all'] = array
125         (
126             'handler' => array
127             (
128                 'org_routamc_photostream_handler_list',
129                 'photostream_tag'
130             ),
131             'fixed_args' => array
132             (
133                 'tag',
134                 'all',
135             ),
136             'variable_args' => 1,
137         );
138
139         // Handle /tag/<username>/<tag>
140         $this->_request_switch['photostream_tag'] = array
141         (
142             'handler' => array
143             (
144                 'org_routamc_photostream_handler_list',
145                 'photostream_tag'
146             ),
147             'fixed_args' => array
148             (
149                 'tag',
150             ),
151             'variable_args' => 2,
152         );
153         
154         // Handle /tag/all/
155         $this->_request_switch['photostream_tags_all'] = array
156         (
157             'handler' => array
158             (
159                 'org_routamc_photostream_handler_list',
160                 'photostream_tags'
161             ),
162             'fixed_args' => array
163             (
164                 'tag',
165                 'all',
166             ),
167         );
168
169         // Handle /tag/<username>/
170         $this->_request_switch['photostream_tags'] = array
171         (
172             'handler' => array
173             (
174                 'org_routamc_photostream_handler_list',
175                 'photostream_tags'
176             ),
177             'fixed_args' => array
178             (
179                 'tag',
180             ),
181             'variable_args' => 1,
182         );
183
184         // Handle /rated/all/<tag>
185         $this->_request_switch['photostream_rated_all'] = array
186         (
187             'handler' => array
188             (
189                 'org_routamc_photostream_handler_list',
190                 'photostream_rated'
191             ),
192             'fixed_args' => array
193             (
194                 'rated',
195                 'all',
196             ),
197         );
198
199         // Handle /rated/<username>/<rating>
200         $this->_request_switch['photostream_rated'] = array
201         (
202             'handler' => array
203             (
204                 'org_routamc_photostream_handler_list',
205                 'photostream_rated'
206             ),
207             'fixed_args' => array
208             (
209                 'rated',
210             ),
211             'variable_args' => 2,
212         );
213
214         // Handle /list/all
215         $this->_request_switch['photostream_list_all'] = array
216         (
217             'handler' => array
218             (
219                 'org_routamc_photostream_handler_list',
220                 'photostream_list'
221             ),
222             'fixed_args' => array
223             (
224                 'list',
225                 'all',
226             ),
227         );
228
229         // Handle /list/<username>
230         $this->_request_switch['photostream_list'] = array
231         (
232             'handler' => array
233             (
234                 'org_routamc_photostream_handler_list',
235                 'photostream_list'
236             ),
237             'fixed_args' => array
238             (
239                 'list',
240             ),
241             'variable_args' => 1,
242         );
243
244         // Handle /batch/<batch_id>
245         $this->_request_switch['photostream_batch'] = array
246         (
247             'handler' => array
248             (
249                 'org_routamc_photostream_handler_list',
250                 'photostream_batch'
251             ),
252             'fixed_args' => array
253             (
254                 'batch',
255             ),
256             'variable_args' => 1,
257         );
258
259         // Handle /photo/<guid>
260         $this->_request_switch['photo'] = Array
261         (
262             'handler' => Array('org_routamc_photostream_handler_view', 'view'),
263             'fixed_args' => Array('photo'),
264             'variable_args' => 1,
265         );
266
267         // Handle /photo/raw/<guid>
268         $this->_request_switch['photo_raw'] = Array
269         (
270             'handler' => Array('org_routamc_photostream_handler_view', 'view'),
271             'fixed_args' => Array('photo', 'raw'),
272             'variable_args' => 1,
273         );
274
275         // Handle /photo/<guid>/<gallery>
276         $this->_request_switch['photo_gallery'] = Array
277         (
278             'handler' => Array('org_routamc_photostream_handler_view', 'view'),
279             'fixed_args' => Array('photo'),
280             'variable_args' => 2,
281         );
282
283         // Handle /edit/<guid>
284         $this->_request_switch['edit'] = Array
285         (
286             'handler' => Array('org_routamc_photostream_handler_admin', 'edit'),
287             'fixed_args' => Array('edit'),
288             'variable_args' => 1,
289         );
290
291         // Handle /delete/guid
292         $this->_request_switch['delete'] = Array
293         (
294             'handler' => Array('org_routamc_photostream_handler_admin', 'delete'),
295             'fixed_args' => Array('delete'),
296             'variable_args' => 1,
297         );
298
299         // Handle /recreate
300         $this->_request_switch['recreate'] = Array
301         (
302             'handler' => Array('org_routamc_photostream_handler_admin', 'recreate'),
303             'fixed_args' => Array('recreate'),
304             'variable_args' => 0,
305         );
306
307
308         $this->_request_switch['api-email'] = Array
309         (
310             'handler' => Array('org_routamc_photostream_handler_api_email', 'import'),
311             'fixed_args' => Array('api', 'email'),
312         );
313         
314         /* not implemented yet
315         $this->_request_switch['api-metaweblog'] = Array
316         (
317             'handler' => Array('org_routamc_photostream_handler_api_metaweblog', 'server'),
318             'fixed_args' => Array('api', 'metaweblog'),
319         );
320         */
321
322         // Handle /
323         $this->_request_switch['index'] = array
324         (
325             'handler' => array
326             (
327                 'org_routamc_photostream_handler_index',
328                 'index'
329             ),
330         );
331
332         $this->_register_feed_handlers();
333     }
334
335     function _register_feed_handlers()
336     {
337         debug_push_class(__CLASS__, __FUNCTION__);
338         debug_print_r('$this->_request_switch is now: ', $this->_request_switch);
339         debug_add('Start');
340         foreach ($this->_request_switch as $handler_id => $switch_data)
341         {
342             debug_add("Checking id '{$handler_id}', it has handler class '{$switch_data['handler'][0]}'");
343             if ($switch_data['handler'][0] !== 'org_routamc_photostream_handler_list')
344             {
345                 debug_add('Not interested, next!');
346                 // We only care about the list views
347                 continue;
348             }
349             $new_id = "feed:{$handler_id}";
350             debug_add("Creating new switch with id '{$new_id}'");
351             // PHP5-TODO: Must be copy-by-value
352             $new_switch = $switch_data;
353             // switch handler to the feed dispatcher
354             $new_switch['handler'] = array('org_routamc_photostream_handler_feed', 'dispatcher');
355             // add a variable arg to end of list for feed type
356             if (!isset($new_switch['variable_args']))
357             {
358                 $new_switch['variable_args'] = 0;
359             }
360             $new_switch['variable_args']++;
361             
362             /* OTOH the variable_args increment server the same purpose and it's easier to just add rss.xml/atom.xml to the url...
363             // Prepend /feed/ to list fixed args
364             array_unshift($new_switch['fixed_args'], 'feed');
365             */
366
367             // Add the new switch
368             debug_print_r("Setting \$this->_request_switch['{$new_id}'] to: ", $new_switch);
369             $this->_request_switch[$new_id] = $new_switch;
370             unset($new_id, $new_switch);
371         }
372         debug_add('Done.');
373         debug_print_r('$this->_request_switch is now: ', $this->_request_switch);
374         debug_pop();
375     }
376
377     /**
378      * Indexes an article.
379      *
380      * This function is usually called statically from various handlers.
381      *
382      * @param midcom_helper_datamanager2_datamanager $dm The Datamanager encaspulating the event.
383      * @param midcom_services_indexer $indexer The indexer instance to use.
384      * @param midcom_db_topic The topic which we are bound to. If this is not an object, the code
385      *     tries to load a new topic instance from the database identified by this parameter.
386      */
387     function index(&$dm, &$indexer, $topic)
388     {
389         if (is_object($topic))
390         {
391             $tmp = new midcom_db_topic($topic);
392             if (! $tmp)
393             {
394                 $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
395                     "Failed to load the topic referenced by {$topic} for indexing, this is fatal.");
396                 // This will exit.
397             }
398             $topic = $tmp;
399         }
400
401         // Don't index directly, that would loose a reference due to limitations
402         // of the index() method. Needs fixes there.
403
404         $nav = new midcom_helper_nav();
405         $node = $nav->get_node($topic->id);
406         $author = $_MIDCOM->auth->get_user($dm->storage->object->creator);
407
408         $document = $indexer->new_document($dm);
409         $document->topic_guid = $topic->guid;
410         $document->component = $topic->component;
411         $document->topic_url = $node[MIDCOM_NAV_FULLURL];
412         $document->author = $author->name;
413         $document->created = $dm->storage->object->created;
414         $document->edited = $dm->storage->object->revised;
415         $indexer->index($document);
416     }
417
418     /**
419      * Populates the node toolbar depending on the users rights.
420      *
421      * @access protected
422      */
423     function _populate_node_toolbar()
424     {
425
426         $this->_node_toolbar->add_item
427         (
428             array
429             (
430                 MIDCOM_TOOLBAR_URL => 'upload.html',
431                 MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('upload photos'),
432                 MIDCOM_TOOLBAR_HELPTEXT => null,
433                 MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/images.png',
434                 MIDCOM_TOOLBAR_ENABLED => $this->_topic->can_do('midgard:create'),
435                 MIDCOM_TOOLBAR_ACCESSKEY => 'n',
436             )
437         );
438         $this->_node_toolbar->add_item
439         (
440             array
441             (
442                 MIDCOM_TOOLBAR_URL => 'recreate.html',
443                 MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('recreate derived images'),
444                 MIDCOM_TOOLBAR_HELPTEXT => null,
445                 // TODO: better icon
446                 MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/recurring.png',
447                 // TODO: Better privilege ?
448                 MIDCOM_TOOLBAR_ENABLED => $this->_topic->can_do('midgard:create'),
449             )
450         );
451
452         if (   $this->_topic->can_do('midgard:update')
453             && $this->_topic->can_do('midcom:component_config'))
454         {
455             $this->_node_toolbar->add_item
456             (
457                 array
458                 (
459                     MIDCOM_TOOLBAR_URL => 'config.html',
460                     MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
461                     MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
462                     MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png',
463                 )
464             );
465         }
466
467     }
468
469     /**
470      * The handle callback populates the toolbars.
471      */
472     function _on_handle($handler, $args)
473     {
474         $this->_request_data['schemadb'] = midcom_helper_datamanager2_schema::load_database($this->_config->get('schemadb'));
475
476         $this->_populate_node_toolbar();
477
478         $_MIDCOM->add_link_head
479         (
480             array
481             (
482                 'rel' => 'stylesheet',
483                 'type' => 'text/css',
484                 'href' => MIDCOM_STATIC_URL."/org.routamc.photostream/photos.css",
485             )
486         );
487
488         // the feed dispatcher needs this information, it might be available otherwisely but I couldn't find it
489         $this->_request_data['request_switch'] =& $this->_request_switch;
490         return true;
491     }
492
493 }
494
495 ?>
496
Note: See TracBrowser for help on using the browser.