PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / libs / HTML / QuickForm2 / Renderer / Smarty.php
matomo / app / libs / HTML / QuickForm2 / Renderer Last commit date
Array.php 6 years ago Default.php 6 years ago Plugin.php 6 years ago Proxy.php 6 years ago Smarty.php 6 years ago
Smarty.php
293 lines
1 <?php
2 /**
3 * A renderer for HTML_QuickForm2 suitable for using with the Smarty template engine.
4 * See: http://www.smarty.net/
5 *
6 * PHP version 5
7 *
8 * LICENSE:
9 *
10 * Copyright (c) 2009, Alain D D Williams <addw@phcomp.co.uk>
11 * Based on the QuickForm2 Array renderer.
12 *
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * The names of the authors may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
35 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * @category HTML
40 * @package HTML_QuickForm2
41 * @author Alain D D Williams <addw@phcomp.co.uk>
42 * @license http://opensource.org/licenses/bsd-license.php New BSD License
43 * @version SCCS: %W% %G% %U%
44 * @link http://pear.php.net/package/HTML_QuickForm2
45 */
46
47 /**
48 * This generates an array, bring in the array renderer and extend it a bit:
49 */
50 // require_once 'HTML/QuickForm2/Renderer/Array.php';
51
52 /**
53 * A renderer for HTML_QuickForm2 building an array of form elements
54 *
55 * Based on Array renderer from HTML_QuickForm 3.x package
56 *
57 * The form array structure is the following:
58 * <pre>
59 * array(
60 * 'id' => form's "id" attribute (string),
61 * 'frozen' => whether the form is frozen (bool),
62 * 'attributes' => attributes for &lt;form&gt; tag (string),
63 * // if form contains required elements:
64 * 'required_note' => note about the required elements (string),
65 * 'requirednote' => note about the required elements (string),
66 * NB: no '_' in the middle
67 * In old_compat this is a span style="font-size:80%;"
68 * with the '*' also color:#ff0000;
69 * Not old_compat it is in a div class="reqnote"
70 * // if 'group_hiddens' option is true:
71 * 'hidden' => array with html of hidden elements (array),
72 * // if 'group_errors' option is true:
73 * 'errors' => array(
74 * '1st element id' => 'Error for the 1st element',
75 * ...
76 * 'nth element id' => 'Error for the nth element'
77 * ),
78 * 'elements' => array(
79 * element_1,
80 * ...
81 * element_N
82 * )
83 * '1st_elements_name' => array for the 1st element,
84 * ... references into 'elements' above
85 * 'nth_elements_name' => array for the nth element,
86 * )
87 * );
88 * </pre>
89 * Where element_i is an array of the form
90 * <pre>
91 * array(
92 * 'id' => element id (string),
93 * 'name' => element name (string),
94 * 'type' => type of the element (string),
95 * 'frozen' => whether element is frozen (bool),
96 * // if element has a label:
97 * 'label' => 'label for the element',
98 * // note that if 'static_labels' option is true and element's label is an
99 * // array then there will be several 'label_*' keys corresponding to
100 * // labels' array keys
101 * 'required' => whether element is required (bool),
102 * // if a validation error is present and 'group_errors' option is false:
103 * 'error' => error associated with the element (string),
104 * // if some style was associated with an element:
105 * 'style' => 'some information about element style (e.g. for Smarty)',
106 *
107 * // if element is not a Container
108 * 'value' => element value (mixed),
109 * 'html' => HTML for the element (string),
110 *
111 * // if element is a Container
112 * 'attributes' => container attributes (string)
113 * // only for groups, if separator is set:
114 * 'separator' => separator for group elements (mixed),
115 * 'elements' => array(
116 * element_1,
117 * ...
118 * element_N
119 * )
120 *
121 * If the type is 'radio' an element (type = 'radio') is created for each choice that the user has,
122 * keyed by the 'id' value. An 'element' will be created having an array with one element key '0'.
123 * The 'id' above will be set to the value in 'name'.
124 * The 'type' of each element will be 'radio'
125 * );
126 * </pre>
127 *
128 * The following additional options are available:
129 * <ul>
130 * <li>'old_compat' - generate something compatible with an old renderer</li>
131 * <li>'key_id' - the key to elements is the field's 'id' rather than 'name'</li>
132 * </ul>
133 *
134 * While almost everything in this class is defined as public, its properties
135 * and those methods that are not published (i.e. not in array returned by
136 * exportMethods()) will be available to renderer plugins only.
137 *
138 * The following methods are published:
139 * - {@link reset()}
140 * - {@link toArray()}
141 * - {@link setStyleForId()}
142 *
143 * @category HTML
144 * @package HTML_QuickForm2
145 * @author Alain D D Williams <addw@phcomp.co.uk>
146 * @version Release: SCCS: %W% %G% %U%
147 */
148 class HTML_QuickForm2_Renderer_Smarty extends HTML_QuickForm2_Renderer_Array
149 {
150 /**
151 * Constructor, adds new options
152 */
153 protected function __construct()
154 {
155 parent::__construct();
156 $this->options += array(
157 'old_compat' => false,
158 'key_id' => false,
159 );
160 }
161
162 /**
163 * Creates an array with fields that are common to all elements
164 *
165 * @param HTML_QuickForm2_Node Element being rendered
166 *
167 * @return array
168 */
169 public function buildCommonFields(HTML_QuickForm2_Node $element)
170 {
171 $keyn = $this->options['key_id'] ? 'id' : 'name';
172
173 $ary = array(
174 'id' => $element->getId(),
175 'frozen' => $element->toggleFrozen(),
176 'name' => $element->getName(),
177 );
178
179 // Key that we use for putting into arrays so that smarty can extract them.
180 // Note that the name may be empty.
181 $key_val = $ary[$keyn];
182 if($key_val == '')
183 $key_val = $ary['id'];
184
185 if ($labels = $element->getLabel()) {
186 if (!is_array($labels) || !$this->options['static_labels']) {
187 $ary['label'] = $labels;
188 } else {
189 foreach ($labels as $key => $label) {
190 $key = is_int($key)? $key + 1: $key;
191 if (1 === $key) {
192 $ary['label'] = $label;
193 } else {
194 $ary['label_' . $key] = $label;
195 }
196 }
197 }
198 }
199
200 // Smarty: group_errors under 'name' or 'id' depending on key_id option:
201 if (($error = $element->getError()) && $this->options['group_errors']) {
202 $this->array['errors'][$key_val] = $error;
203 } elseif ($error) {
204 $ary['error'] = $error;
205 }
206 if (isset($this->styles[$key_val])) {
207 $ary['style'] = $this->styles[$key_val];
208 }
209 if (!$element instanceof HTML_QuickForm2_Container) {
210 $ary['html'] = $element->__toString();
211 } else {
212 $ary['elements'] = array();
213 $ary['attributes'] = $element->getAttributes(true);
214 }
215 return $ary;
216 }
217
218 public function startForm(HTML_QuickForm2_Node $form)
219 {
220 if($this->options['old_compat'])
221 $this->options['group_hiddens'] = true;
222
223 parent::startForm($form);
224 }
225
226 public function finishForm(HTML_QuickForm2_Node $form)
227 {
228 parent::finishForm($form);
229
230 if ($this->hasRequired) {
231
232 // Create element 'requirednote' - note no '_'
233 if($this->options['old_compat']) {
234 // Old QuickForm had the requirednote styled & a different name:
235 $this->array['requirednote'] = preg_replace('|<em>([^<]+)</em>(.*)|',
236 '<span style="font-size:80%; color:#ff0000;">$1</span><span style="font-size:80%;">$2</span>',
237 $this->options['required_note']);
238 } else {
239 $this->array['requirednote'] = '<div class="reqnote">'. $this->options['required_note'] . '</div>';
240 }
241 }
242
243 // Create top level elements keyed by form field 'name' or 'id'
244 if(isset($this->array['elements']['0']))
245 $this->linkToLevelAbove($this->array, $this->array['elements']);
246
247 // For compat: it is expected that 'hidden' is a string, not an array:
248 if($this->options['old_compat'] && isset($this->array['hidden']) && is_array($this->array['hidden']))
249 $this->array['hidden'] = join(' ', $this->array['hidden']);
250 }
251
252 // Look through the elements (numerically indexed) array, make fields
253 // members of the level above. This is so that they can be easily accessed by smarty templates.
254 // If we find a group, recurse down. Used for smarty only.
255 // Key is 'name' or 'id'.
256 private function linkToLevelAbove(&$top, $elements, $inGroup = false)
257 {
258 $key = $this->options['key_id'] ? 'id' : 'name';
259
260 foreach($elements as &$elem) {
261 $top_key = $elem[$key];
262
263 // If in a group, convert something like inGrp[F4grp][F4_1] to F4_1
264 // Don't do if key_id as the value is a straight id.
265 if( !$this->options['key_id'] && $inGroup && $top_key != '') {
266 if(!(preg_match("/\[?([\w_]+)\]?$/i", $top_key, $match)))
267 throw new HTML_QuickForm2_InvalidArgumentException(
268 "linkToLevelAbove can't obtain the name from '$top_key'");
269 $top_key = $match[1];
270 }
271
272 // Radio buttons: several elements with the same name, make an array
273 if(isset($elem['type']) && $elem['type'] == 'radio') {
274 if( ! isset($top[$top_key]))
275 $top[$top_key] = array('id' => $top_key, 'type' => 'radio', 'elements' => array(0));
276 $top[$top_key][$elem['id']] = &$elem;
277 } else // Normal field, just link into the level above.
278 if( ! isset($top[$top_key]))
279 $top[$top_key] = &$elem; // Link into the level above
280
281 // If we have a group link its fields up to this level:
282 if(isset($elem['elements']['0']))
283 $this->linkToLevelAbove($elem, $elem['elements'], true);
284
285 // Link errors to the top level:
286 if(isset($elem['error']) && isset($this->array[$elem['error']]))
287 $this->array['errors'][$top_key] = $this->array[$elem['error']];
288 }
289 }
290
291 /**#@-*/
292 }
293