Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
edit.php
Go to the documentation of this file.
1 <?php
2 /********************************************************************************
3  *
4  * Xataface Web Application Framework for PHP and MySQL
5  * Copyright (C) 2006 Steve Hannah <shannah@sfu.ca>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  *===============================================================================
22  */
33  function handle(&$params){
34  import( 'Dataface/FormTool.php');
35  import( 'Dataface/QuickForm.php');
36 
37 
39  $query =& $app->getQuery();
40  $resultSet =& $app->getResultSet();
41 
42  $currentRecord =& $app->getRecord();
43  $currentTable =& Dataface_Table::loadTable($query['-table']);
44  if ( !isset($query['--tab']) and count($currentTable->tabs($currentRecord)) > 1 ){
45  list($query['--tab']) = array_keys($currentTable->tabs($currentRecord));
46  } else if ( count($currentTable->tabs($currentRecord)) <= 1 ){
47  unset($query['--tab']);
48  }
49 
50 
51 
52  /*
53  *
54  * Create the quickform for the current record.
55  *
56  */
57  //$form = new Dataface_QuickForm($query['-table'], $app->db(), $query);
58  $formTool =& Dataface_FormTool::getInstance();
59 
60 
61  if ( $resultSet->found()> @$query['-cursor']){
62  $form = $formTool->createRecordForm($currentRecord, false, @$query['--tab'], $query);
63  /*
64  * There is either a result to edit, or we are creating a new record.
65  *
66  */
67 
68  $res = $form->_build();
69  if ( PEAR::isError($res) ){
70  error_log($res->toString().implode("\n", $res->getBacktrace()));
71  throw new Exception("An error occurred while building the edit form. See error log for details.", E_USER_ERROR);
72 
73 
74  }
75  $formTool->decorateRecordForm($currentRecord, $form, false, @$query['--tab']);
76 
77 
78  /*
79  *
80  * We need to add the current GET parameter flags (the GET vars starting with '-') so
81  * that the controller knows to pass control to this method again upon form submission.
82  *
83  */
84  foreach ( $query as $key=>$value){
85  if ( strpos($key,'-')===0 ){
86  $form->addElement('hidden', $key);
87  $form->setDefaults( array( $key=>$value) );
88 
89  }
90  }
91 
92  /*
93  * Store the current query string (the portion after the '?') in the form, so we
94  * can retrieve it after and redirect back to our original location.
95  */
96  $form->addElement('hidden', '-query');
97  $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
98 
99 
100  /*
101  *
102  * We have to deal with 3 cases.
103  * 1) The form has not been submitted.
104  * 2) The form was submitted but didn't validate (ie: it had some bad input)
105  * 3) The form was submitted and was validated.
106  *
107  * We deal with Case 3 first...
108  *
109  */
110 
111  if ( $formTool->validateRecordForm($currentRecord, $form, false, @$query['--tab']) ){
112  /*
113  *
114  * The form was submitted and it validated ok. We now process it (ie: save its contents).
115  *
116  */
117  $app->clearMessages();
118  $formTool->handleTabSubmit($currentRecord, $form, @$query['--tab']);
119  if ( !isset($query['--tab']) ){
120  // If we aren't using tabs we just do it the old way.
121  // (If it ain't broke don't fix it
122 
123  $result = $form->process( array( &$form, 'save') );
124  } else {
125  // If we are using tabs, we will use the formtool's
126  // session aware saving function
127 
128  $result = $formTool->saveSession($currentRecord);
129  }
130  $success = true;
131  $response =& Dataface_Application::getResponse();
132 
133  if ( !$result ){
134  error_log("Error occurred in save: ".mysql_error( $app->db()).Dataface_Error::printStackTrace());
135  throw new Exception("An error occurred while attempting to save the record. See error log for details.", E_USER_ERROR);
137 
139  $app->addError($result);
140  $success = false;
141 
142  } else {
143  error_log($result->toString(). implode("\n", $result->getBacktrace()));
144  throw new Exception("An error occurred while attempting to save the record. See error log for details.", E_USER_ERROR);
145 
146  }
147  } else if ( Dataface_Error::isNotice($result) ){
148  $app->addError($result);
149 
150  //$response['--msg'] = @$response['--msg'] ."\n".$result->getMessage();
151  $success = false;
152  }
153 
154 
155  if ( $success ){
156 
157  if (@$query['-response'] == 'json' ){
158  //header('Content-type: text/html; charset="'.$app->_conf['oe'].'"');
159  $rvals = $currentRecord->strvals();
160  $rvals['__title__'] = $currentRecord->getTitle();
161  $rvals['__id__'] = $currentRecord->getId();
162  echo htmlspecialchars(json_encode(array('response_code'=>200, 'record_data'=> $rvals, 'response_message'=>df_translate('Record Successfully Saved', 'Record Successfully Saved'))));
163  return;
164  }
165 
166  import('Dataface/Utilities.php');
167  Dataface_Utilities::fireEvent('after_action_edit', array('record'=>$form->_record));
168  /*
169  *
170  * The original query string will have the -new flag set. We need to remove this
171  * flag so that we don't redirect the user to create another new record.
172  *
173  */
174  $vals = $form->exportValues();
175  $vals['-query'] = preg_replace('/[&\?]-new=[^&]+/i', '', $vals['-query']);
176 
177  $_SESSION['--last_modified_record_url'] = $form->_record->getURL();
178  $_SESSION['--last_modified_record_title'] = $form->_record->getTitle();
179 
180  $msg = implode("\n", $app->getMessages());
181  //$msg =@$response['--msg'];
182  $msg = urlencode(
184  /* i18n id */
185  'Record successfully saved',
186  /* Default success message */
187  "Record successfully saved.<br>"
188  ).$msg
189  );
190 
191  if ( preg_match('/[&\?]-action=edit&/', $vals['-query']) and !$form->_record->checkPermission('edit') ){
192  $vals['-query'] = preg_replace('/([&\?])-action=edit&/', '$1-action=view&', $vals['-query']);
193  } else if ( preg_match('/[&\?]-action=edit$/', $vals['-query']) and !$form->_record->checkPermission('edit') ){
194  $vals['-query'] = preg_replace('/([&\?])-action=edit$/', '$1-action=view', $vals['-query']);
195  }
196  $vals['-query'] = preg_replace('/&?--msg=[^&]*/', '', $vals['-query']);
197 
198  $link = $_SERVER['HOST_URI'].DATAFACE_SITE_HREF.'?'.$vals['-query'].'&--msg='.$msg;
199 
200 
201  /*
202  *
203  * Redirect the user to the appropriate record.
204  *
205  */
206  $app->redirect("$link");
207  }
208  }
209 
210  ob_start();
211  $form->display();
212  $out = ob_get_contents();
213  ob_end_clean();
214 
215  if ( count($form->_errors) > 0 ){
216  $app->clearMessages();
217  $app->addError(PEAR::raiseError("Some errors occurred while processing this form: <ul><li>".implode('</li><li>', $form->_errors)."</li></ul>"));
218  }
219  $context = array('form'=>$out);
220 
221 
222  // Now let's add the tabs to the context
223  $context['tabs'] = $formTool->createHTMLTabs($currentRecord, $form, @$query['--tab']);
224 
225 
226  } else {
227  // no records were found
228  $context = array('form'=>'');
229 
230  if ( isset($_SESSION['--last_modified_record_url']) ){
231  $lastModifiedURL = $_SESSION['--last_modified_record_url'];
232  $lastModifiedTitle = $_SESSION['--last_modified_record_title'];
233  unset($_SESSION['--last_modified_record_title']);
234  unset($_SESSION['--last_modified_record_url']);
235  $app->addMessage(
236  df_translate(
237  'Return to last modified record',
238  'No records matched your request. Click <a href="'.$lastModifiedURL.'">here</a> to return to <em>'.htmlspecialchars($lastModifiedTitle).'</em>.',
239  array('lastModifiedURL'=>$lastModifiedURL,
240  'lastModifiedTitle'=>$lastModifiedTitle
241  )
242  )
243  );
244  } else {
245  $app->addMessage(
247  'No records matched request',
248  'No records matched your request'
249  )
250  );
251  }
252 
253  $query['-template'] = 'Dataface_Main_Template.html';
254  }
255 
256 
257  if ( isset($query['-template']) ) $template = $query['-template'];
258  else if ( isset( $params['action']['template']) ) $template = $params['action']['template'];
259  else $template = 'Dataface_Edit_Record.html';
260 
261 
262  df_display($context, $template, true);
263 
264  }
265 
266 }
267 ?>