Framework  3.9
CSVImport Class Reference

CSVImport provides a generic method for importing Comma-Separated Value files into DataItem mapped database tables. More...

Public Member Functions

 __construct ($class)
 
 import ($file=null)
 
 drawForm ()
 

Public Attributes

 $class
 
 $index_field = null
 
 $truncate = false
 
 $update = false
 
 $ignoreUnknown = false
 
 $disablePrimaryKey = false
 
 $onPreImport = null
 
 $onPostImport = null
 
 $lines = 0
 
 $imported = 0
 
 $limit = -1
 
 $tx
 

Detailed Description

CSVImport provides a generic method for importing Comma-Separated Value files into DataItem mapped database tables.

These files can be easily exported from Excel, Access and other office and database applications.

In order to use a CSV file for an import it must contain a first row with the column names being the associated field names in your target DataItem.

To use this class, instantiate it with the name of the DataItem class you will be importing. You can set various options to manage the import.

index_field specifies which field is used to determine the identity of matching items already in the database

truncate specifies whether you want to truncate the database table before importing the data.

update specifies whether data in the import file should update matching items already in the database. If set to false then matching import records are skipped

onPreImportItem is a callback function that is called for each item after the data values have been read from the file but before the item has been persisted to the database. You can use this callback to manipulate fields during the import process.

onPostImportItem is a callback function that is called for each item after persisting in the database. You can use this callback to perform additional manipulation, such as building cross-reference records.

ignoreUnknown this flag specifies whether an error should be thrown if columns are found in the CSV file that do not match defined database fields in your DataItem object.

Author
andy

Definition at line 74 of file csv_import.inc.

Constructor & Destructor Documentation

◆ __construct()

CSVImport::__construct (   $class)

Definition at line 89 of file csv_import.inc.

90  {
91  $this->class = $class;
92  }

Member Function Documentation

◆ drawForm()

CSVImport::drawForm ( )

Definition at line 236 of file csv_import.inc.

237  {
238 ?>
239 <form method="POST" action="" enctype='multipart/form-data'>
240 <label for="importFile">Import File (CSV Format): </label>
241 <input type="file" name="import_file"></input>
242 <br/>
243 <input type="checkbox" class="checkbox" name="truncate" value="1"/>&nbsp;Delete existing records<br/>
244 <br/>
245 <input type="submit" value="Import Records" class='button'></input>
246 </form>
247 <?
248  }

◆ import()

CSVImport::import (   $file = null)

Definition at line 94 of file csv_import.inc.

95  {
96  if (!$file)
97  {
98  $file = $_FILES["importFile"]["tmp_name"];
99  }
100 
101  $tx = new DataTransaction();
102 
103  try
104  {
105  $proto = new $this->class;
106  $proto->joinTransaction($tx);
107 
108  if ($this->truncate)
109  {
110  $proto->deleteAll();
111  }
112 
113  if ($this->disablePrimaryKey)
114  {
115  $proto->disablePrimaryKey();
116  }
117 
118  $indexed = array();
119 
120  if ($this->index_field)
121  {
122  $indexed = IndexedQuery::create($this->class, "", $this->index_field);
123  }
124 
125  $fp = fopen($file, "r");
126 
127  $fields = fgetcsv($fp);
128 
129  $fileIdx = -1;
130 
131  $i = 0;
132 
133  trace("Importing fields: ".implode(",", $fields), 3);
134 
135  // First column must contain field names that match the target class
136 
137  foreach($fields as $field)
138  {
139  if (!$proto->hasField($field) && !$this->ignoreUnknown)
140  {
141  throw new FakoliException("Unrecognized column: $field");
142  }
143 
144  if ($this->index_field && $field == $this->index_field)
145  {
146  $fileIdx = $i;
147  break;
148  }
149 
150  $i++;
151  }
152 
153  if ($fileIdx < 0 && !$this->ignoreUnknown && !$this->onPreImportItem)
154  {
155  throw new FakoliException("'{$this->index_field}' column not present");
156  }
157 
158  $this->lines = 0;
159  $this->imported = 0;
160 
161  while($values = fgetcsv($fp))
162  {
163  $this->lines++;
164 
165  if ($this->limit > -1 && $this->imported >= $this->limit) continue;
166 
167  $item = new $this->class;
168  $item->joinTransaction($tx);
169 
170  $i = 0;
171  foreach($fields as $field)
172  {
173  $item->set($field, $values[$i++]);
174  }
175 
176  if ($this->onPreImport)
177  {
178  trace("** Calling onPreImport", 3);
179 
180  call_user_func($this->onPreImport, $item);
181  }
182 
183  if ($this->index_field && array_key_exists($values[$fileIdx], $indexed))
184  {
185  if ($this->update)
186  {
187  $src = $indexed[$values[$fileIdx]];
188  foreach($item->getFields() as $f)
189  {
190  if (!isset($item->$f)) $item->set($f, $src->get($f));
191  }
192  }
193  else
194  {
195  trace("Skipping record {$values[$fileIdx]}", 3);
196  continue;
197  }
198  }
199 
200  if ($fileIdx)
201  {
202  trace("Importing record {$values[$fileIdx]} at line {$this->lines}", 3);
203  }
204  else
205  {
206  trace("Importing record at line $iterations", 3);
207  }
208 
209  $item->save();
210 
211  $this->imported++;
212 
213  if ($this->onPostImport)
214  {
215  call_user_func($this->onPostImport, $item);
216  }
217  }
218 
219 
220  if ($this->disablePrimaryKey)
221  {
222  $proto->enablePrimaryKey();
223  }
224 
225  trace ("No more values from CSV", 3);
226 
227  $tx->commit();
228  }
229  catch(Exception $e)
230  {
231  $tx->rollback();
232  throw $e;
233  }
234  }
The DataTransaction class wraps the underlying database's transaction model.
Definition: transaction.inc:48
static create($class, $constraints="", $indexBy="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

Member Data Documentation

◆ $class

CSVImport::$class

Definition at line 76 of file csv_import.inc.

◆ $disablePrimaryKey

CSVImport::$disablePrimaryKey = false

Definition at line 81 of file csv_import.inc.

◆ $ignoreUnknown

CSVImport::$ignoreUnknown = false

Definition at line 80 of file csv_import.inc.

◆ $imported

CSVImport::$imported = 0

Definition at line 85 of file csv_import.inc.

◆ $index_field

CSVImport::$index_field = null

Definition at line 77 of file csv_import.inc.

◆ $limit

CSVImport::$limit = -1

Definition at line 86 of file csv_import.inc.

◆ $lines

CSVImport::$lines = 0

Definition at line 84 of file csv_import.inc.

◆ $onPostImport

CSVImport::$onPostImport = null

Definition at line 83 of file csv_import.inc.

◆ $onPreImport

CSVImport::$onPreImport = null

Definition at line 82 of file csv_import.inc.

◆ $truncate

CSVImport::$truncate = false

Definition at line 78 of file csv_import.inc.

◆ $tx

CSVImport::$tx

Definition at line 87 of file csv_import.inc.

◆ $update

CSVImport::$update = false

Definition at line 79 of file csv_import.inc.


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