2008-08-09 06:53:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Uses the fgetcsv() function to process CSV input.
|
|
|
|
* The input is expected to be UTF8.
|
|
|
|
*
|
|
|
|
* @see http://rfc.net/rfc4180.html
|
|
|
|
* @package cms
|
|
|
|
* @subpackage bulkloading
|
2008-10-02 12:29:41 +02:00
|
|
|
* @author Ingo Schommer, Silverstripe Ltd. (<myfirstname>@silverstripe.com)
|
2008-09-15 16:21:43 +02:00
|
|
|
*
|
|
|
|
* @todo Support for deleting existing records not matched in the import (through relation checks)
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
|
|
|
class CsvBulkLoader extends BulkLoader {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delimiter character (Default: comma).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $delimiter = ',';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enclosure character (Default: doublequote)
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $enclosure = '"';
|
|
|
|
|
|
|
|
/**
|
2008-10-28 04:17:22 +01:00
|
|
|
* Identifies if the has a header row.
|
2008-08-09 06:53:34 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-10-28 04:17:22 +01:00
|
|
|
public $hasHeaderRow = true;
|
2008-08-09 06:53:34 +02:00
|
|
|
|
|
|
|
protected function processAll($filepath, $preview = false) {
|
2008-10-03 02:27:58 +02:00
|
|
|
$results = new BulkLoader_Result();
|
2008-08-28 07:00:34 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
$csv = new CSVParser($filepath, $this->delimiter, $this->enclosure);
|
2008-08-09 06:53:34 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
// ColumnMap has two uses, depending on whether hasHeaderRow is set
|
|
|
|
if($this->columnMap) {
|
|
|
|
if($this->hasHeaderRow) $csv->mapColumns($this->columnMap);
|
|
|
|
else $csv->provideHeaderRow($this->columnMap);
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($csv as $row) {
|
2008-11-18 02:48:37 +01:00
|
|
|
$this->processRecord($row, $this->columnMap, $results, $preview);
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
2008-08-09 06:53:34 +02:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
return $results;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
/**
|
|
|
|
* @todo Better messages for relation checks and duplicate detection
|
2008-10-03 02:27:58 +02:00
|
|
|
* Note that columnMap isn't used
|
2008-09-15 16:21:43 +02:00
|
|
|
*/
|
|
|
|
protected function processRecord($record, $columnMap, &$results, $preview = false) {
|
2008-08-09 06:53:34 +02:00
|
|
|
$class = $this->objectClass;
|
2008-08-09 07:00:42 +02:00
|
|
|
|
|
|
|
// find existing object, or create new one
|
2008-08-09 07:45:43 +02:00
|
|
|
$existingObj = $this->findExistingObject($record, $columnMap);
|
2008-08-09 07:00:42 +02:00
|
|
|
$obj = ($existingObj) ? $existingObj : new $class();
|
2008-08-09 06:53:34 +02:00
|
|
|
|
|
|
|
// first run: find/create any relations and store them on the object
|
|
|
|
// we can't combine runs, as other columns might rely on the relation being present
|
|
|
|
$relations = array();
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($record as $fieldName => $val) {
|
2008-08-09 07:45:43 +02:00
|
|
|
// don't bother querying of value is not set
|
|
|
|
if($this->isNullValue($val)) continue;
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
// checking for existing relations
|
2008-08-09 07:45:43 +02:00
|
|
|
if(isset($this->relationCallbacks[$fieldName])) {
|
2008-08-09 06:53:34 +02:00
|
|
|
// trigger custom search method for finding a relation based on the given value
|
|
|
|
// and write it back to the relation (or create a new object)
|
2008-08-09 07:45:43 +02:00
|
|
|
$relationName = $this->relationCallbacks[$fieldName]['relationname'];
|
|
|
|
$relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}($val, $record);
|
2008-08-09 06:53:34 +02:00
|
|
|
if(!$relationObj || !$relationObj->exists()) {
|
|
|
|
$relationClass = $obj->has_one($relationName);
|
|
|
|
$relationObj = new $relationClass();
|
|
|
|
$relationObj->write();
|
|
|
|
}
|
|
|
|
$obj->setComponent($relationName, $relationObj);
|
|
|
|
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
2008-08-09 07:00:42 +02:00
|
|
|
$obj->write();
|
2008-10-03 02:27:58 +02:00
|
|
|
|
2008-08-09 07:45:43 +02:00
|
|
|
} elseif(strpos($fieldName, '.') !== false) {
|
2008-08-09 06:53:34 +02:00
|
|
|
// we have a relation column with dot notation
|
2008-08-09 07:45:43 +02:00
|
|
|
list($relationName,$columnName) = split('\.', $fieldName);
|
2008-08-09 06:53:34 +02:00
|
|
|
$relationObj = $obj->getComponent($relationName); // always gives us an component (either empty or existing)
|
|
|
|
$obj->setComponent($relationName, $relationObj);
|
|
|
|
$relationObj->write();
|
|
|
|
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
2008-08-09 07:00:42 +02:00
|
|
|
$obj->write();
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2008-08-09 07:00:42 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$obj->flushCache(); // avoid relation caching confusion
|
|
|
|
}
|
|
|
|
$id = ($preview) ? 0 : $obj->write();
|
|
|
|
|
|
|
|
// second run: save data
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($record as $fieldName => $val) {
|
2008-08-09 07:45:43 +02:00
|
|
|
if($this->isNullValue($val, $fieldName)) continue;
|
2008-11-18 02:48:37 +01:00
|
|
|
if(strpos($fieldName, '->') !== FALSE) {
|
|
|
|
$funcName = substr($fieldName, 2);
|
|
|
|
$this->$funcName($obj, $val, $record);
|
|
|
|
} else if($obj->hasMethod("import{$fieldName}")) {
|
2008-08-09 07:45:43 +02:00
|
|
|
$obj->{"import{$fieldName}"}($val, $record);
|
|
|
|
} else {
|
2008-10-03 02:27:58 +02:00
|
|
|
$obj->update(array($fieldName => $val));
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
// write record
|
2008-08-09 06:53:34 +02:00
|
|
|
$id = ($preview) ? 0 : $obj->write();
|
2008-09-15 16:21:43 +02:00
|
|
|
|
|
|
|
// @todo better message support
|
2008-08-09 06:53:34 +02:00
|
|
|
$message = '';
|
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
// save to results
|
|
|
|
if($existingObj) {
|
|
|
|
$results->addUpdated($obj, $message);
|
|
|
|
} else {
|
|
|
|
$results->addCreated($obj, $message);
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
// memory usage
|
2008-08-09 07:00:42 +02:00
|
|
|
unset($existingObj);
|
2008-08-09 06:53:34 +02:00
|
|
|
unset($obj);
|
|
|
|
}
|
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
/**
|
|
|
|
* Find an existing objects based on one or more uniqueness
|
|
|
|
* columns specified via {@link self::$duplicateChecks}
|
|
|
|
*
|
|
|
|
* @param array $record CSV data column
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2008-10-03 02:27:58 +02:00
|
|
|
public function findExistingObject($record) {
|
2008-08-09 07:00:42 +02:00
|
|
|
// checking for existing records (only if not already found)
|
|
|
|
foreach($this->duplicateChecks as $fieldName => $duplicateCheck) {
|
|
|
|
if(is_string($duplicateCheck)) {
|
|
|
|
$SQL_fieldName = Convert::raw2sql($duplicateCheck);
|
2008-08-09 07:45:43 +02:00
|
|
|
if(!isset($record[$fieldName])) {
|
2008-09-12 06:49:15 +02:00
|
|
|
return false;
|
|
|
|
//user_error("CsvBulkLoader:processRecord: Couldn't find duplicate identifier '{$fieldName}' in columns", E_USER_ERROR);
|
2008-08-09 07:45:43 +02:00
|
|
|
}
|
|
|
|
$SQL_fieldValue = $record[$fieldName];
|
2008-08-09 07:00:42 +02:00
|
|
|
$existingRecord = DataObject::get_one($this->objectClass, "`$SQL_fieldName` = '{$SQL_fieldValue}'");
|
|
|
|
if($existingRecord) return $existingRecord;
|
|
|
|
} elseif(is_array($duplicateCheck) && isset($duplicateCheck['callback'])) {
|
2008-08-09 07:45:43 +02:00
|
|
|
$existingRecord = singleton($this->objectClass)->{$duplicateCheck['callback']}($record[$fieldName], $record);
|
2008-08-09 07:00:42 +02:00
|
|
|
if($existingRecord) return $existingRecord;
|
|
|
|
} else {
|
|
|
|
user_error('CsvBulkLoader:processRecord: Wrong format for $duplicateChecks', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine wether any loaded files should be parsed
|
|
|
|
* with a header-row (otherwise we rely on {@link self::$columnMap}.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasHeaderRow() {
|
|
|
|
return ($this->hasHeaderRow || isset($this->columnMap));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|