|
1
|
|
|
<?php |
|
2
|
|
|
namespace Nkey\Caribu\Mvc\View\Controls; |
|
3
|
|
|
|
|
4
|
|
|
use Nkey\Caribu\Mvc\View\Control; |
|
5
|
|
|
use Nkey\Caribu\Mvc\Controller\Request; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A very basic form control |
|
9
|
|
|
* |
|
10
|
|
|
* @author Maik Greubel <[email protected]> |
|
11
|
|
|
* This file is part of Caribu MVC package |
|
12
|
|
|
*/ |
|
13
|
|
|
class Form implements Control |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Render the field elements |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $rendered |
|
20
|
|
|
* The rendered output |
|
21
|
|
|
* @param array $fields |
|
22
|
|
|
* The list of fields |
|
23
|
|
|
* @throws ControlException |
|
24
|
|
|
* |
|
25
|
|
|
* @return string Rendered fields appended to rendered output |
|
26
|
|
|
*/ |
|
27
|
1 |
|
private function renderFields($rendered, $fields) |
|
28
|
|
|
{ |
|
29
|
1 |
|
foreach ($fields as $field) { |
|
30
|
1 |
|
if (! isset($field['name'])) { |
|
31
|
|
|
throw new ControlException("Field must have at least a name!"); |
|
32
|
|
|
} |
|
33
|
1 |
|
$fieldType = isset($field['type']) ? $field['type'] : 'text'; |
|
34
|
1 |
|
$id = isset($field['id']) ? $field['id'] : $field['name']; |
|
35
|
1 |
|
$class = isset($field['class']) ? $field['class'] : $field['name']; |
|
36
|
|
|
|
|
37
|
1 |
|
$rendered .= sprintf('<input type="%s" id="%s" class="%s" name="%s"/>', |
|
38
|
1 |
|
$fieldType, $id, $class, $field['name']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
return $rendered; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Render the buttons elements |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $rendered |
|
48
|
|
|
* The rendered output |
|
49
|
|
|
* @param array $buttons |
|
50
|
|
|
* The list of buttons to render |
|
51
|
|
|
* @throws ControlException |
|
52
|
|
|
* |
|
53
|
|
|
* @return string Rendered buttons appended to rendered output |
|
54
|
|
|
*/ |
|
55
|
1 |
|
private function renderButtons($rendered, $buttons) |
|
56
|
|
|
{ |
|
57
|
1 |
|
foreach ($buttons as $button) { |
|
58
|
1 |
|
if (! isset($button['name'])) { |
|
59
|
|
|
throw new ControlException("Button must have at least a name!"); |
|
60
|
|
|
} |
|
61
|
1 |
|
$buttonType = isset($button['type']) ? $button['type'] : "submit"; |
|
62
|
1 |
|
$id = isset($button['id']) ? $button['id'] : $button['name']; |
|
63
|
1 |
|
$class = isset($button['class']) ? $button['class'] : $button['name']; |
|
64
|
1 |
|
$label = isset($button['label']) ? $button['label'] : $button['name']; |
|
65
|
|
|
|
|
66
|
1 |
|
$rendered .= sprintf('<button type="%s" id="%s" class="%s" name="%s">%s</button>', |
|
67
|
1 |
|
$buttonType, $id, $class, $button['name'], $label); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return $rendered; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* (non-PHPdoc) |
|
75
|
|
|
* |
|
76
|
|
|
* @see \Nkey\Caribu\Mvc\View\Control::render() |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function render(Request $request, $parameters = array()) |
|
79
|
|
|
{ |
|
80
|
1 |
|
$contentPrefix = "/"; |
|
81
|
1 |
|
if (! is_null($request->getContextPrefix())) { |
|
82
|
|
|
$contentPrefix = $request->getContextPrefix(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
$formAction = sprintf("%s%s/%s", $contentPrefix, isset($parameters['controller']) ? |
|
86
|
1 |
|
$parameters['controller'] : lcfirst($request->getController()), isset($parameters['action']) ? |
|
87
|
1 |
|
$parameters['action'] : lcfirst($request->getAction())); |
|
88
|
|
|
|
|
89
|
1 |
|
if (isset($parameters['formAction'])) { |
|
90
|
|
|
$formAction = $parameters['formAction']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$rendered = sprintf('<form action="%s" method="%s"%s>', $formAction, isset($parameters['formMethod']) ? |
|
94
|
1 |
|
$parameters['formMethod'] : "POST", isset($parameters['formClass']) ? |
|
95
|
1 |
|
(sprintf(' class="%s"', $parameters['formClass'])) : ""); |
|
96
|
|
|
|
|
97
|
1 |
|
$rendered = $this->renderFields($rendered, $parameters['fields']); |
|
98
|
|
|
|
|
99
|
1 |
|
$rendered = $this->renderButtons($rendered, $parameters['buttons']); |
|
100
|
|
|
|
|
101
|
1 |
|
$rendered .= sprintf("</form>"); |
|
102
|
|
|
|
|
103
|
1 |
|
return $rendered; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|