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';
52 if ( $relationshipname !== null ) $this->_relationship =& $this->_table->getRelationship($relationshipname);
56 $this->HTML_QuickForm(
"Import Form");
58 if ( isset( $_REQUEST[
'--step'] ) ) $this->_step = $_REQUEST[
'--step'];
70 return ( isset( $_POST[
'__keys__']) and isset( $_POST[
'-table']) );
81 $io->read($_POST[
'__keys__'], $record);
86 $record =& $qt->loadCurrent();
95 if ( !isset($_POST[
'-relationship']) ){
98 'scripts.Dataface.ImportForm.getRelationship.ERROR_RELATIONSHIP_NOT_FOUND',
99 'Field \'-relationship\' not found in Import Form.'
102 $relname = $_POST[
'-relationship'];
103 if (strlen($relname) > 0 ){
104 $rel =&
$table->getRelationship($relname);
113 if ( !isset( $_GET[
'-relationship'] ) ){
117 $relname = $_GET[
'-relationship'];
118 if (strlen($relname) > 0 ){
119 $rel =&
$table->getRelationship($relname);
131 if ( $this->_built )
return;
133 $mainQuery =
$app->getQuery();
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');
145 $this->setDefaults( array(
'-table'=>$this->_table->tablename,
146 '--step'=>$this->_step,
148 '-query'=>
$_SERVER[
'QUERY_STRING']) );
150 if ( $this->_relationship !== null ){
151 $this->setDefaults( array(
'-relationship'=>$this->_relationship->getName()));
158 $factory =
new HTML_QuickForm(
'factory');
160 $keyDefaults = array();
161 foreach ( array_keys($this->_table->keys()) as $key ){
162 $keyEls[] = $factory->addElement(
'hidden', $key);
166 $this->addGroup($keyEls,
'__keys__');
169 if ( is_object($this->_record) ){
170 foreach ( array_keys($this->_table->keys()) as $key ){
171 $keyvals[$key] = $this->_record->getValueAsString($key);
174 $this->setDefaults( array(
'__keys__'=>$keyvals) );
180 if ( intval($this->_step) === 1 ){
184 if ( $this->_relationship === null ){
186 $filters =& $this->_table->getImportFilters();
187 $currentTableName = $this->_table->tablename;
189 $df_factory = df_create_new_record_form($currentTableName);
192 $fields = $this->_table->fields(
false,
true);
195 $domainTablename = $this->_relationship->getDomainTable();
199 $destTables =& $this->_relationship->getDestinationTables();
200 $domainTablename = $destTables[0];
203 $currentTable =& $domainTable;
204 $currentTablename = $domainTable->tablename;
205 $filters =& $domainTable->getImportFilters();
208 $df_factory = df_create_new_record_form($domainTable->tablename);
212 $fields = $domainTable->fields(
false,
true);
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;
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)'));
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';
230 if (
$fields[$field][
'widget'][
'type'] ==
'hidden' ){
233 $tempEl = $df_factory->_buildWidget(
$fields[$field]);
235 if (!$tempEl->getLabel() || $tempEl->_type ==
'hidden' ) {
237 $defaultValsEl->addField($tempEl);
244 $this->addElement(
'submit',
'submit',
'Submit');
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');
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');
255 array(
'--importTablename'=>$_REQUEST[
'--importTablename'])
259 $returnPage = @
$_SERVER[
'HTTP_REFERER'];
260 if ( isset($mainQuery[
'-redirect']) ){
261 $returnPage = $mainQuery[
'-redirect'];
262 }
else if ( isset($mainQuery[
'--redirect']) ){
263 $returnPage = $mainQuery[
'--redirect'];
267 if ( isset($this->_relationship) ){
268 $returnPage =
$app->url(
'-action=related_records_list&-relationship='.$this->_relationship->getName());
270 $returnPage =
$app->url(
'-action=list');
275 $this->addElement(
'hidden',
'--redirect');
276 $this->setDefaults(array(
'--redirect'=>$returnPage));
280 $this->_built =
true;
286 if ( $this->_step == 2 ){
287 require_once
'Dataface/RecordGrid.php';
291 $grid->id=
"import-records-preview";
292 df_display(array(
'preview_data'=>$grid->toHTML(),
'num_records'=>count($records)),
'ImportForm_step2.html');
302 $dumpFile = $_SESSION[
'__dataface__import_data__'];
303 $importData = unserialize(file_get_contents($dumpFile));
306 if ( $this->_relationship !== null ){
307 $tablename = $this->_relationship->getDomainTable();
309 $destTables =& $this->_relationship->getDestinationTables();
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__']);
320 $class = $row[
'__CLASS__'];
321 $importRecord =
new $class($row);
322 $records[] = $importRecord->getValues();
323 unset($importRecord);
333 function import($values){
335 if ( intval($this->_step) === 1 ){
339 $upload =& $this->getElement(
'upload');
340 if ( $upload->isUploadedFile() ){
344 $val =& $upload->getValue();
345 $data = file_get_contents($val[
'tmp_name']);
352 $data = $values[
'content'];
357 $relname = ( $this->_relationship === null ) ? null : $this->_relationship->getName();
359 $importTablename = $io->importData($this->_record, $data, $values[
'filter'], $relname,
false, @$values[
'__default_values__']);
360 return $importTablename;
363 else if ( $this->_step == 2 ){
366 $relname = ( $this->_relationship === null ) ? null : $this->_relationship->getName();
367 $records = $io->importData($this->_record, $values[
'--importTablename'], @$values[
'filter'], $relname,
true);
376 if ( intval($this->_step) === 1 ){
378 return (!empty($_POST[
'filter']) and !empty($_POST[
'-query']) and !empty($_POST[
'-table']));
380 return (!empty($_POST[
'-query']) and !empty($_POST[
'-table']) and !empty($_POST[
'--importTablename']));