![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 /*------------------------------------------------------------------------------- 00003 * Xataface Web Application Framework 00004 * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *------------------------------------------------------------------------------- 00020 */ 00021 import('Dataface/QuickForm.php'); 00025 class Dataface_TranslationForm extends Dataface_QuickForm { 00026 00027 var $sourceLanguage = null; // The name of the source language (2-digit code) 00028 var $destinationLanguage = null; // The name of the destination language (2-digit code) 00029 var $records = array(); // map of language codes to records for that language. 00030 var $_dest_translatedFields; 00031 var $translatableLanguages = array(); 00032 00033 function getGoogleLanguage($lang){ 00034 if ( $lang == 'zt' ) return 'zh-TW'; 00035 else if ( $lang == 'zh' ) return 'zh-CN'; 00036 else return $lang; 00037 } 00038 00047 function Dataface_TranslationForm(&$record, $source=null, $dest=null, $query='', $fieldnames=null){ 00048 $app =& Dataface_Application::getInstance(); 00049 00050 if ( is_string($record) ){ 00051 // $record is just the name of a table. 00052 $table =& Dataface_Table::loadTable($record); 00053 } else { 00054 $table =& $record->_table; 00055 } 00056 00057 $translations =& $table->getTranslations(); 00058 foreach (array_keys($translations) as $trans){ 00059 $table->getTranslation($trans); 00060 } 00061 //print_r($translations); 00062 if ( !isset($translations) || count($translations) < 2 ){ 00063 // there are no translations to be made 00064 throw new Exception( 00065 df_translate( 00066 'scripts.Dataface.TranslationForm.ERROR_NO_TRANSLATIONS', 00067 'Attempt to translate a record in a table "'.$table->tablename.'" that contains no translations.', 00068 array('table'=>$table->tablename) 00069 ) 00070 , E_USER_ERROR); 00071 } 00072 00073 $this->translatableLanguages = array_keys($translations); 00074 00075 $source = ( isset($source) ? $source : $this->translatableLanguages[0] ); 00076 $dest = ( isset($dest) ? $dest : $this->translatableLanguages[1] ); 00077 $i=0; 00078 while ( $dest == $source ){ 00079 if ( $i>count($this->translatableLanguages)-1 ) throw new Exception("Failed to find an eligible language to translate to."); 00080 $dest = $this->translatableLanguages[$i++]; 00081 } 00082 00083 00084 00085 $this->sourceLanguage = $source; 00086 $this->destinationLanguage = $dest; 00087 00088 $this->_dest_translatedFields = $translations[$this->destinationLanguage]; 00089 00090 00091 00092 $this->Dataface_QuickForm($record, '', $query, 'translation_form', false, $fieldnames, $this->destinationLanguage ); 00093 $this->_renderer->elementTemplate = 'Dataface_TranslationForm_element.html'; 00094 $this->_renderer->groupeElementTemplate = 'Dataface_TranslationForm_groupelement.html'; 00095 00096 $this->loadRecords(); // loads all of the translations from the database. 00097 00098 00099 } 00100 00101 function loadRecords(){ 00102 $keyMissing = true; 00103 if ( isset( $this->_record ) ){ 00104 $keyMissing = false; 00105 foreach ( array_keys($this->_table->keys()) as $key ){ 00106 if ( !$this->_record->val($key) ){ 00107 // the current record is missing a primary key. 00108 // we need to reload the record. 00109 $keyMissing = true; 00110 break; 00111 00112 } 00113 } 00114 } 00115 00116 if ( $keyMissing ){ 00117 return PEAR::raiseError( 00118 df_translate( 00119 'scripts.Dataface.TranslationForm.ERROR_NO_RECORD_FOUND', 00120 "No record was found to be translated." 00121 ), E_USER_ERROR); 00122 } 00123 00124 // Now we want to load all of the translations for the current record for use on this 00125 // translation form. 00126 $query = array(); 00127 foreach ( array_keys($this->_table->keys()) as $key ){ 00128 $query[$key] = '='.$this->_record->strval($key); 00129 } 00130 $io = new Dataface_IO($this->_table->tablename); 00131 00132 foreach ( $this->translatableLanguages as $lang ){ 00133 $io->lang = $lang; 00134 $record = new Dataface_Record($this->_table->tablename, array()); 00135 $io->read($query, $record); 00136 $this->records[$lang] =& $record; 00137 unset($record); 00138 } 00139 00140 unset($this->_record); 00141 $this->_record =& $this->records[$this->destinationLanguage]; 00142 00143 } 00144 00145 00146 function _buildWidget($field){ 00147 // notice that we pass $field by value here- so we can make changes without changing it 00148 // throughout the rest of the application. 00149 00150 00151 $res =& parent::_buildWidget($field); 00152 if ( is_a($res, 'HTML_QuickForm_element') and is_array($this->_dest_translatedFields) and !in_array( $field['name'], $this->_dest_translatedFields ) ){ 00153 $res->freeze(); 00154 } 00155 if ( $field['widget']['type'] != 'hidden' ){ 00156 $res->setProperty('translation', $this->records[$this->sourceLanguage]->display($field['name'])); 00157 } 00158 return $res; 00159 00160 } 00161 00162 function getFormTemplate(){ 00163 $atts =& $this->_table->attributes(); 00164 00165 import('Dataface/TranslationTool.php'); 00166 $tt = new Dataface_TranslationTool(); 00167 00168 $status_selector_html = $tt->getHTMLStatusSelector($this->_record, $this->destinationLanguage,'__translation__[status]'); 00169 $trec =& $tt->getTranslationRecord($this->_record, $this->destinationLanguage); 00170 $strec =& $tt->getTranslationRecord($this->_record, $this->sourceLanguage); 00171 return " 00172 <form{attributes}> 00173 <fieldset> 00174 <legend>".$atts['label']."</legend> 00175 <table class=\"translation-form-table\"> 00176 <thead> 00177 <tr> 00178 <th width=\"150\" class=\"translation-label-cell-header\"><!-- Field name--></th> 00179 <th width=\"325\" class=\"source-translation-cell-header\">".df_translate('scripts.Dataface.TranslationForm.LABEL_SOURCE_TRANSLATION','Source Translation')."</th> 00180 <th width=\"325\" class=\"destination-translation-cell-header\">".df_translate('scripts.Dataface.TranslationForm.LABEL_DESTINATION_TRANSLATION','Destination Translation')."</th> 00181 </tr> 00182 </thead> 00183 <tbody> 00184 <tr><th>".df_translate('scripts.Dataface.TranslationForm.LABEL_TRANSLATION_STATUS','Translation Status').":</th> 00185 <td>".df_translate('scripts.Dataface.TranslationForm.LABEL_VERSION','Version').": ".$strec->val('version')."</td> 00186 <td>$status_selector_html Version: ".$trec->val('version')."</td> 00187 </tr> 00188 {content} 00189 </tbody> 00190 </table> 00191 </fieldset> 00192 </form>"; 00193 00194 } 00195 00196 function getGroupTemplate($name){ 00197 $name = $this->_formatFieldName($name); 00198 $group =& $this->_table->getField($name); 00199 //print_r($group); 00200 //if ( isset($this->_fields[$name]['widget']['description'] )){ 00201 // $description = $this->_fields[$name]['widget']['description']; 00202 //} else { 00203 // $description = ''; 00204 //} 00205 $context = array( 'group'=>&$group, 'content'=>'{content}'); 00206 $skinTool =& Dataface_SkinTool::getInstance(); 00207 ob_start(); 00208 $skinTool->display($context, 'Dataface_TranslationForm_group.html'); 00209 $o = ob_get_contents(); 00210 ob_end_clean(); 00211 00212 return $o; 00213 } 00214 00215 function getFieldGroupTemplate($name){ 00216 $name = $this->_formatFieldName($name); 00217 $group =& $this->_table->getFieldgroup($name); 00218 //print_r($group); 00219 //if ( isset($this->_fields[$name]['widget']['description'] )){ 00220 // $description = $this->_fields[$name]['widget']['description']; 00221 //} else { 00222 // $description = ''; 00223 //} 00224 00225 $o = "<tr> 00226 <th>".$group['label']."</th> 00227 <td></td> 00228 <td> 00229 <table width=\"100%\" border=\"0\"> 00230 {content} 00231 </table> 00232 </td> 00233 00234 </tr>"; 00235 return $o; 00236 } 00237 00238 function save($values){ 00239 $res = parent::save($values); 00240 00241 import('Dataface/TranslationTool.php'); 00242 $tt = new Dataface_TranslationTool(); 00243 $tt->setTranslationStatus($this->_record, $this->destinationLanguage, $_POST['__translation__']['status']); 00244 return $res; 00245 } 00246 00247 function display(){ 00248 if ( $this->_resultSet->found()>0 || $this->_new ){ 00249 $res = $this->_build(); 00250 if ( PEAR::isError($res) ){ 00251 return $res; 00252 } 00253 else { 00254 //$this->displayTabs(); 00255 if ( !$this->_new and !Dataface_PermissionsTool::edit($this->_record) ){ 00256 $this->freeze(); 00257 } 00258 00259 if ( $this->_new and !Dataface_PermissionsTool::checkPermission('new',$this->_table) ){ 00260 $this->freeze(); 00261 } 00262 $this->accept($this->_renderer); 00263 //$formTool =& Dataface_FormTool::getInstance(); 00264 00265 00266 if ( $this->_new || Dataface_PermissionsTool::view($this->_record) ){ 00267 echo $this->_renderer->toHtml(); 00268 //echo $formTool->display($this); 00269 } else { 00270 echo "<p>".df_translate('scripts.GLOBAL.INSUFFICIENT_PERMISSIONS_TO_VIEW_RECORD','Sorry you have insufficient permissions to view this record.')."</p>"; 00271 } 00272 //parent::display(); 00273 } 00274 } else { 00275 echo "<p>".df_translate('scripts.GLOBAL.NO_RECORDS_MATCHED_REQUEST','No records matched your request.')."</p>"; 00276 } 00277 } 00278 00279 00280 00281 }