1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Forms\Rendering;
13:
14: use Nette,
15: Nette\Utils\Html;
16:
17:
18:
19: 20: 21: 22: 23:
24: class DefaultFormRenderer extends Nette\Object implements Nette\Forms\IFormRenderer
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: public $wrappers = array(
64: 'form' => array(
65: 'container' => NULL,
66: 'errors' => TRUE,
67: ),
68:
69: 'error' => array(
70: 'container' => 'ul class=error',
71: 'item' => 'li',
72: ),
73:
74: 'group' => array(
75: 'container' => 'fieldset',
76: 'label' => 'legend',
77: 'description' => 'p',
78: ),
79:
80: 'controls' => array(
81: 'container' => 'table',
82: ),
83:
84: 'pair' => array(
85: 'container' => 'tr',
86: '.required' => 'required',
87: '.optional' => NULL,
88: '.odd' => NULL,
89: ),
90:
91: 'control' => array(
92: 'container' => 'td',
93: '.odd' => NULL,
94:
95: 'errors' => FALSE,
96: 'description' => 'small',
97: 'requiredsuffix' => '',
98:
99: '.required' => 'required',
100: '.text' => 'text',
101: '.password' => 'text',
102: '.file' => 'text',
103: '.submit' => 'button',
104: '.image' => 'imagebutton',
105: '.button' => 'button',
106: ),
107:
108: 'label' => array(
109: 'container' => 'th',
110: 'suffix' => NULL,
111: 'requiredsuffix' => '',
112: ),
113:
114: 'hidden' => array(
115: 'container' => 'div',
116: ),
117: );
118:
119:
120: protected $form;
121:
122:
123: protected $counter;
124:
125:
126:
127: 128: 129: 130: 131: 132:
133: public function render(Nette\Forms\Form $form, $mode = NULL)
134: {
135: if ($this->form !== $form) {
136: $this->form = $form;
137: $this->init();
138: }
139:
140: $s = '';
141: if (!$mode || $mode === 'begin') {
142: $s .= $this->renderBegin();
143: }
144: if ((!$mode && $this->getValue('form errors')) || $mode === 'errors') {
145: $s .= $this->renderErrors();
146: }
147: if (!$mode || $mode === 'body') {
148: $s .= $this->renderBody();
149: }
150: if (!$mode || $mode === 'end') {
151: $s .= $this->renderEnd();
152: }
153: return $s;
154: }
155:
156:
157:
158:
159: public function setClientScript()
160: {
161: trigger_error(__METHOD__ . '() is deprecated; use unobstructive JavaScript instead.', E_USER_WARNING);
162: return $this;
163: }
164:
165:
166:
167: 168: 169: 170:
171: protected function init()
172: {
173:
174: $wrapper = & $this->wrappers['control'];
175: foreach ($this->form->getControls() as $control) {
176: if ($control->isRequired() && isset($wrapper['.required'])) {
177: $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
178: }
179:
180: $el = $control->getControlPrototype();
181: if ($el->getName() === 'input' && isset($wrapper['.' . $el->type])) {
182: $el->class($wrapper['.' . $el->type], TRUE);
183: }
184: }
185: }
186:
187:
188:
189: 190: 191: 192:
193: public function renderBegin()
194: {
195: $this->counter = 0;
196:
197: foreach ($this->form->getControls() as $control) {
198: $control->setOption('rendered', FALSE);
199: }
200:
201: if (strcasecmp($this->form->getMethod(), 'get') === 0) {
202: $el = clone $this->form->getElementPrototype();
203: $url = explode('?', (string) $el->action, 2);
204: $el->action = $url[0];
205: $s = '';
206: if (isset($url[1])) {
207: foreach (preg_split('#[;&]#', $url[1]) as $param) {
208: $parts = explode('=', $param, 2);
209: $name = urldecode($parts[0]);
210: if (!isset($this->form[$name])) {
211: $s .= Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
212: }
213: }
214: $s = "\n\t" . $this->getWrapper('hidden container')->setHtml($s);
215: }
216: return $el->startTag() . $s;
217:
218:
219: } else {
220: return $this->form->getElementPrototype()->startTag();
221: }
222: }
223:
224:
225:
226: 227: 228: 229:
230: public function renderEnd()
231: {
232: $s = '';
233: foreach ($this->form->getControls() as $control) {
234: if ($control instanceof Nette\Forms\Controls\HiddenField && !$control->getOption('rendered')) {
235: $s .= (string) $control->getControl();
236: }
237: }
238: if (iterator_count($this->form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
239: $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
240: }
241: if ($s) {
242: $s = $this->getWrapper('hidden container')->setHtml($s) . "\n";
243: }
244:
245: return $s . $this->form->getElementPrototype()->endTag() . "\n";
246: }
247:
248:
249:
250: 251: 252: 253:
254: public function renderErrors(Nette\Forms\IControl $control = NULL)
255: {
256: $errors = $control === NULL ? $this->form->getErrors() : $control->getErrors();
257: if (count($errors)) {
258: $ul = $this->getWrapper('error container');
259: $li = $this->getWrapper('error item');
260:
261: foreach ($errors as $error) {
262: $item = clone $li;
263: if ($error instanceof Html) {
264: $item->add($error);
265: } else {
266: $item->setText($error);
267: }
268: $ul->add($item);
269: }
270: return "\n" . $ul->render(0);
271: }
272: }
273:
274:
275:
276: 277: 278: 279:
280: public function renderBody()
281: {
282: $s = $remains = '';
283:
284: $defaultContainer = $this->getWrapper('group container');
285: $translator = $this->form->getTranslator();
286:
287: foreach ($this->form->getGroups() as $group) {
288: if (!$group->getControls() || !$group->getOption('visual')) {
289: continue;
290: }
291:
292: $container = $group->getOption('container', $defaultContainer);
293: $container = $container instanceof Html ? clone $container : Html::el($container);
294:
295: $s .= "\n" . $container->startTag();
296:
297: $text = $group->getOption('label');
298: if ($text instanceof Html) {
299: $s .= $text;
300:
301: } elseif (is_string($text)) {
302: if ($translator !== NULL) {
303: $text = $translator->translate($text);
304: }
305: $s .= "\n" . $this->getWrapper('group label')->setText($text) . "\n";
306: }
307:
308: $text = $group->getOption('description');
309: if ($text instanceof Html) {
310: $s .= $text;
311:
312: } elseif (is_string($text)) {
313: if ($translator !== NULL) {
314: $text = $translator->translate($text);
315: }
316: $s .= $this->getWrapper('group description')->setText($text) . "\n";
317: }
318:
319: $s .= $this->renderControls($group);
320:
321: $remains = $container->endTag() . "\n" . $remains;
322: if (!$group->getOption('embedNext')) {
323: $s .= $remains;
324: $remains = '';
325: }
326: }
327:
328: $s .= $remains . $this->renderControls($this->form);
329:
330: $container = $this->getWrapper('form container');
331: $container->setHtml($s);
332: return $container->render(0);
333: }
334:
335:
336:
337: 338: 339: 340: 341:
342: public function renderControls($parent)
343: {
344: if (!($parent instanceof Nette\Forms\Container || $parent instanceof Nette\Forms\ControlGroup)) {
345: throw new Nette\InvalidArgumentException("Argument must be FormContainer or FormGroup instance.");
346: }
347:
348: $container = $this->getWrapper('controls container');
349:
350: $buttons = NULL;
351: foreach ($parent->getControls() as $control) {
352: if ($control->getOption('rendered') || $control instanceof Nette\Forms\Controls\HiddenField || $control->getForm(FALSE) !== $this->form) {
353:
354:
355: } elseif ($control instanceof Nette\Forms\Controls\Button) {
356: $buttons[] = $control;
357:
358: } else {
359: if ($buttons) {
360: $container->add($this->renderPairMulti($buttons));
361: $buttons = NULL;
362: }
363: $container->add($this->renderPair($control));
364: }
365: }
366:
367: if ($buttons) {
368: $container->add($this->renderPairMulti($buttons));
369: }
370:
371: $s = '';
372: if (count($container)) {
373: $s .= "\n" . $container . "\n";
374: }
375:
376: return $s;
377: }
378:
379:
380:
381: 382: 383: 384:
385: public function renderPair(Nette\Forms\IControl $control)
386: {
387: $pair = $this->getWrapper('pair container');
388: $pair->add($this->renderLabel($control));
389: $pair->add($this->renderControl($control));
390: $pair->class($this->getValue($control->isRequired() ? 'pair .required' : 'pair .optional'), TRUE);
391: $pair->class($control->getOption('class'), TRUE);
392: if (++$this->counter % 2) {
393: $pair->class($this->getValue('pair .odd'), TRUE);
394: }
395: $pair->id = $control->getOption('id');
396: return $pair->render(0);
397: }
398:
399:
400:
401: 402: 403: 404: 405:
406: public function renderPairMulti(array $controls)
407: {
408: $s = array();
409: foreach ($controls as $control) {
410: if (!$control instanceof Nette\Forms\IControl) {
411: throw new Nette\InvalidArgumentException("Argument must be array of IFormControl instances.");
412: }
413: $s[] = (string) $control->getControl();
414: }
415: $pair = $this->getWrapper('pair container');
416: $pair->add($this->renderLabel($control));
417: $pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
418: return $pair->render(0);
419: }
420:
421:
422:
423: 424: 425: 426:
427: public function renderLabel(Nette\Forms\IControl $control)
428: {
429: $head = $this->getWrapper('label container');
430:
431: if ($control instanceof Nette\Forms\Controls\Checkbox || $control instanceof Nette\Forms\Controls\Button) {
432: return $head->setHtml(($head->getName() === 'td' || $head->getName() === 'th') ? ' ' : '');
433:
434: } else {
435: $label = $control->getLabel();
436: $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
437: if ($label instanceof Html) {
438: $label->setHtml($label->getHtml() . $suffix);
439: $suffix = '';
440: }
441: return $head->setHtml((string) $label . $suffix);
442: }
443: }
444:
445:
446:
447: 448: 449: 450:
451: public function renderControl(Nette\Forms\IControl $control)
452: {
453: $body = $this->getWrapper('control container');
454: if ($this->counter % 2) {
455: $body->class($this->getValue('control .odd'), TRUE);
456: }
457:
458: $description = $control->getOption('description');
459: if ($description instanceof Html) {
460: $description = ' ' . $control->getOption('description');
461:
462: } elseif (is_string($description)) {
463: $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
464:
465: } else {
466: $description = '';
467: }
468:
469: if ($control->isRequired()) {
470: $description = $this->getValue('control requiredsuffix') . $description;
471: }
472:
473: if ($this->getValue('control errors')) {
474: $description .= $this->renderErrors($control);
475: }
476:
477: if ($control instanceof Nette\Forms\Controls\Checkbox || $control instanceof Nette\Forms\Controls\Button) {
478: return $body->setHtml((string) $control->getControl() . (string) $control->getLabel() . $description);
479:
480: } else {
481: return $body->setHtml((string) $control->getControl() . $description);
482: }
483: }
484:
485:
486:
487: 488: 489: 490:
491: protected function getWrapper($name)
492: {
493: $data = $this->getValue($name);
494: return $data instanceof Html ? clone $data : Html::el($data);
495: }
496:
497:
498:
499: 500: 501: 502:
503: protected function getValue($name)
504: {
505: $name = explode(' ', $name);
506: $data = & $this->wrappers[$name[0]][$name[1]];
507: return $data;
508: }
509:
510: }
511: