forked from midgardproject/midgardmvc_helper_forms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.php
181 lines (162 loc) · 5.47 KB
/
group.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @package midgardmvc_helper_forms
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
class midgardmvc_helper_forms_group
{
private $items = array();
private $name = '';
private $label = '';
protected $namespace = '';
public function __construct($name='')
{
$this->name = $name;
}
public function __get($key)
{
switch ($key)
{
case 'name':
return $this->name;
case 'items':
return $this->items;
}
if (!isset($this->items[$key]))
{
throw new InvalidArgumentException("Key '{$key}' not set for group '{$this->name}'");
}
if (isset($this->items[$key]))
{
// Return individual item
return $this->items[$key];
}
}
public function __set($key, $value)
{
if (isset($this->items[$key]))
{
$this->items[$key] = $value;
}
}
public function set_label($label)
{
$this->label = $label;
}
public function add_group($name)
{
//$this->items[$name] = new midgardmvc_helper_forms_group("{$this->name}_{$name}");
$this->items[$name] = new midgardmvc_helper_forms_group($name);
return $this->items[$name];
}
public function add_field($name, $field, $required = false, array $actions = array())
{
if (strpos($field, '_') === false)
{
// Built-in type called using the shorthand notation
$field = "midgardmvc_helper_forms_field_{$field}";
}
$prefixed_name = $name;
if (strlen($this->name) > 0)
{
//merge group name and field name to ensure namespacing between groups
$prefixed_name = $this->name."_".$name;
}
$this->items[$name] = new $field($prefixed_name, $required, $actions);
// If there are values stored in session, set them
$mvc = midgardmvc_core::get_instance();
if ($mvc->sessioning->exists('midgardmvc_helper_forms', "stored_{$this->namespace}"))
{
$stored = $mvc->sessioning->get('midgardmvc_helper_forms', "stored_{$this->namespace}");
if (isset($stored[$prefixed_name]))
{
$this->items[$name]->set_value($stored[$prefixed_name]);
}
}
return $this->items[$name];
}
public function process_post()
{
foreach ($this->items as $name => $item)
{
if ($item instanceof midgardmvc_helper_forms_group)
{
// Tell group to process items
$item->process_post();
continue;
}
$prefixed_name = $name;
if (strlen($this->name) > 0)
{
//merge group name and field name to ensure namespacing between groups
$prefixed_name = $this->name."_".$name;
}
$value = null;
if (isset($_POST[$prefixed_name]))
{
$value = $_POST[$prefixed_name];
}
elseif(isset($_GET[$prefixed_name]))
{
$value = $_GET[$prefixed_name];
}
elseif(isset($_FILES[$prefixed_name]))
{
$value = $_FILES[$prefixed_name];
}
//If item is a field with the proper name, do magic
//if (isset($value) && $value != null)
//{
//Set value to the field
$item->set_value($value);
//Read actions
$actions = $item->get_actions();
//If there are manually defined actions in the array, run them
if ( is_array($actions)
&& count($actions) > 0)
{
foreach($actions as $action)
{
if (method_exists($item, $action))
{
$item->$action();
}
else
{
$classname = get_class($item);
throw new Exception("No action method '$action' implemented for the class '$classname'");
}
}
}
//...or just do what people usually want to do with form fields
else
{
//Default: First clean, then validate
$item->clean();
$item->validate();
}
//}
}
}
public function __toString()
{
$form_string = "<fieldset>\n";
if ($this->label)
{
$form_string .= "<legend>{$this->label}</legend>\n";
}
foreach ($this->items as $item)
{
if ($item instanceof midgardmvc_helper_forms_group)
{
$form_string .= $item;
continue;
}
$form_string .= $item->widget;
}
$form_string .= "</fieldset>\n";
return $form_string;
}
}