forked from midgardproject/midgardmvc_helper_forms
/
form.php
88 lines (81 loc) · 2.63 KB
/
form.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
<?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_form extends midgardmvc_helper_forms_group
{
private $mvc = null;
private $post_processed = false;
public function __construct($form_namespace)
{
$this->mvc = midgardmvc_core::get_instance();
$this->namespace = $form_namespace;
//parent::__construct($form_namespace);
parent::__construct(); //Construct root group with empty name
}
public function __get($key)
{
if ( isset($this->items[$key])
&& $this->mvc->context->request_method == 'post'
&& !$this->post_processed)
{
// TODO: Process??
}
if ($key == 'namespace')
{
return $this->namespace;
//return parent::__get('name');
}
return parent::__get($key);
}
public function process_post()
{
parent::process_post();
$this->post_processed = true;
}
// Stores values to session
public function store()
{
$mvc = midgardmvc_core::get_instance();
$stored = array();
foreach ($this->items as $name => $item)
{
if (!$item instanceof midgardmvc_helper_forms_field)
{
continue;
}
$stored[$name] = $item->get_value();
}
$mvc->sessioning->set('midgardmvc_helper_forms', "stored_{$this->namespace}", $stored);
}
public function clean_store()
{
$mvc = midgardmvc_core::get_instance();
if (!$mvc->sessioning->exists('midgardmvc_helper_forms', "stored_{$this->namespace}"))
{
return;
}
$mvc->sessioning->remove('midgardmvc_helper_forms', "stored_{$this->namespace}");
}
public function __toString()
{
$form_string = "<form method='post' action=''>\n";
$form_string .= "<input type='hidden' name='midgardmvc_helper_forms_namespace' value='{$this->namespace}' />\n";
foreach ($this->items as $item)
{
if ($item instanceof midgardmvc_helper_forms_group)
{
$form_string .= $item;
continue;
}
$form_string .= $item->widget;
}
$form_string .= "<input type='submit' class='midgardmvc_helper_forms_form_save' value='Save' />\n";
$form_string .= "</form>\n";
return $form_string;
}
}
?>