From 06e43b48539e86d4bb2fc4172ddae7c61f03b124 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 27 Jul 2020 00:00:00 +0000 Subject: [PATCH] Updated import of resource templates to manage multiple data types. --- .../Admin/ResourceTemplateController.php | 59 +++++++++++++++---- .../resource-template/review-import.phtml | 50 +++++++++------- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/application/src/Controller/Admin/ResourceTemplateController.php b/application/src/Controller/Admin/ResourceTemplateController.php index d02c31a46f..8050bb9198 100644 --- a/application/src/Controller/Admin/ResourceTemplateController.php +++ b/application/src/Controller/Admin/ResourceTemplateController.php @@ -79,12 +79,12 @@ public function importAction() } } else { // Process review import form. - $import = json_decode($this->params()->fromPost('import'), true); - $dataTypes = $this->params()->fromPost('data_types', []); + $import = json_decode($form->getData()['import'], true); + $dataTypes = $this->params()->fromPost('data_types'); $import['o:label'] = $this->params()->fromPost('label'); - foreach ($dataTypes as $key => $dataType) { - $import['o:resource_template_property'][$key]['o:data_type'] = $dataType; + foreach ($dataTypes as $key => $dataTypeList) { + $import['o:resource_template_property'][$key]['o:data_type'] = $dataTypeList; } $response = $this->api($form)->create('resource_templates', $import); @@ -121,6 +121,8 @@ public function importAction() * the property. By design, the API will only hydrate members and data types * that are flagged as valid. * + * @todo Manage direct import of data types from Value Suggest and other modules. + * * @param array $import * @return array */ @@ -187,8 +189,25 @@ protected function flagValid(array $import) ])->getContent(); if ($prop) { $import['o:resource_template_property'][$key]['o:property'] = ['o:id' => $prop->id()]; - if (in_array($import['o:resource_template_property'][$key]['data_type_name'], $dataTypes)) { - $import['o:resource_template_property'][$key]['o:data_type'] = $import['o:resource_template_property'][$key]['data_type_name']; + // Check the deprecated "data_type_name" if needed and + // normalize it. + if (!array_key_exists('data_types', $import['o:resource_template_property'][$key])) { + $import['o:resource_template_property'][$key]['data_types'] = [[ + 'name' => $import['o:resource_template_property'][$key]['data_type_name'], + 'label' => $import['o:resource_template_property'][$key]['data_type_label'], + ]]; + } + $importDataTypes = []; + foreach ($import['o:resource_template_property'][$key]['data_types'] as $dataType) { + $importDataTypes[$dataType['name']] = $dataType; + } + $import['o:resource_template_property'][$key]['data_types'] = $importDataTypes; + // Prepare the list of standard data types. + $import['o:resource_template_property'][$key]['o:data_type'] = []; + foreach ($importDataTypes as $name => $importDataType) { + if (in_array($name, $dataTypes)) { + $import['o:resource_template_property'][$key]['o:data_type'][] = $importDataType['name']; + } } } } @@ -276,6 +295,11 @@ protected function importIsValid($import) // invalid o:resource_template_property format return false; } + + // Manage import from an export of Omeka < 3.0. + $oldExport = !array_key_exists('data_types', $property); + + // Check missing o:resource_template_property info. if (!array_key_exists('vocabulary_namespace_uri', $property) || !array_key_exists('vocabulary_label', $property) || !array_key_exists('local_name', $property) @@ -284,12 +308,17 @@ protected function importIsValid($import) || !array_key_exists('o:alternate_comment', $property) || !array_key_exists('o:is_required', $property) || !array_key_exists('o:is_private', $property) - || !array_key_exists('data_type_name', $property) - || !array_key_exists('data_type_label', $property) ) { - // missing o:resource_template_property info return false; } + if ($oldExport + && (!array_key_exists('data_type_name', $property) + || !array_key_exists('data_type_label', $property) + )) { + return false; + } + + // Check invalid o:resource_template_property info. if (!is_string($property['vocabulary_namespace_uri']) || !is_string($property['vocabulary_label']) || !is_string($property['local_name']) @@ -298,10 +327,16 @@ protected function importIsValid($import) || (!is_string($property['o:alternate_comment']) && !is_null($property['o:alternate_comment'])) || !is_bool($property['o:is_required']) || !is_bool($property['o:is_private']) - || (!is_string($property['data_type_name']) && !is_null($property['data_type_name'])) - || (!is_string($property['data_type_label']) && !is_null($property['data_type_label'])) ) { - // invalid o:resource_template_property info + return false; + } + if ($oldExport) { + if ((!is_string($property['data_type_name']) && !is_null($property['data_type_name'])) + || (!is_string($property['data_type_label']) && !is_null($property['data_type_label'])) + ) { + return false; + } + } elseif (!is_array($property['data_types']) && !is_null($property['data_types'])) { return false; } } diff --git a/application/view/omeka/admin/resource-template/review-import.phtml b/application/view/omeka/admin/resource-template/review-import.phtml index ab500db3a7..e0660a09dc 100644 --- a/application/view/omeka/admin/resource-template/review-import.phtml +++ b/application/view/omeka/admin/resource-template/review-import.phtml @@ -6,6 +6,11 @@ $form->prepare(); $this->htmlElement('body')->appendAttribute('class', 'resource-templates review'); echo $this->pageTitle($import['o:label'], 1, $translate('Resource templates'), $translate('Review')); ?> + + + form()->openTag($form); ?>
@@ -55,12 +60,11 @@ if (!$hasVocab) { - - + @@ -77,33 +81,39 @@ if (!$hasVocab) { } elseif (!$hasProperty) { $errorText = sprintf('Unknown property "%s" in vocabulary "%s": %s:%s', $property['label'], $property['vocabulary_label'], $property['vocabulary_prefix'], $property['local_name']); } -// Set the data type -if (is_null($property['data_type_name'])) { - $dataType = 'Default'; -} elseif (in_array($property['data_type_name'], ['literal', 'uri', 'resource'])) { - $dataType = $this->escapeHtml($property['data_type_label']); -} elseif ($hasProperty) { - $dataTypeSelect = $this->dataType()->getSelect( - sprintf('data_types[%s]', $key), - isset($property['o:data_type']) ? $property['o:data_type'] : null, - ['style' => 'width:100%;', 'class' => 'chosen-select'] - ); - $dataType = sprintf('%s
%s', $this->escapeHtml($property['data_type_label']), $dataTypeSelect); +// Prepare the data types (display the labels, then the select if needed). +if ($hasProperty) { + if (empty($property['data_types'])): + $dataTypes = $translate('Default'); + else: + $dataTypes = ''; + if (count($property['data_types']) !== count($property['o:data_type'])): + $dataTypes .= $this->dataType()->getSelect( + sprintf('data_types[%s]', $key), + isset($property['o:data_type']) ? $property['o:data_type'] : null, + ['style' => 'width:100%;', 'class' => 'chosen-select', 'multiple' => 'multiple', 'data-placeholder' => $translate('Select data types…')] + ); + endif; + endif; } else { - $dataType = $property['data_type_label']; + $dataTypes = $property['data_type_label']; } ?> - - - - + + + + - +
escapeHtml($property['label']); ?>escapeHtml($property['o:alternate_label']); ?>escapeHtml($property['o:alternate_comment']); ?>
escapeHtml($errorText); ?>