1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Forms;
13:
14: use Nette,
15: Nette\Web\Html;
16:
17:
18:
19: 20: 21: 22: 23:
24: class DefaultFormRenderer extends Nette\Object implements 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(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: $uri = explode('?', (string) $el->action, 2);
204: $el->action = $uri[0];
205: $s = '';
206: if (isset($uri[1])) {
207: foreach (preg_split('#[;&]#', $uri[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 HiddenField && !$control->getOption('rendered')) {
235: $s .= (string) $control->getControl();
236: }
237: }
238: if ($s) {
239: $s = $this->getWrapper('hidden container')->setHtml($s) . "\n";
240: }
241:
242: return $s . $this->form->getElementPrototype()->endTag() . "\n";
243: }
244:
245:
246:
247: 248: 249: 250: 251:
252: public function renderErrors(IFormControl $control = NULL)
253: {
254: $errors = $control === NULL ? $this->form->getErrors() : $control->getErrors();
255: if (count($errors)) {
256: $ul = $this->getWrapper('error container');
257: $li = $this->getWrapper('error item');
258:
259: foreach ($errors as $error) {
260: $item = clone $li;
261: if ($error instanceof Html) {
262: $item->add($error);
263: } else {
264: $item->setText($error);
265: }
266: $ul->add($item);
267: }
268: return "\n" . $ul->render(0);
269: }
270: }
271:
272:
273:
274: 275: 276: 277:
278: public function renderBody()
279: {
280: $s = $remains = '';
281:
282: $defaultContainer = $this->getWrapper('group container');
283: $translator = $this->form->getTranslator();
284:
285: foreach ($this->form->getGroups() as $group) {
286: if (!$group->getControls() || !$group->getOption('visual')) continue;
287:
288: $container = $group->getOption('container', $defaultContainer);
289: $container = $container instanceof Html ? clone $container : Html::el($container);
290:
291: $s .= "\n" . $container->startTag();
292:
293: $text = $group->getOption('label');
294: if ($text instanceof Html) {
295: $s .= $text;
296:
297: } elseif (is_string($text)) {
298: if ($translator !== NULL) {
299: $text = $translator->translate($text);
300: }
301: $s .= "\n" . $this->getWrapper('group label')->setText($text) . "\n";
302: }
303:
304: $text = $group->getOption('description');
305: if ($text instanceof Html) {
306: $s .= $text;
307:
308: } elseif (is_string($text)) {
309: if ($translator !== NULL) {
310: $text = $translator->translate($text);
311: }
312: $s .= $this->getWrapper('group description')->setText($text) . "\n";
313: }
314:
315: $s .= $this->renderControls($group);
316:
317: $remains = $container->endTag() . "\n" . $remains;
318: if (!$group->getOption('embedNext')) {
319: $s .= $remains;
320: $remains = '';
321: }
322: }
323:
324: $s .= $remains . $this->renderControls($this->form);
325:
326: $container = $this->getWrapper('form container');
327: $container->setHtml($s);
328: return $container->render(0);
329: }
330:
331:
332:
333: 334: 335: 336: 337:
338: public function renderControls($parent)
339: {
340: if (!($parent instanceof FormContainer || $parent instanceof FormGroup)) {
341: throw new \InvalidArgumentException("Argument must be FormContainer or FormGroup instance.");
342: }
343:
344: $container = $this->getWrapper('controls container');
345:
346: $buttons = NULL;
347: foreach ($parent->getControls() as $control) {
348: if ($control->getOption('rendered') || $control instanceof HiddenField || $control->getForm(FALSE) !== $this->form) {
349: 350:
351: } elseif ($control instanceof Button) {
352: $buttons[] = $control;
353:
354: } else {
355: if ($buttons) {
356: $container->add($this->renderPairMulti($buttons));
357: $buttons = NULL;
358: }
359: $container->add($this->renderPair($control));
360: }
361: }
362:
363: if ($buttons) {
364: $container->add($this->renderPairMulti($buttons));
365: }
366:
367: $s = '';
368: if (count($container)) {
369: $s .= "\n" . $container . "\n";
370: }
371:
372: return $s;
373: }
374:
375:
376:
377: 378: 379: 380: 381:
382: public function renderPair(IFormControl $control)
383: {
384: $pair = $this->getWrapper('pair container');
385: $pair->add($this->renderLabel($control));
386: $pair->add($this->renderControl($control));
387: $pair->class($this->getValue($control->isRequired() ? 'pair .required' : 'pair .optional'), TRUE);
388: $pair->class($control->getOption('class'), TRUE);
389: if (++$this->counter % 2) $pair->class($this->getValue('pair .odd'), TRUE);
390: $pair->id = $control->getOption('id');
391: return $pair->render(0);
392: }
393:
394:
395:
396: 397: 398: 399: 400:
401: public function renderPairMulti(array $controls)
402: {
403: $s = array();
404: foreach ($controls as $control) {
405: if (!$control instanceof IFormControl) {
406: throw new \InvalidArgumentException("Argument must be array of IFormControl instances.");
407: }
408: $s[] = (string) $control->getControl();
409: }
410: $pair = $this->getWrapper('pair container');
411: $pair->add($this->renderLabel($control));
412: $pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
413: return $pair->render(0);
414: }
415:
416:
417:
418: 419: 420: 421: 422:
423: public function renderLabel(IFormControl $control)
424: {
425: $head = $this->getWrapper('label container');
426:
427: if ($control instanceof Checkbox || $control instanceof Button) {
428: return $head->setHtml(($head->getName() === 'td' || $head->getName() === 'th') ? ' ' : '');
429:
430: } else {
431: $label = $control->getLabel();
432: $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
433: if ($label instanceof Html) {
434: $label->setHtml($label->getHtml() . $suffix);
435: $suffix = '';
436: }
437: return $head->setHtml((string) $label . $suffix);
438: }
439: }
440:
441:
442:
443: 444: 445: 446: 447:
448: public function renderControl(IFormControl $control)
449: {
450: $body = $this->getWrapper('control container');
451: if ($this->counter % 2) $body->class($this->getValue('control .odd'), TRUE);
452:
453: $description = $control->getOption('description');
454: if ($description instanceof Html) {
455: $description = ' ' . $control->getOption('description');
456:
457: } elseif (is_string($description)) {
458: $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
459:
460: } else {
461: $description = '';
462: }
463:
464: if ($control->isRequired()) {
465: $description = $this->getValue('control requiredsuffix') . $description;
466: }
467:
468: if ($this->getValue('control errors')) {
469: $description .= $this->renderErrors($control);
470: }
471:
472: if ($control instanceof Checkbox || $control instanceof Button) {
473: return $body->setHtml((string) $control->getControl() . (string) $control->getLabel() . $description);
474:
475: } else {
476: return $body->setHtml((string) $control->getControl() . $description);
477: }
478: }
479:
480:
481:
482: 483: 484: 485:
486: protected function getWrapper($name)
487: {
488: $data = $this->getValue($name);
489: return $data instanceof Html ? clone $data : Html::el($data);
490: }
491:
492:
493:
494: 495: 496: 497:
498: protected function getValue($name)
499: {
500: $name = explode(' ', $name);
501: $data = & $this->wrappers[$name[0]][$name[1]];
502: return $data;
503: }
504:
505: }
506: