Xataface 2.0
Xataface Application Framework
Dataface/ImportForm.php
Go to the documentation of this file.
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  */
00032 require_once 'HTML/QuickForm.php';
00033 require_once 'HTML/QuickForm/optionalelement.php';
00034 require_once 'Dataface/IO.php';
00035 require_once 'Dataface/Record.php';
00036 
00040 class Dataface_ImportForm extends HTML_QuickForm {
00041 
00042         var $_table;
00043         var $_relationship = null;
00044         var $_record = null;
00045         var $_built = false;
00046         var $_step = 1;
00047         var $_filterNames=array();
00048         
00049         
00050         function Dataface_ImportForm( $tablename, $relationshipname=null ){
00051                 $this->_table =& Dataface_Table::loadTable($tablename);
00052                 if ( $relationshipname !== null ) $this->_relationship =& $this->_table->getRelationship($relationshipname);
00053                 else $this->_relationship =& $this->getRelationship($this->_table);
00054                 
00055                 $this->_record =& $this->getRecord();
00056                 $this->HTML_QuickForm("Import Form");
00057                 
00058                 if ( isset( $_REQUEST['--step'] ) ) $this->_step = $_REQUEST['--step'];
00059         
00060 
00061         }
00062         
00063         
00064         
00069         function formSubmitted(){
00070                 return ( isset( $_POST['__keys__']) and isset( $_POST['-table']) );
00071         }
00072         
00077         function &getRecord(){
00078                 if ( Dataface_ImportForm::formSubmitted() ){
00079                         $record = new Dataface_Record($_POST['-table'], array());
00080                         $io = new Dataface_IO($_POST['-table']);
00081                         $io->read($_POST['__keys__'], $record);
00082                         return $record;
00083                 } else {
00084                         $app =& Dataface_Application::getInstance();
00085                         $qt =& Dataface_QueryTool::loadResult($app->_currentTable);
00086                         $record =& $qt->loadCurrent();
00087                         return $record;
00088                 }
00089         }
00090         
00091         
00092         function &getRelationship(&$table){
00093                 
00094                 if ( Dataface_ImportForm::formSubmitted() ){
00095                         if ( !isset($_POST['-relationship']) ){
00096                                 throw new Exception(
00097                                         df_translate(
00098                                                 'scripts.Dataface.ImportForm.getRelationship.ERROR_RELATIONSHIP_NOT_FOUND',
00099                                                 'Field \'-relationship\' not found in Import Form.'
00100                                                 ), E_USER_ERROR);
00101                         }
00102                         $relname = $_POST['-relationship'];
00103                         if (strlen($relname) > 0 ){
00104                                 $rel =& $table->getRelationship($relname);
00105                                 return $rel;
00106                         } else {
00107                                 $null = null;
00108                                 return $null;
00109                         }
00110                 }
00111                 
00112                 else {
00113                         if ( !isset( $_GET['-relationship'] ) ){
00114                                 $null = null;
00115                                 return $null;
00116                         } else {
00117                                 $relname = $_GET['-relationship'];
00118                                 if (strlen($relname) > 0 ){
00119                                         $rel =& $table->getRelationship($relname);
00120                                         return $rel;
00121                                 }
00122                         }
00123                 }
00124                 $null = null;
00125                 return $null;
00126                 
00127         
00128         }
00129         
00130         function _build(){
00131                 if ( $this->_built ) return;
00132                 $app =& Dataface_Application::getInstance();
00133                 $mainQuery = $app->getQuery();
00134                 
00135                 /*
00136                  * Add necessary flag fields so that the controller will find its way back here
00137                  * on submit.
00138                  */
00139                 $this->addElement('hidden','-table');
00140                 $this->addElement('hidden','--step');
00141                 $this->addElement('hidden','-relationship');
00142                 $this->addElement('hidden','-query');
00143                 $this->addElement('hidden','-action');
00144                 
00145                 $this->setDefaults( array('-table'=>$this->_table->tablename,
00146                                                                 '--step'=>$this->_step,
00147                                                                 '-action'=>'import',
00148                                                                 '-query'=>$_SERVER['QUERY_STRING']) );
00149                 
00150                 if ( $this->_relationship !== null ){
00151                         $this->setDefaults( array('-relationship'=>$this->_relationship->getName()));
00152                 }
00153                 
00154                 /*
00155                  * Add keys of the current record as hidden fields so that we know we are importing
00156                  * into the correct record.
00157                  */
00158                 $factory = new HTML_QuickForm('factory');
00159                 $keyEls = array();
00160                 $keyDefaults = array();
00161                 foreach ( array_keys($this->_table->keys()) as $key ){
00162                         $keyEls[] = $factory->addElement('hidden', $key);
00163                         
00164                         
00165                 }
00166                 $this->addGroup($keyEls,'__keys__');
00167                 $keyvals = array();
00168 
00169                 if ( is_object($this->_record) ){
00170                         foreach ( array_keys($this->_table->keys()) as $key ){
00171                                 $keyvals[$key] = $this->_record->getValueAsString($key);
00172                         }
00173                 }
00174                 $this->setDefaults( array('__keys__'=>$keyvals) );
00175                 
00176                 /*
00177                  * Now add the fields of the form.
00178                  */
00179 
00180                 if ( intval($this->_step) === 1 ){
00181                         /*
00182                          * Import filters define what formats can be imported into the table.
00183                          */
00184                         if ( $this->_relationship === null ){
00185 
00186                                 $filters =& $this->_table->getImportFilters();
00187                                 $currentTableName = $this->_table->tablename;
00188                                 $currentTable =& $this->_table;
00189                                 $df_factory = df_create_new_record_form($currentTableName);
00190                                 
00191                                                                                 
00192                                 $fields = $this->_table->fields(false,true);
00193                                 
00194                         } else {
00195                                 $domainTablename = $this->_relationship->getDomainTable();
00196 
00197                                 
00198                                 if ( PEAR::isError($domainTable) ){
00199                                         $destTables =& $this->_relationship->getDestinationTables();
00200                                         $domainTablename = $destTables[0];
00201                                 }
00202                                 $domainTable =& Dataface_Table::loadTable($domainTablename);
00203                                 $currentTable =& $domainTable;
00204                                 $currentTablename = $domainTable->tablename;
00205                                 $filters =& $domainTable->getImportFilters();
00206                                 //$df_factory = df_create_new_related_record_form($currentTablename, $this->_relationship->getName());
00207 
00208                                 $df_factory = df_create_new_record_form($domainTable->tablename);
00209 
00210                                 //$df_factory->_build();
00211 
00212                                 $fields = $domainTable->fields(false,true);
00213                         
00214                                 
00215                         }
00216                         
00217                         $options = array(0=>df_translate('scripts.GLOBAL.FORMS.OPTION_PLEASE_SELECT','Please select ...'));
00218                         foreach ( array_keys($filters) as $key ){
00219                                 $options[$key] = $filters[$key]->label;
00220                                 $this->_filterNames[] = $key;
00221                         }
00222                         $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)'));
00223                         
00224                         $this->addElement('textarea','content',df_translate('scripts.Dataface.ImportForm._build.LABEL_PASTE_IMPORT_DATA','Paste Import Data'), array('cols'=>60,'rows'=>10));
00225                         $this->addElement('file','upload',df_translate('scripts.Dataface.ImportForm._build.LABEL_UPLOAD_IMPORT_DATA','Upload Import Data'));
00226                         $defaultValsEl =& $this->addElement( 'optionalelement', '__default_values__', 'Default Values');
00227                         require_once 'dataface-public-api.php';
00228                         
00229                         foreach (array_keys($fields) as $field){
00230                                 if ( $fields[$field]['widget']['type'] == 'hidden' ){
00231                                         $fields[$field]['widget']['type'] = 'text';
00232                                 }
00233                                 $tempEl = $df_factory->_buildWidget($fields[$field]);
00234                                         
00235                                 if (!$tempEl->getLabel() || $tempEl->_type == 'hidden' ) {
00236                                 } else{
00237                                         $defaultValsEl->addField($tempEl);
00238                                 }
00239                                 unset($tempEl);
00240                         }
00241                         
00242                         
00243                         
00244                         $this->addElement('submit', 'submit', 'Submit');
00245                 
00246                         $this->addRule('filter','required',df_translate('scripts.Dataface.ImportForm._build.MESSAGE_IMPORT_FILE_FORMAT_REQUIRED','Import File Format is a required field'),null,'client');
00247                 } else {
00248                         /*
00249                          * We are in step 2 where we are verifying the data only.
00250                          */
00251                         //$this->addElement('submit', 'back', 'Data is incorrect. Go back');
00252                         $this->addElement('submit', 'continue', df_translate('scripts.Dataface.ImportForm._build.MESSAGE_PROCEED_WITH_IMPORT','Looks good.  Proceed with import'));
00253                         $this->addElement('hidden', '--importTablename');
00254                         $this->setDefaults(
00255                                 array('--importTablename'=>$_REQUEST['--importTablename'])
00256                         );
00257                 }
00258                 // Set the return page
00259                 $returnPage = @$_SERVER['HTTP_REFERER'];
00260                 if ( isset($mainQuery['-redirect']) ){
00261                         $returnPage = $mainQuery['-redirect'];
00262                 } else if ( isset($mainQuery['--redirect']) ){
00263                         $returnPage = $mainQuery['--redirect'];
00264                 }
00265 
00266                 if ( !$returnPage ){
00267                         if ( isset($this->_relationship) ){
00268                                 $returnPage = $app->url('-action=related_records_list&-relationship='.$this->_relationship->getName());
00269                         } else {
00270                                 $returnPage = $app->url('-action=list');
00271                         }
00272                         
00273                 }
00274                 
00275                 $this->addElement('hidden','--redirect');
00276                 $this->setDefaults(array('--redirect'=>$returnPage));
00277                 
00278                 
00279         
00280                 $this->_built = true;
00281         }
00282         
00283         function display(){
00284                 $this->_build();
00285                 
00286                 if ( $this->_step == 2 ){
00287                         require_once 'Dataface/RecordGrid.php';
00288                         $records = $this->loadImportTable();
00289 
00290                         $grid = new Dataface_RecordGrid($records);
00291                         $grid->id="import-records-preview";
00292                         df_display(array('preview_data'=>$grid->toHTML(), 'num_records'=>count($records)), 'ImportForm_step2.html');
00293                 
00294                 }
00295                 $res = parent::display();
00296                 return $res;
00297         }
00298         
00299         function loadImportTable(){
00300                 
00301                 
00302                 $dumpFile = $_SESSION['__dataface__import_data__'];
00303                 $importData = unserialize(file_get_contents($dumpFile));
00304                 
00305                 $tablename = $this->_table->tablename;
00306                 if ( $this->_relationship !== null ){
00307                         $tablename = $this->_relationship->getDomainTable();
00308                         if ( PEAR::isError($tablename) ){
00309                                 $destTables =& $this->_relationship->getDestinationTables();
00310                                 $tablename = $destTables[0]->tablename;
00311                         }
00312                 }
00313                 
00314                 $records = array();
00315                 foreach ($importData['rows'] as $row){
00316                         if ( isset($row['__CLASS__']) and isset($row['__CLASSPATH__']) ){
00317                                 if ( @$row['__CLASSPATH__'] and !class_exists($row['__CLASS__']) ){
00318                                         import($row['__CLASSPATH__']);
00319                                 }
00320                                 $class = $row['__CLASS__'];
00321                                 $importRecord = new $class($row);
00322                                 $records[] = $importRecord->getValues();
00323                                 unset($importRecord);
00324                         } else {
00325                                 $records[] = new Dataface_Record($tablename, $row);
00326                         }
00327                 }
00328                 
00329                 return $records;
00330         
00331         }
00332         
00333         function import($values){
00334         
00335                 if ( intval($this->_step) === 1 ){
00336                 
00337                         
00338                         
00339                         $upload =& $this->getElement('upload');
00340                         if ( $upload->isUploadedFile() ){
00341                                 /*
00342                                  * A file was uploaded.
00343                                  */
00344                                 $val =& $upload->getValue();
00345                                 $data = file_get_contents($val['tmp_name']);
00346                                 
00347                         
00348                         } else {
00349                                 /*
00350                                  * No file was uploaded so we will get data from the paste field.
00351                                  */
00352                                 $data = $values['content'];
00353                         }
00354                         
00355                         
00356                         $io = new Dataface_IO($this->_table->tablename);
00357                         $relname = ( $this->_relationship === null ) ? null : $this->_relationship->getName();
00358 
00359                         $importTablename = $io->importData($this->_record, $data, $values['filter'], $relname, false,  @$values['__default_values__']);
00360                         return $importTablename;
00361                 }
00362                 
00363                 else if ( $this->_step == 2 ){
00364                 
00365                         $io = new Dataface_IO($this->_table->tablename);
00366                         $relname = ( $this->_relationship === null ) ? null : $this->_relationship->getName();
00367                         $records = $io->importData($this->_record, $values['--importTablename'], @$values['filter'], $relname, true);
00368                         return $records;
00369                 }
00370                 
00371         
00372         }
00373         
00374         function validate(){
00375         
00376                 if ( intval($this->_step) === 1 ){
00377 
00378                         return (!empty($_POST['filter']) and !empty($_POST['-query']) and !empty($_POST['-table']));
00379                 } else {
00380                         return (!empty($_POST['-query']) and !empty($_POST['-table']) and !empty($_POST['--importTablename']));
00381                         
00382                         
00383                 }
00384         
00385         }
00386         
00387 
00388 
00389 }
 All Data Structures Namespaces Files Functions Variables Enumerations