Skip to content

Commit

Permalink
Updated export of resource templates to manage multiple data types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Jul 27, 2020
1 parent fc970e3 commit b779c1c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Omeka\Api\Representation;

use Omeka\Entity\ResourceTemplateProperty;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\ServiceLocatorInterface;

class ResourceTemplatePropertyRepresentation extends AbstractRepresentation
Expand Down Expand Up @@ -91,12 +90,10 @@ public function dataType()
return null;
}
$dataType = reset($dataTypes);
try {
return $this->getServiceLocator()->get('Omeka\DataTypeManager')->get($dataType);
} catch (ServiceNotFoundException $e) {
// Treat an unknown data type as "Default".
return null;
}
// Treat an unknown data type as "Default".
return $this->getServiceLocator()->get('Omeka\DataTypeManager')->has($dataType)
? $dataType
: null;
}

/**
Expand All @@ -112,11 +109,9 @@ public function dataTypes()
$dataTypeManager = $this->getServiceLocator()->get('Omeka\DataTypeManager');
$result = [];
foreach ($dataTypes as $dataType) {
try {
$dataTypeManager->get($dataType);
// Treat an unknown data type as "Default".
if ($dataTypeManager->has($dataType)) {
$result[] = $dataType;
} catch (ServiceNotFoundException $e) {
// Treat an unknown data type as "Default".
}
}
return $result;
Expand All @@ -137,17 +132,19 @@ public function dataTypeLabel()
}

/**
* @return array Associative array of data type names and labels.
* @return array List of data type names and labels.
*/
public function dataTypeLabels()
{
$dataTypes = $this->dataTypes();
$dataTypes = array_flip($dataTypes);
$result = [];
$dataTypeManager = $this->getServiceLocator()->get('Omeka\DataTypeManager');
foreach (array_keys($dataTypes) as $dataType) {
$dataTypes[$dataType] = $dataTypeManager->get($dataType)->getLabel();
foreach ($this->dataTypes() as $dataType) {
$result[] = [
'name' => $dataType,
'label' => $dataTypeManager->get($dataType)->getLabel(),
];
}
return $dataTypes;
return $result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,13 @@ public function exportAction()
$dataTypeLabel = $dataType->getLabel();
}

$dataTypes = array_map(function ($label, $name) {
return ['name' => $name, 'label' => $label];
}, $templateProperty->dataTypesLabels());

// 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_types' => array_values($dataTypes),
'data_types' => $templateProperty->dataTypeLabels(),
'vocabulary_namespace_uri' => $vocab->namespaceUri(),
'vocabulary_label' => $vocab->label(),
'local_name' => $property->localName(),
Expand Down
4 changes: 2 additions & 2 deletions application/view/omeka/admin/resource-template/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ $this->htmlElement('body')->appendAttribute('class', 'resource-templates show');
</td>
<td>
<ul>
<?php foreach ($resourceTemplateProperty->dataTypeLabels() as $label): ?>
<li><?php echo $escape($label); ?></li>
<?php foreach ($resourceTemplateProperty->dataTypeLabels() as $dataTypeLabel): ?>
<li><?php echo $escape($dataTypeLabel['label']); ?></li>
<?php endforeach; ?>
</ul>
</td>
Expand Down

0 comments on commit b779c1c

Please sign in to comment.