Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 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: Alexey Borzov <borz_off@cs.msu.su>                          |
17
// |          Adam Daniel <adaniel1@eesus.jnj.com>                        |
18
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
19
// |          Thomas Schulz <ths@4bconsult.de>                            |
20
// +----------------------------------------------------------------------+
21
//
22
// $Id$
23
 
24
require_once 'HTML/QuickForm/Renderer.php';
25
 
26
/**
27
 * A concrete renderer for HTML_QuickForm, makes an array of form contents
28
 *
29
 * Based on old toArray() code.
30
 *
31
 * The form array structure is the following:
32
 * array(
33
 *   'frozen'           => 'whether the form is frozen',
34
 *   'javascript'       => 'javascript for client-side validation',
35
 *   'attributes'       => 'attributes for <form> tag',
36
 *   'requirednote      => 'note about the required elements',
37
 *   // if we set the option to collect hidden elements
38
 *   'hidden'           => 'collected html of all hidden elements',
39
 *   // if there were some validation errors:
40
 *   'errors' => array(
41
 *     '1st element name' => 'Error for the 1st element',
42
 *     ...
43
 *     'nth element name' => 'Error for the nth element'
44
 *   ),
45
 *   // if there are no headers in the form:
46
 *   'elements' => array(
47
 *     element_1,
48
 *     ...
49
 *     element_N
50
 *   )
51
 *   // if there are headers in the form:
52
 *   'sections' => array(
53
 *     array(
54
 *       'header'   => 'Header text for the first header',
55
 *       'name'     => 'Header name for the first header',
56
 *       'elements' => array(
57
 *          element_1,
58
 *          ...
59
 *          element_K1
60
 *       )
61
 *     ),
62
 *     ...
63
 *     array(
64
 *       'header'   => 'Header text for the Mth header',
65
 *       'name'     => 'Header name for the Mth header',
66
 *       'elements' => array(
67
 *          element_1,
68
 *          ...
69
 *          element_KM
70
 *       )
71
 *     )
72
 *   )
73
 * );
74
 *
75
 * where element_i is an array of the form:
76
 * array(
77
 *   'name'      => 'element name',
78
 *   'value'     => 'element value',
79
 *   'type'      => 'type of the element',
80
 *   'frozen'    => 'whether element is frozen',
81
 *   'label'     => 'label for the element',
82
 *   'required'  => 'whether element is required',
83
 *   'error'     => 'error associated with the element',
84
 *   'style'     => 'some information about element style (e.g. for Smarty)',
85
 *   // if element is not a group
86
 *   'html'      => 'HTML for the element'
87
 *   // if element is a group
88
 *   'separator' => 'separator for group elements',
89
 *   'elements'  => array(
90
 *     element_1,
91
 *     ...
92
 *     element_N
93
 *   )
94
 * );
95
 *
96
 * @access public
97
 */
98
class HTML_QuickForm_Renderer_Array extends HTML_QuickForm_Renderer
99
{
100
   /**
101
    * An array being generated
102
    * @var array
103
    */
104
    var $_ary;
105
 
106
   /**
107
    * Number of sections in the form (i.e. number of headers in it)
108
    * @var integer
109
    */
110
    var $_sectionCount;
111
 
112
   /**
113
    * Current section number
114
    * @var integer
115
    */
116
    var $_currentSection;
117
 
118
   /**
119
    * Array representing current group
120
    * @var array
121
    */
122
    var $_currentGroup = null;
123
 
124
   /**
125
    * Additional style information for different elements
126
    * @var array
127
    */
128
    var $_elementStyles = array();
129
 
130
   /**
131
    * true: collect all hidden elements into string; false: process them as usual form elements
132
    * @var bool
133
    */
134
    var $_collectHidden = false;
135
 
136
   /**
137
    * true:  render an array of labels to many labels, $key 0 named 'label', the rest "label_$key"
138
    * false: leave labels as defined
139
    * @var bool
140
    */
141
    var $_staticLabels = false;
142
 
143
   /**
144
    * Constructor
145
    *
146
    * @param  bool    true: collect all hidden elements into string; false: process them as usual form elements
147
    * @param  bool    true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key"
148
    * @access public
149
    */
150
    public function __construct($collectHidden = false, $staticLabels = false) {
151
        parent::__construct();
152
        $this->_collectHidden = $collectHidden;
153
        $this->_staticLabels  = $staticLabels;
154
    } // end constructor
155
 
156
    /**
157
     * Old syntax of class constructor. Deprecated in PHP7.
158
     *
159
     * @deprecated since Moodle 3.1
160
     */
161
    public function HTML_QuickForm_Renderer_Array($collectHidden = false, $staticLabels = false) {
162
        debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
163
        self::__construct($collectHidden, $staticLabels);
164
    }
165
 
166
   /**
167
    * Returns the resultant array
168
    *
169
    * @access public
170
    * @return array
171
    */
172
    function toArray()
173
    {
174
        return $this->_ary;
175
    }
176
 
177
 
178
    function startForm(&$form)
179
    {
180
        $this->_ary = array(
181
            'frozen'            => $form->isFrozen(),
182
            'javascript'        => $form->getValidationScript(),
183
            'attributes'        => $form->getAttributes(true),
184
            'requirednote'      => $form->getRequiredNote(),
185
            'errors'            => array()
186
        );
187
        if ($this->_collectHidden) {
188
            $this->_ary['hidden'] = '';
189
        }
190
        $this->_currentSection = null;
191
        $this->_sectionCount   = 0;
192
    } // end func startForm
193
 
194
 
195
    function renderHeader(&$header)
196
    {
197
        $this->_ary['sections'][$this->_sectionCount] = array(
198
            'header' => $header->toHtml(),
199
            'name'   => $header->getName()
200
        );
201
        $this->_currentSection = $this->_sectionCount++;
202
    } // end func renderHeader
203
 
204
 
205
    function renderElement(&$element, $required, $error)
206
    {
207
        $elAry = $this->_elementToArray($element, $required, $error);
208
        if (!empty($error)) {
209
            $this->_ary['errors'][$elAry['name']] = $error;
210
        }
211
        $this->_storeArray($elAry);
212
    } // end func renderElement
213
 
214
 
215
    function renderHidden(&$element)
216
    {
217
        if ($this->_collectHidden) {
218
            $this->_ary['hidden'] .= $element->toHtml() . "\n";
219
        } else {
220
            $this->renderElement($element, false, null);
221
        }
222
    } // end func renderHidden
223
 
224
 
225
    function startGroup(&$group, $required, $error)
226
    {
227
        $this->_currentGroup = $this->_elementToArray($group, $required, $error);
228
        if (!empty($error)) {
229
            $this->_ary['errors'][$this->_currentGroup['name']] = $error;
230
        }
231
    } // end func startGroup
232
 
233
 
234
    function finishGroup(&$group)
235
    {
236
        $this->_storeArray($this->_currentGroup);
237
        $this->_currentGroup = null;
238
    } // end func finishGroup
239
 
240
 
241
   /**
242
    * Creates an array representing an element
243
    *
244
    * @access private
245
    * @param  object    An HTML_QuickForm_element object
246
    * @param  bool      Whether an element is required
247
    * @param  string    Error associated with the element
248
    * @return array
249
    */
250
    function _elementToArray(&$element, $required, $error)
251
    {
252
        $ret = array(
253
            'name'      => $element->getName(),
254
            'value'     => $element->getValue(),
255
            'type'      => $element->getType(),
256
            'frozen'    => $element->isFrozen(),
257
            'required'  => $required,
258
            'error'     => $error
259
        );
260
        // render label(s)
261
        $labels = $element->getLabel();
262
        if (is_array($labels) && $this->_staticLabels) {
263
            foreach($labels as $key => $label) {
264
                $key = is_int($key)? $key + 1: $key;
265
                if (1 === $key) {
266
                    $ret['label'] = $label;
267
                } else {
268
                    $ret['label_' . $key] = $label;
269
                }
270
            }
271
        } else {
272
            $ret['label'] = $labels;
273
        }
274
 
275
        // set the style for the element
276
        if (isset($this->_elementStyles[$ret['name']])) {
277
            $ret['style'] = $this->_elementStyles[$ret['name']];
278
        }
279
        if ('group' == $ret['type']) {
280
            $ret['separator'] = $element->_separator;
281
            $ret['elements']  = array();
282
        } else {
283
            $ret['html']      = $element->toHtml();
284
        }
285
        return $ret;
286
    }
287
 
288
 
289
   /**
290
    * Stores an array representation of an element in the form array
291
    *
292
    * @access private
293
    * @param array  Array representation of an element
294
    * @return void
295
    */
296
    function _storeArray($elAry)
297
    {
298
        // where should we put this element...
299
        if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
300
            $this->_currentGroup['elements'][] = $elAry;
301
        } elseif (isset($this->_currentSection)) {
302
            $this->_ary['sections'][$this->_currentSection]['elements'][] = $elAry;
303
        } else {
304
            $this->_ary['elements'][] = $elAry;
305
        }
306
    }
307
 
308
 
309
   /**
310
    * Sets a style to use for element rendering
311
    *
312
    * @param mixed      element name or array ('element name' => 'style name')
313
    * @param string     style name if $elementName is not an array
314
    * @access public
315
    * @return void
316
    */
317
    function setElementStyle($elementName, $styleName = null)
318
    {
319
        if (is_array($elementName)) {
320
            $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
321
        } else {
322
            $this->_elementStyles[$elementName] = $styleName;
323
        }
324
    }
325
}
326
?>