Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
new.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(){
34  import( 'Dataface/FormTool.php');
35  import( 'Dataface/QuickForm.php');
37  $query =& $app->getQuery();
38 
39  $new = true;
40 
41  $currentRecord = new Dataface_Record($query['-table'], array());
42  $currentTable =& Dataface_Table::loadTable($query['-table']);
43  if ( !isset($query['--tab']) and count($currentTable->tabs($currentRecord)) > 1 ){
44  list($query['--tab']) = array_keys($currentTable->tabs($currentRecord));
45  } else if ( count($currentTable->tabs($currentRecord)) <= 1 ){
46  unset($query['--tab']);
47  }
48  $formTool =& Dataface_FormTool::getInstance();
49  $form = $formTool->createRecordForm($currentRecord, true, @$query['--tab'], $query);
50 
51 
52  //$form = new Dataface_QuickForm($query['-table'], $app->db(), $query, '',$new);
53  $res = $form->_build();
54  if ( PEAR::isError($res) ){
55  error_log($res->toString().Dataface_Error::printStackTrace());
56  throw new Exception("Error occurred while building the new record form. See error log for details.", E_USER_ERROR);
57 
58 
59  }
60  $formTool->decorateRecordForm($currentRecord, $form, true, @$query['--tab']);
61  /*
62  *
63  * We need to add the current GET parameter flags (the GET vars starting with '-') so
64  * that the controller knows to pass control to this method again upon form submission.
65  *
66  */
67  foreach ( $query as $key=>$value){
68  if ( strpos($key,'-')===0 ){
69  $form->addElement('hidden', $key);
70  $form->setDefaults( array( $key=>$value) );
71 
72  }
73  }
74 
75  /*
76  * Store the current query string (the portion after the '?') in the form, so we
77  * can retrieve it after and redirect back to our original location.
78  */
79  $form->addElement('hidden', '-query');
80  $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
81 
82 
83  /*
84  *
85  * We have to deal with 3 cases.
86  * 1) The form has not been submitted.
87  * 2) The form was submitted but didn't validate (ie: it had some bad input)
88  * 3) The form was submitted and was validated.
89  *
90  * We deal with Case 3 first...
91  *
92  */
93 
94  if ( $formTool->validateRecordForm($currentRecord, $form, true, @$query['--tab']) ){
95 
96  /*
97  *
98  * The form was submitted and it validated ok. We now process it (ie: save its contents).
99  *
100  */
101  $formTool->handleTabSubmit($currentRecord, $form, @$query['--tab']);
102  if ( !isset($query['--tab']) ){
103  // If we aren't using tabs we just do it the old way.
104  // (If it ain't broke don't fix it
105  $result = $form->process( array( &$form, 'save') );
106  } else {
107  // If we are using tabs, we will use the formtool's
108  // session aware saving function
109  $result = $formTool->saveSession($currentRecord, true);
110  }
111 
112  $success = true;
113  $response =& Dataface_Application::getResponse();
114 
115  if ( !$result ){
116  throw new Exception("Error occurred in save: ".mysql_error( $app->db()), E_USER_ERROR);
118  //echo "Error..";
120  $success = false;
121  $form->_errors[] = $result->getMessage();
122 
123  } else {
124  //echo "not dup entry"; exit;
125  error_log($result->toString()."\n".implode("\n", $result->getBacktrace()));
126  throw new Exception("An error occurred while attempting to save the record. See server error log for details.", E_USER_ERROR);
127 
128  }
129  } else if ( Dataface_Error::isNotice($result) ){
130 
131  $app->addError($result);
132  $success = false;
133  }
134 
135  if ( $success){
136 
137  if (@$query['-response'] == 'json' ){
138  //header('Content-type: application/json; charset="'.$app->_conf['oe'].'"');
139  $rvals = $currentRecord->strvals();
140  $rvals['__title__'] = $currentRecord->getTitle();
141  $rvals['__id__'] = $currentRecord->getId();
142  echo json_encode(array('response_code'=>200, 'record_data'=> $rvals, 'response_message'=>df_translate('Record Successfully Saved', 'Record Successfully Saved')));
143  return;
144  }
145  import('Dataface/Utilities.php');
146 
147 
148  Dataface_Utilities::fireEvent('after_action_new', array('record'=>$currentRecord));
149 
150  /*
151  *
152  * Since the form created a new record, then it makes more sense to redirect to this newly
153  * created record than to the old record. We used the 'keys' of the new record to generate
154  * a redirect link.
155  *
156  */
157  //$query = $form->_record->getValues(array_keys($form->_record->_table->keys()));
158  $currentRecord->secureDisplay = false;
159  if ( $currentRecord->checkPermission('edit') ){
160  $nextAction = 'edit';
161  } else {
162  $nextAction = 'view';
163  }
164  $url = $currentRecord->getURL(array('-action'=>$nextAction));
165 
166 
167  $msg = implode("\n", $app->getMessages());//@$response['--msg'];
168  $msg = urlencode(trim(
170  /* i18n id */
171  "Record successfully saved",
172  /* Default message */
173  "Record successfully saved."
174  )."\n".$msg));
175  if ( strpos($url, '?') === false ) $url .= '?';
176  $link = $url.'&--msg='.$msg;
177  $app->redirect("$link");
178 
179  }
180 
181  }
182 
183  ob_start();
184  $form->setDefaults($_GET);
185  $form->display();
186  $out = ob_get_contents();
187  ob_end_clean();
188 
189  if ( count($form->_errors) > 0 ){
190  //$app->clearMessages();
191  //$app->addError(PEAR::raiseError("Some errors occurred while processing this form: <ul><li>".implode('</li><li>', $form->_errors)."</li></ul>"));
192  }
193 
194  $context = array('form'=>&$out);
195  $context['tabs'] = $formTool->createHTMLTabs($currentRecord, $form, @$query['--tab']);
196 
197  df_display($context, 'Dataface_New_Record.html', true);
198  }
199 }
200 
201 ?>