Skip to content

Commit

Permalink
Updated import/export of resource templates to manage multiple data t…
Browse files Browse the repository at this point in the history
…ypes.
  • Loading branch information
Daniel-KM authored and Daniel Berthereau committed Sep 18, 2020
1 parent b39a92a commit dc11c44
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 42 deletions.
79 changes: 57 additions & 22 deletions application/src/Controller/Admin/ResourceTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ 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);
$import['o:label'] = $this->params()->fromPost('label');
foreach ($dataTypes as $key => $dataType) {
$import['o:resource_template_property'][$key]['o:data_type'] = $dataType;

$dataTypes = $this->params()->fromPost('data_types');
if ($dataTypes) {
foreach ($dataTypes as $key => $dataTypeList) {
$import['o:resource_template_property'][$key]['o:data_type'] = $dataTypeList;
}
}

$response = $this->api($form)->create('resource_templates', $import);
Expand Down Expand Up @@ -121,6 +123,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
*/
Expand Down Expand Up @@ -187,8 +191,31 @@ 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])) {
if (!empty($import['o:resource_template_property'][$key]['data_type_name'])
&& !empty($import['o:resource_template_property'][$key]['data_type_label'])
) {
$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'],
]];
} else {
$import['o:resource_template_property'][$key]['data_types'] = [];
}
}
$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'];
}
}
}
}
Expand Down Expand Up @@ -276,6 +303,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)
Expand All @@ -284,12 +316,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'])
Expand All @@ -298,10 +335,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;
}
}
Expand Down Expand Up @@ -356,21 +399,13 @@ public function exportAction()
$property = $templateProperty->property();
$vocab = $property->vocabulary();

$dataTypeName = $templateProperty->dataType();
$dataTypeLabel = null;
if ($dataTypeName) {
$dataType = $this->dataTypeManager->get($dataTypeName);
$dataTypeLabel = $dataType->getLabel();
}

// Note that "position" is implied by array order.
$export['o:resource_template_property'][] = [
'o:alternate_label' => $templateProperty->alternateLabel(),
'o:alternate_comment' => $templateProperty->alternateComment(),
'o:is_required' => $templateProperty->isRequired(),
'o:is_private' => $templateProperty->isPrivate(),
'data_type_name' => $dataTypeName,
'data_type_label' => $dataTypeLabel,
'data_types' => $templateProperty->dataTypeLabels(),
'vocabulary_namespace_uri' => $vocab->namespaceUri(),
'vocabulary_label' => $vocab->label(),
'local_name' => $property->localName(),
Expand Down
50 changes: 30 additions & 20 deletions application/view/omeka/admin/resource-template/review-import.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
?>

<style>
#properties .data-types .tablesaw-cell-content { display: block; }
</style>

<?php echo $this->form()->openTag($form); ?>
<div id="page-actions">
<button><?php echo $translate('Complete import'); ?></button>
Expand Down Expand Up @@ -55,12 +60,11 @@ if (!$hasVocab) {
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>

<table id="properties" class="tablesaw" data-tablesaw-mode="stack">
<thead>
<tr>
<th><?php echo $translate('Original label'); ?></th>
<th><?php echo $translate('Data type'); ?></th>
<th><?php echo $translate('Data types'); ?></th>
<th><?php echo $translate('Alternate label'); ?></th>
<th><?php echo $translate('Alternate comment'); ?></th>
<th><?php echo $translate('Required?'); ?></th>
Expand All @@ -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<br>%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 = '<ul>';
foreach ($property['data_types'] as $dataType):
$dataTypes .= '<li>' . (isset($dataType['label']) ? $translate($dataType['label']) : $dataType['name']) . '</li>';
endforeach;
$dataTypes .= '</ul>';
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'];
}
?>
<tr style="background-color:#<?php echo $hasProperty ? 'cdffcd;' : 'ffe6e6'; ?>">
<td><?php echo $this->escapeHtml($property['label']); ?></td>
<td><?php echo $dataType; ?></td>
<td><?php echo $this->escapeHtml($property['o:alternate_label']); ?></td>
<td><?php echo $this->escapeHtml($property['o:alternate_comment']); ?></td>
<td><?php echo $escape($property['label']); ?></td>
<td class="data-types"><?php echo $dataTypes; ?></td>
<td><?php echo $escape($property['o:alternate_label']); ?></td>
<td><?php echo $escape($property['o:alternate_comment']); ?></td>
<td><?php echo $property['o:is_required'] ? $translate('Yes') : $translate('No'); ?></td>
<td><?php echo $property['o:is_private'] ? $translate('Yes') : $translate('No'); ?></td>
</tr>
<?php if (!$hasProperty): ?>
<tr>
<td colspan="5" style="background-color:#ffe6e6; color:red;"><?php echo $this->escapeHtml($errorText); ?></td>
<td colspan="5" style="background-color:#ffe6e6; color:red;"><?php echo $escape($errorText); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
Expand Down

0 comments on commit dc11c44

Please sign in to comment.