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