-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathActionForm.class.php
More file actions
201 lines (174 loc) · 6.26 KB
/
ActionForm.class.php
File metadata and controls
201 lines (174 loc) · 6.26 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/* Copyright (c) Anuko International Ltd. https://www.anuko.com
License: See license.txt */
// TODO: Refactor this entire class.
import('ttDate');
class ActionForm {
var $mName = "";
var $mSessionCell;
var $mValues = array(); // values without localisation
var $mVariables = array();
var $mForm = null;
var $mInitForm = false;
var $name = "";
function __construct($name, &$form, $request=null) {
$this->setName($name);
$this->setForm($form);
//if ($request) $this->initAttributes($request);
$this->initAttributes($request);
}
function setForm(&$form) {
$this->mForm = $form;
$elements = $form->getElements();
if (is_array($elements))
$this->setVariablesNames(array_keys($elements));
}
function &getFormElement($name) {
if ($this->mForm!=null) {
return $this->mForm->elements[$name];
}
return null;
}
function getName() {
return $this->mName;
}
function setName($name) {
$this->name = $name;
$this->mSessionCell = "formbean_".$this->name;
}
/**
* init parameters and form
*
* @param object $request
*/
function initAttributes(&$request) {
$submit_flag = (is_object($request) && ($request->isPost()));
if ($submit_flag) {
// fill ActionForm and Form from Request
foreach ($this->mVariables as $name) {
if ($this->mForm->elements[$name] && $request->getParameter($name)) {
$this->mForm->elements[$name]->setValue($request->getParameter($name));
$this->mValues[$name] = $this->mForm->elements[$name]->getValue();
}
}
} else {
// fill ActionForm from Session
$this->loadBean();
}
// fill Form by ActionForm
if ($this->mForm) {
$elements = $this->mForm->getElements();
foreach ($elements as $name=>$el) {
// TODO: Refactor this. This is a temporary fix for project multiple select. mValues should probably contain just project, not project[].
if ($name == 'project' && isset($this->mValues['project[]'])) {
$this->mForm->elements['project']->setValue($this->mValues['project[]']);
}
// End of TODO.
if ($this->mForm->elements[$name] && isset($this->mValues[$name])) {
$this->mForm->elements[$name]->setValue($this->mValues[$name]);
}
}
$this->mInitForm = true;
}
}
function setVariablesNames($namelist) {
$this->mVariables = $namelist;
}
function setAttribute($name,$value) {
global $user;
$this->mValues[$name] = $value;
if ($this->mForm) {
if (isset($this->mForm->elements[$name])) {
if ($this->mForm->elements[$name]->class=="DateField") {
// We get here when loading a fav report. Refactor this entire class.
$dt = new ttDate($value, $user->getDateFormat());
$value = $dt->toString();
}
$this->mForm->elements[$name]->setValueSafe($value);
}
}
}
function getAttribute($name) {
return @$this->mValues[$name];
}
function getAttributes() {
return $this->mValues;
}
function validate(&$actionMapping, &$request) {
return null;
}
function setAttributes($value) {
global $user;
$this->mValues = $value;
if (is_array($this->mValues))
foreach ($this->mValues as $name=>$value) {
if ($this->mForm) {
if (isset($this->mForm->elements[$name])) {
if ($this->mForm->elements[$name]->class=="DateField") {
// We get here when changing to --- no --- fav report. Refactor this entire class.
$dt = new ttDate($value, $user->getDateFormat());
$value = $dt->toString();
}
$this->mForm->elements[$name]->setValueSafe($value);
}
}
}
}
function dump() {
print_r($this->mValues);
}
function saveBean() {
if ($this->mForm) {
$elements = $this->mForm->getElements();
$el_list = array();
foreach ($elements as $el) {
$el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
$_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
}
$_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
}
//print_r($_SESSION);
}
// saveDetachedAttribute saves a "detached" from form named attributed in session.
// There is no element in the form for it.
// Intended use is to add something to the session, when a form bean created on one page
// is used on other pages (ex.: reportForm).
// For example, to generate a timesheet we need a user_id, which is determined when a report
// is generated on report.php, using a bean created in reports.php.
function saveDetachedAttribute($name, $value) {
$_SESSION[$this->mSessionCell .'_'.$name] = $value;
}
function getDetachedAttribute($name) {
return $_SESSION[$this->mSessionCell.'_'.$name];
}
function loadBean() {
$el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
if (is_array($el_list)) {
foreach ($el_list as $ref_el) {
// restore form elements
import('form.'.$ref_el["class"]);
$class_name = $ref_el["class"];
$el = new $class_name($ref_el["name"]);
$el->localize();
$el->setValueSafe(@$_SESSION[$this->mSessionCell . "_" .$el->getName()]);
if ($this->mForm && !isset($this->mForm->elements[$ref_el["name"]])) {
$this->mForm->elements[$ref_el["name"]] = &$el;
}
$this->mValues[$el->getName()] = $el->getValue();
}
}
//print_r($_SESSION);
}
function destroyBean() {
$el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
if (is_array($el_list)) {
foreach ($el_list as $ref_el) {
unset($_SESSION[$this->mSessionCell . "_" .$ref_el["name"]]);
}
}
unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
}
function isSaved() {
return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);
}
}