1 |
jonen |
1.1 |
<? |
2 |
|
|
/* |
3 |
|
|
## ----------------------------------------------------------------------------- |
4 |
jonen |
1.4 |
## $Id: EditDataItem.php,v 1.3 2003/04/04 23:58:02 jonen Exp $ |
5 |
jonen |
1.1 |
## ----------------------------------------------------------------------------- |
6 |
jonen |
1.2 |
## $Log: EditDataItem.php,v $ |
7 |
jonen |
1.4 |
## Revision 1.3 2003/04/04 23:58:02 jonen |
8 |
|
|
## + saving of items sould now work with new 'GenericSource' structure |
9 |
|
|
## |
10 |
jonen |
1.3 |
## Revision 1.2 2003/04/04 00:43:22 jonen |
11 |
|
|
## + complete rework |
12 |
|
|
## |
13 |
jonen |
1.2 |
## Revision 1.1 2003/03/27 01:18:06 jonen |
14 |
|
|
## + inital commit (was orginal 'DataItem.php') |
15 |
|
|
## |
16 |
jonen |
1.1 |
## Revision 1.4 2003/02/27 17:33:14 cvsmax |
17 |
|
|
## + enabled saving of data objects at confirm action |
18 |
|
|
## + fixed bug with POST/GET vars at form confirm |
19 |
|
|
## o ToDo: fix all bugs related to un-wanted hidden form elements |
20 |
|
|
## caused by mixed/duplicated POST/GET vars! |
21 |
|
|
## |
22 |
|
|
## Revision 1.3 2003/02/26 21:37:53 cvsmax |
23 |
|
|
## + reworked to enable linking if item value is an array or special expression |
24 |
|
|
## |
25 |
|
|
## Revision 1.2 2003/02/25 03:46:59 cvsmax |
26 |
|
|
## + refactored complete to work now with phphtmllib FormProcessor/Content |
27 |
|
|
## o ToDo: field validation seems not to work... |
28 |
|
|
## |
29 |
|
|
## Revision 1.1 2003/02/22 17:27:31 cvsmax |
30 |
|
|
## + renamed from DataEdit.php |
31 |
|
|
## + added simple support for class 'FormElementsInterface' which is an interface |
32 |
|
|
## to class 'FormBuilder' by binarycloud |
33 |
|
|
## o ToDo: refactor code to use new (added 21.02.03!) 'FormProcess' classes by phphtmllib |
34 |
|
|
## + class 'DataItem' now extends class 'InfoTable' |
35 |
|
|
## |
36 |
|
|
## Revision 1.1 2003/02/21 03:19:35 cvsmax |
37 |
|
|
## + first init |
38 |
|
|
## |
39 |
|
|
## |
40 |
|
|
## ----------------------------------------------------------------------------- |
41 |
|
|
*/ |
42 |
|
|
|
43 |
|
|
class EditDataItem extends StandardFormContent { |
44 |
|
|
|
45 |
jonen |
1.2 |
/** |
46 |
|
|
* holds reference to source object. |
47 |
|
|
*/ |
48 |
|
|
var $_datasource = NULL; |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* container for arguments needed for e.g. setting the source object. |
52 |
|
|
*/ |
53 |
jonen |
1.1 |
var $_options = array(); |
54 |
jonen |
1.2 |
|
55 |
jonen |
1.1 |
var $_confirm_msg = "Die Daten wurden erfolgreich gespeichert!"; |
56 |
|
|
|
57 |
jonen |
1.2 |
function EditDataItem($title, $options = array()) { |
58 |
jonen |
1.1 |
$this->_options = $options; |
59 |
jonen |
1.2 |
// prefetch data |
60 |
|
|
$this->data_prefetch(); |
61 |
jonen |
1.1 |
|
62 |
|
|
$parent = get_parent_class($this); |
63 |
jonen |
1.2 |
$this->$parent($title, $PHP_SELF, 600); |
64 |
jonen |
1.1 |
} |
65 |
|
|
|
66 |
|
|
function set_data_source(&$source) { |
67 |
jonen |
1.2 |
$this->_datasource = &$source; |
68 |
jonen |
1.1 |
} |
69 |
|
|
|
70 |
|
|
function get_data_source() { |
71 |
|
|
$initial_locator = php::mkComponent( 'DataSource::Locator', array( adapter_type => 'phpHtmlLib' ) ); |
72 |
|
|
$proxy = php::mkComponent('DataSource::Generic', $initial_locator, $this->_options['data_locator_meta']); |
73 |
|
|
$source = $proxy->get_adapter(); |
74 |
|
|
$this->set_data_source( &$source ); |
75 |
|
|
} |
76 |
|
|
|
77 |
jonen |
1.2 |
function _check_datasource($function_name) { |
78 |
|
|
if ( !is_object($this->_datasource) ) { |
79 |
|
|
user_error("DataList::".$function_name."() - DataListSource object is not set"); |
80 |
|
|
exit; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
function data_prefetch() { |
85 |
|
|
$this->get_data_source(); |
86 |
|
|
$this->_check_datasource("data_prefetch"); |
87 |
|
|
|
88 |
|
|
$this->_datasource->do_query(); |
89 |
|
|
} |
90 |
jonen |
1.1 |
|
91 |
|
|
/** |
92 |
|
|
* This method gets called EVERY time the object is |
93 |
|
|
* created. It is used to build all of the |
94 |
|
|
* FormElement objects used in this Form. |
95 |
|
|
* |
96 |
|
|
*/ |
97 |
|
|
function form_init_elements() { |
98 |
|
|
//we want an confirmation page for this form. |
99 |
|
|
$this->set_confirm(); |
100 |
|
|
|
101 |
jonen |
1.4 |
$utils = php::mkComponent('WebExplorer::utils'); |
102 |
|
|
$hidden = $this->get_hidden_elements(); |
103 |
|
|
$options = $this->_options['decode_args']; |
104 |
|
|
|
105 |
jonen |
1.2 |
foreach($this->_datasource->_result as $key => $value) { |
106 |
jonen |
1.1 |
if($value) { |
107 |
|
|
$validation = TRUE; |
108 |
|
|
} else { |
109 |
|
|
$validation = FALSE; |
110 |
|
|
} |
111 |
jonen |
1.4 |
if(!$utils->decode_item_expr($value, $hidden, $options) && !$utils->decode_item_array($value, $hidden, $options)) { |
112 |
jonen |
1.1 |
$this->add_element( new FEText($key, $validation, "350px") ); |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
/** |
119 |
|
|
* This method is called only the first time the form |
120 |
|
|
* page is hit. This enables u to query a DB and |
121 |
|
|
* pre populate the FormElement objects with data. |
122 |
|
|
* |
123 |
|
|
*/ |
124 |
|
|
function form_init_data() { |
125 |
|
|
//Pull some valies from the DB |
126 |
|
|
//and set the initial values of some of the |
127 |
|
|
//form fields. |
128 |
|
|
|
129 |
jonen |
1.4 |
$utils = php::mkComponent('WebExplorer::utils'); |
130 |
|
|
$hidden = $this->get_hidden_elements(); |
131 |
|
|
$options = $this->_options['decode_args']; |
132 |
|
|
|
133 |
jonen |
1.2 |
foreach($this->_datasource->_result as $key => $value) { |
134 |
jonen |
1.4 |
if(!$utils->decode_item_expr($value, $hidden, $options) && !$utils->decode_item_array($value, $hidden, $options)) { |
135 |
jonen |
1.1 |
$this->set_element_value($key, $value); |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
|
139 |
jonen |
1.2 |
/* OLD !! |
140 |
jonen |
1.1 |
$post_vars = array_join_merge($_GET, $_POST); |
141 |
|
|
foreach ($post_vars as $var => $val) { |
142 |
|
|
$this->set_hidden_element_value($var, $val); |
143 |
|
|
} |
144 |
jonen |
1.2 |
*/ |
145 |
jonen |
1.1 |
|
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
function form_content() { |
150 |
|
|
$this->add_form_block($this->_caption, $this->_dataitem() ); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
function &_dataitem() { |
154 |
|
|
$table = &html_table($this->_width,0,2); |
155 |
|
|
|
156 |
jonen |
1.2 |
foreach($this->_datasource->_result as $key => $value) { |
157 |
jonen |
1.1 |
// if item is match by expression we will replace it with an link object |
158 |
jonen |
1.2 |
if($this->_options['decode']) { |
159 |
jonen |
1.4 |
$utils = php::mkComponent('WebExplorer::utils'); |
160 |
|
|
$hidden = $this->get_hidden_elements(); |
161 |
|
|
$options = $this->_options['decode_args']; |
162 |
|
|
if($utils->decode_item_expr($value, $hidden, $options)) { |
163 |
|
|
$table->add_row($key, $utils->decode_item_expr($value, $hidden, $options)); |
164 |
jonen |
1.2 |
} |
165 |
|
|
// if item is an Array we will replace it with an selection form object |
166 |
jonen |
1.4 |
elseif($utils->decode_item_array($value, $hidden, $options) ) { |
167 |
|
|
$table->add_row($key, $utils->decode_item_array($value, $hidden, $options)); |
168 |
jonen |
1.2 |
} else { |
169 |
|
|
$table->add_row($this->element_label($key), $this->element_form($key)); |
170 |
|
|
} |
171 |
|
|
} else { |
172 |
jonen |
1.1 |
$table->add_row($this->element_label($key), $this->element_form($key)); |
173 |
|
|
} |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
return $table; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
|
|
* This method gets called after the FormElement data has |
181 |
|
|
* passed the validation. This enables you to validate the |
182 |
|
|
* data against some backend mechanism, say a DB. |
183 |
|
|
* |
184 |
|
|
*/ |
185 |
|
|
function form_backend_validation() { |
186 |
|
|
//$this->add_error("Ass", "some bogus error happened"); |
187 |
|
|
//return FALSE; |
188 |
|
|
//foreach($this->raw_data as $key => $value) { |
189 |
|
|
// if($this->_elements[$key]->get_value()) { |
190 |
|
|
// $this->add_error($key, "empty field!"); |
191 |
|
|
// return FALSE; |
192 |
|
|
// } else { |
193 |
|
|
// return TRUE; |
194 |
|
|
// } |
195 |
|
|
// } |
196 |
|
|
$this->set_action("Confirm"); |
197 |
|
|
return TRUE; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
|
201 |
|
|
/** |
202 |
|
|
* This function is used to show an intermediary |
203 |
|
|
* confirmation page. Use this function to |
204 |
|
|
* show a confirmation of the data that was |
205 |
|
|
* submitted by the user. |
206 |
|
|
* This will get called after all of the |
207 |
|
|
* form data was successfully validated. |
208 |
|
|
* All of the form data will automatically |
209 |
|
|
* be created as hidden form fields. All you |
210 |
|
|
* have to do is show the data, and a confirm |
211 |
|
|
* submit button. |
212 |
|
|
* |
213 |
|
|
* @return mixed - either raw html, or some |
214 |
|
|
* container HTMLTag object. |
215 |
|
|
*/ |
216 |
|
|
function form_confirm( ) { |
217 |
|
|
$title = "Form Confirmation / " . $this->_options['caption']; |
218 |
|
|
$title .="( ".$this->_required_field_marker." ".$this->_required_field_text." )"; |
219 |
|
|
$table = new InfoTable($title, $this->_width); |
220 |
|
|
|
221 |
|
|
$this->build_confirm_table( $table ); |
222 |
|
|
|
223 |
|
|
//now add the confirmation button |
224 |
|
|
$td = new TDtag(array("colspan" => 2, |
225 |
|
|
"class" => "contentnovertical", |
226 |
|
|
"align" => "center"), |
227 |
jonen |
1.3 |
form_submit('ecdfe', "Confirm"), |
228 |
jonen |
1.1 |
_HTML_SPACE, |
229 |
|
|
$this->add_action("Edit")); |
230 |
|
|
|
231 |
|
|
if ($this->_cancel_action) { |
232 |
|
|
$td->add(_HTML_SPACE, $this->add_cancel()); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
$table->add_row( $td ); |
236 |
|
|
|
237 |
|
|
return $table; |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
/** |
244 |
|
|
* This method is called ONLY after ALL validation has |
245 |
|
|
* passed. This is the method that allows you to |
246 |
|
|
* do something with the data, say insert/update records |
247 |
|
|
* in the DB. |
248 |
|
|
*/ |
249 |
|
|
function confirm_action() { |
250 |
|
|
|
251 |
jonen |
1.4 |
$utils = php::mkComponent('WebExplorer::utils'); |
252 |
|
|
$hidden = $this->get_hidden_elements(); |
253 |
|
|
$options = $this->_options['decode_args']; |
254 |
|
|
|
255 |
jonen |
1.2 |
foreach($this->_datasource->_result as $label => $value_old) { |
256 |
jonen |
1.4 |
if(!$utils->decode_item_expr($value_old, $hidden, $options) && !$utils->decode_item_array($value_old, $hidden, $options)) { |
257 |
jonen |
1.1 |
$value_new = $this->_elements[$label]->get_value(); |
258 |
jonen |
1.2 |
$data[$label] = $value_new; |
259 |
|
|
} |
260 |
jonen |
1.1 |
} |
261 |
jonen |
1.3 |
// write new data to datasource |
262 |
jonen |
1.2 |
$this->_options['data_locator_meta']['action'] = "write"; |
263 |
|
|
$this->_options['data_locator_meta']['data'] = $data; |
264 |
jonen |
1.3 |
// TODO: 'data_prefetch()' will never return ANYTHING! |
265 |
|
|
// Implement some error-handling there or somewhere. |
266 |
jonen |
1.2 |
$error = $this->data_prefetch(); |
267 |
jonen |
1.3 |
|
268 |
jonen |
1.2 |
//$error = $this->_datasource->set($item); |
269 |
jonen |
1.1 |
if(!$error) { |
270 |
jonen |
1.3 |
// fetch fresh data |
271 |
|
|
$this->_options['data_locator_meta']['action'] = "read"; |
272 |
|
|
// unset previous initialed 'data' var |
273 |
|
|
$this->_options['data_locator_meta']['data'] = NULL; |
274 |
|
|
$this->data_prefetch(); |
275 |
|
|
|
276 |
jonen |
1.1 |
$this->set_action_message($this->_confirm_msg); |
277 |
|
|
return TRUE; |
278 |
|
|
} else { |
279 |
|
|
return FALSE; |
280 |
|
|
} |
281 |
|
|
} |
282 |
|
|
|
283 |
jonen |
1.2 |
/** |
284 |
|
|
* This function is used to build the standard |
285 |
|
|
* buttons for a form. |
286 |
|
|
* |
287 |
|
|
* @return ButtonPanel |
288 |
|
|
*/ |
289 |
|
|
function form_content_buttons() { |
290 |
|
|
$div = new DIVtag( array("style" => "background-color: #eeeeee;". |
291 |
|
|
"padding-top:5px;padding-bottom:5px", |
292 |
|
|
"align"=>"center", "nowrap"), |
293 |
jonen |
1.3 |
form_submit('ecdfe', "Save"), |
294 |
jonen |
1.2 |
_HTML_SPACE, |
295 |
jonen |
1.3 |
//$this->add_cancel() |
296 |
|
|
form_submit('ecdfc', "Cancel") |
297 |
|
|
); |
298 |
jonen |
1.2 |
return $div; |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
|
302 |
jonen |
1.4 |
function get_hidden_elements() { |
303 |
|
|
foreach($this->_hidden_elements as $label => $object) { |
304 |
|
|
$value = $this->_hidden_elements[$label]->get_value(); |
305 |
|
|
$tmp_array[$label] = $value; |
306 |
jonen |
1.1 |
} |
307 |
jonen |
1.4 |
return $tmp_array; |
308 |
jonen |
1.1 |
} |
309 |
|
|
|
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
|
313 |
|
|
?> |