1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Forms helper |
5
|
|
|
* |
6
|
|
|
* Ntentan Framework |
7
|
|
|
* Copyright (c) 2008-2012 James Ekow Abaka Ainooson |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
10
|
|
|
* a copy of this software and associated documentation files (the |
11
|
|
|
* "Software"), to deal in the Software without restriction, including |
12
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
13
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
14
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
15
|
|
|
* the following conditions: |
16
|
|
|
* |
17
|
|
|
* The above copyright notice and this permission notice shall be |
18
|
|
|
* included in all copies or substantial portions of the Software. |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
21
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
22
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
23
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
24
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
25
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
26
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
27
|
|
|
* |
28
|
|
|
* @author James Ainooson <[email protected]> |
29
|
|
|
* @copyright Copyright 2010 James Ekow Abaka Ainooson |
30
|
|
|
* @license MIT |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
namespace ntentan\honam\engines\php\helpers; |
35
|
|
|
|
36
|
|
|
use ntentan\honam\engines\php\Helper; |
37
|
|
|
use ntentan\honam\engines\php\helpers\form\Container; |
38
|
|
|
use ntentan\honam\exceptions\HelperException; |
39
|
|
|
use ntentan\utils\Text; |
40
|
|
|
use \ReflectionClass; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Forms helper for rendering forms. |
44
|
|
|
*/ |
45
|
|
|
class FormHelper extends Helper |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
private $container; |
49
|
|
|
private $data = array(); |
50
|
|
|
private $errors = array(); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Renders the form when the value is used as a string |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
public function __toString() |
58
|
|
|
{ |
59
|
3 |
|
$this->container->setData($this->data); |
60
|
3 |
|
$this->container->setErrors($this->errors); |
61
|
3 |
|
$return = (string) $this->container; |
62
|
3 |
|
$this->container = null; |
63
|
3 |
|
return $return; |
64
|
|
|
} |
65
|
|
|
|
66
|
11 |
|
private function createElement($element, $args) |
67
|
|
|
{ |
68
|
11 |
|
$elementClass = new ReflectionClass(__NAMESPACE__ . "\\form\\" . $element); |
69
|
11 |
|
$element = $elementClass->newInstanceArgs($args == null ? array() : $args); |
70
|
11 |
|
$element->setTemplateRenderer($this->templateRenderer); |
71
|
11 |
|
return $element; |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
public function create() |
75
|
|
|
{ |
76
|
8 |
|
$args = func_get_args(); |
77
|
8 |
|
$element = array_shift($args); |
78
|
8 |
|
return $this->createElement($element, $args); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
public function setId($id) |
82
|
|
|
{ |
83
|
3 |
|
$this->getContainer()->setId($id); |
84
|
3 |
|
} |
85
|
|
|
|
86
|
2 |
|
public function add() |
87
|
|
|
{ |
88
|
2 |
|
$args = func_get_args(); |
89
|
2 |
|
$element = array_shift($args); |
90
|
2 |
|
$this->getContainer()->add($this->createElement($element, $args)); |
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function setErrors($errors) |
95
|
|
|
{ |
96
|
1 |
|
$this->errors = $errors; |
97
|
1 |
|
return $this->container; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function setData($data) |
101
|
|
|
{ |
102
|
2 |
|
$this->data = $data; |
103
|
2 |
|
return $this->container; |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
public function open($formId = '') |
107
|
|
|
{ |
108
|
7 |
|
$this->container = $this->create('Form'); |
109
|
7 |
|
if ($formId != '') { |
110
|
1 |
|
$this->container->setId($formId); |
111
|
|
|
} |
112
|
7 |
|
$this->container->setRenderMode(form\Container::RENDER_MODE_HEAD); |
113
|
7 |
|
return $this->container; |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
public function close($submit = 'Submit') |
117
|
|
|
{ |
118
|
7 |
|
$arguments = func_get_args(); |
119
|
7 |
|
if ($submit === false) { |
120
|
1 |
|
$this->container->setShowSubmit(false); |
121
|
7 |
|
} else if (count($arguments) > 0) { |
122
|
3 |
|
$this->container->setSubmitValues($arguments); |
123
|
|
|
} |
124
|
7 |
|
$this->container->setRenderMode(form\Container::RENDER_MODE_FOOT); |
125
|
7 |
|
return $this->container; |
126
|
|
|
} |
127
|
|
|
|
128
|
7 |
|
public function __call($function, $arguments) |
129
|
|
|
{ |
130
|
7 |
|
if (substr($function, 0, 5) == "open_") { |
131
|
1 |
|
$container = $this->createElement(Text::ucamelize(substr($function, 5, strlen($function))), $arguments); |
132
|
1 |
|
$container->setRenderMode(Container::RENDER_MODE_HEAD); |
133
|
1 |
|
return $container; |
134
|
|
|
} |
135
|
|
|
|
136
|
7 |
|
elseif (substr($function, 0, 6) == "close_") { |
137
|
1 |
|
$container = $this->createElement(Text::ucamelize(substr($function, 5, strlen($function))), $arguments); |
138
|
1 |
|
$container->setRenderMode(Container::RENDER_MODE_FOOT); |
139
|
1 |
|
return $container; |
140
|
|
|
} |
141
|
|
|
|
142
|
7 |
|
elseif (substr($function, 0, 4) == "get_") { |
143
|
5 |
|
$element = $this->createElement(Text::ucamelize(substr($function, 4, strlen($function))), $arguments); |
144
|
5 |
|
$name = $element->getName(); |
145
|
5 |
|
if (isset($this->data[$name])) { |
146
|
1 |
|
$element->setValue($this->data[$name]); |
147
|
|
|
} |
148
|
5 |
|
if (isset($this->errors[$name])) { |
149
|
1 |
|
$element->setErrors($this->errors[$name]); |
150
|
|
|
} |
151
|
5 |
|
return $element; |
152
|
|
|
} |
153
|
|
|
|
154
|
2 |
|
elseif (substr($function, 0, 4) == "add_") { |
155
|
1 |
|
$element = $this->createElement(Text::ucamelize(substr($function, 4, strlen($function))), $arguments); |
156
|
1 |
|
return $this->container->add($element); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
else { |
160
|
1 |
|
throw new HelperException("Function *$function* not found in form helper."); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
private function getContainer() |
165
|
|
|
{ |
166
|
3 |
|
if ($this->container === null) { |
167
|
3 |
|
$this->container = $this->createElement("Form", []); |
168
|
|
|
} |
169
|
3 |
|
return $this->container; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|