PluginProbe
Packeta / 1.6.1
Packeta v1.6.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Bridges / FormsLatte / Runtime.php

Runtime.php in Packeta 1.6.1, at deps/nette/forms/src/Bridges/FormsLatte/Runtime.php

131 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Bridges\FormsLatte;
9
10 use Packetery\Latte;
11 use Packetery\Nette;
12 use Packetery\Nette\Forms\Form;
13 use Packetery\Nette\Utils\Html;
14 /**
15 * Runtime helpers for Latte.
16 * @internal
17 */
18 class Runtime
19 {
20 use \Packetery\Nette\StaticClass;
21 /**
22 * Renders form begin.
23 */
24 public static function renderFormBegin(Form $form, array $attrs, bool $withTags = \true) : string
25 {
26 $form->fireRenderEvents();
27 foreach ($form->getControls() as $control) {
28 $control->setOption('rendered', \false);
29 }
30 $el = $form->getElementPrototype();
31 $el->action = (string) $el->action;
32 $el = clone $el;
33 if ($form->isMethod('get')) {
34 $el->action = \preg_replace('~\\?[^#]*~', '', $el->action, 1);
35 }
36 $el->addAttributes($attrs);
37 return $withTags ? $el->startTag() : $el->attributes();
38 }
39 /**
40 * Renders form end.
41 */
42 public static function renderFormEnd(Form $form, bool $withTags = \true) : string
43 {
44 $s = '';
45 if ($form->isMethod('get')) {
46 foreach (\preg_split('#[;&]#', (string) \parse_url($form->getElementPrototype()->action, \PHP_URL_QUERY), -1, \PREG_SPLIT_NO_EMPTY) as $param) {
47 $parts = \explode('=', $param, 2);
48 $name = \urldecode($parts[0]);
49 $prefix = \explode('[', $name, 2)[0];
50 if (!isset($form[$prefix])) {
51 $s .= Html::el('input', ['type' => 'hidden', 'name' => $name, 'value' => \urldecode($parts[1])]);
52 }
53 }
54 }
55 foreach ($form->getControls() as $control) {
56 if ($control->getOption('type') === 'hidden' && !$control->getOption('rendered')) {
57 $s .= $control->getControl();
58 }
59 }
60 if (\iterator_count($form->getComponents(\true, \Packetery\Nette\Forms\Controls\TextInput::class)) < 2) {
61 $s .= "<!--[if IE]><input type=IEbug disabled style=\"display:none\"><![endif]-->\n";
62 }
63 return $s . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : '');
64 }
65 /**
66 * Generates blueprint of form.
67 */
68 public static function renderBlueprint($form) : void
69 {
70 $dummyForm = new Form();
71 $dict = new \SplObjectStorage();
72 foreach ($form->getControls() as $name => $input) {
73 $dict[$input] = $dummyInput = new class extends \Packetery\Nette\Forms\Controls\BaseControl
74 {
75 public $inner;
76 public function getLabel($name = null)
77 {
78 return $this->inner->getLabel() ? '{label ' . $this->inner->lookupPath(Form::class) . '/}' : null;
79 }
80 public function getControl()
81 {
82 return '{input ' . $this->inner->lookupPath(Form::class) . '}';
83 }
84 public function isRequired() : bool
85 {
86 return $this->inner->isRequired();
87 }
88 public function getOption($key, $default = null)
89 {
90 return $key === 'rendered' ? parent::getOption($key) : $this->inner->getOption($key, $default);
91 }
92 };
93 $dummyInput->inner = $input;
94 $dummyForm->addComponent($dummyInput, (string) $dict->count());
95 $dummyInput->addError('{inputError ' . $input->lookupPath(Form::class) . '}');
96 }
97 foreach ($form->getGroups() as $group) {
98 $dummyGroup = $dummyForm->addGroup();
99 foreach ($group->getOptions() as $k => $v) {
100 $dummyGroup->setOption($k, $v);
101 }
102 foreach ($group->getControls() as $control) {
103 if ($dict[$control]) {
104 $dummyGroup->add($dict[$control]);
105 }
106 }
107 }
108 $renderer = clone $form->getRenderer();
109 $dummyForm->setRenderer($renderer);
110 if ($renderer instanceof \Packetery\Nette\Forms\Rendering\DefaultFormRenderer) {
111 $renderer->wrappers['error']['container'] = $renderer->getWrapper('error container')->setAttribute('n:ifcontent', \true);
112 $renderer->wrappers['error']['item'] = $renderer->getWrapper('error item')->setAttribute('n:foreach', '$form->getOwnErrors() as $error');
113 $renderer->wrappers['control']['errorcontainer'] = $renderer->getWrapper('control errorcontainer')->setAttribute('n:ifcontent', \true);
114 $dummyForm->addError('{$error}');
115 \ob_start();
116 $dummyForm->render('end');
117 $end = \ob_get_clean();
118 }
119 \ob_start();
120 $dummyForm->render();
121 $body = \ob_get_clean();
122 $body = \str_replace($dummyForm->getElementPrototype()->startTag(), '<form n:name="' . $form->getName() . '">', $body);
123 $body = \str_replace($end ?? '', '</form>', $body);
124 $blueprint = new Latte\Runtime\Blueprint();
125 $end = $blueprint->printCanvas();
126 $blueprint->printHeader('Form ' . $form->getName());
127 echo '<xmp>', $body, '</xmp>';
128 echo $end;
129 }
130 }
131