ImportSource: take over modifier loading logic

This commit is contained in:
Thomas Gelf 2016-07-20 13:40:43 +02:00
parent 8d2f546a2e
commit 38bf19eb92
4 changed files with 31 additions and 55 deletions

View File

@ -76,24 +76,10 @@ class ImportsourceHookTable extends QuickTable
protected function applyModifiers() protected function applyModifiers()
{ {
$modifiers = $this->source->fetchRowModifiers(); $modifiers = $this->source->getRowModifiers();
if (empty($modifiers)) {
return;
}
$propertyModifiers = array();
foreach ($modifiers as $mod) {
if (! array_key_exists($mod->property_name, $propertyModifiers)) {
$propertyModifiers[$mod->property_name] = array();
}
$propertyModifiers[$mod->property_name][] = $mod->getInstance();
}
foreach ($this->dataCache as & $row) { foreach ($this->dataCache as & $row) {
foreach ($propertyModifiers as $key => $mods) { foreach ($modifiers as $key => $mods) {
foreach ($mods as $mod) { foreach ($mods as $mod) {
if (is_array($row->$key)) { if (is_array($row->$key)) {
foreach ($row->$key as & $k) { foreach ($row->$key as & $k) {

View File

@ -26,28 +26,9 @@ abstract class PropertyModifierHook
return $class; return $class;
} }
public static function loadById($property_id, Db $connection) public function getTargetProperty()
{ {
$db = $connection->getDbAdapter(); return null;
$modifier = $db->fetchRow(
$db->select()->from(
'import_row_modifier',
array('id', 'provider_class')
)->where('property_id = ?', $property_id)
);
$settings = $db->fetchPairs(
$db->select()->from(
'import_row_modifier_settings',
array('setting_name', 'setting_value')
)->where('modifier_id = ?', $modifier->id)
);
$obj = new $modifier->provider_class;
$obj->setSettings($settings);
$obj->setDb($db);
return $obj;
} }
public function setDb(Db $db) public function setDb(Db $db)

View File

@ -166,7 +166,7 @@ class Import
)->fetchData(); )->fetchData();
} }
$modifiers = $this->prepareModifiers(); $modifiers = $this->source->getRowModifiers();
if (empty($modifiers)) { if (empty($modifiers)) {
return $this->data; return $this->data;
@ -224,23 +224,6 @@ class Import
return $this->properties[$checksum]; return $this->properties[$checksum];
} }
// TODO: move to ImportSource, this duplicates logic from preview
protected function prepareModifiers()
{
$modifiers = $this->source->fetchRowModifiers();
$propertyModifiers = array();
foreach ($modifiers as $mod) {
if (! array_key_exists($mod->property_name, $propertyModifiers)) {
$propertyModifiers[$mod->property_name] = array();
}
$propertyModifiers[$mod->property_name][] = $mod->getInstance();
}
return $propertyModifiers;
}
/** /**
* Walk through each row, prepare properties and calculate checksums * Walk through each row, prepare properties and calculate checksums
*/ */

View File

@ -30,6 +30,8 @@ class ImportSource extends DbObjectWithSettings
protected $settingsRemoteId = 'source_id'; protected $settingsRemoteId = 'source_id';
private $rowModifiers;
public function fetchLastRun($required = false) public function fetchLastRun($required = false)
{ {
return $this->fetchLastRunBefore(time() + 1, $required); return $this->fetchLastRunBefore(time() + 1, $required);
@ -75,6 +77,15 @@ class ImportSource extends DbObjectWithSettings
return null; return null;
} }
public function getRowModifiers()
{
if ($this->rowModifiers === null) {
$this->prepareRowModifiers();
}
return $this->rowModifiers;
}
public function fetchRowModifiers() public function fetchRowModifiers()
{ {
$db = $this->getDb(); $db = $this->getDb();
@ -87,6 +98,21 @@ class ImportSource extends DbObjectWithSettings
); );
} }
protected function prepareRowModifiers()
{
$modifiers = array();
foreach ($this->fetchRowModifiers() as $mod) {
if (! array_key_exists($mod->property_name, $modifiers)) {
$modifiers[$mod->property_name] = array();
}
$modifiers[$mod->property_name][] = $mod->getInstance();
}
$this->rowModifiers = $modifiers;
}
public function checkForChanges($runImport = false) public function checkForChanges($runImport = false)
{ {
$hadChanges = false; $hadChanges = false;