1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FormMacros.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Forms! |
9
|
|
|
* @subpackage Latte |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 31.01.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace IPub\Forms\Latte; |
16
|
|
|
|
17
|
|
|
use Latte\Compiler; |
18
|
|
|
use Latte\MacroNode; |
19
|
|
|
use Latte\PhpWriter; |
20
|
|
|
use Latte\Macros\MacroSet; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Forms additional latte macros |
24
|
|
|
* |
25
|
|
|
* @package iPublikuj:Forms! |
26
|
|
|
* @subpackage Latte |
27
|
|
|
* |
28
|
|
|
* @author Adam Kadlec <[email protected]> |
29
|
|
|
*/ |
30
|
1 |
|
final class Macros extends MacroSet |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Register latte macros |
34
|
|
|
*/ |
35
|
|
|
public static function install(Compiler $compiler) |
36
|
|
|
{ |
37
|
1 |
|
$me = new static($compiler); |
38
|
|
|
|
39
|
1 |
|
$me->addMacro('button', [$me, 'macroButton'], [$me, 'macroButtonEnd']); |
40
|
1 |
|
$me->addMacro('caption', [$me, 'macroCaption']); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Renders button beggining tag |
45
|
|
|
* {button ...} |
46
|
|
|
* |
47
|
|
|
* @param MacroNode $node |
48
|
|
|
* @param PhpWriter $writer |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function macroButton(MacroNode $node, PhpWriter $writer) |
53
|
|
|
{ |
54
|
|
|
$code = '$_input = (is_object(%node.word) ? %node.word : end($this->global->formsStack)[%node.word]);'; |
55
|
|
|
$code .= '$_attributes[$_input->getName()] = %node.array;'; |
56
|
|
|
$code .= '$_buttonAttrs = $_input->getControl()->attrs;'; |
57
|
|
|
$code .= '$_buttonCaption = isset($_buttonAttrs[\'value\']) === TRUE ? $_buttonAttrs[\'value\'] : NULL;'; |
58
|
|
|
$code .= 'unset($_buttonAttrs[\'type\'], $_buttonAttrs[\'value\']);'; |
59
|
|
|
$code .= '$_buttonAttrs[\'type\'] = \'submit\';'; // Prevent button type="image" |
60
|
|
|
$code .= '$_buttonControl = \Nette\Utils\Html::el(\'button\')->addAttributes(array_merge((array) $_buttonAttrs,$_attributes[$_input->getName()]));'; |
61
|
|
|
$code .= 'echo $_buttonControl->startTag();'; |
62
|
|
|
|
63
|
|
|
return $writer->write($code); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Renders button end tag |
68
|
|
|
* {/button} |
69
|
|
|
* |
70
|
|
|
* @param MacroNode $node |
71
|
|
|
* @param PhpWriter $writer |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function macroButtonEnd(MacroNode $node, PhpWriter $writer) |
76
|
|
|
{ |
77
|
|
|
$code = 'echo $_buttonControl->endTag();'; |
78
|
|
|
$code .= 'unset($_buttonControl);'; |
79
|
|
|
$code .= 'unset($_buttonCaption);'; |
80
|
|
|
$code .= 'unset($_buttonAttrs);'; |
81
|
|
|
$code .= 'unset($_attributes);'; |
82
|
|
|
$code .= 'unset($_input);'; |
83
|
|
|
|
84
|
|
|
return $writer->write($code); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Render button caption |
89
|
|
|
* |
90
|
|
|
* @param MacroNode $node |
91
|
|
|
* @param PhpWriter $writer |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function macroCaption(MacroNode $node, PhpWriter $writer) |
96
|
|
|
{ |
97
|
|
|
if ($node->args !== '') { |
98
|
|
|
$code = '$_input = (is_object(%node.word) ? %node.word : end($this->global->formsStack)[%node.word]);'; |
99
|
|
|
$code .= 'echo isset($_input->getControl()->attrs[\'value\']) === TRUE ? $_input->getControl()->attrs[\'value\'] : NULL;'; |
100
|
|
|
$code .= 'unset($_input);'; |
101
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$code = 'echo isset($_buttonCaption) ? $_buttonCaption : NULL;'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $writer->write($code); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|