root/trunk/midcom/midcom.helper.datamanager2/controller.php

Revision 17556, 12.2 kB (checked in by flack, 4 weeks ago)

yet more PHP5-style constructors

  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * @package midcom.helper.datamanager2
4  * @author The Midgard Project, http://www.midgard-project.org
5  * @version $Id$
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  * Datamanager 2 Data Manager controller base class.
12  *
13  * This class encapsulates a controlling instance of the Datamanager class system. You do not
14  * need to use it, it is possible to implement your own, custom form processing solely on the
15  * basis of form/datamanager classes. The controllers are intended to ease the integration work
16  * and provide more advanced frameworks for example for multi-page forms or AJAX callbacks.
17  *
18  * The base class implements only a framework for controllers, along with a factory methods for
19  * getting real instances which you need to initialize. For all instances, you have to set
20  * the schema database using the load_schemadb() helper.
21  *
22  * See the individual
23  * subclass documentations for details about the initialization procedure.
24  *
25  * <b>You cannot use this class directly, consider it as an abstract base class!</b>
26  *
27  * @package midcom.helper.datamanager2
28  * @abstract
29  */
30 class midcom_helper_datamanager2_controller extends midcom_baseclasses_components_purecode
31 {
32     /**
33      * The schemadb to handle by this controller.
34      *
35      * This is a list of midcom_helper_datamanager2_schema instances, indexed
36      * by their name. Set this member using the load_schemadb or set_schemadb
37      * helpers unless you know what you're doing.
38      *
39      * @var Array
40      */
41     var $schemadb = Array();
42
43     /**
44      * The datamanager instance which is used for data I/O processing.
45      *
46      * Set this member using the set_storage() helper function unless you
47      * definitely know what you're doing.
48      *
49      * @var midcom_helper_datamanager2
50      */
51     var $datamanager = null;
52
53     /**
54      * The form manager instance which is currently in use by this class.
55      *
56      * This should always be the a single instance, even for multi-page forms.
57      * Usually, it is created by the controller class during initialization.
58      *
59      * @var midcom_helper_datamanager2_formmanager
60      */
61     var $formmanager = null;
62
63     /**
64      * Lock timeout defines the length of lock in seconds.
65      *
66      * @access public
67      * @var integer
68      */
69     var $lock_timeout = null;
70
71     /**
72      * Override the whole locking scheme
73      *
74      * @access public
75      * @var boolean
76      */
77     var $lock_object = true;
78
79     /**
80      * Initializes the class. The real startup is done by the initialize() call.
81      */
82     function __construct()
83     {
84          $this->_component = 'midcom.helper.datamanager2';
85          parent::__construct();
86     }
87
88     /**
89      * Empty default implementation, this calls won't do much.
90      *
91      * @return boolean Indicating success.
92      */
93     function initialize()
94     {
95         if (is_null($this->lock_timeout))
96         {
97             $this->lock_timeout = $GLOBALS['midcom_config']['metadata_lock_timeout'];
98         }
99         
100         return true;
101     }
102
103     /**
104      * Loads a schema definition from disk and creates the corresponding schema
105      * class instances.
106      *
107      * If you have an array of schema classes already, use set_schemadb() instead.
108      *
109      * @param mixed $schemapath A schema database source suitable for use with
110      *     midcom_helper_datamanager2_schema::load_database()
111      * @see midcom_helper_datamanager2_schema::load_database()
112      */
113     function load_schemadb($schemapath)
114     {
115         // We were unable to shortcut, so we try to load the schema database now.
116         $this->schemadb = midcom_helper_datamanager2_schema::load_database($schemapath);
117     }
118
119     /**
120      * Uses an already loaded schema database. If you want to load a schema database
121      * from disk, use the load_schemadb method instead.
122      *
123      * @param array &$schemadb The schema database to use, this must be an array of midcom_helper_datamanager2_schema
124      *     instances, which is taken by reference.
125      * @see load_schemadb()
126      */
127     function set_schemadb(&$schemadb)
128     {
129         foreach ($schemadb as $key => $value)
130         {
131             if (! is_a($value, 'midcom_helper_datamanager2_schema'))
132             {
133                 debug_push_class(__CLASS__, __FUNCTION__);
134                 debug_print_r('The database passed was:', $schemadb);
135                 debug_pop();
136                 $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
137                     'An invalid schema database has been passed to the midcom_helper_datamanager2_controller::set_schemadb method.');
138                 // This will exit.
139             }
140         }
141
142         $this->schemadb =& $schemadb;
143     }
144
145     /**
146      * Sets the current datamanager instance to the storage object given, which may either
147      * be a MidCOM DBA object (which is encapsulated by a midgard datamanager storage instance).
148      *
149      * You must load a schema database before actually
150      *
151      * @param object &$storage A reference to either an initialized datamanager, an initialized
152      *     storage backend or to a DBA compatible class instance.
153      * @param string $schema This is an optional schema name that should be used to edit the
154      *     storage object. If it is null, the controller will try to autodetect the schema
155      *     to use by using the datamanager's autoset_storage interface.
156      */
157     function set_storage(&$storage, $schema = null)
158     {
159         if (count($this->schemadb) == 0)
160         {
161             $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
162                 'You cannot set a storage object for a DM2 controller object without loading a schema database previously.');
163             // This will exit.
164         }
165
166         if (is_a($storage, 'midcom_helper_datamanager2_datamanager'))
167         {
168             $this->datamanager =& $storage;
169         }
170         else if (   is_a($storage, 'midcom_helper_datamanager2_storage')
171                  || $_MIDCOM->dbclassloader->is_midcom_db_object($storage))
172         {
173             $this->datamanager = new midcom_helper_datamanager2_datamanager($this->schemadb);
174             if ($schema === null)
175             {
176                 if (! $this->datamanager->autoset_storage($storage))
177                 {
178                     debug_push_class(__CLASS__, __FUNCTION__);
179                     debug_print_r('We got this storage object:', $storage);
180                     debug_pop();
181                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
182                         'Failed to automatically create a datamanager instance for a storage object or a MidCOM type. See the debug level log for more information.');
183                     // This will exit().
184                 }
185             }
186             else
187             {
188                 if (! $this->datamanager->set_schema($schema))
189                 {
190                     debug_push_class(__CLASS__, __FUNCTION__);
191                     debug_add("Tried to set the schema {$schema}");
192                     debug_print_r('We got this storage object:', $storage);
193                     debug_pop();
194                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
195                         'Failed to set the autocreated datamanager\'s schema. See the debug level log for more information.');
196                     // This will exit().
197                 }
198                 if (! $this->datamanager->set_storage($storage))
199                 {
200                     debug_push_class(__CLASS__, __FUNCTION__);
201                     debug_add("Tried to set the schema {$schema}");
202                     debug_print_r('We got this storage object:', $storage);
203                     debug_pop();
204                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
205                         'Failed to set the autocreated datamanager\'s storage object. See the debug level log for more information.');
206                     // This will exit().
207                 }
208             }
209         }
210         else
211         {
212             debug_push_class(__CLASS__, __FUNCTION__);
213             debug_print_r('Storage object passed was:', $storage);
214             debug_pop();
215             $_MIDCOM->generate_error(MIDCOM_ERRCRIT,
216                 'You must pass either a datamanager subclass, an initialized storage encapsulation or a MidCOM DBA object to datamanager2_controller::set_storage()');
217             // This will exit.
218         }
219     }
220
221     /**
222      * This is a static factory method which lets you dynamically create controller instances.
223      * It takes care of loading the required class files. The returned instances will be created
224      * but not initialized.
225      *
226      * On any error (class not found etc.) the factory method will call generate_error.
227      *
228      * <b>This function must be called statically.</b>
229      *
230      * @param string $type The type of the controller (the file name from the controller directory).
231      * @return midcom_helper_datamanager2_controller A reference to the newly created controller instance.
232      * @static
233      */
234     function & create($type)
235     {
236         $filename = MIDCOM_ROOT . "/midcom/helper/datamanager2/controller/{$type}.php";
237         $classname = "midcom_helper_datamanager2_controller_{$type}";
238         require_once($filename);
239         /**
240          * Php 4.4.1 does not allow you to return a reference to an expression.
241          * http://www.php.net/release_4_4_0.php
242          */
243         $class = new $classname();
244         return $class;
245     }
246
247     /**
248      * This function should process the form data sent to the server. Its behavior is dependant
249      * on the controller used, see the individual class documentations for details.
250      *
251      * @return string The exitcode of the form processing, usually related to the formmanager
252      *     result constants.
253      */
254     function process_form()
255     {
256         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
257     }
258
259     /**
260      * This function invokes the display_form() hook on the form manager class.
261      */
262     function display_form()
263     {
264         // Prevent temporary objects from failing
265         if (   $this->lock_object
266             && isset($this->datamanager->storage)
267             && isset($this->datamanager->storage->object)
268             && isset($this->datamanager->storage->object->guid))
269         {
270             // Get the metadata object
271             $metadata =& $this->datamanager->storage->object->get_metadata();
272             
273             if ($metadata->is_locked())
274             {
275                 // Drop us to uncached state when locked
276                 $_MIDCOM->cache->content->uncached();
277                 $this->show_unlock();
278                 return;
279             }
280         }
281         
282         $this->formmanager->display_form();
283     }
284     
285     /**
286      * Show the lock status
287      *
288      * @access public
289      */
290     function show_unlock()
291     {
292         if (   function_exists('mgd_is_element_loaded')
293             && mgd_is_element_loaded('midcom_helper_datamanager2_unlock'))
294         {
295             mgd_show_element('midcom_helper_datamanager2_unlock');
296         }
297         else
298         {
299             $metadata =& $this->datamanager->storage->object->get_metadata();
300             $person = new midcom_db_person($this->datamanager->storage->object->metadata->locker);
301             ?>
302                 <div class="midcom_helper_datamanager2_unlock">
303                     <h2><?php echo $this->_l10n->get('object locked'); ?></h2>
304                     <p>
305                         <?php echo sprintf($this->_l10n->get('this object was locked by %s'), $person->name); ?>.
306                         <?php echo sprintf($this->_l10n->get('lock will expire on %s'), strftime('%x %X', ($metadata->get('locked') + ($GLOBALS['midcom_config']['metadata_lock_timeout'] * 60)))); ?>.
307                     </p>
308             <?php
309             if ($metadata->can_unlock())
310             {
311                 echo "<form method=\"post\">\n";
312                 echo "    <p class=\"unlock\">\n";
313                 echo "        <input type=\"hidden\" name=\"midcom_helper_datamanager2_object\" value=\"{$this->datamanager->storage->object->guid}\" />\n";
314                 echo "        <input type=\"submit\" name=\"midcom_helper_datamanager2_unlock\" value=\"" . $this->_l10n->get('unlock') . "\" class=\"unlock\" />\n";
315                 echo "    </p>\n";
316                 echo "</form>\n";
317             }
318             ?>
319                 </div>
320             <?php
321         }
322     }
323 }
324 ?>
Note: See TracBrowser for help on using the browser.