Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ImportForm.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  */
32 require_once 'HTML/QuickForm.php';
33 require_once 'HTML/QuickForm/optionalelement.php';
34 require_once 'Dataface/IO.php';
35 require_once 'Dataface/Record.php';
36 
40 class Dataface_ImportForm extends HTML_QuickForm {
41 
42  var $_table;
43  var $_relationship = null;
44  var $_record = null;
45  var $_built = false;
46  var $_step = 1;
47  var $_filterNames=array();
48 
49 
50  function Dataface_ImportForm( $tablename, $relationshipname=null ){
51  $this->_table =& Dataface_Table::loadTable($tablename);
52  if ( $relationshipname !== null ) $this->_relationship =& $this->_table->getRelationship($relationshipname);
53  else $this->_relationship =& $this->getRelationship($this->_table);
54 
55  $this->_record =& $this->getRecord();
56  $this->HTML_QuickForm("Import Form");
57 
58  if ( isset( $_REQUEST['--step'] ) ) $this->_step = $_REQUEST['--step'];
59 
60 
61  }
62 
63 
64 
69  function formSubmitted(){
70  return ( isset( $_POST['__keys__']) and isset( $_POST['-table']) );
71  }
72 
77  function &getRecord(){
79  $record = new Dataface_Record($_POST['-table'], array());
80  $io = new Dataface_IO($_POST['-table']);
81  $io->read($_POST['__keys__'], $record);
82  return $record;
83  } else {
85  $qt =& Dataface_QueryTool::loadResult($app->_currentTable);
86  $record =& $qt->loadCurrent();
87  return $record;
88  }
89  }
90 
91 
92  function &getRelationship(&$table){
93 
95  if ( !isset($_POST['-relationship']) ){
96  throw new Exception(
97  df_translate(
98  'scripts.Dataface.ImportForm.getRelationship.ERROR_RELATIONSHIP_NOT_FOUND',
99  'Field \'-relationship\' not found in Import Form.'
100  ), E_USER_ERROR);
101  }
102  $relname = $_POST['-relationship'];
103  if (strlen($relname) > 0 ){
104  $rel =& $table->getRelationship($relname);
105  return $rel;
106  } else {
107  $null = null;
108  return $null;
109  }
110  }
111 
112  else {
113  if ( !isset( $_GET['-relationship'] ) ){
114  $null = null;
115  return $null;
116  } else {
117  $relname = $_GET['-relationship'];
118  if (strlen($relname) > 0 ){
119  $rel =& $table->getRelationship($relname);
120  return $rel;
121  }
122  }
123  }
124  $null = null;
125  return $null;
126 
127 
128  }
129 
130  function _build(){
131  if ( $this->_built ) return;
133  $mainQuery = $app->getQuery();
134 
135  /*
136  * Add necessary flag fields so that the controller will find its way back here
137  * on submit.
138  */
139  $this->addElement('hidden','-table');
140  $this->addElement('hidden','--step');
141  $this->addElement('hidden','-relationship');
142  $this->addElement('hidden','-query');
143  $this->addElement('hidden','-action');
144 
145  $this->setDefaults( array('-table'=>$this->_table->tablename,
146  '--step'=>$this->_step,
147  '-action'=>'import',
148  '-query'=>$_SERVER['QUERY_STRING']) );
149 
150  if ( $this->_relationship !== null ){
151  $this->setDefaults( array('-relationship'=>$this->_relationship->getName()));
152  }
153 
154  /*
155  * Add keys of the current record as hidden fields so that we know we are importing
156  * into the correct record.
157  */
158  $factory = new HTML_QuickForm('factory');
159  $keyEls = array();
160  $keyDefaults = array();
161  foreach ( array_keys($this->_table->keys()) as $key ){
162  $keyEls[] = $factory->addElement('hidden', $key);
163 
164 
165  }
166  $this->addGroup($keyEls,'__keys__');
167  $keyvals = array();
168 
169  if ( is_object($this->_record) ){
170  foreach ( array_keys($this->_table->keys()) as $key ){
171  $keyvals[$key] = $this->_record->getValueAsString($key);
172  }
173  }
174  $this->setDefaults( array('__keys__'=>$keyvals) );
175 
176  /*
177  * Now add the fields of the form.
178  */
179 
180  if ( intval($this->_step) === 1 ){
181  /*
182  * Import filters define what formats can be imported into the table.
183  */
184  if ( $this->_relationship === null ){
185 
186  $filters =& $this->_table->getImportFilters();
187  $currentTableName = $this->_table->tablename;
188  $currentTable =& $this->_table;
189  $df_factory = df_create_new_record_form($currentTableName);
190 
191 
192  $fields = $this->_table->fields(false,true);
193 
194  } else {
195  $domainTablename = $this->_relationship->getDomainTable();
196 
197 
198  if ( PEAR::isError($domainTable) ){
199  $destTables =& $this->_relationship->getDestinationTables();
200  $domainTablename = $destTables[0];
201  }
202  $domainTable =& Dataface_Table::loadTable($domainTablename);
203  $currentTable =& $domainTable;
204  $currentTablename = $domainTable->tablename;
205  $filters =& $domainTable->getImportFilters();
206  //$df_factory = df_create_new_related_record_form($currentTablename, $this->_relationship->getName());
207 
208  $df_factory = df_create_new_record_form($domainTable->tablename);
209 
210  //$df_factory->_build();
211 
212  $fields = $domainTable->fields(false,true);
213 
214 
215  }
216 
217  $options = array(0=>df_translate('scripts.GLOBAL.FORMS.OPTION_PLEASE_SELECT','Please select ...'));
218  foreach ( array_keys($filters) as $key ){
219  $options[$key] = $filters[$key]->label;
220  $this->_filterNames[] = $key;
221  }
222  $this->addElement('select','filter',df_translate('scripts.Dataface.ImportForm._build.LABEL_IMPORT_FILE_FORMAT','Import File Format:'),$options, array('onchange'=>'updateFilterDescription(this.options[this.options.selectedIndex].value)'));
223 
224  $this->addElement('textarea','content',df_translate('scripts.Dataface.ImportForm._build.LABEL_PASTE_IMPORT_DATA','Paste Import Data'), array('cols'=>60,'rows'=>10));
225  $this->addElement('file','upload',df_translate('scripts.Dataface.ImportForm._build.LABEL_UPLOAD_IMPORT_DATA','Upload Import Data'));
226  $defaultValsEl =& $this->addElement( 'optionalelement', '__default_values__', 'Default Values');
227  require_once 'dataface-public-api.php';
228 
229  foreach (array_keys($fields) as $field){
230  if ( $fields[$field]['widget']['type'] == 'hidden' ){
231  $fields[$field]['widget']['type'] = 'text';
232  }
233  $tempEl = $df_factory->_buildWidget($fields[$field]);
234 
235  if (!$tempEl->getLabel() || $tempEl->_type == 'hidden' ) {
236  } else{
237  $defaultValsEl->addField($tempEl);
238  }
239  unset($tempEl);
240  }
241 
242 
243 
244  $this->addElement('submit', 'submit', 'Submit');
245 
246  $this->addRule('filter','required',df_translate('scripts.Dataface.ImportForm._build.MESSAGE_IMPORT_FILE_FORMAT_REQUIRED','Import File Format is a required field'),null,'client');
247  } else {
248  /*
249  * We are in step 2 where we are verifying the data only.
250  */
251  //$this->addElement('submit', 'back', 'Data is incorrect. Go back');
252  $this->addElement('submit', 'continue', df_translate('scripts.Dataface.ImportForm._build.MESSAGE_PROCEED_WITH_IMPORT','Looks good. Proceed with import'));
253  $this->addElement('hidden', '--importTablename');
254  $this->setDefaults(
255  array('--importTablename'=>$_REQUEST['--importTablename'])
256  );
257  }
258  // Set the return page
259  $returnPage = @$_SERVER['HTTP_REFERER'];
260  if ( isset($mainQuery['-redirect']) ){
261  $returnPage = $mainQuery['-redirect'];
262  } else if ( isset($mainQuery['--redirect']) ){
263  $returnPage = $mainQuery['--redirect'];
264  }
265 
266  if ( !$returnPage ){
267  if ( isset($this->_relationship) ){
268  $returnPage = $app->url('-action=related_records_list&-relationship='.$this->_relationship->getName());
269  } else {
270  $returnPage = $app->url('-action=list');
271  }
272 
273  }
274 
275  $this->addElement('hidden','--redirect');
276  $this->setDefaults(array('--redirect'=>$returnPage));
277 
278 
279 
280  $this->_built = true;
281  }
282 
283  function display(){
284  $this->_build();
285 
286  if ( $this->_step == 2 ){
287  require_once 'Dataface/RecordGrid.php';
288  $records = $this->loadImportTable();
289 
290  $grid = new Dataface_RecordGrid($records);
291  $grid->id="import-records-preview";
292  df_display(array('preview_data'=>$grid->toHTML(), 'num_records'=>count($records)), 'ImportForm_step2.html');
293 
294  }
295  $res = parent::display();
296  return $res;
297  }
298 
299  function loadImportTable(){
300 
301 
302  $dumpFile = $_SESSION['__dataface__import_data__'];
303  $importData = unserialize(file_get_contents($dumpFile));
304 
305  $tablename = $this->_table->tablename;
306  if ( $this->_relationship !== null ){
307  $tablename = $this->_relationship->getDomainTable();
308  if ( PEAR::isError($tablename) ){
309  $destTables =& $this->_relationship->getDestinationTables();
310  $tablename = $destTables[0]->tablename;
311  }
312  }
313 
314  $records = array();
315  foreach ($importData['rows'] as $row){
316  if ( isset($row['__CLASS__']) and isset($row['__CLASSPATH__']) ){
317  if ( @$row['__CLASSPATH__'] and !class_exists($row['__CLASS__']) ){
318  import($row['__CLASSPATH__']);
319  }
320  $class = $row['__CLASS__'];
321  $importRecord = new $class($row);
322  $records[] = $importRecord->getValues();
323  unset($importRecord);
324  } else {
325  $records[] = new Dataface_Record($tablename, $row);
326  }
327  }
328 
329  return $records;
330 
331  }
332 
333  function import($values){
334 
335  if ( intval($this->_step) === 1 ){
336 
337 
338 
339  $upload =& $this->getElement('upload');
340  if ( $upload->isUploadedFile() ){
341  /*
342  * A file was uploaded.
343  */
344  $val =& $upload->getValue();
345  $data = file_get_contents($val['tmp_name']);
346 
347 
348  } else {
349  /*
350  * No file was uploaded so we will get data from the paste field.
351  */
352  $data = $values['content'];
353  }
354 
355 
356  $io = new Dataface_IO($this->_table->tablename);
357  $relname = ( $this->_relationship === null ) ? null : $this->_relationship->getName();
358 
359  $importTablename = $io->importData($this->_record, $data, $values['filter'], $relname, false, @$values['__default_values__']);
360  return $importTablename;
361  }
362 
363  else if ( $this->_step == 2 ){
364 
365  $io = new Dataface_IO($this->_table->tablename);
366  $relname = ( $this->_relationship === null ) ? null : $this->_relationship->getName();
367  $records = $io->importData($this->_record, $values['--importTablename'], @$values['filter'], $relname, true);
368  return $records;
369  }
370 
371 
372  }
373 
374  function validate(){
375 
376  if ( intval($this->_step) === 1 ){
377 
378  return (!empty($_POST['filter']) and !empty($_POST['-query']) and !empty($_POST['-table']));
379  } else {
380  return (!empty($_POST['-query']) and !empty($_POST['-table']) and !empty($_POST['--importTablename']));
381 
382 
383  }
384 
385  }
386 
387 
388 
389 }