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
|
|
|
namespace ntentan\honam\engines\php\helpers; |
34
|
|
|
|
35
|
|
|
use ntentan\honam\engines\php\Helper; |
36
|
|
|
|
37
|
|
|
use ntentan\utils\Text; |
38
|
|
|
use \ReflectionMethod; |
39
|
|
|
use \ReflectionClass; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Forms helper for rendering forms. |
43
|
|
|
*/ |
44
|
|
|
class FormHelper extends Helper |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
private $container; |
48
|
|
|
private $data = array(); |
49
|
|
|
private $errors = array(); |
50
|
|
|
|
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
\ntentan\honam\TemplateEngine::appendPath( |
54
|
|
|
__DIR__ . "/../../templates/forms" |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Renders the form when the value is used as a string |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function __toString() |
63
|
|
|
{ |
64
|
|
|
$this->container->setData($this->data); |
65
|
|
|
$this->container->setErrors($this->errors); |
66
|
|
|
$return = (string) $this->container; |
67
|
|
|
$this->container = null; |
68
|
|
|
return $return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function create() |
72
|
|
|
{ |
73
|
|
|
$args = func_get_args(); |
74
|
|
|
$element = __NAMESPACE__ . "\\form\\" . array_shift($args); |
75
|
|
|
$element = new ReflectionClass($element); |
76
|
|
|
return $element->newInstanceArgs($args == null ? array() : $args); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setId($id) |
80
|
|
|
{ |
81
|
|
|
$this->getContainer()->setId($id); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function add() |
85
|
|
|
{ |
86
|
|
|
$args = func_get_args(); |
87
|
|
|
if (is_string($args[0])) { |
88
|
|
|
$elementClass = new ReflectionMethod(__NAMESPACE__ . "\\FormHelper", 'create'); |
89
|
|
|
$element = $elementClass->invokeArgs(null, $args); |
90
|
|
|
$this->getContainer()->add($element); |
91
|
|
|
} |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setErrors($errors) |
96
|
|
|
{ |
97
|
|
|
$this->errors = $errors; |
98
|
|
|
return $this->container; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setData($data) |
102
|
|
|
{ |
103
|
|
|
$this->data = $data; |
104
|
|
|
return $this->container; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function open($formId = '') |
108
|
|
|
{ |
109
|
|
|
$this->container = new form\Form(); |
110
|
|
|
if ($formId != '') { |
111
|
|
|
$this->container->setId($formId); |
112
|
|
|
} |
113
|
|
|
$this->container->setRenderMode(form\Container::RENDER_MODE_HEAD); |
114
|
|
|
return $this->container; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function close($submit = 'Submit') |
118
|
|
|
{ |
119
|
|
|
$arguments = func_get_args(); |
120
|
|
|
if ($submit === false) { |
121
|
|
|
$this->container->setShowSubmit(false); |
122
|
|
|
} else if (count($arguments) > 0) { |
123
|
|
|
$this->container->setSubmitValues($arguments); |
124
|
|
|
} |
125
|
|
|
$this->container->setRenderMode(form\Container::RENDER_MODE_FOOT); |
126
|
|
|
return $this->container; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function __call($function, $arguments) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
if (substr($function, 0, 5) == "open_") { |
132
|
|
|
$container = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 5, strlen($function))); |
133
|
|
|
$containerClass = new ReflectionClass($container); |
134
|
|
|
$containerObject = $containerClass->newInstanceArgs($arguments); |
135
|
|
|
$containerObject->setRenderMode(form\Container::RENDER_MODE_HEAD); |
136
|
|
|
$return = $containerObject; |
137
|
|
View Code Duplication |
} elseif (substr($function, 0, 6) == "close_") { |
|
|
|
|
138
|
|
|
$container = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 6, strlen($function))); |
139
|
|
|
$containerClass = new ReflectionClass($container); |
140
|
|
|
$containerObject = $containerClass->newInstanceArgs($arguments); |
141
|
|
|
$containerObject->setRenderMode(form\Container::RENDER_MODE_FOOT); |
142
|
|
|
$return = $containerObject; |
143
|
|
|
} elseif (substr($function, 0, 4) == "get_") { |
144
|
|
|
$element = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 4, strlen($function))); |
145
|
|
|
$elementClass = new ReflectionClass($element); |
146
|
|
|
$elementObject = $elementClass->newInstanceArgs($arguments); |
147
|
|
|
$name = $elementObject->getName(); |
148
|
|
|
if (isset($this->data[$name])) { |
149
|
|
|
$elementObject->setValue($this->data[$name]); |
150
|
|
|
} |
151
|
|
|
if (isset($this->errors[$name])) { |
152
|
|
|
$elementObject->setErrors($this->errors[$name]); |
153
|
|
|
} |
154
|
|
|
$return = $elementObject; |
155
|
|
View Code Duplication |
} elseif (substr($function, 0, 4) == "add_") { |
|
|
|
|
156
|
|
|
$element = __NAMESPACE__ . "\\form\\" . Text::ucamelize(substr($function, 4, strlen($function))); |
157
|
|
|
$elementClass = new ReflectionClass($element); |
158
|
|
|
$elementObject = $elementClass->newInstanceArgs($arguments); |
159
|
|
|
$return = $this->container->add($elementObject); |
160
|
|
|
} else { |
161
|
|
|
throw new \ntentan\honam\exceptions\HelperException("Function *$function* not found in form helper."); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private function getContainer() |
168
|
|
|
{ |
169
|
|
|
if ($this->container === null) { |
170
|
|
|
$this->container = new form\Form(); |
171
|
|
|
} |
172
|
|
|
return $this->container; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.