Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: element.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
21
 
22
require_once('HTML/Common.php');
23
 
24
/**
25
 * Base class for form elements
26
 *
27
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
28
 * @author       Bertrand Mansion <bmansion@mamasam.com>
29
 * @version      1.3
30
 * @since        PHP4.04pl1
31
 * @access       public
32
 * @abstract
33
 */
34
class HTML_QuickForm_element extends HTML_Common
35
{
36
    // {{{ properties
37
 
38
    /**
39
     * Label of the field
40
     * @var       string
41
     * @since     1.3
42
     * @access    private
43
     */
44
    var $_label = '';
45
 
46
    /**
47
     * Form element type
48
     * @var       string
49
     * @since     1.0
50
     * @access    private
51
     */
52
    var $_type = '';
53
 
54
    /**
55
     * Flag to tell if element is frozen
56
     * @var       boolean
57
     * @since     1.0
58
     * @access    private
59
     */
60
    var $_flagFrozen = false;
61
 
62
    /**
63
     * Does the element support persistant data when frozen
64
     * @var       boolean
65
     * @since     1.3
66
     * @access    private
67
     */
68
    var $_persistantFreeze = false;
69
 
70
    // }}}
71
    // {{{ constructor
72
 
73
    /**
74
     * Class constructor
75
     *
76
     * @param    string     Name of the element
77
     * @param    mixed      Label(s) for the element
78
     * @param    mixed      Associative array of tag attributes or HTML attributes name="value" pairs
79
     * @since     1.0
80
     * @access    public
81
     * @return    void
82
     */
83
    function HTML_QuickForm_element($elementName=null, $elementLabel=null, $attributes=null)
84
    {
85
        HTML_Common::HTML_Common($attributes);
86
        if (isset($elementName)) {
87
            $this->setName($elementName);
88
        }
89
        if (isset($elementLabel)) {
90
            $this->setLabel($elementLabel);
91
        }
92
    } //end constructor
93
 
94
    // }}}
95
    // {{{ apiVersion()
96
 
97
    /**
98
     * Returns the current API version
99
     *
100
     * @since     1.0
101
     * @access    public
102
     * @return    float
103
     */
104
    function apiVersion()
105
    {
106
        return 2.0;
107
    } // end func apiVersion
108
 
109
    // }}}
110
    // {{{ getType()
111
 
112
    /**
113
     * Returns element type
114
     *
115
     * @since     1.0
116
     * @access    public
117
     * @return    string
118
     */
119
    function getType()
120
    {
121
        return $this->_type;
122
    } // end func getType
123
 
124
    // }}}
125
    // {{{ setName()
126
 
127
    /**
128
     * Sets the input field name
129
     *
130
     * @param     string    $name   Input field name attribute
131
     * @since     1.0
132
     * @access    public
133
     * @return    void
134
     */
135
    function setName($name)
136
    {
137
        // interface method
138
    } //end func setName
139
 
140
    // }}}
141
    // {{{ getName()
142
 
143
    /**
144
     * Returns the element name
145
     *
146
     * @since     1.0
147
     * @access    public
148
     * @return    string
149
     */
150
    function getName()
151
    {
152
        // interface method
153
    } //end func getName
154
 
155
    // }}}
156
    // {{{ setValue()
157
 
158
    /**
159
     * Sets the value of the form element
160
     *
161
     * @param     string    $value      Default value of the form element
162
     * @since     1.0
163
     * @access    public
164
     * @return    void
165
     */
166
    function setValue($value)
167
    {
168
        // interface
169
    } // end func setValue
170
 
171
    // }}}
172
    // {{{ getValue()
173
 
174
    /**
175
     * Returns the value of the form element
176
     *
177
     * @since     1.0
178
     * @access    public
179
     * @return    mixed
180
     */
181
    function getValue()
182
    {
183
        // interface
184
        return null;
185
    } // end func getValue
186
 
187
    // }}}
188
    // {{{ freeze()
189
 
190
    /**
191
     * Freeze the element so that only its value is returned
192
     *
193
     * @access    public
194
     * @return    void
195
     */
196
    function freeze()
197
    {
198
        $this->_flagFrozen = true;
199
    } //end func freeze
200
 
201
    // }}}
202
    // {{{ unfreeze()
203
 
204
   /**
205
    * Unfreezes the element so that it becomes editable
206
    *
207
    * @access public
208
    * @return void
209
    * @since  3.2.4
210
    */
211
    function unfreeze()
212
    {
213
        $this->_flagFrozen = false;
214
    }
215
 
216
    // }}}
217
    // {{{ getFrozenHtml()
218
 
219
    /**
220
     * Returns the value of field without HTML tags
221
     *
222
     * @since     1.0
223
     * @access    public
224
     * @return    string
225
     */
226
    function getFrozenHtml()
227
    {
228
        $value = $this->getValue();
229
        return ('' != $value? htmlspecialchars($value): '&nbsp;') .
230
               $this->_getPersistantData();
231
    } //end func getFrozenHtml
232
 
233
    // }}}
234
    // {{{ _getPersistantData()
235
 
236
   /**
237
    * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
238
    *
239
    * @access private
240
    * @return string
241
    */
242
    function _getPersistantData()
243
    {
244
        if (!$this->_persistantFreeze) {
245
            return '';
246
        } else {
247
            $id = $this->getAttribute('id');
248
            return '<input type="hidden"' .
249
                   (isset($id)? ' id="' . $id . '"': '') .
250
                   ' name="' . $this->getName() . '"' .
251
                   ' value="' . htmlspecialchars($this->getValue()) . '" />';
252
        }
253
    }
254
 
255
    // }}}
256
    // {{{ isFrozen()
257
 
258
    /**
259
     * Returns whether or not the element is frozen
260
     *
261
     * @since     1.3
262
     * @access    public
263
     * @return    bool
264
     */
265
    function isFrozen()
266
    {
267
        return $this->_flagFrozen;
268
    } // end func isFrozen
269
 
270
    // }}}
271
    // {{{ setPersistantFreeze()
272
 
273
    /**
274
     * Sets wether an element value should be kept in an hidden field
275
     * when the element is frozen or not
276
     *
277
     * @param     bool    $persistant   True if persistant value
278
     * @since     2.0
279
     * @access    public
280
     * @return    void
281
     */
282
    function setPersistantFreeze($persistant=false)
283
    {
284
        $this->_persistantFreeze = $persistant;
285
    } //end func setPersistantFreeze
286
 
287
    // }}}
288
    // {{{ setLabel()
289
 
290
    /**
291
     * Sets display text for the element
292
     *
293
     * @param     string    $label  Display text for the element
294
     * @since     1.3
295
     * @access    public
296
     * @return    void
297
     */
298
    function setLabel($label)
299
    {
300
        $this->_label = $label;
301
    } //end func setLabel
302
 
303
    // }}}
304
    // {{{ getLabel()
305
 
306
    /**
307
     * Returns display text for the element
308
     *
309
     * @since     1.3
310
     * @access    public
311
     * @return    string
312
     */
313
    function getLabel()
314
    {
315
        return $this->_label;
316
    } //end func getLabel
317
 
318
    // }}}
319
    // {{{ _findValue()
320
 
321
    /**
322
     * Tries to find the element value from the values array
323
     *
324
     * @since     2.7
325
     * @access    private
326
     * @return    mixed
327
     */
328
    function _findValue(&$values)
329
    {
330
        if (empty($values)) {
331
            return null;
332
        }
333
        $elementName = $this->getName();
334
        if (isset($values[$elementName])) {
335
            return $values[$elementName];
336
        } elseif (strpos($elementName, '[')) {
337
            $myVar = "['" . str_replace(array(']', '['), array('', "']['"), $elementName) . "']";
338
            return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
339
        } else {
340
            return null;
341
        }
342
    } //end func _findValue
343
 
344
    // }}}
345
    // {{{ onQuickFormEvent()
346
 
347
    /**
348
     * Called by HTML_QuickForm whenever form event is made on this element
349
     *
350
     * @param     string    $event  Name of event
351
     * @param     mixed     $arg    event arguments
352
     * @param     object    $caller calling object
353
     * @since     1.0
354
     * @access    public
355
     * @return    void
356
     */
357
    function onQuickFormEvent($event, $arg, &$caller)
358
    {
359
        switch ($event) {
360
            case 'createElement':
361
                $className = get_class($this);
362
                $this->$className($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
363
                break;
364
            case 'addElement':
365
                $this->onQuickFormEvent('createElement', $arg, $caller);
366
                $this->onQuickFormEvent('updateValue', null, $caller);
367
                break;
368
            case 'updateValue':
369
                // constant values override both default and submitted ones
370
                // default values are overriden by submitted
371
                $value = $this->_findValue($caller->_constantValues);
372
                if (null === $value) {
373
                    $value = $this->_findValue($caller->_submitValues);
374
                    if (null === $value) {
375
                        $value = $this->_findValue($caller->_defaultValues);
376
                    }
377
                }
378
                if (null !== $value) {
379
                    $this->setValue($value);
380
                }
381
                break;
382
            case 'setGroupValue':
383
                $this->setValue($arg);
384
        }
385
        return true;
386
    } // end func onQuickFormEvent
387
 
388
    // }}}
389
    // {{{ accept()
390
 
391
   /**
392
    * Accepts a renderer
393
    *
394
    * @param object     An HTML_QuickForm_Renderer object
395
    * @param bool       Whether an element is required
396
    * @param string     An error message associated with an element
397
    * @access public
398
    * @return void
399
    */
400
    function accept(&$renderer, $required=false, $error=null)
401
    {
402
        $renderer->renderElement($this, $required, $error);
403
    } // end func accept
404
 
405
    // }}}
406
    // {{{ _generateId()
407
 
408
   /**
409
    * Automatically generates and assigns an 'id' attribute for the element.
410
    *
411
    * Currently used to ensure that labels work on radio buttons and
412
    * checkboxes. Per idea of Alexander Radivanovich.
413
    *
414
    * @access private
415
    * @return void
416
    */
417
    function _generateId()
418
    {
419
        static $idx = 1;
420
 
421
        if (!$this->getAttribute('id')) {
422
            $this->updateAttributes(array('id' => 'qf_' . substr(md5(microtime() . $idx++), 0, 6)));
423
        }
424
    } // end func _generateId
425
 
426
    // }}}
427
    // {{{ exportValue()
428
 
429
   /**
430
    * Returns a 'safe' element's value
431
    *
432
    * @param  array   array of submitted values to search
433
    * @param  bool    whether to return the value as associative array
434
    * @access public
435
    * @return mixed
436
    */
437
    function exportValue(&$submitValues, $assoc = false)
438
    {
439
        $value = $this->_findValue($submitValues);
440
        if (null === $value) {
441
            $value = $this->getValue();
442
        }
443
        return $this->_prepareValue($value, $assoc);
444
    }
445
 
446
    // }}}
447
    // {{{ _prepareValue()
448
 
449
   /**
450
    * Used by exportValue() to prepare the value for returning
451
    *
452
    * @param  mixed   the value found in exportValue()
453
    * @param  bool    whether to return the value as associative array
454
    * @access private
455
    * @return mixed
456
    */
457
    function _prepareValue($value, $assoc)
458
    {
459
        if (null === $value) {
460
            return null;
461
        } elseif (!$assoc) {
462
            return $value;
463
        } else {
464
            $name = $this->getName();
465
            if (!strpos($name, '[')) {
466
                return array($name => $value);
467
            } else {
468
                $valueAry = array();
469
                $myIndex  = "['" . str_replace(array(']', '['), array('', "']['"), $name) . "']";
470
                eval("\$valueAry$myIndex = \$value;");
471
                return $valueAry;
472
            }
473
        }
474
    }
475
 
476
    // }}}
477
} // end class HTML_QuickForm_element
478
?>