Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
'adapter' => 'uri',
],
'resource' => [
'label' => 'Omeka resource (by ID)', // @translate
'label' => 'Omeka resource', // @translate
'adapter' => 'resource',
],
],
Expand All @@ -192,7 +192,7 @@
'csv_import_multivalue_separator' => ',',
'csv_import_rows_by_batch' => 20,
'csv_import_global_language' => '',
'csv_import_identifier_property' => '',
'csv_import_identifier_property' => 'dcterms:identifier',
'csv_import_automap_check_names_alone' => false,
],
],
Expand Down
42 changes: 21 additions & 21 deletions src/Form/MappingForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,27 @@ public function init()
],
]);

$basicSettingsFieldset->add([
'name' => 'identifier_property',
'type' => PropertySelect::class,
'options' => [
'label' => 'Resource identifier property', // @translate
'info' => 'Use this property, generally "dcterms:identifier", to identify the existing resources to link or to get. In all cases, it is strongly recommended to add one or more unique identifiers to all your resources.', // @translate
'empty_option' => 'Select below', // @translate
'prepend_value_options' => [
'internal_id' => 'Internal ID', // @translate
],
'term_as_value' => true,
],
'attributes' => [
'value' => $userSettings->get(
'csv_import_identifier_property',
$default['csv_import_identifier_property']),
'class' => 'chosen-select',
'data-placeholder' => 'Select a property', // @translate
],
]);

$this->add([
'type' => 'fieldset',
'name' => 'advanced-settings',
Expand Down Expand Up @@ -319,27 +340,6 @@ public function init()
]);
}

$advancedSettingsFieldset->add([
'name' => 'identifier_property',
'type' => PropertySelect::class,
'options' => [
'label' => 'Resource identifier property', // @translate
'info' => 'Use this property, generally "dcterms:identifier", to identify the existing resources, so it will be possible to update them. One column of the file must map the selected property. In all cases, it is strongly recommended to add one ore more unique identifiers to all your resources.', // @translate
'empty_option' => 'Select below', // @translate
'prepend_value_options' => [
'internal_id' => 'Internal ID', // @translate
],
'term_as_value' => true,
],
'attributes' => [
'value' => $userSettings->get(
'csv_import_identifier_property',
$default['csv_import_identifier_property']),
'class' => 'action-option chosen-select',
'data-placeholder' => 'Select a property', // @translate
],
]);

$advancedSettingsFieldset->add([
'name' => 'action_unidentified',
'type' => 'radio',
Expand Down
10 changes: 3 additions & 7 deletions src/Job/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function perform()
// The main identifier property may be used as term or as id in some
// places, so prepare it one time only.
if (empty($args['identifier_property']) || $args['identifier_property'] === 'internal_id') {
$this->identifierPropertyId = $args['identifier_property'];
$this->identifierPropertyId = 'internal_id';
} elseif (is_numeric($args['identifier_property'])) {
$this->identifierPropertyId = (int) $args['identifier_property'];
} else {
Expand All @@ -170,12 +170,8 @@ public function perform()
$this->rowsByBatch = (int) $args['rows_by_batch'];
}

// The core allows batch processes only for creation and deletion.
if (!in_array($args['action'], [self::ACTION_CREATE, self::ACTION_DELETE, self::ACTION_SKIP])
// It allows to identify resources too, so to use a new resource
// from a previous row.
|| ($args['action'] === self::ACTION_CREATE && $this->resourceType === 'resources')
) {
// The core allows batch processes only for deletion.
if (!in_array($args['action'], [self::ACTION_DELETE, self::ACTION_SKIP])) {
$this->rowsByBatch = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function init(array $args, ServiceLocatorInterface $serviceLocator)
$this->args = $args;
$this->serviceLocator = $serviceLocator;
$this->logger = $serviceLocator->get('Omeka\Logger');
$this->api = $serviceLocator->get('Omeka\ApiManager');
$this->api = $serviceLocator->get('ControllerPluginManager')->get('api');
}

public function getServiceLocator()
Expand Down
50 changes: 49 additions & 1 deletion src/Mapping/PropertyMapping.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
<?php
namespace CSVImport\Mapping;

use CSVImport\Mvc\Controller\Plugin\FindResourcesFromIdentifiers;
use Omeka\Stdlib\Message;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Renderer\PhpRenderer;

class PropertyMapping extends AbstractMapping
{
protected $label = 'Properties'; // @translate
protected $name = 'property-selector';

/**
* @var FindResourcesFromIdentifiers
*/
protected $findResourceFromIdentifier;

/**
* @var int|string
*/
protected $propertyIdentifier;

public function getSidebar(PhpRenderer $view)
{
return $view->csvPropertySelector($view->translate('Properties'), false);
}

public function init(array $args, ServiceLocatorInterface $serviceLocator)
{
parent::init($args, $serviceLocator);
$this->findResourceFromIdentifier = $serviceLocator->get('ControllerPluginManager')
->get('findResourceFromIdentifier');

// The main identifier property may be used as term or as id in some
// places, so prepare it one time only.
if (empty($args['identifier_property']) || $args['identifier_property'] === 'internal_id') {
$this->propertyIdentifier = 'internal_id';
} elseif (is_numeric($args['identifier_property'])) {
$this->propertyIdentifier = (int) $args['identifier_property'];
} else {
$result = $this->api
->searchOne('properties', ['term' => $args['identifier_property']])->getContent();
$this->propertyIdentifier = $result ? $result->id() : 'internal_id';
}
}

public function processRow(array $row)
{
// Reset the data and the map between rows.
Expand Down Expand Up @@ -46,6 +78,7 @@ public function processRow(array $row)
}

$dataTypeAdapters = $this->getDataTypeAdapters();
$findResourceFromIdentifier = $this->findResourceFromIdentifier;

// Get default option values.
$globalLanguage = isset($this->args['global_language']) ? $this->args['global_language'] : '';
Expand Down Expand Up @@ -98,8 +131,9 @@ public function processRow(array $row)
break;

case 'resource':
$identifier = $this->findResource($value, $this->propertyIdentifier);
$valueData = [
'value_resource_id' => $value,
'value_resource_id' => $identifier,
'property_id' => $propertyId,
'type' => $type,
];
Expand Down Expand Up @@ -148,4 +182,18 @@ protected function getDataTypeAdapters()
}
return $dataTypeAdapters;
}

protected function findResource($identifier, $propertyIdentifier = 'o:id')
{
$resourceType = $this->args['resource_type'];
$findResourceFromIdentifier = $this->findResourceFromIdentifier;
$resourceId = $findResourceFromIdentifier($identifier, $propertyIdentifier, $resourceType);
if (empty($resourceId)) {
$this->logger->err(new Message('"%s" (%s) is not a valid resource identifier.', // @translate
$identifier, $propertyIdentifier));
$this->setHasErr(true);
return false;
}
return $resourceId;
}
}