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