Xataface 2.0
Xataface Application Framework
actions/new.php
Go to the documentation of this file.
00001 <?php
00002 /********************************************************************************
00003  *
00004  *  Xataface Web Application Framework for PHP and MySQL
00005  *  Copyright (C) 2006  Steve Hannah <shannah@sfu.ca>
00006  *  
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Lesser General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2.1 of the License, or (at your option) any later version.
00011  *  
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Lesser General Public License for more details.
00016  *  
00017  *  You should have received a copy of the GNU Lesser General Public
00018  *  License along with this library; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  *===============================================================================
00022  */
00032 class dataface_actions_new {
00033         function handle(){
00034                 import( 'Dataface/FormTool.php');
00035                 import( 'Dataface/QuickForm.php');
00036                 $app =& Dataface_Application::getInstance();
00037                 $query =& $app->getQuery();
00038                 
00039                 $new = true;
00040                 
00041                 $currentRecord = new Dataface_Record($query['-table'], array());
00042                 $currentTable =& Dataface_Table::loadTable($query['-table']);
00043                 if ( !isset($query['--tab']) and count($currentTable->tabs($currentRecord)) > 1 ){
00044                         list($query['--tab']) = array_keys($currentTable->tabs($currentRecord));
00045                 } else if ( count($currentTable->tabs($currentRecord)) <= 1 ){
00046                         unset($query['--tab']);
00047                 }
00048                 $formTool =& Dataface_FormTool::getInstance();
00049                 $form = $formTool->createRecordForm($currentRecord, true, @$query['--tab'], $query);
00050                 
00051                 
00052                 //$form = new Dataface_QuickForm($query['-table'], $app->db(),  $query, '',$new);
00053                 $res = $form->_build();
00054                 if ( PEAR::isError($res) ){
00055                         error_log($res->toString().Dataface_Error::printStackTrace());
00056                         throw new Exception("Error occurred while building the new record form.  See error log for details.", E_USER_ERROR);
00057                         
00058                 
00059                 }
00060                 $formTool->decorateRecordForm($currentRecord, $form, true, @$query['--tab']);   
00061                 /*
00062                  *
00063                  * We need to add the current GET parameter flags (the GET vars starting with '-') so
00064                  * that the controller knows to pass control to this method again upon form submission.
00065                  *
00066                  */
00067                 foreach ( $query as $key=>$value){
00068                         if ( strpos($key,'-')===0 ){
00069                                 $form->addElement('hidden', $key);
00070                                 $form->setDefaults( array( $key=>$value) );
00071                                 
00072                         }
00073                 }
00074                                 
00075                 /*
00076                  * Store the current query string (the portion after the '?') in the form, so we 
00077                  * can retrieve it after and redirect back to our original location.
00078                  */
00079                 $form->addElement('hidden', '-query');
00080                 $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
00081                                 
00082                                 
00083                 /*
00084                  * 
00085                  * We have to deal with 3 cases.
00086                  *      1) The form has not been submitted.
00087                  *      2) The form was submitted but didn't validate (ie: it had some bad input)
00088                  *      3) The form was submitted and was validated.
00089                  *
00090                  * We deal with Case 3 first...
00091                  *
00092                  */
00093                         
00094                 if ( $formTool->validateRecordForm($currentRecord, $form, true, @$query['--tab']) ){
00095                         
00096                         /*
00097                          *
00098                          * The form was submitted and it validated ok.  We now process it (ie: save its contents).
00099                          *
00100                          */
00101                         $formTool->handleTabSubmit($currentRecord, $form, @$query['--tab']);
00102                         if ( !isset($query['--tab']) ){
00103                                 // If we aren't using tabs we just do it the old way.
00104                                 // (If it ain't broke don't fix it
00105                                 $result = $form->process( array( &$form, 'save') );
00106                         } else {
00107                                 // If we are using tabs, we will use the formtool's 
00108                                 // session aware saving function
00109                                 $result = $formTool->saveSession($currentRecord, true);
00110                         }
00111                         
00112                         $success = true;
00113                         $response =& Dataface_Application::getResponse();
00114                         
00115                         if ( !$result ){
00116                                 throw new Exception("Error occurred in save: ".mysql_error( $app->db()), E_USER_ERROR);
00117                         } else if ( PEAR::isError($result) && !Dataface_Error::isNotice($result) ){
00118                                 //echo "Error..";
00119                                 if ( Dataface_Error::isDuplicateEntry($result) ){
00120                                         $success = false;
00121                                         $form->_errors[] = $result->getMessage();
00122                                         
00123                                 } else {
00124                                         //echo "not dup entry"; exit;
00125                                         error_log($result->toString()."\n".implode("\n", $result->getBacktrace()));
00126                                         throw new Exception("An error occurred while attempting to save the record.  See server error log for details.", E_USER_ERROR);
00127 
00128                                 }
00129                         } else if ( Dataface_Error::isNotice($result) ){
00130                         
00131                                 $app->addError($result);
00132                                 $success = false;
00133                         }
00134                         
00135                         if ( $success){
00136                                 
00137                                 if (@$query['-response'] == 'json' ){
00138                                         //header('Content-type: text/json; charset="'.$app->_conf['oe'].'"');
00139                                         $rvals = $currentRecord->strvals();
00140                                         $rvals['__title__'] = $currentRecord->getTitle();
00141                                         $rvals['__id__'] = $currentRecord->getId();
00142                                         echo json_encode(array('response_code'=>200, 'record_data'=> $rvals, 'response_message'=>df_translate('Record Successfully Saved', 'Record Successfully Saved')));
00143                                         return;
00144                                 }
00145                                 import('Dataface/Utilities.php');
00146                                 
00147                                 
00148                                 Dataface_Utilities::fireEvent('after_action_new', array('record'=>$currentRecord));
00149                                         
00150                                 /*
00151                                  *
00152                                  * Since the form created a new record, then it makes more sense to redirect to this newly
00153                                  * created record than to the old record.  We used the 'keys' of the new record to generate
00154                                  * a redirect link.
00155                                  *
00156                                  */
00157                                 //$query = $form->_record->getValues(array_keys($form->_record->_table->keys()));
00158                                 $currentRecord->secureDisplay = false;
00159                                 if ( $currentRecord->checkPermission('edit') ){
00160                                         $nextAction = 'edit';
00161                                 } else {
00162                                         $nextAction = 'view';
00163                                 }
00164                                 $url = $currentRecord->getURL(array('-action'=>$nextAction));
00165                                 
00166                                 
00167                                 $msg = implode("\n", $app->getMessages());//@$response['--msg'];
00168                                 $msg = urlencode(trim(
00169                                         Dataface_LanguageTool::translate(
00170                                                 /* i18n id */
00171                                                 "Record successfully saved",
00172                                                 /* Default message */
00173                                                 "Record successfully saved."
00174                                         )."\n".$msg));
00175                                 if ( strpos($url, '?') === false ) $url .= '?';
00176                                 $link = $url.'&--msg='.$msg; 
00177                                 $app->redirect("$link");
00178                                 
00179                         }
00180                 
00181                 }
00182                 
00183                 ob_start();
00184                 $form->setDefaults($_GET);
00185                 $form->display();
00186                 $out = ob_get_contents();
00187                 ob_end_clean();
00188                 
00189                 if ( count($form->_errors) > 0 ){
00190                         $app->clearMessages();
00191                         $app->addError(PEAR::raiseError("Some errors occurred while processing this form: <ul><li>".implode('</li><li>', $form->_errors)."</li></ul>"));
00192                 }
00193                 
00194                 $context = array('form'=>&$out);
00195                 $context['tabs'] = $formTool->createHTMLTabs($currentRecord, $form, @$query['--tab']);
00196                         
00197                 df_display($context, 'Dataface_New_Record.html', true);
00198         }
00199 }
00200 
00201 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations