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

Revision 17556, 10.1 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 Type 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  * <b>Type configuration:</b>
17  *
18  * - Now uses class members, which should use initializers (var $name = 'default_value';)
19  *   for configuration defaults.
20  * - The schema configuration ('type_config') is merged using the semantics
21  *   $type->$key = $value;
22  *
23  * @package midcom.helper.datamanager2
24  */
25 class midcom_helper_datamanager2_type extends midcom_baseclasses_components_purecode
26 {
27     /**
28      * Set this to true during one of the startup callbacks if you need to have the
29      * datastorage (de)serialized automatically during I/O operations.
30      *
31      * @var boolean
32      */
33     var $serialized_storage = false;
34
35     /**
36      * This field contains the reason for the failed validation.
37      *
38      * The string can be safely assumed to be localized, and is only valid if a
39      * validation has failed previously. This field will be cleared prior to a
40      * new validation attempt. You may use simple inline HTML in these errors.
41      *
42      * @var string
43      */
44     var $validation_error = '';
45
46     /**
47      * This variable contains configuration data which is not directly related to the
48      * operation of the type, but required for the operation of external tools like the
49      * storage manager.
50      *
51      * The type should never touch this variable, which is controlled
52      * by a corresponding getter/setter pair.
53      *
54      * @var Array
55      * @access private
56      * @see set_external_config()
57      * @see get_external_config()
58      */
59     var $_external_config = Array();
60
61     /**
62      * The name field holds the name of the field the datatype is encapsulating.
63      *
64      * This maps to the schema's field name. You should never have to change them.
65      *
66      * @var string
67      */
68     var $name = '';
69
70     /**
71      * A reference to the storage object that this type is using.
72      *
73      * Use this for attachment management. The variable may be null until
74      * actual processing starts. It may also change during the lifetime
75      * of a type. You should therefore be careful.
76      *
77      * @var midcom_helper_datamanager2_storage
78      * @access protected
79      */
80     var $storage = null;
81
82     /**
83      * Constructor.
84      *
85      * Nothing fancy, the actual startup work is done by the initialize call.
86      */
87     function __construct()
88     {
89         $this->_component = 'midcom.helper.datamanager2';
90         parent::__construct();
91     }
92
93     /**
94      * Initializes and configures the type.
95      *
96      * @param string $name The name of the field to which this type is bound.
97      * @param Array $config The configuration data which should be used to customize the type.
98      * @param midcom_helper_datamanager2_storage &$storage A reference to the storage object to use.
99      * @return boolean Indicating success. If this is false, the type will be unusable.
100      */
101     function initialize($name, $config, &$storage)
102     {
103         $this->name = $name;
104         $this->set_storage($storage);
105
106         // Call the event handler for configuration in case we have some defaults that cannot
107         // be covered by the class initializers.
108         $this->_on_configuring($config);
109
110         // Assign the configuration values.
111         foreach ($config as $key => $value)
112         {
113             $this->$key = $value;
114         }
115
116         if (! $this->_on_initialize())
117         {
118             return false;
119         }
120
121         return true;
122     }
123
124     /**
125      * This function, is called  before the configuration keys are merged into the types
126      * configuration.
127      *
128      * @param Array $config The configuration passed to the type.
129      */
130     function _on_configuring($config) {}
131
132     /**
133      * Small helper which sets the current storage object to a new one. The
134      * object is used by-reference.
135      *
136      * @var midcom_helper_datamanager2_storage &$storage A reference to the storage object to use.
137      */
138     function set_storage(&$storage)
139     {
140         $this->storage =& $storage;
141     }
142
143     /**
144      * This event handler is called after construction, so passing references to $this to the
145      * outside is safe at this point.
146      *
147      * @return boolean Indicating success, false will abort the type construction sequence.
148      * @access protected
149      */
150     function _on_initialize()
151     {
152         return true;
153     }
154
155     /**
156      * Converts from storage format to "operational" format, which might include
157      * more information then the pure storage format.
158      *
159      * Depending on the $serialized_storage member, the framework will
160      * automatically deal with deserialization of the information.
161      *
162      * This function must be overwritten.
163      *
164      * @param mixed $source The storage data structure.
165      */
166     function convert_from_storage ($source)
167     {
168         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
169     }
170
171     /**
172      * Converts from "operational" format to from storage format.
173      *
174      * Depending on the $serialized_storage member, the framework will
175      * automatically deal with deserialization of the information.
176      *
177      * This function must be overwritten.
178      *
179      * @return mixed The data to store into the object, or null on failure.
180      */
181     function convert_to_storage()
182     {
183         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
184     }
185
186     /**
187      * Constructs the object based on its CSV representation (which is already decoded in terms
188      * of escaping.)
189      *
190      * This function must be overwritten.
191      *
192      * @param string $source The CSV representation that has to be parsed.
193      */
194     function convert_from_csv ($source)
195     {
196         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
197     }
198
199     /**
200      * Transforms the current object's state into a CSV string representation.
201      *
202      * Escaping and other encoding is done by the caller, you just return the string.
203      *
204      * This function must be overwritten.
205      *
206      * @return mixed The data to store into the object, or null on failure.
207      */
208     function convert_to_csv()
209     {
210         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
211     }
212
213     /**
214      * Transforms the current object's state into a email-friendly string representation.
215      *
216      * Escaping and other encoding is done by the caller, you just return the string.
217      *
218      * If this method is not overwritten, convert_to_csv will be used instead.
219      *
220      * @return mixed The data to store into the object, or null on failure.
221      */
222     function convert_to_email()
223     {
224         return $this->convert_to_csv();
225     }
226
227     /**
228      * Transforms the current object's state into HTML representation.
229      *
230      * This is used for displaying type contents in an automatic fashion.
231      *
232      * This function must be overwritten.
233      *
234      * @return mixed The rendered content.
235      */
236     function convert_to_html()
237     {
238         die ('The function ' . __CLASS__ . '::' . __FUNCTION__ . ' must be implemented in subclasses.');
239     }
240
241     /**
242      * Transforms the current objects' state into 'raw' representation.
243      * Usually the convert_to_storage -method returns suitable value, but in
244      * some datatypes (like privilege, blobs-based ones and tags),
245      * convert_to_storage does database IO directly and returns less useful data.
246      *
247      * @see convert_to_storage
248      * @return mixed The rendered content.
249      */
250     function convert_to_raw()
251     {
252         return $this->convert_to_storage();
253     }
254
255     /**
256      * Main validation interface, currently only calls the main type callback, but this
257      * can be extended later by a configurable callback into the component.
258      *
259      * @return boolean Indicating value validity.
260      */
261     function validate()
262     {
263         $this->validation_error = '';
264         return $this->_on_validate();
265     }
266
267     /**
268      * Type-specific validation callback, this is executed before any custom validation
269      * rules which apply through the customization interface.
270      *
271      * In case validation fails, you should assign an (already translated) error message
272      * to the validation_error public member.
273      *
274      * @access protected
275      * @return boolean Indicating value validity.
276      */
277     function _on_validate()
278     {
279         return true;
280     }
281
282     /**
283      * Gets an external configuration option referenced by its key.
284      *
285      * Besides other parts in the datamanager framework, nobody should ever
286      * have to touch this information.
287      *
288      * @param string $key The key by which this configuration option is referenced.
289      * @return mixed The configuration value, which is null if the key wasn't found.
290      */
291     function get_external_config($key)
292     {
293         if (! array_key_exists($key, $this->_external_config))
294         {
295             return null;
296         }
297         return $this->_external_config[$key];
298     }
299
300     /**
301      * Sets an external configuration option.
302      *
303      * Besides other parts in the datamanager framework, nobody should ever
304      * have to touch this information.
305      *
306      * @param string $key The key by which this configuration option is referenced.
307      * @param mixed $value The configuration value.
308      */
309     function set_external_config($key, $value)
310     {
311         $this->_external_config[$key] = $value;
312     }
313
314     /**
315      * Checks whether the current user has the given privilege on the storage backend.
316      *
317      * The storage backend is responsible for the actual execution of this operation,
318      * so this is merely a shortcut.
319      *
320      * @param string $privilege The privilege to check against.
321      * @return boolean true if the user has the permission, false otherwise.
322      */
323     function can_do($privilege)
324     {
325         return $this->storage->can_do($privilege);
326     }
327 }
328
329 ?>
Note: See TracBrowser for help on using the browser.