Framework  3.9
DataImportManager Class Reference

Import data from a cvs file. More...

Public Member Functions

 __construct ($class_name, $id="")
 
 column ($field, $position, $label="", $options=array(), $importer="", $update_empty=true)
 Adds a data import column definition. More...
 
 additional ($field, $label="", $template="", $update_empty)
 If values are set using logic that does not refer to data in a particular column, use additional with a callback function as the template. More...
 
 match ()
 
 formatList ($items, $nameField)
 When instantiating a DataImportColumn, use this function to build an array of option key/value pairs for lookup. More...
 
 import ()
 Import and save the data. More...
 
 button ($text, $url, $confirm=null, $isScript=false)
 Adds a custom button to the form. More...
 
 loadData ($fp)
 Load the data from the spreadsheet (cvs file) into the DataItem objects. More...
 
 log ($text)
 Store info, errors, and warnings in a log file and ouput. More...
 
 writeLog ()
 Output the log. More...
 
 importOneRow ($row, $row_number)
 Given a data row, set the value into each field of the DataItem object. More...
 
 preProcessRow ($obj)
 If implementing classes may wish to add custom logic for handling a row before it is saved or displayed in preview. More...
 
 getSavedObj ($obj)
 Called by the build preview table method. More...
 
 findMatch ($obj)
 Check the db for a record that matches the values for the field(s) given in the matches array. More...
 
 setFilter ($obj, $old)
 If a match to a saved obj is found, set the filter to include only those fields that have changed. More...
 
 getColumn ($field)
 
 init (&$obj, $row_number)
 
 save ()
 
 formatSaveLogText ($row_number, $obj, $new)
 For each row saved, output text stating the row was updated and which fields set. More...
 
 getPrimaryKey ()
 
 getClassName ()
 Get the display (prettified) version of the class name for the import. More...
 
 buildInputTable ()
 Describe the expected columns to the user above the Browse File form. More...
 
 writeScript ()
 
 drawView ()
 
 preview ()
 Load the data into preview objects and display in a table. More...
 
 drawForm ()
 Display the expected input column layout and show a File upload renderer for the user to select the csv file to be imported. More...
 
 uploadDataImportFile ()
 
 drawButtons ()
 Draws any additional buttons specified in the calling script. More...
 
 openFile ()
 
 getFilePath ()
 Implementing classes may wish to retrieve differently (e.g., document library) More...
 
 buildPreviewTable ()
 Display in a DataListView the values read in and set into the data objects and show a Warnings column to list values not found in the lookup table. More...
 
 previewRowStyle ($obj)
 
 createPreviewData ()
 Clone each of the objs that would be saved to the db on import and set into a preview object that has the formatted value for each field and any warning info or other details for display. More...
 
 formatPreviewCell ($obj, $column)
 When building the preview table, format the column using the supplied DataImportColumn preview template callback. More...
 
 importCell ($column, $obj, $value)
 Given the input value read in, set the value into the field of the object. More...
 
 formatRowIndex ($obj)
 

Public Attributes

 $class_name
 name of data item class to be updated More...
 
 $id
 
 $log
 
 $objs = array()
 Contains data read in from the input file. More...
 
 $import = false
 
 $preview = false
 whether we are in preview mode More...
 
 $matches = array()
 Field(s) to use to check for a match in db - can be empty. More...
 
 $savedObjs = array()
 Matches found in the db for data in the import file, indexed by primary key. More...
 
 $onPreProcessRow = null
 callback for after each row of data is loaded and before it is saved or displayed in preview table More...
 
 $filepath = null
 optionally set known file path for csv file; otherwise provide form for user to specify More...
 
 $validateDataFile
 optional callback to validate data contents of the file More...
 
 $buttons = array()
 The custom buttons collection. More...
 
 $onSaveComplete = null
 Callback event handler that is fired after all the imported rows are saved. More...
 
 $field = "csv_file"
 
 $cssClass = ""
 
 $table
 
 $quiet = false
 Quiet mode - generate no output to the HTML stream. More...
 

Detailed Description

Import data from a cvs file.

Requires specifying the columns to be imported and their position in the csv file.

You may wish to instantiate a helper file for custom handling.

e.g.,

$helper = new UserImportHelper();

$mgr = new DataImportManager(SiteUser, "import_users"); $mgr->column("first_name", 1); $mgr->column("last_name", 2); $mgr->column("email", 3); $mgr->column("role", 4);

$mgr->match("email");

$mgr->onPreProcessRow = array($helper, preProcessRow); $mgr->onSaveComplete = array($helper, sendEmail);

$script .= $mgr->writeScript(); $mgr->drawView();

If a column needs special handling such as finding a key based on a text string in a reference table, use a callback.

If the input column will provide a name value and the import manager will retrieve its key value, then instantiate the column as follows:

$this->column("grade_id", 1, "Grade", $this->formatList($grades, "grade_name"));

Definition at line 73 of file data_import_manager.inc.

Constructor & Destructor Documentation

◆ __construct()

DataImportManager::__construct (   $class_name,
  $id = "" 
)
Parameters
String$class_name- name of the DataItem class being imported

Definition at line 98 of file data_import_manager.inc.

99  {
100  $this->class_name = $class_name;
101  $this->id = ($id) ? $id : codify(strtolower($this->class_name)) . "_data_import";
102 
103  if($_POST["import"])
104  {
105  $this->import = true;
106  }
107  }
$class_name
name of data item class to be updated
codify($name)
Takes a text string and converts it into a code-compliant format, suitable for use as a variable name...
Definition: functions.inc:1399

Member Function Documentation

◆ additional()

DataImportManager::additional (   $field,
  $label = "",
  $template = "",
  $update_empty 
)

If values are set using logic that does not refer to data in a particular column, use additional with a callback function as the template.

Parameters
string$field
string$label
string$template
boolean$update_empty
Returns
DataImportManager

Definition at line 140 of file data_import_manager.inc.

141  {
142  if(!$label)
143  {
144  $obj = new $this->class_name;
145  $label = $obj->prettifyFieldName($field);
146  }
147  $this->columns[] = new DataImportColumn($field, 0, $label, null, $template, $update_empty);
148 
149  return $this;
150  }

◆ buildInputTable()

DataImportManager::buildInputTable ( )

Describe the expected columns to the user above the Browse File form.

Definition at line 548 of file data_import_manager.inc.

549  {
550  $table = new DataListView($this->columns, $this->id);
551  $table->column("Column Name", "{field}")
552  ->column("Title/Label", "{label}")
553  ->column("Options", "{formatOptions()}")
554  ->column("Position", "{position}")
555  ;
556 
557  $table->sortable = false;
558  $table->paginate = false;
559  $table->filter = false;
560 
561  return $table;
562  }
DataListView displays a list of DataItems (or InnerJoinResults) in tabular format.
Definition: data_view.inc:56

◆ buildPreviewTable()

DataImportManager::buildPreviewTable ( )

Display in a DataListView the values read in and set into the data objects and show a Warnings column to list values not found in the lookup table.

Definition at line 752 of file data_import_manager.inc.

753  {
754  $pObjs = $this->createPreviewData();
755 
756  $pk = $this->getPrimaryKey();
757  $table = new DataListView($pObjs, $this->id);
758  $table->column("Row", array($this, formatRowIndex));
759  $table->column(prettify($pk), "{{$pk}}");
760 
761  foreach($this->columns as $column)
762  {
763  $field = $column->field;
764  $table->column($column->label, "{{$field}}");
765  }
766 
767  $table->onStartRow = array($this, previewRowStyle);
768  $table->emptyMessage = "No data to be imported.";
769  $table->sortable = false;
770  $table->paginate = false;
771  $table->filter = false;
772 
773  return $table;
774  }
createPreviewData()
Clone each of the objs that would be saved to the db on import and set into a preview object that has...
prettify($name)
Takes a variable or field name and converts it into a human-readable version (assuming that the origi...
Definition: functions.inc:1413

◆ button()

DataImportManager::button (   $text,
  $url,
  $confirm = null,
  $isScript = false 
)

Adds a custom button to the form.

Parameters
string$textthe button label text
string$urlthe URL to handle the button press
string$confirmoptional confirmation message
boolean$isScripttrue if the url is javascript code to execute, false if it is a URL to redirect to

Definition at line 213 of file data_import_manager.inc.

214  {
215  $this->buttons[] = array('text' => $text, 'url' => $url, 'confirm' => $confirm, 'isScript' => $isScript);
216  }

◆ column()

DataImportManager::column (   $field,
  $position,
  $label = "",
  $options = array(),
  $importer = "",
  $update_empty = true 
)

Adds a data import column definition.

Parameters
string$field- column name for table imported to
number$position- column order/position in import
string$label- label for the column
array$options- set of valid options for the imported data, if lookup is used
callback$importer- optional custom import function for the column

Definition at line 118 of file data_import_manager.inc.

119  {
120  if(!$label)
121  {
122  $obj = new $this->class_name;
123  $label = $obj->prettifyFieldName($field);
124  }
125  $this->columns[] = new DataImportColumn($field, $position, $label, $options, $importer, $update_empty);
126 
127  return $this;
128  }

◆ createPreviewData()

DataImportManager::createPreviewData ( )

Clone each of the objs that would be saved to the db on import and set into a preview object that has the formatted value for each field and any warning info or other details for display.

Returns
array of objects

Definition at line 792 of file data_import_manager.inc.

793  {
794  $filter = new InclusionFilter();
795  foreach($this->columns as $column)
796  {
797  $filter->add($column->field);
798  }
799 
800  $pObjs = array();
801  if(count($this->objs))
802  {
803  foreach($this->objs as $obj)
804  {
805  $pObj = clone $obj;
806  $pObj->filter = $filter;
807  foreach($this->columns as $column)
808  {
809  $pObj->filter->add($column->field);
810  $pObj->set($column->field, $this->formatPreviewCell($pObj, $column));
811  }
812 
813  $pObjs[] = $pObj;
814  }
815  }
816 
817  return $pObjs;
818  }
Used to place a filter on the contents of a DataItem-derived object.

◆ drawButtons()

DataImportManager::drawButtons ( )

Draws any additional buttons specified in the calling script.

Definition at line 677 of file data_import_manager.inc.

678  {
679  $buttons = array();
680 
681  foreach($this->buttons as $button)
682  {
683  $url = ($button['isScript']) ? $button['url'] : "go('{$button['url']}');";
684 
685  if ($button['confirm'])
686  {
687  $link = "if (confirm('".jsSafe($button['confirm'])."')) $url; return false;";
688  }
689  else
690  {
691  $link = "$url; return false;";
692  }
693 
694  if(preg_match("/import=1/", $url) && !count($this->objs))
695  {
696  continue;
697  }
698  $buttons[] = "<input type='button' class='{$this->buttonCSS}' onclick=\"$link\" value=\"{$button['text']}\"/>";
699  }
700 
701  if($this->preview && $this->table && count($this->table->items))
702  {
703  array_unshift($buttons, "<input type='submit' name='import' class='button' value='Import File'/>\n");
704  }
705 
706  echo "<div class='button_row'>" . implode("&nbsp;&nbsp;", $buttons) . "</div>";
707  }
$buttons
The custom buttons collection.
preview()
Load the data into preview objects and display in a table.

◆ drawForm()

DataImportManager::drawForm ( )

Display the expected input column layout and show a File upload renderer for the user to select the csv file to be imported.

Definition at line 634 of file data_import_manager.inc.

635  {
636  $table = $this->buildInputTable();
637  echo "<p><label>Description of columns expected in import file:</label></p>\n";
638  $table->drawView();
639 
640  echo "</br></br><form method='POST' action='' enctype='multipart/form-data'>
641  <label>CSV file to import: </label><input type='file' name='{$this->field}'/><br/><br/>
642  <input type='submit' name='import' class='button' value='Import File'/>
643  <input type='submit' name='preview' class='button' value='Preview Import'/>
644  </form>";
645  }
buildInputTable()
Describe the expected columns to the user above the Browse File form.

◆ drawView()

DataImportManager::drawView ( )

Definition at line 569 of file data_import_manager.inc.

570  {
571  $file = $this->getFilePath();
572 
573  $class = ($this->cssClass) ? " class='{$this->cssClass}'" : "";
574  echo "<div id='data_import'{$class}>\n";
575 
576  if(!$file)
577  {
578  $this->drawForm();
579  }
580  else if(!$this->import)
581  {
582  $this->preview();
583  }
584  else
585  {
586  $this->import();
587  }
588  echo "</div>\n";
589  }
getFilePath()
Implementing classes may wish to retrieve differently (e.g., document library)
drawForm()
Display the expected input column layout and show a File upload renderer for the user to select the c...

◆ findMatch()

DataImportManager::findMatch (   $obj)

Check the db for a record that matches the values for the field(s) given in the matches array.

Parameters
obj$obj

Definition at line 359 of file data_import_manager.inc.

360  {
361  if(!count($this->matches))
362  {
363  return null;
364  }
365 
366  foreach($this->matches as $field => $dump)
367  {
368  $constraint[] = $field . "=:$field";
369  }
370 
371  $query = Query::create($this->class_name, "WHERE " . implode(" AND ", $constraint));
372 
373  foreach($this->matches as $field => $dump)
374  {
375  $query->bind(":$field", $obj->$field);
376  }
377 
378  $foundObjs = $query->execute();
379 
380  return (count($foundObjs)) ? $foundObjs[0] : null;
381  }
static create($class, $constraints="")
Static factory method to create a new Query.
Definition: query.inc:358

◆ formatList()

DataImportManager::formatList (   $items,
  $nameField 
)

When instantiating a DataImportColumn, use this function to build an array of option key/value pairs for lookup.

Parameters
Array$items- array of DataItem objects in the lookup table
String$nameField- the field in the table that supplies the name that is meant to match the input value of the spreadsheet.

Definition at line 168 of file data_import_manager.inc.

169  {
170  if(count($items) == 0)
171  {
172  return array();
173  }
174 
175  $out = array();
176  foreach($items as $item)
177  {
178  $pk = $item->getPrimaryKey();
179  $out[$item->$pk] = $item->$nameField;
180  }
181 
182  return $out;
183  }

◆ formatPreviewCell()

DataImportManager::formatPreviewCell (   $obj,
  $column 
)

When building the preview table, format the column using the supplied DataImportColumn preview template callback.

Parameters
obj$obj
obj$column
Returns
mixed

Definition at line 828 of file data_import_manager.inc.

829  {
830  $old = $this->getSavedObj($obj);
831 
832  if(is_callable($column->preview_template))
833  {
834  return call_user_func($column->preview_template, $obj, $old);
835  }
836  }
getSavedObj($obj)
Called by the build preview table method.

◆ formatRowIndex()

DataImportManager::formatRowIndex (   $obj)

Definition at line 865 of file data_import_manager.inc.

866  {
867  return $obj->row_number;
868  }

◆ formatSaveLogText()

DataImportManager::formatSaveLogText (   $row_number,
  $obj,
  $new 
)

For each row saved, output text stating the row was updated and which fields set.

If new row, write inserted.

Parameters
number$row_number
obj$obj
boolean$new
Returns
string

Definition at line 485 of file data_import_manager.inc.

486  {
487  $pk = $this->getPrimaryKey();
488  $name = $this->getClassName();
489  $fields = $obj->getFields();
490  $out = array();
491 
492  if(!$new)
493  {
494  $filter = $obj->getFilter();
495  foreach($fields as $field => $type)
496  {
497  if($filter && !$filter->isExcluded($field))
498  {
499  $out[] = $field . " " . $obj->$field;
500  }
501  }
502  $text = "<b>Updated</b> $name from row number $row_number $pk {$obj->$pk} " . implode(" ", $out);
503  }
504  else
505  {
506  foreach($fields as $field => $type)
507  {
508  $out[] = $field . " " . $obj->$field;
509  }
510 
511  $text = "<b>Inserted</b> into $name from row number $row_number $pk {$obj->$pk} " . implode(" ", $out);
512  }
513 
514  foreach($this->columns as $column)
515  {
516  $warning = $column->getWarning($obj);
517  if($warning)
518  {
519  $text .= "<span class='warning'>{$warning}</warning>";
520  }
521  }
522 
523  return $text;
524  }
getClassName()
Get the display (prettified) version of the class name for the import.

◆ getClassName()

DataImportManager::getClassName ( )

Get the display (prettified) version of the class name for the import.

Definition at line 537 of file data_import_manager.inc.

538  {
540  $obj = new $class_name;
541  return $obj->prettifyClassName();
542  }

◆ getColumn()

DataImportManager::getColumn (   $field)

Definition at line 418 of file data_import_manager.inc.

419  {
420  if(!count($this->columns)) return false;
421 
422  if(count($this->columns))
423  {
424  foreach($this->columns as $column)
425  {
426  if($column->field == $field)
427  {
428  return $column;
429  }
430  }
431  }
432 
433  return null;
434  }

◆ getFilePath()

DataImportManager::getFilePath ( )

Implementing classes may wish to retrieve differently (e.g., document library)

Returns
String

Definition at line 729 of file data_import_manager.inc.

730  {
731  if($this->filepath)
732  {
733  return $this->filepath;
734  }
735  else if($_FILES[$this->field]["tmp_name"])
736  {
737  return $_FILES[$this->field]["tmp_name"];
738  }
739  else if($_POST[$this->field])
740  {
741  return $_POST[$this->field];
742  }
743 
744  return null;
745  }
$filepath
optionally set known file path for csv file; otherwise provide form for user to specify

◆ getPrimaryKey()

DataImportManager::getPrimaryKey ( )

Definition at line 526 of file data_import_manager.inc.

527  {
529  $obj = new $class_name;
530  return $obj->getPrimaryKey();
531  }

◆ getSavedObj()

DataImportManager::getSavedObj (   $obj)

Called by the build preview table method.

If during import, a record was found in the db that matched the given object, that match was saved to the savedObjs array.

Parameters
obj$obj
Returns
multitype:|NULL

Definition at line 341 of file data_import_manager.inc.

342  {
343  $pk = $this->getPrimaryKey();
344 
345  if(array_key_exists($obj->$pk, $this->savedObjs))
346  {
347  return $this->savedObjs[$obj->$pk];
348  }
349 
350  return null;
351  }

◆ import()

DataImportManager::import ( )

Import and save the data.

Definition at line 188 of file data_import_manager.inc.

189  {
190  $fp = $this->openFile();
191  if(!$fp) return;
192 
193  if($this->loadData($fp))
194  {
195  $this->save();
196  if (!$this->quiet)
197  {
198  $this->drawButtons();
199  $this->writeLog();
200  }
201  }
202  }
drawButtons()
Draws any additional buttons specified in the calling script.
loadData($fp)
Load the data from the spreadsheet (cvs file) into the DataItem objects.
writeLog()
Output the log.

◆ importCell()

DataImportManager::importCell (   $column,
  $obj,
  $value 
)

Given the input value read in, set the value into the field of the object.

If this data column is set using the options lookup and the value is not found, save the warning to display in the DataListView row.

Parameters
$obj
$column
$value

Definition at line 851 of file data_import_manager.inc.

852  {
853  if (is_callable($column->importer) && $column->position > 0)
854  {
855  return call_user_func($column->importer, $obj, $value);
856  }
857  else if (is_callable($column->importer) && $column->position == 0)
858  {
859  return call_user_func($column->importer, $obj);
860  }
861 
862  else $column->import($obj, $value);
863  }

◆ importOneRow()

DataImportManager::importOneRow (   $row,
  $row_number 
)

Given a data row, set the value into each field of the DataItem object.

Parameters
$row
$row_number

Definition at line 276 of file data_import_manager.inc.

277  {
279  $obj = new $class_name;
280 
281  $this->init($obj, $row_number);
282 
283  foreach($this->columns as $column)
284  {
285  $position = $column->position;
286  if($position == 0) continue;
287  $this->importCell($column, $obj, $row[$position-1]);
288  }
289 
290  // Load additional columns after in case their value depends on rows
291  // loaded from spreadsheet
292  foreach($this->columns as $column)
293  {
294  if($column->position > 0) continue;
295  $this->importCell($column, $obj, 0);
296  }
297 
298  $obj = $this->preProcessRow($obj);
299 
300  return $obj;
301  }
preProcessRow($obj)
If implementing classes may wish to add custom logic for handling a row before it is saved or display...
importCell($column, $obj, $value)
Given the input value read in, set the value into the field of the object.
init(&$obj, $row_number)

◆ init()

DataImportManager::init ( $obj,
  $row_number 
)

Definition at line 442 of file data_import_manager.inc.

443  {
444  // use for indexing
445  $obj->set("row_number", $row_number);
446  }

◆ loadData()

DataImportManager::loadData (   $fp)

Load the data from the spreadsheet (cvs file) into the DataItem objects.

Definition at line 222 of file data_import_manager.inc.

223  {
224  trace("Loading Data...", 3);
225 
226  // Use for indexing warnings
227  $row_number = 1;
228  $first = true;
229  $read = 0;
230 
231  while (($row = fgetcsv($fp)) !== FALSE)
232  {
233  $read++;
234  if($first && $this->validateDataFile && !call_user_func($this->validateDataFile, $row))
235  {
236  return false;
237  }
238  $first = false;
239  $obj = $this->importOneRow($row, $row_number);
240  if($obj)
241  {
242  $this->objs[$obj->row_number] = $obj;
243  $row_number++;
244  }
245  }
246 
247  trace("Read {$row_number} objects from {$read} rows", 3);
248  return true;
249  }
importOneRow($row, $row_number)
Given a data row, set the value into each field of the DataItem object.
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ log()

DataImportManager::log (   $text)

Store info, errors, and warnings in a log file and ouput.

Parameters
$text

Definition at line 256 of file data_import_manager.inc.

257  {
258  $this->log .= $text . "</br></br>";
259  }
log($text)
Store info, errors, and warnings in a log file and ouput.

◆ match()

DataImportManager::match ( )

Definition at line 152 of file data_import_manager.inc.

153  {
154  foreach(func_get_args() as $field)
155  {
156  $this->matches[$field] = true;
157  }
158  }

◆ openFile()

DataImportManager::openFile ( )

Definition at line 709 of file data_import_manager.inc.

710  {
711  $filepath = $this->getFilePath();
712  $fp = fopen($filepath, 'r');
713 
714  if (fgetcsv($fp) == FALSE)
715  {
716  echo "<div class='error'>Cannot open data file {$filepath}.</div>\n";
717  return null;
718  }
719 
720  return $fp;
721  }

◆ preProcessRow()

DataImportManager::preProcessRow (   $obj)

If implementing classes may wish to add custom logic for handling a row before it is saved or displayed in preview.

Parameters
$obj

Definition at line 310 of file data_import_manager.inc.

311  {
312  if(count($this->matches))
313  {
314  $savedObj = $this->findMatch($obj);
315  if($savedObj)
316  {
317  $pk = $this->getPrimaryKey();
318  $this->savedObjs[$savedObj->$pk] = $savedObj;
319  $obj->$pk = $savedObj->$pk;
320  $this->setFilter($obj, $savedObj);
321  if(!$obj->getFilter()) return null;
322  }
323  }
324 
325  if ($this->onPreProcessRow)
326  {
327  call_user_func($this->onPreProcessRow, $obj, $savedObj);
328  }
329 
330  return $obj;
331  }
findMatch($obj)
Check the db for a record that matches the values for the field(s) given in the matches array.
setFilter($obj, $old)
If a match to a saved obj is found, set the filter to include only those fields that have changed.

◆ preview()

DataImportManager::preview ( )

Load the data into preview objects and display in a table.

Definition at line 594 of file data_import_manager.inc.

595  {
596  $this->preview = true;
597  $this->uploadDataImportFile();
598 
599  $fp = $this->openFile();
600  if(!$fp) return;
601 
602  if($this->loadData($fp))
603  {
604  $this->table = $this->buildPreviewTable();
605  }
606 
607  if(count($this->table->items))
608  {
609  echo "<form method='POST' action='' enctype='multipart/form-data'>\n";
610  echo "<input type='hidden' name='{$this->field}' value='{$this->filepath}'/>\n";
611  $this->drawButtons();
612  }
613 
614  echo $this->log;
615  $this->table->drawView();
616 
617  if(!count($this->table->items) && !$this->filepath)
618  {
619  $this->drawForm();
620  }
621 
622  $this->drawButtons();
623 
624  if(count($this->table->items))
625  {
626  echo "</form>\n";
627  }
628  }
buildPreviewTable()
Display in a DataListView the values read in and set into the data objects and show a Warnings column...

◆ previewRowStyle()

DataImportManager::previewRowStyle (   $obj)

Definition at line 776 of file data_import_manager.inc.

777  {
778  $pk = $this->getPrimaryKey();
779  if(!$obj->$pk)
780  {
781  return "data_import_row_new";
782  }
783  }

◆ save()

DataImportManager::save ( )

Definition at line 448 of file data_import_manager.inc.

449  {
450  if(count($this->objs) == 0)
451  {
452  return;
453  }
454 
455  foreach($this->objs as $row_number => $obj)
456  {
457  $pk = $this->getPrimaryKey();
458  $new = false;
459 
460  if(!$obj->$pk)
461  {
462  $new = true;
463  }
464  $obj->save();
465  $this->log($this->formatSaveLogText($row_number, $obj, $new));
466  }
467 
468  if ($this->onSaveComplete)
469  {
470  call_user_func_array($this->onSaveComplete, array($this));
471  }
472 
473  return true;
474  }
formatSaveLogText($row_number, $obj, $new)
For each row saved, output text stating the row was updated and which fields set.

◆ setFilter()

DataImportManager::setFilter (   $obj,
  $old 
)

If a match to a saved obj is found, set the filter to include only those fields that have changed.

Parameters
obj$obj
obj$old
Returns
obj

Definition at line 391 of file data_import_manager.inc.

392  {
393  $fields = $obj->getFields();
394  $changed = false;
395 
396  $obj->filter = new InclusionFilter();
397  $old->filter = new InclusionFilter();
398  foreach($fields as $field => $type)
399  {
400  $column = $this->getColumn($field);
401  if(!$column) continue;
402  $value = $obj->$field;
403  if(($value || $column->update_empty) && $obj->$field != $old->$field)
404  {
405  $changed = true;
406  $obj->filter->add($field);
407  $old->filter->add($field);
408  }
409  }
410 
411  if(!$changed)
412  {
413  $obj->filter = null;
414  $old->filter = null;
415  }
416  }

◆ uploadDataImportFile()

DataImportManager::uploadDataImportFile ( )

Definition at line 647 of file data_import_manager.inc.

648  {
649  global $config;
650 
651  $file = $_FILES[$this->field]["tmp_name"];
652 
653  if(!$file)
654  {
655  return;
656  }
657 
658  $fullpath = $config["uploadbase"] . DIRECTORY_SEPARATOR . basename($file);
659 
660  if (file_exists($fullpath))
661  {
662  unlink($fullpath);
663  }
664 
665  trace("Uploading $file to $fullpath", 3);
666 
667  move_uploaded_file($file, $fullpath);
668  chmod($fullpath, 0755);
669 
670  $this->filepath = $fullpath;
671  }

◆ writeLog()

DataImportManager::writeLog ( )

Output the log.

Definition at line 264 of file data_import_manager.inc.

265  {
266  if (!$this->quiet) echo $this->log;
267  }

◆ writeScript()

DataImportManager::writeScript ( )

Definition at line 564 of file data_import_manager.inc.

565  {
566  return "";
567  }

Member Data Documentation

◆ $buttons

DataImportManager::$buttons = array()

The custom buttons collection.

Definition at line 86 of file data_import_manager.inc.

◆ $class_name

DataImportManager::$class_name

name of data item class to be updated

Definition at line 75 of file data_import_manager.inc.

◆ $cssClass

DataImportManager::$cssClass = ""

Definition at line 89 of file data_import_manager.inc.

◆ $field

DataImportManager::$field = "csv_file"

Definition at line 88 of file data_import_manager.inc.

◆ $filepath

DataImportManager::$filepath = null

optionally set known file path for csv file; otherwise provide form for user to specify

Definition at line 84 of file data_import_manager.inc.

◆ $id

DataImportManager::$id

Definition at line 76 of file data_import_manager.inc.

◆ $import

DataImportManager::$import = false

Definition at line 79 of file data_import_manager.inc.

◆ $log

DataImportManager::$log

Definition at line 77 of file data_import_manager.inc.

◆ $matches

DataImportManager::$matches = array()

Field(s) to use to check for a match in db - can be empty.

Definition at line 81 of file data_import_manager.inc.

◆ $objs

DataImportManager::$objs = array()

Contains data read in from the input file.

Definition at line 78 of file data_import_manager.inc.

◆ $onPreProcessRow

DataImportManager::$onPreProcessRow = null

callback for after each row of data is loaded and before it is saved or displayed in preview table

Definition at line 83 of file data_import_manager.inc.

◆ $onSaveComplete

DataImportManager::$onSaveComplete = null

Callback event handler that is fired after all the imported rows are saved.

Definition at line 87 of file data_import_manager.inc.

◆ $preview

DataImportManager::$preview = false

whether we are in preview mode

Definition at line 80 of file data_import_manager.inc.

◆ $quiet

DataImportManager::$quiet = false

Quiet mode - generate no output to the HTML stream.

Definition at line 91 of file data_import_manager.inc.

◆ $savedObjs

DataImportManager::$savedObjs = array()

Matches found in the db for data in the import file, indexed by primary key.

Definition at line 82 of file data_import_manager.inc.

◆ $table

DataImportManager::$table

Definition at line 90 of file data_import_manager.inc.

◆ $validateDataFile

DataImportManager::$validateDataFile

optional callback to validate data contents of the file

Definition at line 85 of file data_import_manager.inc.


The documentation for this class was generated from the following file: