/
FormMacros.php
351 lines (289 loc) · 8.02 KB
/
FormMacros.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Addons and code snippets for Nette Framework. (unofficial)
*
* @author Jan Tvrdík
* @license MIT
*/
namespace JanTvrdik\Templates;
use Nette;
use Nette\Latte\DefaultMacros;
use Nette\Latte\ParseException;
/**
* Latte macros for comfortable form rendering.
*
* List of modifiers:
* ------------------
* {form ...} – class, style
* {input ...} – value, caption, size, rows, cols, placeholder, class, style
* {label ...} – text, class, style
*
* <code>
* {form registrationForm}
* <p class="error" n:foreach="$formErrors as $error">{$error}</p>
* <table>
* <tr>
* <th>{label name, text => "Username"}</th>
* <td>{input name}</td>
* </tr>
* <tr>
* <th>{label password, text => "Password"}</th>
* <td>{input password}</td>
* </tr>
* {formContainer contact}
* <tr>
* <th>{label mail, text => "E-mail"}</th>
* <td>{input mail}</td>
* </tr>
* <tr>
* <th>{label twitter, text => "Twitter"}</th>
* <td>{input twitter}</td>
* </tr>
* {/formContainer}
* <tr>
* <th></th>
* <td>{input register, caption => "Register"}</td>
* </tr>
* </table>
* {/form}
* </code>
*
* @author Jan Marek, Jan Tvrdík, Daniel Robenek
* @version 2.2
* @link http://nette.merxes.cz/form-macros/
*/
class FormMacros
{
/** @var DefaultMacros */
protected static $latte;
/** @var array # => Nette\Forms\FormContainer */
private static $containerStack;
/**
* Static class – cannot be instantiated.
*/
final public function __construct()
{
throw new \LogicException('Cannot instantiate static class ' . get_class($this));
}
/**
* Registers form macros.
*
* @return void
*/
public static function register(DefaultMacros $latte)
{
$latte->macros['form'] = '<?php %' . __CLASS__ . '::macroBegin% ?>';
$latte->macros['/form'] = '<?php ' . __CLASS__ . '::endForm() ?>';
$latte->macros['formContainer'] = '<?php %' . __CLASS__ . '::macroBeginContainer% ?>';
$latte->macros['/formContainer'] = '<?php ' . __CLASS__ . '::endContainer() ?>';
$latte->macros['input'] = '<?php %' . __CLASS__ . '::macroInput% ?>';
$latte->macros['label'] = '<?php %' . __CLASS__ . '::macroLabel% ?>';
$latte->macros['inputValue'] = '<?php %' . __CLASS__ . '::macroInputValue% ?>';
self::$latte = $latte;
}
/**
* Returns current form.
*
* @return Nette\Forms\Form
*/
public static function getForm()
{
return self::$containerStack[0];
}
/**
* {form ...}
*
* @param string
* @return string
*/
public static function macroBegin($content)
{
list($name, $modifiers) = self::fetchNameAndModifiers($content);
return '$formErrors = ' . __CLASS__ . "::beginForm($name, \$control, $modifiers)->getErrors()";
}
/**
* Helper for {form ...} macro.
*
* @param Nette\Forms\Form|string form instance or form name in given control
* @param Nette\Application\UI\PresenterComponent
* @param array list of modifiers (name => value)
* @return Nette\Forms\Form
*/
public static function beginForm($form, Nette\Application\UI\PresenterComponent $control, array $modifiers = NULL)
{
$form = ($form instanceof Nette\Forms\Form ? $form : $control->getComponent($form));
self::$containerStack = array($form);
if ($modifiers) self::addAttributes($form->getElementPrototype(), $modifiers, array('class', 'style'));
$form->render('begin');
return $form;
}
/**
* Helper for {/form} macro.
*
* @return void
* @throws ParseException if some containers remain unclosed
*/
public static function endForm()
{
if (count(self::$containerStack) > 1) {
throw new ParseException('There are some unclosed containers.');
}
self::getForm()->render('end');
self::$containerStack = array();
}
/**
* {formContainer ...}
*
* @param string
* @return string
*/
public static function macroBeginContainer($content)
{
$name = self::$latte->formatString(self::$latte->fetchToken($content));
return __CLASS__ . '::beginContainer(' . $name . ')';
}
/**
* Helper for {formContainer ...} macro.
*
* @param string container name
* @return void
* @throws ParseException if container is not Nette\Forms\FormContainer instance
*/
public static function beginContainer($name)
{
$container = end(self::$containerStack)->getComponent($name);
if (!$container instanceof Nette\Forms\Container) {
throw new ParseException('Form container must be instance of Nette\Forms\FormContainer.');
}
self::$containerStack[] = $container;
}
/**
* Helper for {/formContainer} macro.
*
* @return void
* @throws ParseException if there is no container to close
*/
public static function endContainer()
{
if (count(self::$containerStack) < 2) {
throw new ParseException('Trying to close container which is not open.');
}
array_pop(self::$containerStack);
}
/**
* {input ...}
*
* @param string
* @return string
*/
public static function macroInput($content)
{
list($name, $modifiers) = self::fetchNameAndModifiers($content);
return __CLASS__ . "::input($name, $modifiers)";
}
/**
* Helper for {input ...} macro.
*
* @param string input name
* @param array list of modifiers (name => value)
* @return void
*/
public static function input($name, array $modifiers = NULL)
{
$input = self::getControl($name)->getControl();
if ($modifiers) {
self::addAttributes($input, $modifiers, array('value', 'size', 'rows', 'cols', 'placeholder', 'class', 'style'));
if (isset($modifiers['caption'])) $input->value = $modifiers['caption'];
}
echo $input;
}
/**
* {label ...}
*
* @param string
* @return string
*/
public static function macroLabel($content)
{
list($name, $modifiers) = self::fetchNameAndModifiers($content);
return __CLASS__ . "::label($name, $modifiers)";
}
/**
* Helper for {label ...} macro.
*
* @param string input name
* @param array list of modifiers (name => value)
* @return void
*/
public static function label($name, array $modifiers = NULL)
{
$label = self::getControl($name)->getLabel();
if ($modifiers) {
self::addAttributes($label, $modifiers, array('class', 'style'));
if (isset($modifiers['text'])) $label->setText($modifiers['text']);
}
echo $label;
}
/**
* {inputValue ...}
*
* @param string
* @return string
*/
public static function macroInputValue($content)
{
$name = self::$latte->formatString(self::$latte->fetchToken($content));
return __CLASS__ . "::inputValue($name)";
}
/**
* Helper for {inputValue ...} macro.
*
* @param string input name
* @return void
*/
public static function inputValue($name)
{
echo self::getControl($name)->getValue();
}
/**
* Returns form control.
*
* @param string input name
* @return Nette\Forms\IControl
*/
protected static function getControl($name)
{
return end(self::$containerStack)->getComponent($name);
}
/**
* Parses given string a returns formatted name and modifiers as array.
*
* @param string
* @return array 0 => name, 1 => modifiers
*/
protected static function fetchNameAndModifiers($code)
{
$name = self::$latte->formatString(self::$latte->fetchToken($code));
$modifiers = self::$latte->formatArray($code) ?: 'array()';
return array($name, $modifiers);
}
/**
* Adds allowed attributes to given element.
*
* @param Nette\Utils\Html
* @param array list of attributes (name => value)
* @param array list of allowed attributes (# => name)
* @return void
*/
protected static function addAttributes(Nette\Utils\Html $el, array $attributes, array $allowedAttributes)
{
foreach ($attributes as $attribute => $value) {
if (!in_array($attribute, $allowedAttributes)) continue;
if (is_array($el->$attribute)) {
$el->{$attribute}[] = $value;
} else {
$el->$attribute = $value;
}
}
}
}