34 require_once realpath(dirname(__FILE__).
"/data_item.inc");
 
   35 require_once realpath(dirname(__FILE__).
"/transaction.inc");
 
   91           $this->
class = $class;
 
   94      function import($file = 
null)
 
   98                $file = $_FILES[
"importFile"][
"tmp_name"];
 
  106                $proto->joinTransaction(
$tx);
 
  113                if ($this->disablePrimaryKey)
 
  115                     $proto->disablePrimaryKey();
 
  120                if ($this->index_field)
 
  125                $fp = fopen($file, 
"r");
 
  127                $fields = fgetcsv($fp);
 
  133                trace(
"Importing fields: ".implode(
",", $fields), 3);
 
  137                foreach($fields as $field)
 
  139                     if (!$proto->hasField($field) && !$this->ignoreUnknown)
 
  141                          throw new FakoliException(
"Unrecognized column: $field");
 
  144                     if ($this->index_field && $field == $this->index_field)
 
  153                if ($fileIdx < 0 && !$this->ignoreUnknown && !$this->onPreImportItem)
 
  155                     throw new FakoliException(
"'{$this->index_field}' column not present");
 
  161                while($values = fgetcsv($fp))
 
  165                     if ($this->limit > -1 && $this->imported >= $this->limit) 
continue;
 
  168                     $item->joinTransaction(
$tx);
 
  171                     foreach($fields as $field)
 
  173                          $item->set($field, $values[$i++]);
 
  176                     if ($this->onPreImport)
 
  178                          trace(
"** Calling onPreImport", 3);
 
  180                          call_user_func($this->onPreImport, $item);
 
  183                     if ($this->index_field && array_key_exists($values[$fileIdx], $indexed))
 
  187                               $src = $indexed[$values[$fileIdx]];
 
  188                               foreach($item->getFields() as $f)
 
  190                                    if (!isset($item->$f)) $item->set($f, $src->get($f));
 
  195                               trace(
"Skipping record {$values[$fileIdx]}", 3);
 
  202                          trace(
"Importing record {$values[$fileIdx]} at line {$this->lines}", 3);
 
  206                          trace(
"Importing record at line $iterations", 3);
 
  213                     if ($this->onPostImport)
 
  215                          call_user_func($this->onPostImport, $item);
 
  220                if ($this->disablePrimaryKey)
 
  222                     $proto->enablePrimaryKey();
 
  225                trace (
"No more values from CSV", 3);        
 
  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>
 
  243 <input type=
"checkbox" class=
"checkbox" name=
"truncate" value=
"1"/> Delete existing records<br/>
 
  245 <input type=
"submit" value=
"Import Records" class=
'button'></input>
 
CSVImport provides a generic method for importing Comma-Separated Value files into DataItem mapped da...
The DataTransaction class wraps the underlying database's transaction model.
static create($class, $constraints="", $indexBy="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.