private function replaceOrDelete($row, &$settings, IImportElementType $service, array $fields)
{
// Set criteria according to elementtype
$criteria = $service->setCriteria($settings);
// Set up criteria model for matching
$cmodel = array();
foreach ($settings['map'] as $key => $value) {
if (isset($settings['unique'][$key]) && intval($settings['unique'][$key]) == 1 && $value != 'dont') {
// Unique value should have a value
if (trim($fields[$value]) != '') {
$criteria->{$value} = $cmodel[$value] = $fields[$value];
} else {
// Else stop the operation - chance of success is only small
$this->log[$row] = craft()->import_history->log($settings['history'], $row, array(array(Craft::t('Tried to match criteria but its value was not set.'))));
return;
}
}
}
// If there's a match...
if (count($cmodel) && $criteria->count()) {
// Get current user
$currentUser = craft()->users->getUserById($settings['user']);
// If we're deleting
if ($currentUser->can('delete') && $settings['behavior'] == ImportModel::BehaviorDelete) {
// Get elements to delete
$elements = $criteria->find();
// Fire an 'onBeforeImportDelete' event
$event = new Event($this, array('elements' => $elements));
$this->onBeforeImportDelete($event);
// Give event the chance to blow off deletion
if ($event->performAction) {
try {
// Do it
if (!$service->delete($elements)) {
// Log errors when unsuccessful
$this->log[$row] = craft()->import_history->log($settings['history'], $row, array(array(Craft::t('Something went wrong while deleting this row.'))));
}
} catch (Exception $e) {
// Something went terribly wrong, assume its only this row
$this->log[$row] = craft()->import_history->log($settings['history'], $row, array('exception' => array($e->getMessage())));
}
}
// Skip rest and continue
return;
} elseif ($currentUser->can('append') || $currentUser->can('replace')) {
// Fill new EntryModel with match
return $criteria->first();
} else {
// No permissions!
throw new Exception(Craft::t('Tried to import without permission.'));
}
}
// Else do nothing
return;
}