-
Notifications
You must be signed in to change notification settings - Fork 3
/
form.php
125 lines (110 loc) · 3.33 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
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
<?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;
private $action = '';
private $method = 'post';
private $submit = '';
public function __construct($form_namespace)
{
$this->mvc = midgardmvc_core::get_instance();
parent::__construct($form_namespace);
$this->set_submit(null, $this->mvc->i18n->get('save', 'midgardmvc_helper_forms'));
}
public function __get($key)
{
if ( isset($this->items[$key])
&& $this->mvc->context->request_method == 'post'
&& !$this->post_processed)
{
// TODO: Process??
}
if ($key == 'namespace')
{
return parent::__get('name');
}
return parent::__get($key);
}
public function set_action($action)
{
$this->action = $action;
}
public function set_method($method)
{
$this->method = $method;
}
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_type)
{
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}");
}
/**
* Sets the submit button of the form
* using the class definition for CSS
* and the label for the value attirbute
*/
public function set_submit($class = null, $label = '', $disabled = null)
{
if (is_null($class))
{
$class = "midgardmvc_helper_forms_form_save";
}
if ($disabled)
{
$disabled = 'disabled="disabled"';
}
$this->submit = "<input type='submit' class='{$class}' value='{$label}' {$disabled}/>\n";
}
public function __toString()
{
$form_string = "<form method='{$this->method}' action='{$this->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;
}
if (!$this->readonly)
{
$form_string .= $this->submit;
}
$form_string .= "</form>\n";
return $form_string;
}
}
?>