Xataface 2.0
Xataface Application Framework
actions/translate.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class dataface_actions_translate {
00004 
00005         function handle(&$params){
00006                 import('Dataface/TranslationForm.php');
00007                                 
00008                 $app =& Dataface_Application::getInstance();
00009                 $query =& $app->getQuery();
00010                 $resultSet =& $app->getResultSet();
00011                 
00012                 $source = ( isset($_REQUEST['-sourceLanguage']) ? $_REQUEST['-sourceLanguage'] : $app->_conf['default_language']);
00013                 $dest = ( isset($_REQUEST['-destinationLanguage']) ? $_REQUEST['-destinationLanguage'] : null);
00014                 
00015                 
00016                 if ( $resultSet->found()>0){
00017                         $form = new Dataface_TranslationForm($query['-table'], $source, $dest);
00018                         /*
00019                          * There is either a result to edit, or we are creating a new record.
00020                          *
00021                          */
00022                          
00023                         $res = $form->_build();
00024                         if ( PEAR::isError($res) ){
00025                                 throw new Exception($res->toString().Dataface_Error::printStackTrace(), E_USER_ERROR);
00026                         
00027                         }
00028                         
00029                         /*
00030                          *
00031                          * We need to add the current GET parameter flags (the GET vars starting with '-') so
00032                          * that the controller knows to pass control to this method again upon form submission.
00033                          *
00034                          */
00035                         foreach ( $query as $key=>$value){
00036                                 if ( strpos($key,'-')===0 ){
00037                                         $form->addElement('hidden', $key);
00038                                         $form->setDefaults( array( $key=>$value) );
00039                                         
00040                                 }
00041                         }
00042                         
00043                         /*
00044                          * Store the current query string (the portion after the '?') in the form, so we 
00045                          * can retrieve it after and redirect back to our original location.
00046                          */
00047                         $form->addElement('hidden', '-query');
00048                         $form->setDefaults( array( '-action'=>$query['-action'],'-query'=>$_SERVER['QUERY_STRING']) );
00049                         
00050                         
00051                         /*
00052                          * 
00053                          * We have to deal with 3 cases.
00054                          *      1) The form has not been submitted.
00055                          *      2) The form was submitted but didn't validate (ie: it had some bad input)
00056                          *      3) The form was submitted and was validated.
00057                          *
00058                          * We deal with Case 3 first...
00059                          *
00060                          */
00061                 
00062                         if ( $form->validate() ){
00063                                 /*
00064                                  *
00065                                  * The form was submitted and it validated ok.  We now process it (ie: save its contents).
00066                                  *
00067                                  */
00068                                 $app->clearMessages();
00069                                 $result = $form->process( array( &$form, 'save') );
00070                                 $success = true;
00071                                 $response =& Dataface_Application::getResponse();
00072                                 
00073                                 if ( !$result ){
00074                                         error_log("Error occurred in save: ".mysql_error( $app->db()).Dataface_Error::printStackTrace());
00075                                         throw new Exception("Error occurred in save.  See error log for details.");
00076                                         
00077                                         
00078                                 } else if ( PEAR::isError($result) && !Dataface_Error::isNotice($result) ){
00079                                         //echo "Error..";
00080                                         if ( Dataface_Error::isDuplicateEntry($result) ){
00081                                                 return $result;
00082                                                 
00083                                         } else {
00084                                                 //echo "not dup entry"; exit;
00085                                                 throw new Exception($result->toString(), E_USER_ERROR);
00086                                                 
00087                                         }
00088                                 } else if ( Dataface_Error::isNotice($result) ){
00089                                         $app->addError($result);
00090 
00091                                         //$response['--msg'] = @$response['--msg'] ."\n".$result->getMessage();
00092                                         $success = false;
00093                                 }
00094                                 
00095                                 
00096                                 if ( $success ){
00097                                         /*
00098                                          *
00099                                          * The original query string will have the -new flag set.  We need to remove this 
00100                                          * flag so that we don't redirect the user to create another new record.
00101                                          *
00102                                          */
00103                                         $vals = $form->exportValues();
00104                                         $vals['-query'] = preg_replace('/[&\?]-new=[^&]+/i', '', $vals['-query']);
00105                                         
00106                                         $msg = implode("\n", $app->getMessages());
00107                                         //$msg =@$response['--msg'];
00108                                         $msg = urlencode(
00109                                                 Dataface_LanguageTool::translate(
00110                                                         /* i18n id */
00111                                                         'Record successfully translated',
00112                                                         /* Default success message */
00113                                                         "Record successfully translated.<br>"
00114                                                 ).$msg
00115                                         );
00116                                         $link = $_SERVER['HOST_URI'].DATAFACE_SITE_HREF.'?'.$vals['-query'].'&--msg='.$msg;
00117                                         
00118                                         
00119                                         /*
00120                                          *
00121                                          * Redirect the user to the appropriate record.
00122                                          *
00123                                          */
00124                                         $app->redirect($link);
00125                                         
00126                                 }
00127                         }
00128                         
00129                         ob_start();
00130                         $form->display();
00131                         $out = ob_get_contents();
00132                         ob_end_clean();
00133                         
00134                         
00135                         $context = array('form'=>$out, 'formObj'=>$form);
00136                         
00137                                  
00138                 } else {
00139                         // no records were found
00140                         $context = array('form'=>'', 'formObj'=>$form);
00141                         $app->addMessage(
00142                                 Dataface_LanguageTool::translate(
00143                                         'No records matched request',
00144                                         'No records matched your request'
00145                                         )
00146                                 );
00147                 }
00148                 
00149                 
00150                 if ( isset($query['-template']) ) $template = $query['-template'];
00151                 else if ( isset( $params['action']['template']) ) $template = $params['action']['template'];
00152                 else $template = 'Dataface_Translate_Record.html';
00153                 df_display($context, $template, true);
00154                 
00155         }
00156 
00157 }
All Data Structures Namespaces Files Functions Variables Enumerations