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

Revision 15234, 11.0 kB (checked in by flack, 6 months ago)

some spelling/phpdoc fixes

  • 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 Widget base class.
12  *
13  * As with all subclasses, the actual initialization is done in the initialize() function,
14  * not in the constructor, to allow for error handling.
15  *
16  * Quick glance at the changes
17  *
18  * - No more form prefixes, use the field name as a form field name
19  * - Now uses class members, which should use initializers (var $name = 'default_value';)
20  *   for configuration defaults.
21  * - The schema configuration ('widget_config') is merged using the semantics
22  *   $widget->$key = $value;
23  *
24  * @package midcom.helper.datamanager2
25  */
26 class midcom_helper_datamanager2_widget extends midcom_baseclasses_components_purecode
27 {
28     /**
29      * This is a reference to the type we're based on.
30      *
31      * @var midcom_helper_datamanager2_type
32      * @access protected
33      */
34     var $_type = null;
35
36     /**
37      * This variable contains configuration data which is not directly related to the
38      * operation of the type, but required for the operation of external tools like the
39      * storage manager. The type should never touch this variable, which is controlled
40      * by a corresponding getter/setter pair.
41      *
42      * @var Array
43      * @access private
44      * @see set_external_config()
45      * @see get_external_config()
46      */
47     var $_external_config = Array();
48
49     /**
50      * The name field holds the name of the field the widget is encapsulating.
51      *
52      * This maps to the schema's field name. You should never have to change them.
53      *
54      * @var string
55      * @access public
56      */
57     var $name = '';
58
59     /**
60      * A reference to the schema field we should draw.
61      *
62      * Description texts etc. are taken from here.
63      *
64      * @var Array
65      * @access protected
66      */
67     var $_field = null;
68
69     /**
70      * The schema (not the schema <i>database!</i>) to use for operation.
71      *
72      * This variable will always contain a parsed representation of the schema, so that
73      * one can swiftly switch between individual schemas of the Database.
74      *
75      * This member is initialized by-reference.
76      *
77      * @var Array
78      * @access protected
79      */
80     var $_schema = null;
81
82     /**
83      * The QuickForm we are using.
84      *
85      * @var HTML_QuickForm
86      * @access protected
87      */
88     var $_form = null;
89
90     /**
91      * This is the Namespace to use for all HTML/CSS/JS elements.
92      *
93      * It is deduced by the formmanager and tries to be as smart as possible to work safely with
94      * more then one form on a page.
95      *
96      * You have to prefix all elements which must be unique using this string (it includes a
97      * trailing underscore).
98      *
99      * @var string
100      * @access private
101      */
102     var $_namespace = null;
103
104     /**
105      * Whether widget should always load its dependencies on initialization, or only during
106      * add_elements_to_form call.
107      *
108      * @var boolean
109      * @access private
110      */
111     var $_initialize_dependencies = false;
112
113     /**
114      * Constructor.
115      *
116      * Nothing fancy, the actual startup work is done by the initialize call.
117      */
118     function midcom_helper_datamanager2_widget()
119     {
120         $this->_component = 'midcom.helper.datamanager2';
121         parent::midcom_baseclasses_components_purecode();
122     }
123
124     /**
125      * Initializes and configures the widget.
126      *
127      * @param string $name The name of the field to which this widget is bound.
128      * @param Array $config The configuration data which should be used to customize the widget.
129      * @param midcom_helper_datamanager2_schema &$schema A reference to the full schema object.
130      * @param midcom_helper_datamanager2_type &$type A reference to the type to which we are bound.
131      * @param string $namespace The namespace to use including the trailing underscore.
132      * @param boolean $initialize_dependencies Whether to load JS and other dependencies on initialize
133      * @return boolean Indicating success. If this is false, the type will be unusable.
134      */
135     function initialize($name, $config, &$schema, &$type, $namespace, $initialize_dependencies = false)
136     {
137         $this->name = $name;
138         $this->_schema =& $schema;
139         $this->_field =& $schema->fields[$this->name];
140         $this->_type =& $type;
141         $this->_namespace = $namespace;
142         $this->_initialize_dependencies = $initialize_dependencies;
143
144         // Call the event handler for configuration in case we have some defaults that cannot
145         // be covered by the class initializers.
146         $this->_on_configuring();
147
148         // Assign the configuration values.
149         foreach ($config as $key => $value)
150         {
151             $this->$key = $value;
152         }
153
154         if (! $this->_on_initialize())
155         {
156             return false;
157         }
158         return true;
159     }
160
161     /**
162      * Set the form reference.
163      *
164      * @param HTMLQuickForm &$form The form to use.
165      */
166     function set_form(&$form)
167     {
168         $this->_form =& $form;
169     }
170
171     /**
172      * This function is called  before the configuration keys are merged into the types
173      * configuration.
174      */
175     function _on_configuring() {}
176
177     /**
178      * This event handler is called during construction, so passing references to $this to the
179      * outside is unsafe at this point.
180      *
181      * @return boolean Indicating success, false will abort the type construction sequence.
182      * @access protected
183      */
184     function _on_initialize()
185     {
186         return true;
187     }
188
189     /**
190      * Gets an external configuration option referenced by its key.
191      *
192      * Besides other parts in the datamanager framework, nobody should ever have to
193      * touch this information.
194      *
195      * @param string $key The key by which this configuration option is referenced.
196      * @return mixed The configuration value, which is null if the key wasn't found.
197      */
198     function get_external_config($key)
199     {
200         if (! array_key_exists($key, $this->_external_config))
201         {
202             return null;
203         }
204         return $this->_external_config[$key];
205     }
206
207     /**
208      * Sets an external configuration option.
209      *
210      * Besides other parts in the datamanager framework, nobody should ever have to
211      * touch this information.
212      *
213      * @param string $key The key by which this configuration option is referenced.
214      * @param mixed $value The configuration value.
215      */
216     function set_external_config($key, $value)
217     {
218         $this->_external_config[$key] = $value;
219     }
220
221     /**
222      * This call, which must be overridden by subclasses, adds the necessary form elements
223      * to the form passed by reference.
224      *
225      * This must be overridden in subclasses (honor the reference!).
226      *
227      * @param HTML_QuickForm $form A reference to the form to add the elements to.
228      */
229     function add_elements_to_form()
230     {
231         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
232     }
233
234     /**
235      * Returns the default value for this field as required by HTML_Quickform.
236      *
237      * You may either return a single value for simple types, or an array of form
238      * field name / value pairs in case of composite types. A value of null indicates
239      * no applicable default.
240      *
241      * This default implementation returns null unconditionally.
242      *
243      * @return mixed The default value as outlined above.
244      */
245     function get_default()
246     {
247         return null;
248     }
249
250     /**
251      * This is a shortcut to the translate_schema_string function.
252      *
253      * @param string $string The string to be translated.
254      * @return string The translated string.
255      * @see midcom_helper_datamanager2_schema::translate_schema_string()
256      */
257     function _translate($string)
258     {
259         return $this->_schema->translate_schema_string($string);
260     }
261
262     /**
263      * This function is invoked if the widget should extract the corresponding data
264      * from the form results passed in $results.
265      *
266      * Form validation has already been done before, this function will only be called
267      * if and only if the form validation succeeds.
268      *
269      * @param Array $results The complete form results, you need to extract all values
270      *     relevant for your type yourself.
271      */
272     function sync_type_with_widget($results)
273     {
274          die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
275     }
276
277     /**
278      * This event handler is called if and only if the Formmanager detects an actual
279      * form submission (this is tracked using a hidden form member).
280      *
281      * No Form validation has been done at this point. The event is triggered on all
282      * submissions with the exception of the cancel and previous form events.
283      *
284      * You should be careful when using this event for data
285      * processing therefore. Its main application is the processing of additional buttons
286      * placed into the form by the widget.
287      *
288      * The implementation of this handler is optional.
289      *
290      * @param Array $results The complete form results, you need to extract all values
291      *     relevant for your type yourself.
292      */
293     function on_submit($results) {}
294
295     /**
296      * When called, this method should display the current data without any
297      * editing widget or surrounding braces in plain and simple HTML.
298      *
299      * The default implementation calls the type's convert_to_html method.
300      */
301     function render_content ()
302     {
303         return $this->_type->convert_to_html();
304     }
305
306     /**
307      * Freezes all form elements associated with the widget.
308      *
309      * The default implementation works on the default field name, you don't need to override
310      * this function unless you have multiple widgets in the form.
311      *
312      * This maps to the HTML_QuickForm_element::freeze() function.
313      */
314     function freeze()
315     {
316         $element =& $this->_form->getElement($this->name);
317         if (method_exists($element, 'freeze'))
318         {
319             $element->freeze();
320         }
321     }
322
323     /**
324      * Unfreezes all form elements associated with the widget.
325      *
326      * The default implementation works on the default field name, you don't need to override
327      * this function unless you have multiple widgets in the form.
328      *
329      * This maps to the HTML_QuickForm_element::unfreeze() function.
330      */
331     function unfreeze()
332     {
333         $element =& $this->_form->getElement($this->name);
334         $element->unfreeze();
335     }
336
337     /**
338      * Checks if the widget is frozen.
339      *
340      * The default implementation works on the default field name, usually you don't need to
341      * override this function unless you have some strange form element logic.
342      *
343      * This maps to the HTML_QuickForm_element::isFrozen() function.
344      *
345      * @return boolean True if the element is frozen, false otherwise.
346      */
347     function is_frozen()
348     {
349         $element =& $this->_form->getElement($this->name);
350         return $element->isFrozen();
351     }
352
353 }
354
355 ?>
Note: See TracBrowser for help on using the browser.