root/trunk/midcom/midcom.helper.replicator/manager.php

Revision 18029, 16.0 kB (checked in by bergie, 4 days ago)

Ensure object is a DBA one before populating toolbars, fixes #303

Line 
1 <?php
2 /**
3  * @package midcom.helper.replicator
4  * @author The Midgard Project, http://www.midgard-project.org
5  * @version $Id: acl_editor.php 4207 2006-09-26 08:41:44Z bergie $
6  * @copyright The Midgard Project, http://www.midgard-project.org
7  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
8  */
9
10 /**
11  * Basic handler class
12  * @package midcom.helper.replicator
13  */
14 class midcom_helper_replicator_manager extends midcom_baseclasses_components_handler
15 {
16     /**
17      * The current local configuration.
18      *
19      * @var midcom_helper_configuration
20      */
21     var $_local_config;
22
23     /**
24      * The object we're managing
25      *
26      * @var object
27      * @access private
28      */
29     var $_subscription = null;
30
31     /**
32      * The Datamanager of the member to display
33      *
34      * @var midcom_helper_datamanager2_datamanager
35      * @access private
36      */
37     var $_datamanager = null;
38
39     /**
40      * The Controller of the member used for editing
41      *
42      * @var midcom_helper_datamanager2_controller_simple
43      * @access private
44      */
45     var $_controller = null;
46
47     /**
48      * The schema database in use, available only while a datamanager is loaded.
49      *
50      * @var Array
51      * @access private
52      */
53     var $_schemadb = null;
54
55     /**
56      * The schema to use for the new subscription.
57      *
58      * @var string
59      * @access private
60      */
61     var $_schema = 'default';
62
63     /**
64      * The defaults to use for the new subscription.
65      *
66      * @var Array
67      * @access private
68      */
69     var $_defaults = Array();
70
71     function __construct()
72     {
73         parent::__construct();
74     }
75
76     function get_plugin_handlers()
77     {
78         $_MIDCOM->load_library('midgard.admin.asgard');
79         return array
80         (
81             'list' => array
82             (
83                 'handler' => array('midcom_helper_replicator_manager', 'list'),
84             ),
85             'create' => array
86             (
87                 'handler' => array('midcom_helper_replicator_manager', 'create'),
88                 'fixed_args' => 'create',
89                 'variable_args' => 1,
90             ),
91             'edit' => array
92             (
93                 'handler' => array('midcom_helper_replicator_manager', 'edit'),
94                 'fixed_args' => 'edit',
95                 'variable_args' => 1,
96             ),
97             'object' => array
98             (
99                 'handler' => array('midcom_helper_replicator_manager', 'object'),
100                 'fixed_args' => 'object',
101                 'variable_args' => 1,
102             ),
103         );
104     }
105
106     function _on_initialize()
107     {
108         $_MIDCOM->load_library('midcom.helper.replicator');
109         $_MIDCOM->load_library('midcom.helper.datamanager2');
110
111         // Ensure we get the correct styles
112         $_MIDCOM->style->prepend_component_styledir('midcom.helper.replicator');
113
114         $component_data =& $GLOBALS['midcom_component_data']['midcom.helper.replicator'];
115         $this->_local_config = $component_data['config'];
116
117         $this->_load_schemadb();
118
119         midgard_admin_asgard_plugin::prepare_plugin('', &$this->_request_data);
120         foreach (array_keys($this->_schemadb) as $name)
121         {
122             if (preg_match('/midcom.helper.replicator\/create\//', $_MIDGARD['uri']))
123             {
124                 $prefix = '';
125             }
126             else
127             {
128                 $prefix = 'create/';
129             }
130
131             $this->_request_data['asgard_toolbar']->add_item
132             (
133                 array
134                 (
135                     MIDCOM_TOOLBAR_URL => "{$prefix}{$name}/",
136                     MIDCOM_TOOLBAR_LABEL => sprintf
137                     (
138                         $this->_l10n_midcom->get('create %s'),
139                         $_MIDCOM->i18n->get_string($this->_schemadb[$name]->description, 'midcom.helper.replicator')
140                     ),
141                     MIDCOM_TOOLBAR_ICON => 'midcom.helper.replicator/replicate-server-16.png',
142                 )
143             );
144         }
145     }
146
147     function _update_breadcrumb($handler_id)
148     {
149         $tmp = Array();
150         $tmp[] = Array
151         (
152             MIDCOM_NAV_URL => '__mfa/asgard_midcom.helper.replicator/',
153             MIDCOM_NAV_NAME => $_MIDCOM->i18n->get_string('replication subscriptions', 'midcom.helper.replicator'),
154         );
155
156         switch ($handler_id)
157         {
158             case '____mfa-asgard_midcom.helper.replicator-edit':
159                 $tmp[] = Array
160                 (
161                     MIDCOM_NAV_URL => "__mfa/asgard_midcom.helper.replicator/edit/{$this->_subscription->guid}/",
162                     MIDCOM_NAV_NAME => sprintf($this->_l10n_midcom->get('edit %s: %s'), $this->_request_data['view_type'], $this->_subscription->title),
163                 );
164                 break;
165             case '____mfa-asgard_midcom.helper.replicator-create':
166                 $tmp[] = Array
167                 (
168                     MIDCOM_NAV_URL => "__mfa/asgard_midcom.helper.replicator/create/{$this->_schema}/",
169                     MIDCOM_NAV_NAME => $this->_request_data['view_title'],
170                 );
171                 break;
172         }
173
174         $_MIDCOM->set_custom_context_data('midcom.helper.nav.breadcrumb', $tmp);
175     }
176
177     /**
178      * Loads and prepares the schema database.
179      *
180      * Special treatement is done for the name field, which is set readonly for non-creates
181      * if the simple_name_handling config option is set. (using an auto-generated urlname based
182      * on the title, if it is missing.)
183      *
184      * The operations are done on all available schemas within the DB.
185      */
186     function _load_schemadb()
187     {
188         $this->_schemadb = midcom_helper_datamanager2_schema::load_database('file:/midcom/helper/replicator/config/schemadb_default.inc');
189     }
190
191     function _handler_list($handler_id, $args, &$data)
192     {
193         $_MIDCOM->auth->require_user_do('midcom.helper.replicator:manage');
194
195         $qb = midcom_helper_replicator_subscription_dba::new_query_builder();
196         $qb->add_order('title');
197         $data['subscriptions'] = $qb->execute();
198
199         $this->_update_breadcrumb($handler_id);
200
201         $data['view_title'] = $_MIDCOM->i18n->get_string('replication subscriptions', 'midcom.helper.replicator');
202         $_MIDCOM->set_pagetitle($data['view_title']);
203
204         return true;
205     }
206
207     /**
208      *
209      * @param mixed $handler_id The ID of the handler.
210      * @param mixed &$data The local request data.
211      */
212     function _show_list($handler_id, &$data)
213     {
214         midcom_show_style('midgard_admin_asgard_header');
215         midcom_show_style('midgard_admin_asgard_middle');
216
217         $data['schemadb'] =& $this->_schemadb;
218         $data['local_config'] =& $this->_local_config;
219         midcom_show_style('midcom-helper-replicator-list');
220
221         midcom_show_style('midgard_admin_asgard_footer');
222
223     }
224
225     /**
226      * Internal helper, loads the controller for the current article. Any error triggers a 500.
227      *
228      * @access private
229      */
230     function _load_controller()
231     {
232         $this->_controller =& midcom_helper_datamanager2_controller::create('simple');
233         $this->_controller->schemadb =& $this->_schemadb;
234         $this->_controller->set_storage($this->_subscription, $this->_subscription->exporter);
235         if (! $this->_controller->initialize())
236         {
237             $_MIDCOM->generate_error(MIDCOM_ERRCRIT, "Failed to initialize a DM2 controller instance for article {$this->_article->id}.");
238             // This will exit.
239         }
240     }
241
242     /**
243      * Internal helper, fires up the creation mode controller. Any error triggers a 500.
244      *
245      * @access private
246      */
247     function _load_create_controller()
248     {
249         $this->_controller =& midcom_helper_datamanager2_controller::create('create');
250         $this->_controller->schemadb =& $this->_schemadb;
251         $this->_controller->schemaname = $this->_schema;
252         $this->_controller->defaults = $this->_defaults;
253         $this->_controller->callback_object =& $this;
254         if (! $this->_controller->initialize())
255         {
256             $_MIDCOM->generate_error(MIDCOM_ERRCRIT, "Failed to initialize a DM2 create controller.");
257             // This will exit.
258         }
259     }
260
261     function _modify_schema_for_object($subscription)
262     {
263         $transporter = midcom_helper_replicator_transporter::create($subscription);
264         $transporter->add_ui_options($this->_schemadb[$subscription->exporter]);
265     }
266
267     function _handler_edit($handler_id, $args, &$data)
268     {
269         $this->_subscription = new midcom_helper_replicator_subscription_dba($args[0]);
270         if (   !is_object($this->_subscription)
271             || !isset($this->_subscription->guid)
272             || empty($this->_subscription->guid))
273         {
274             return false;
275         }
276         $this->_subscription->require_do('midgard:update');
277         $this->subscription->autoserialize_filters = false;
278
279         $_MIDCOM->bind_view_to_object($this->_subscription, $this->_subscription->exporter);
280
281         $this->_modify_schema_for_object($this->_subscription);
282
283         // Load the datamanager controller
284         $this->_load_controller();
285
286         switch ($this->_controller->process_form())
287         {
288             case 'save':
289                 $_MIDCOM->relocate("__mfa/asgard_midcom.helper.replicator/edit/{$this->_subscription->guid}/");
290                 // This will exit.
291
292             case 'cancel':
293                 $_MIDCOM->relocate('__mfa/asgard_midcom.helper.replicator/');
294                 // This will exit.
295         }
296
297         $data['view_type'] = $_MIDCOM->i18n->get_string($this->_schemadb[$this->_subscription->exporter]->description, 'midcom.helper.replicator');
298
299         $this->_update_breadcrumb($handler_id);
300
301         $data['view_title'] = $this->_subscription->title;
302         $_MIDCOM->set_pagetitle($data['view_title']);
303
304         return true;
305     }
306
307     /**
308      *
309      * @param mixed $handler_id The ID of the handler.
310      * @param mixed &$data The local request data.
311      */
312     function _show_edit($handler_id, &$data)
313     {
314         midcom_show_style('midgard_admin_asgard_header');
315         midcom_show_style('midgard_admin_asgard_middle');
316
317         $data['controller'] =& $this->_controller;
318         midcom_show_style('midcom-helper-replicator-edit');
319
320         midcom_show_style('midgard_admin_asgard_footer');
321     }
322
323     /**
324      * DM2 creation callback, binds to the current content topic.
325      */
326     function & dm2_create_callback (&$controller)
327     {
328         $this->_subscription = new midcom_helper_replicator_subscription_dba();
329         $this->_subscription->exporter = $this->_schema;
330
331         if (! $this->_subscription->create())
332         {
333             debug_push_class(__CLASS__, __FUNCTION__);
334             debug_print_r('We operated on this object:', $this->_subscription);
335             debug_pop();
336             $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
337                 "Failed to create a new subscription, cannot continue. Error: " . mgd_errstr());
338             // This will exit.
339         }
340         $this->subscription->autoserialize_filters = false;
341
342         return $this->_subscription;
343     }
344
345     /**
346      * Displays an article edit view.
347      *
348      * Note, that the article for non-index mode operation is automatically determined in the can_handle
349      * phase.
350      *
351      * If create privileges apply, we relocate to the index creation article,
352      */
353     function _handler_create($handler_id, $args, &$data)
354     {
355         $_MIDCOM->auth->require_user_do('midcom.helper.replicator:manage');
356
357         $this->_schema = $args[0];
358         if (!array_key_exists($this->_schema, $this->_schemadb))
359         {
360             return false;
361         }
362         $this->_schema =& $this->_schema;
363
364         $this->_load_create_controller();
365
366         switch ($this->_controller->process_form())
367         {
368             case 'save':
369                 $_MIDCOM->relocate("__mfa/asgard_midcom.helper.replicator/edit/{$this->_subscription->guid}/");
370                 // This will exit.
371
372             case 'cancel':
373                 $_MIDCOM->relocate('__mfa/asgard_midcom.helper.replicator/');
374                 // This will exit.
375         }
376
377         $data['view_title'] = sprintf($this->_l10n_midcom->get('create %s'), $_MIDCOM->i18n->get_string($this->_schemadb[$this->_schema]->description, 'midcom.helper.replicator'));
378         $_MIDCOM->set_pagetitle("{$this->_topic->extra}: {$data['view_title']}");
379
380         $this->_update_breadcrumb($handler_id);
381
382         return true;
383     }
384
385     /**
386      *
387      * @param mixed $handler_id The ID of the handler.
388      * @param mixed &$data The local request data.
389      */
390     function _show_create($handler_id, &$data)
391     {
392         midcom_show_style('midgard_admin_asgard_header');
393         midcom_show_style('midgard_admin_asgard_middle');
394
395         $data['controller'] =& $this->_controller;
396         midcom_show_style('midcom-helper-replicator-create');
397
398         midcom_show_style('midgard_admin_asgard_footer');
399     }
400
401     function _resolve_object_title($object)
402     {
403         $vars = get_object_vars($object);
404
405         if (   array_key_exists('title', $vars)
406             && $object->title)
407         {
408             return $object->title;
409         }
410         elseif (array_key_exists('name', $vars))
411         {
412             return $object->name;
413         }
414         else
415         {
416             return "#{$object->id}";
417         }
418     }
419
420     /**
421      * Displays replication information for an object
422      */
423     function _handler_object($handler_id, $args, &$data)
424     {
425         $bind_toolbar = true;
426         $data['object'] = $_MIDCOM->dbfactory->get_object_by_guid($args[0]);
427         if (!$data['object'])
428         {
429             $bind_toolbar = false;
430             // Fallback to midgard core to find deleted object
431             foreach($_MIDGARD['schema']['types'] as $mgdschema_class => $dummy)
432             {
433                 if (empty($mgdschema_class))
434                 {
435                     continue;
436                 }
437                 $qb = new midgard_query_builder($mgdschema_class);
438                 $qb->add_constraint('guid', '=', $args[0]);
439                 $qb->include_deleted();
440                 $objects = $qb->execute();
441                 if (empty($objects))
442                 {
443                     continue;
444                 }
445                 $data['object'] = $objects[0];
446                 break;
447             }
448         }
449         if (!$data['object'])
450         {
451             return false;
452         }
453
454         if (isset($_POST['midcom_helper_replicator_requeue']))
455         {
456             $qmanager =& midcom_helper_replicator_queuemanager::get();
457             //TODO: Is there a more graceful way to do this ? (see also mirror.php)
458             $GLOBALS['midcom_helper_replicator_exporter_retry_mode'] = true;
459             if (!$qmanager->add_to_queue($data['object']))
460             {
461                 $_MIDCOM->uimessages->add
462                 (
463                     $this->_l10n->get('midcom.helper.replicator'),
464                     $this->_l10n->get('re-queuing failed'),
465                     'warning'
466                 );
467             }
468             unset($GLOBALS['midcom_helper_replicator_exporter_retry_mode']);
469         }
470
471         $data['language_code'] = '';
472         midgard_admin_asgard_plugin::bind_to_object($data['object'], $handler_id, &$data);
473
474         $data['view_title'] = sprintf($_MIDCOM->i18n->get_string('replication information for %s', 'midcom.helper.replicator'), $this->_resolve_object_title($data['object']));
475         $_MIDCOM->set_pagetitle($data['view_title']);
476
477         $_MIDCOM->add_link_head
478         (
479             array
480             (
481                 'rel' => 'stylesheet',
482                 'type' => 'text/css',
483                 'href' => MIDCOM_STATIC_URL."/midcom.helper.replicator/replicator.css",
484             )
485         );
486
487         $data['queue_root_dir'] = preg_replace('%/{2,}|/$%', '', $this->_local_config->get('queue_root_dir'));
488
489         return true;
490     }
491
492     /**
493      *
494      * @param mixed $handler_id The ID of the handler.
495      * @param mixed &$data The local request data.
496      */
497     function _show_object($handler_id, &$data)
498     {
499         midcom_show_style('midgard_admin_asgard_header');
500         midcom_show_style('midgard_admin_asgard_middle');
501
502         midcom_show_style('midcom-helper-replicator-object');
503
504         midcom_show_style('midgard_admin_asgard_footer');
505
506     }
507 }
508 ?>
Note: See TracBrowser for help on using the browser.