Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
TranslationForm.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 import('Dataface/QuickForm.php');
26 
27  var $sourceLanguage = null; // The name of the source language (2-digit code)
28  var $destinationLanguage = null; // The name of the destination language (2-digit code)
29  var $records = array(); // map of language codes to records for that language.
31  var $translatableLanguages = array();
32 
33  function getGoogleLanguage($lang){
34  if ( $lang == 'zt' ) return 'zh-TW';
35  else if ( $lang == 'zh' ) return 'zh-CN';
36  else return $lang;
37  }
38 
47  function Dataface_TranslationForm(&$record, $source=null, $dest=null, $query='', $fieldnames=null){
49 
50  if ( is_string($record) ){
51  // $record is just the name of a table.
53  } else {
54  $table =& $record->_table;
55  }
56 
57  $translations =& $table->getTranslations();
58  foreach (array_keys($translations) as $trans){
59  $table->getTranslation($trans);
60  }
61  //print_r($translations);
62  if ( !isset($translations) || count($translations) < 2 ){
63  // there are no translations to be made
64  throw new Exception(
65  df_translate(
66  'scripts.Dataface.TranslationForm.ERROR_NO_TRANSLATIONS',
67  'Attempt to translate a record in a table "'.$table->tablename.'" that contains no translations.',
68  array('table'=>$table->tablename)
69  )
70  , E_USER_ERROR);
71  }
72 
73  $this->translatableLanguages = array_keys($translations);
74 
75  $source = ( isset($source) ? $source : $this->translatableLanguages[0] );
76  $dest = ( isset($dest) ? $dest : $this->translatableLanguages[1] );
77  $i=0;
78  while ( $dest == $source ){
79  if ( $i>count($this->translatableLanguages)-1 ) throw new Exception("Failed to find an eligible language to translate to.");
80  $dest = $this->translatableLanguages[$i++];
81  }
82 
83 
84 
85  $this->sourceLanguage = $source;
86  $this->destinationLanguage = $dest;
87 
88  $this->_dest_translatedFields = $translations[$this->destinationLanguage];
89 
90 
91 
92  $this->Dataface_QuickForm($record, '', $query, 'translation_form', false, $fieldnames, $this->destinationLanguage );
93  $this->_renderer->elementTemplate = 'Dataface_TranslationForm_element.html';
94  $this->_renderer->groupeElementTemplate = 'Dataface_TranslationForm_groupelement.html';
95 
96  $this->loadRecords(); // loads all of the translations from the database.
97 
98 
99  }
100 
101  function loadRecords(){
102  $keyMissing = true;
103  if ( isset( $this->_record ) ){
104  $keyMissing = false;
105  foreach ( array_keys($this->_table->keys()) as $key ){
106  if ( !$this->_record->val($key) ){
107  // the current record is missing a primary key.
108  // we need to reload the record.
109  $keyMissing = true;
110  break;
111 
112  }
113  }
114  }
115 
116  if ( $keyMissing ){
117  return PEAR::raiseError(
118  df_translate(
119  'scripts.Dataface.TranslationForm.ERROR_NO_RECORD_FOUND',
120  "No record was found to be translated."
121  ), E_USER_ERROR);
122  }
123 
124  // Now we want to load all of the translations for the current record for use on this
125  // translation form.
126  $query = array();
127  foreach ( array_keys($this->_table->keys()) as $key ){
128  $query[$key] = '='.$this->_record->strval($key);
129  }
130  $io = new Dataface_IO($this->_table->tablename);
131 
132  foreach ( $this->translatableLanguages as $lang ){
133  $io->lang = $lang;
134  $record = new Dataface_Record($this->_table->tablename, array());
135  $io->read($query, $record);
136  $this->records[$lang] =& $record;
137  unset($record);
138  }
139 
140  unset($this->_record);
141  $this->_record =& $this->records[$this->destinationLanguage];
142 
143  }
144 
145 
146  function _buildWidget($field){
147  // notice that we pass $field by value here- so we can make changes without changing it
148  // throughout the rest of the application.
149 
150 
151  $res =& parent::_buildWidget($field);
152  if ( is_a($res, 'HTML_QuickForm_element') and is_array($this->_dest_translatedFields) and !in_array( $field['name'], $this->_dest_translatedFields ) ){
153  $res->freeze();
154  }
155  if ( $field['widget']['type'] != 'hidden' ){
156  $res->setProperty('translation', $this->records[$this->sourceLanguage]->display($field['name']));
157  }
158  return $res;
159 
160  }
161 
162  function getFormTemplate(){
163  $atts =& $this->_table->attributes();
164 
165  import('Dataface/TranslationTool.php');
166  $tt = new Dataface_TranslationTool();
167 
168  $status_selector_html = $tt->getHTMLStatusSelector($this->_record, $this->destinationLanguage,'__translation__[status]');
169  $trec =& $tt->getTranslationRecord($this->_record, $this->destinationLanguage);
170  $strec =& $tt->getTranslationRecord($this->_record, $this->sourceLanguage);
171  return "
172  <form{attributes}>
173  <fieldset>
174  <legend>".$atts['label']."</legend>
175  <table class=\"translation-form-table\">
176  <thead>
177  <tr>
178  <th width=\"150\" class=\"translation-label-cell-header\"><!-- Field name--></th>
179  <th width=\"325\" class=\"source-translation-cell-header\">".df_translate('scripts.Dataface.TranslationForm.LABEL_SOURCE_TRANSLATION','Source Translation')."</th>
180  <th width=\"325\" class=\"destination-translation-cell-header\">".df_translate('scripts.Dataface.TranslationForm.LABEL_DESTINATION_TRANSLATION','Destination Translation')."</th>
181  </tr>
182  </thead>
183  <tbody>
184  <tr><th>".df_translate('scripts.Dataface.TranslationForm.LABEL_TRANSLATION_STATUS','Translation Status').":</th>
185  <td>".df_translate('scripts.Dataface.TranslationForm.LABEL_VERSION','Version').": ".$strec->val('version')."</td>
186  <td>$status_selector_html Version: ".$trec->val('version')."</td>
187  </tr>
188  {content}
189  </tbody>
190  </table>
191  </fieldset>
192  </form>";
193 
194  }
195 
196  function getGroupTemplate($name){
197  $name = $this->_formatFieldName($name);
198  $group =& $this->_table->getField($name);
199  //print_r($group);
200  //if ( isset($this->_fields[$name]['widget']['description'] )){
201  // $description = $this->_fields[$name]['widget']['description'];
202  //} else {
203  // $description = '';
204  //}
205  $context = array( 'group'=>&$group, 'content'=>'{content}');
206  $skinTool =& Dataface_SkinTool::getInstance();
207  ob_start();
208  $skinTool->display($context, 'Dataface_TranslationForm_group.html');
209  $o = ob_get_contents();
210  ob_end_clean();
211 
212  return $o;
213  }
214 
215  function getFieldGroupTemplate($name){
216  $name = $this->_formatFieldName($name);
217  $group =& $this->_table->getFieldgroup($name);
218  //print_r($group);
219  //if ( isset($this->_fields[$name]['widget']['description'] )){
220  // $description = $this->_fields[$name]['widget']['description'];
221  //} else {
222  // $description = '';
223  //}
224 
225  $o = "<tr>
226  <th>".$group['label']."</th>
227  <td></td>
228  <td>
229  <table width=\"100%\" border=\"0\">
230  {content}
231  </table>
232  </td>
233 
234  </tr>";
235  return $o;
236  }
237 
238  function save($values){
239  $res = parent::save($values);
240 
241  import('Dataface/TranslationTool.php');
242  $tt = new Dataface_TranslationTool();
243  $tt->setTranslationStatus($this->_record, $this->destinationLanguage, $_POST['__translation__']['status']);
244  return $res;
245  }
246 
247  function display(){
248  if ( $this->_resultSet->found()>0 || $this->_new ){
249  $res = $this->_build();
250  if ( PEAR::isError($res) ){
251  return $res;
252  }
253  else {
254  //$this->displayTabs();
255  if ( !$this->_new and !Dataface_PermissionsTool::edit($this->_record) ){
256  $this->freeze();
257  }
258 
259  if ( $this->_new and !Dataface_PermissionsTool::checkPermission('new',$this->_table) ){
260  $this->freeze();
261  }
262  $this->accept($this->_renderer);
263  //$formTool =& Dataface_FormTool::getInstance();
264 
265 
266  if ( $this->_new || Dataface_PermissionsTool::view($this->_record) ){
267  echo $this->_renderer->toHtml();
268  //echo $formTool->display($this);
269  } else {
270  echo "<p>".df_translate('scripts.GLOBAL.INSUFFICIENT_PERMISSIONS_TO_VIEW_RECORD','Sorry you have insufficient permissions to view this record.')."</p>";
271  }
272  //parent::display();
273  }
274  } else {
275  echo "<p>".df_translate('scripts.GLOBAL.NO_RECORDS_MATCHED_REQUEST','No records matched your request.')."</p>";
276  }
277  }
278 
279 
280 
281 }