partial('common/reference-skiplinks'); ?>
getHelperPluginManager(); $url = $plugins->get('url'); $api = $plugins->get('api'); $escape = $plugins->get('escapeHtml'); $translate = $plugins->get('translate'); // This is the Omeka default title. May be different in the database. $emptyTitle = $translate('[Untitled]'); $siteSlug = $this->params()->fromRoute('site-slug') ?? (($site = $plugins->get('Laminas\View\Helper\ViewModel')->getRoot()->getVariable('site')) ? $site->slug() : null) // TODO Store the default site slug, not only the default site id. // If no default site, get the first public one. ?? (($site = $api->searchOne('sites', ['id' => $plugins->get('setting')('default_site'), 'sort_by' => 'is_public', 'sort_order' => 'desc'])->getContent()) ? $site->slug() : null); $this->headLink()->prependStylesheet($this->assetUrl('css/reference.css', 'Reference')); $mapResourcesControllers = ['resources' => 'resource', 'items' => 'item', 'item_sets' => 'item-set', 'media' => 'media']; $controllerName = $mapResourcesControllers[$options['resource_name']]; // Set default values. $options += [ 'by_initial' => false, 'link_to_single' => true, 'total' => true, 'raw' => false, 'raw_sub' => false, 'skiplinks' => true, 'headings' => true, 'custom_url' => false, ]; $options['custom_url'] = $options['custom_url'] && $options['link_to_single']; $hasInitial = array_key_exists('initial', $references[key($references)]); $listByMax = empty($options['list_by_max']) ? false : (int) $options['list_by_max']; $seeAll = $escape($translate('See all…')); // @translate $andMore = $escape($translate('And %d more.')); // @translate $hasSubjectProperty = !empty($options['subject_property']); $options['link_to_single'] = $options['link_to_single'] && !$listByMax; $isRecursive = $listByMax || $hasSubjectProperty; $options['raw_sub'] = $options['raw_sub'] || !$isRecursive; $querySource = $query; // Normally, field types should not be mixed currently. $fieldTypes = [ 'o:ResourceClass' => 'resource_class_id', 'o:ResourceTemplate' => 'resource_template_id', 'o:ItemSet' => 'item_set_id', 'resource_classes' => 'resource_class_id', 'resource_templates' => 'resource_template_id', 'item_sets' => 'item_set_id', // Other cases are properties. ]; $mainFieldType = $fieldTypes[$request['o:field'][0]['@type']] ?? null; // Set default templates according to options for improved performance. $template = ''; $replaceBase = []; if ($options['headings']) { if ($options['skiplinks']) { $templateHeading = '

{heading_text}

' . PHP_EOL; } else { $templateHeading = '

{heading_text}

' . PHP_EOL; } $template .= '{heading}'; $replaceBase['{heading}'] = ''; } $replaceBase['{reference_text}'] = ''; // List by max may require templateUrlSingle. if (!$options['raw'] || ($listByMax && !$options['raw_sub'])) { $tempPlaceholder = '__temp_placeholder__'; $templateUrlSingle = str_replace($tempPlaceholder, '{reference}', $url('site/resource-id', ['site-slug' => $siteSlug, 'controller' => $controllerName, 'id' => $tempPlaceholder], false)); } if ($options['raw']) { if ($options['total']) { $template .= '
{reference_text}{reference_total}{recursive}
' . PHP_EOL; $replaceBase['{reference_total}'] = ''; } else { $template .= '
{reference_text}{recursive}
' . PHP_EOL; } } else { $replaceBase['{reference_url}'] = ''; if ($options['total']) { $template .= '
{reference_text}{reference_total}{recursive}
' . PHP_EOL; $replaceBase['{reference_total}'] = ''; } else { $template .= '
{reference_text}{recursive}
' . PHP_EOL; } if (!$options['custom_url']) { // Prepare the custom url one time with the original query as the base of the url, // avoiding overriding with the reference value. $query = $querySource; $queryEmpty = $querySource; // Normally, types cannot be mixed, so it should be possible to use the main field type. $hasEmptyFieldProperty = false; foreach ($request['o:field'] as $field) { $fieldType = $fieldTypes[$field['@type']] ?? null; if ($fieldType) { $query[$fieldType][] = $field['o:id']; $queryEmpty[$fieldType][] = $field['o:id']; if (!$hasEmptyFieldProperty) { $hasEmptyFieldProperty = true; $query['property'][] = ['joiner' => 'or', 'property' => 'dcterms:title', 'type' => 'eq', 'text' => '__temp_placeholder__']; $queryEmpty['property'][] = ['joiner' => 'and', 'property' => 'dcterms:title', 'type' => 'nex']; } } else { $query['property'][] = ['joiner' => 'or', 'property' => $field['o:term'], 'type' => 'eq', 'text' => '__temp_placeholder__']; } } // The default url helper doesn't encode "&" as "&", so append it manually. $templateUrlEmpty = str_replace('__temp_placeholder__', '{reference}', $url('site/resource', ['site-slug' => $siteSlug, 'controller' => $controllerName], false) . '?' . http_build_query($queryEmpty, '', '&', PHP_QUERY_RFC3986)); $templateUrl = str_replace('__temp_placeholder__', '{reference}', $url('site/resource', ['site-slug' => $siteSlug, 'controller' => $controllerName], false) . '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986)); } } $querier = function ($reference, $emptyReferenceValue, $limit = null) use ($querySource, $request, $fieldTypes) { $query = $querySource; // Normally, types cannot be mixed, so it should be possible to use the main field type. $hasEmptyFieldProperty = false; foreach ($request['o:field'] as $field) { $fieldType = $fieldTypes[$field['@type']] ?? null; if ($fieldType) { $query[$fieldType][] = $field['o:id']; if ($emptyReferenceValue) { if (!$hasEmptyFieldProperty) { $hasEmptyFieldProperty = true; $query['property'][] = ['joiner' => 'or', 'property' => 'dcterms:title', 'type' => 'nex']; } } else { $query['property'][] = ['joiner' => 'or', 'property' => 'dcterms:title', 'type' => 'eq', 'text' => $reference]; } } else { $query['property'][] = ['joiner' => 'or', 'property' => $field['o:term'], 'type' => 'eq', 'text' => $reference]; } } $query['limit'] = $limit; return $query; }; ?> partial('common/reference-skiplinks'); ?>
$currentHeadingId, '{heading_text}' => $currentHeading, ]; $heading = str_replace(array_keys($heading), array_values($heading), $templateHeading); $replace['{heading}'] = $heading; } } // FIXME ListByMax may need referenceUrl. if ($options['raw']) { $referenceText = $reference; $referenceNumber = true; $replace['{reference_text}'] = $referenceText; } else { $referenceNumber = !$options['link_to_single'] || $referenceData['total'] > 1; if ($options['custom_url']) { if ($referenceNumber) { $referenceUrl = $url('site/resource', ['site-slug' => $siteSlug, 'controller' => $controllerName], ['query' => $querier($reference, $emptyReferenceValue)], false); } else { // Don't use the resource to get the url, it's too slow, even if it allows to have customized url. Disable the feature if needed. // $referenceUrl = $api->read($resourceName, $referenceData['first'])->getContent()->siteUrl(); $referenceUrl = str_replace('{reference}', rawurlencode($referenceData['first']), $templateUrlSingle); } } else { if ($referenceNumber) { if ($mainFieldType) { $referenceUrl = $emptyReferenceValue ? $templateUrlEmpty : str_replace('{reference}', rawurlencode($reference), $templateUrl); } else { $referenceUrl = str_replace('{reference}', rawurlencode($reference), $templateUrl); } } else { $referenceUrl = str_replace('{reference}', rawurlencode($referenceData['first']), $templateUrlSingle); } } $replace['{reference_text}'] = $referenceValue; $replace['{reference_url}'] = $referenceUrl; } // Can be null when references are set directly. if ($options['total'] && $referenceNumber && @$referenceData['total']) { $referenceTotal = ' (' . $referenceData['total'] . ')'; $replace['{reference_total}'] = $referenceTotal; } $recursive = ''; // TODO To be improved to avoid to read from the api (resources by reference or linked resources). // TODO Make it recursive. if ($listByMax) { $recursive .= ''; } elseif ($hasSubjectProperty && !empty($referenceData['first'])) { /** @var \Omeka\Api\Representation\AbstractResourceEntityRepresentation $res */ $res = $api->read('resources', $referenceData['first'])->getContent(); // $res = $api->read($res->resourceName(), $referenceData['first'])->getContent(); $subjectValues = $res->subjectValues(null, null, $options['subject_property']['id']); if (count($subjectValues)) { $recursive .= ''; } } $replace['{recursive}'] = $recursive; // Process the output of one reference (and eventually the initial heading). echo str_replace(array_keys($replace), array_values($replace), $template); ?>