Xataface 2.0
Xataface Application Framework
actions/edit.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_edit {
00033         function handle(&$params){
00034                 import( 'Dataface/FormTool.php');
00035                 import( 'Dataface/QuickForm.php');
00036                 
00037                                 
00038                 $app =& Dataface_Application::getInstance();
00039                 $query =& $app->getQuery();
00040                 $resultSet =& $app->getResultSet();
00041                 
00042                 $currentRecord =& $app->getRecord();
00043                 $currentTable =& Dataface_Table::loadTable($query['-table']);
00044                 if ( !isset($query['--tab']) and count($currentTable->tabs($currentRecord)) > 1 ){
00045                         list($query['--tab']) = array_keys($currentTable->tabs($currentRecord));
00046                 } else if ( count($currentTable->tabs($currentRecord)) <= 1 ){
00047                         unset($query['--tab']);
00048                 }
00049                 
00050                 
00051                 
00052                 /*
00053                  *
00054                  * Create the quickform for the current record.
00055                  *
00056                  */
00057                 //$form = new Dataface_QuickForm($query['-table'], $app->db(),  $query);
00058                 $formTool =& Dataface_FormTool::getInstance();
00059                 
00060                 
00061                 if ( $resultSet->found()> @$query['-cursor']){
00062                         $form = $formTool->createRecordForm($currentRecord, false, @$query['--tab'], $query);
00063                         /*
00064                          * There is either a result to edit, or we are creating a new record.
00065                          *
00066                          */
00067                          
00068                         $res = $form->_build();
00069                         if ( PEAR::isError($res) ){
00070                                 error_log($res->toString().implode("\n", $res->getBacktrace()));
00071                                 throw new Exception("An error occurred while building the edit form.  See error log for details.", E_USER_ERROR);
00072                                 
00073                         
00074                         }
00075                         $formTool->decorateRecordForm($currentRecord, $form, false, @$query['--tab']);
00076                         
00077                         
00078                         /*
00079                          *
00080                          * We need to add the current GET parameter flags (the GET vars starting with '-') so
00081                          * that the controller knows to pass control to this method again upon form submission.
00082                          *
00083                          */
00084                         foreach ( $query as $key=>$value){
00085                                 if ( strpos($key,'-')===0 ){
00086                                         $form->addElement('hidden', $key);
00087                                         $form->setDefaults( array( $key=>$value) );
00088                                         
00089                                 }
00090                         }
00091                         
00092                         /*
00093                          * Store the current query string (the portion after the '?') in the form, so we 
00094                          * can retrieve it after and redirect back to our original location.
00095                          */
00096                         $form->addElement('hidden', '-query');
00097                         $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
00098                         
00099                         
00100                         /*
00101                          * 
00102                          * We have to deal with 3 cases.
00103                          *      1) The form has not been submitted.
00104                          *      2) The form was submitted but didn't validate (ie: it had some bad input)
00105                          *      3) The form was submitted and was validated.
00106                          *
00107                          * We deal with Case 3 first...
00108                          *
00109                          */
00110                 
00111                         if ( $formTool->validateRecordForm($currentRecord, $form, false, @$query['--tab']) ){
00112                                 /*
00113                                  *
00114                                  * The form was submitted and it validated ok.  We now process it (ie: save its contents).
00115                                  *
00116                                  */
00117                                 $app->clearMessages();
00118                                 $formTool->handleTabSubmit($currentRecord, $form, @$query['--tab']);
00119                                 if ( !isset($query['--tab']) ){
00120                                         // If we aren't using tabs we just do it the old way.
00121                                         // (If it ain't broke don't fix it
00122                                         
00123                                         $result = $form->process( array( &$form, 'save') );
00124                                 } else {
00125                                         // If we are using tabs, we will use the formtool's 
00126                                         // session aware saving function
00127                                         
00128                                         $result = $formTool->saveSession($currentRecord);
00129                                 }
00130                                 $success = true;
00131                                 $response =& Dataface_Application::getResponse();
00132                                 
00133                                 if ( !$result ){
00134                                         error_log("Error occurred in save: ".mysql_error( $app->db()).Dataface_Error::printStackTrace());
00135                                         throw new Exception("An error occurred while attempting to save the record.  See error log for details.", E_USER_ERROR);
00136                                 } else if ( PEAR::isError($result) && !Dataface_Error::isNotice($result) ){
00137                                         
00138                                         if ( Dataface_Error::isDuplicateEntry($result) ){
00139                                                 $app->addError($result);
00140                                                 $success = false;
00141                                                 
00142                                         } else {
00143                                                 error_log($result->toString(). implode("\n", $result->getBacktrace()));
00144                                                 throw new Exception("An error occurred while attempting to save the record.  See error log for details.", E_USER_ERROR);
00145                                                 
00146                                         }
00147                                 } else if ( Dataface_Error::isNotice($result) ){
00148                                         $app->addError($result);
00149 
00150                                         //$response['--msg'] = @$response['--msg'] ."\n".$result->getMessage();
00151                                         $success = false;
00152                                 }
00153                                 
00154                                 
00155                                 if ( $success ){
00156                                         
00157                                         if (@$query['-response'] == 'json' ){
00158                                                 //header('Content-type: text/json; charset="'.$app->_conf['oe'].'"');
00159                                                 $rvals = $currentRecord->strvals();
00160                                                 $rvals['__title__'] = $currentRecord->getTitle();
00161                                                 $rvals['__id__'] = $currentRecord->getId();
00162                                                 echo json_encode(array('response_code'=>200, 'record_data'=> $rvals, 'response_message'=>df_translate('Record Successfully Saved', 'Record Successfully Saved')));
00163                                                 return;
00164                                         }
00165                                         
00166                                         import('Dataface/Utilities.php');
00167                                         Dataface_Utilities::fireEvent('after_action_edit', array('record'=>$form->_record));
00168                                         /*
00169                                          *
00170                                          * The original query string will have the -new flag set.  We need to remove this 
00171                                          * flag so that we don't redirect the user to create another new record.
00172                                          *
00173                                          */
00174                                         $vals = $form->exportValues();
00175                                         $vals['-query'] = preg_replace('/[&\?]-new=[^&]+/i', '', $vals['-query']);
00176                                         
00177                                         $_SESSION['--last_modified_record_url'] = $form->_record->getURL();
00178                                         $_SESSION['--last_modified_record_title'] = $form->_record->getTitle();
00179                                         
00180                                         $msg = implode("\n", $app->getMessages());
00181                                         //$msg =@$response['--msg'];
00182                                         $msg = urlencode(
00183                                                 Dataface_LanguageTool::translate(
00184                                                         /* i18n id */
00185                                                         'Record successfully saved',
00186                                                         /* Default success message */
00187                                                         "Record successfully saved.<br>"
00188                                                 ).$msg
00189                                         );
00190                                         
00191                                         if ( preg_match('/[&\?]-action=edit&/', $vals['-query']) and !$form->_record->checkPermission('edit') ){
00192                                                 $vals['-query'] = preg_replace('/([&\?])-action=edit&/', '$1-action=view&', $vals['-query']);
00193                                         } else if ( preg_match('/[&\?]-action=edit$/', $vals['-query']) and !$form->_record->checkPermission('edit') ){
00194                                                 $vals['-query'] = preg_replace('/([&\?])-action=edit$/', '$1-action=view', $vals['-query']);
00195                                         }
00196                                         
00197                                         $link = $_SERVER['HOST_URI'].DATAFACE_SITE_HREF.'?'.$vals['-query'].'&--msg='.$msg;
00198                                         
00199                                         
00200                                         /*
00201                                          *
00202                                          * Redirect the user to the appropriate record.
00203                                          *
00204                                          */
00205                                         $app->redirect("$link");
00206                                 }
00207                         }
00208                         
00209                         ob_start();
00210                         $form->display();
00211                         $out = ob_get_contents();
00212                         ob_end_clean();
00213                         
00214                         if ( count($form->_errors) > 0 ){
00215                                 $app->clearMessages();
00216                                 $app->addError(PEAR::raiseError("Some errors occurred while processing this form: <ul><li>".implode('</li><li>', $form->_errors)."</li></ul>"));
00217                         }
00218                         $context = array('form'=>$out);
00219                         
00220                         
00221                         // Now let's add the tabs to the context
00222                         $context['tabs'] = $formTool->createHTMLTabs($currentRecord, $form, @$query['--tab']);
00223                         
00224                                  
00225                 } else {
00226                         // no records were found
00227                         $context = array('form'=>'');
00228                         
00229                         if ( isset($_SESSION['--last_modified_record_url']) ){
00230                                 $lastModifiedURL = $_SESSION['--last_modified_record_url'];
00231                                 $lastModifiedTitle = $_SESSION['--last_modified_record_title'];
00232                                 unset($_SESSION['--last_modified_record_title']);
00233                                 unset($_SESSION['--last_modified_record_url']);
00234                                 $app->addMessage(
00235                                         df_translate(
00236                                                 'Return to last modified record',
00237                                                 'No records matched your request.  Click <a href="'.$lastModifiedURL.'">here</a> to return to <em>'.htmlspecialchars($lastModifiedTitle).'</em>.',
00238                                                 array('lastModifiedURL'=>$lastModifiedURL,
00239                                                          'lastModifiedTitle'=>$lastModifiedTitle
00240                                                         )
00241                                                 )
00242                                         );
00243                         } else {
00244                                 $app->addMessage(
00245                                         Dataface_LanguageTool::translate(
00246                                                 'No records matched request',
00247                                                 'No records matched your request'
00248                                                 )
00249                                         );
00250                         }
00251                         
00252                         $query['-template'] = 'Dataface_Main_Template.html';
00253                 }
00254                 
00255                 
00256                 if ( isset($query['-template']) ) $template = $query['-template'];
00257                 else if ( isset( $params['action']['template']) ) $template = $params['action']['template'];
00258                 else $template = 'Dataface_Edit_Record.html';
00259                 
00260 
00261                 df_display($context, $template, true);
00262                 
00263         }
00264         
00265 }
00266 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations