From c4d4a6632444988ba52304de5c110a6faf6be67a Mon Sep 17 00:00:00 2001 From: Joeli Takala Date: Wed, 10 Nov 2021 21:39:36 +0200 Subject: [PATCH 1/6] Functionality for adding custom labels and tooltips for properties in concept view --- controller/WebController.php | 8 +++++- model/VocabularyConfig.php | 50 ++++++++++++++++++++++++++++++++++ tests/VocabularyConfigTest.php | 18 ++++++++++++ tests/testconfig.ttl | 16 +++++++++++ view/concept-shared.twig | 21 ++++++++++++-- 5 files changed, 110 insertions(+), 3 deletions(-) diff --git a/controller/WebController.php b/controller/WebController.php index 97a02d921..300c14b12 100644 --- a/controller/WebController.php +++ b/controller/WebController.php @@ -178,6 +178,11 @@ public function invokeVocabularyConcept(Request $request) if ($this->notModified($results[0])) { return; } + $customLabels = $vocab->getConfig()->getPropertyLabelOverrides(); + + $logger = $this->model->getLogger(); + $logger->info("Custom Labels\n" . print_r($customLabels, TRUE)); + $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); $template = (in_array('skos:Concept', $results[0]->getType()) || in_array('skos:ConceptScheme', $results[0]->getType())) ? $this->twig->loadTemplate('concept-info.twig') : $this->twig->loadTemplate('group-contents.twig'); @@ -191,7 +196,8 @@ public function invokeVocabularyConcept(Request $request) 'bread_crumbs' => $crumbs['breadcrumbs'], 'combined' => $crumbs['combined'], 'request' => $request, - 'plugin_params' => $pluginParameters) + 'plugin_params' => $pluginParameters, + 'custom_labels' => $customLabels) ); } diff --git a/model/VocabularyConfig.php b/model/VocabularyConfig.php index 117533758..363d132d5 100644 --- a/model/VocabularyConfig.php +++ b/model/VocabularyConfig.php @@ -8,6 +8,7 @@ class VocabularyConfig extends BaseConfig private $pluginRegister; private $pluginParameters = array(); private $languageOrderCache = array(); + private $labelOverrides = array(); const DEFAULT_PROPERTY_ORDER = array("rdf:type", "dc:isReplacedBy", "skos:definition", "skos:broader", "isothes:broaderGeneric", @@ -33,6 +34,7 @@ public function __construct($resource, $globalPlugins=array()) $this->resource = $resource; $this->globalPlugins = $globalPlugins; $this->setParameterizedPlugins(); + $this->setPropertyLabelOverrides(); $pluginArray = $this->getPluginArray(); $this->pluginRegister = new PluginRegister($pluginArray); } @@ -123,6 +125,46 @@ private function setPluginParameters(Easyrdf\Resource $pluginResource) : void } } + /** + * Sets array of configured property label overrides + * @return void + */ + private function setPropertyLabelOverrides() : void + { + $this->labelOverrides = array(); + $overrides = $this->resource->allResources('skosmos:propertyLabelOverride'); + if ($overrides) { + foreach ($overrides as $override) { + $this->setLabelOverride($override); + } + } + } + + /** + * Updates array of label overrides by adding a new override from the configuration file + * @param Easyrdf\Resource $labelOverride + * @return void + */ + private function setLabelOverride(Easyrdf\Resource $override) : void + { + $labelProperty = $override->getResource('skosmos:property'); + $labelPropUri = $labelProperty->shorten(); + if (empty($this->labelOverrides[$labelPropUri])) { + $this->labelOverrides[$labelPropUri] = array(); + } + $newOverrides = array(); + + $labels = $override->allLiterals('rdfs:label'); //property label overrides + foreach ($labels as $label) { + $newOverrides['label'][$label->getLang()] = $label->getValue(); + } + $descriptions = $override->allLiterals('schema:description'); //optionally override property label tooltips + foreach ($descriptions as $description) { + $newOverrides['description'][$description->getLang()] = $description->getValue(); + } + $this->labelOverrides[$labelPropUri] = array_merge($newOverrides, $this->labelOverrides[$labelPropUri]); + } + /** * Get the SPARQL endpoint URL for this vocabulary * @@ -505,6 +547,14 @@ public function getPluginParameters() { return $this->pluginParameters; } + /** + * Get the list of property label overrides + * @return array of custom property labels + */ + public function getPropertyLabelOverrides() { + return $this->labelOverrides; + } + /** * Returns the vocabulary default sidebar view. * @return string name of the view diff --git a/tests/VocabularyConfigTest.php b/tests/VocabularyConfigTest.php index 895c7bda8..e87440883 100644 --- a/tests/VocabularyConfigTest.php +++ b/tests/VocabularyConfigTest.php @@ -618,4 +618,22 @@ public function testGetPropertyOrderCustom() { $this->assertEquals($order, $params); } + /** + * @covers VocabularyConfig::getPropertyLabelOverrides + * @covers VocabularyConfig::setPropertyLabelOverrides + * @covers VocabularyConfig::setLabelOverride + */ + public function testGetPropertyLabelOverrides() { + $vocab = $this->model->getVocabulary('conceptPropertyLabels'); + + $overrides = $vocab->getConfig()->getPropertyLabelOverrides(); + + $expected = array( + 'skos:prefLabel' => array( 'label' => array ( 'en' => 'Caption', 'fi' => 'Luokka' ) ), + 'skos:notation' => array( 'label' => array ( 'en' => 'UDC number', 'fi' => 'UDC-numero', 'sv' => 'UDC-nummer' ), + 'description' => array( 'en' => 'Class Number') ), + ); + $this->assertEquals($expected, $overrides); + } + } diff --git a/tests/testconfig.ttl b/tests/testconfig.ttl index ddc8b8ad6..0d83a3798 100644 --- a/tests/testconfig.ttl +++ b/tests/testconfig.ttl @@ -298,6 +298,22 @@ skosmos:language "fi"; skosmos:sparqlGraph . +:conceptPropertyLabels a skosmos:Vocabulary, void:Dataset ; + dc:title "Test custom property labels"@en ; + dc:subject :cat_science ; + dc:type mdrtype:ONTOLOGY ; + void:dataDump ; + void:uriSpace "http://www.skosmos.skos/test/"; + skosmos:defaultLanguage "en"; + skosmos:language "en"; + skosmos:propertyLabelOverride + [ skosmos:property skos:prefLabel ; + rdfs:label "Caption"@en, "Luokka"@fi ], + [ skosmos:property skos:notation ; + rdfs:label "UDC number"@en, "UDC-numero"@fi, "UDC-nummer"@sv ; + schema:description "Class Number"@en ] ; + skosmos:sparqlGraph . + :cbd a skosmos:Vocabulary, void:Dataset ; dc11:title "A vocabulary for testing blank node processing and reification"@en ; dc:subject :cat_general ; diff --git a/view/concept-shared.twig b/view/concept-shared.twig index 7670c41ff..95416ddda 100644 --- a/view/concept-shared.twig +++ b/view/concept-shared.twig @@ -42,7 +42,16 @@ {% spaceless %}
-

{% set subPrefLabelTranslation = concept.preferredSubpropertyLabelTranslation(request.lang) %}{% if subPrefLabelTranslation %}{{ subPrefLabelTranslation }}{% else %}{{ 'skos:prefLabel'|trans }}{% endif %}

+

+ {% set subPrefLabelTranslation = concept.preferredSubpropertyLabelTranslation(request.lang) %} + {% if subPrefLabelTranslation %} + {{ subPrefLabelTranslation }} + {% elseif custom_labels['skos:prefLabel'] %} + {{ custom_labels['skos:prefLabel']['label'][request.contentLang] }} + {% else %} + {{ 'skos:prefLabel'|trans }} + {% endif %} +

{% if concept.foundBy %} {# hit has been found through an alternative label #} {{ concept.foundBy }} > @@ -90,7 +99,15 @@ {% if property.getSubPropertyOf != 'skos:hiddenLabel' %}
-

{{ property.label }}

+ {% if custom_labels[property.type] %} +

{{ custom_labels[property.type]['label'][request.contentLang] }}

+ {% else %} +

{{ property.label }}

+ {% endif %}
{% if request.vocab.config.hasMultiLingualProperty(property.type) %} From 70af06a1b88083c7a0dc5db3b2b2b2168b90a2f9 Mon Sep 17 00:00:00 2001 From: Joeli Takala Date: Wed, 10 Nov 2021 22:11:56 +0200 Subject: [PATCH 2/6] Tidying up after scrutinizer feedback --- controller/WebController.php | 3 --- model/VocabularyConfig.php | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/controller/WebController.php b/controller/WebController.php index 300c14b12..734514afe 100644 --- a/controller/WebController.php +++ b/controller/WebController.php @@ -180,9 +180,6 @@ public function invokeVocabularyConcept(Request $request) } $customLabels = $vocab->getConfig()->getPropertyLabelOverrides(); - $logger = $this->model->getLogger(); - $logger->info("Custom Labels\n" . print_r($customLabels, TRUE)); - $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); $template = (in_array('skos:Concept', $results[0]->getType()) || in_array('skos:ConceptScheme', $results[0]->getType())) ? $this->twig->loadTemplate('concept-info.twig') : $this->twig->loadTemplate('group-contents.twig'); diff --git a/model/VocabularyConfig.php b/model/VocabularyConfig.php index 363d132d5..938dd8013 100644 --- a/model/VocabularyConfig.php +++ b/model/VocabularyConfig.php @@ -73,7 +73,6 @@ public function getPluginArray() : array /** * Sets array of parameterized plugins - * @param Easyrdf\Resource $pluginResource * @return void */ private function setParameterizedPlugins() : void @@ -133,7 +132,7 @@ private function setPropertyLabelOverrides() : void { $this->labelOverrides = array(); $overrides = $this->resource->allResources('skosmos:propertyLabelOverride'); - if ($overrides) { + if (!empty($overrides)) { foreach ($overrides as $override) { $this->setLabelOverride($override); } From c0ed36a454c0a8d1187f2aeb349cef14418b621b Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Thu, 11 Nov 2021 14:53:19 +0200 Subject: [PATCH 3/6] Use UI language, not content language for custom/overridden labels and tooltips --- view/concept-shared.twig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/concept-shared.twig b/view/concept-shared.twig index 95416ddda..1b38c0808 100644 --- a/view/concept-shared.twig +++ b/view/concept-shared.twig @@ -47,7 +47,7 @@ {% if subPrefLabelTranslation %} {{ subPrefLabelTranslation }} {% elseif custom_labels['skos:prefLabel'] %} - {{ custom_labels['skos:prefLabel']['label'][request.contentLang] }} + {{ custom_labels['skos:prefLabel']['label'][request.lang] }} {% else %} {{ 'skos:prefLabel'|trans }} {% endif %} @@ -101,10 +101,10 @@
{% if custom_labels[property.type] %}

{{ custom_labels[property.type]['label'][request.contentLang] }}

+ ">{{ custom_labels[property.type]['label'][request.lang] }} {% else %}

{{ property.label }}

{% endif %} From b46f5a8f547fb2c60b1f96b7d6dc2604a7e3e399 Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Thu, 11 Nov 2021 15:00:33 +0200 Subject: [PATCH 4/6] More specific tests for custom property labels and tooltips --- view/concept-shared.twig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/concept-shared.twig b/view/concept-shared.twig index 1b38c0808..0abd1bef1 100644 --- a/view/concept-shared.twig +++ b/view/concept-shared.twig @@ -46,7 +46,7 @@ {% set subPrefLabelTranslation = concept.preferredSubpropertyLabelTranslation(request.lang) %} {% if subPrefLabelTranslation %} {{ subPrefLabelTranslation }} - {% elseif custom_labels['skos:prefLabel'] %} + {% elseif custom_labels['skos:prefLabel']['label'][request.lang] %} {{ custom_labels['skos:prefLabel']['label'][request.lang] }} {% else %} {{ 'skos:prefLabel'|trans }} @@ -99,9 +99,9 @@ {% if property.getSubPropertyOf != 'skos:hiddenLabel' %}
- {% if custom_labels[property.type] %} + {% if custom_labels[property.type]['label'][request.lang] %}

{{ custom_labels[property.type]['label'][request.lang] }}

From 265d40ba605aaf9b27f591209f0df93b467ab064 Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Thu, 11 Nov 2021 15:07:09 +0200 Subject: [PATCH 5/6] Make it possible to override either custom label or tooltip, independently of each other --- view/concept-shared.twig | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/view/concept-shared.twig b/view/concept-shared.twig index 0abd1bef1..36d1d69ec 100644 --- a/view/concept-shared.twig +++ b/view/concept-shared.twig @@ -99,15 +99,16 @@ {% if property.getSubPropertyOf != 'skos:hiddenLabel' %}
- {% if custom_labels[property.type]['label'][request.lang] %} -

{{ custom_labels[property.type]['label'][request.lang] }}

- {% else %} -

{{ property.label }}

- {% endif %} +

+ {% if custom_labels[property.type]['label'][request.lang] %} + {{ custom_labels[property.type]['label'][request.lang] }} + {% else %} + {{ property.label }} + {% endif %} +

{% if request.vocab.config.hasMultiLingualProperty(property.type) %} From dc4342a263e1b2596da249d88e01f2037b89b534 Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Thu, 11 Nov 2021 15:13:17 +0200 Subject: [PATCH 6/6] Use rdfs:comment, not schema:description, to define property tooltip overrides --- model/VocabularyConfig.php | 2 +- tests/testconfig.ttl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/model/VocabularyConfig.php b/model/VocabularyConfig.php index 938dd8013..e33dc3249 100644 --- a/model/VocabularyConfig.php +++ b/model/VocabularyConfig.php @@ -157,7 +157,7 @@ private function setLabelOverride(Easyrdf\Resource $override) : void foreach ($labels as $label) { $newOverrides['label'][$label->getLang()] = $label->getValue(); } - $descriptions = $override->allLiterals('schema:description'); //optionally override property label tooltips + $descriptions = $override->allLiterals('rdfs:comment'); //optionally override property label tooltips foreach ($descriptions as $description) { $newOverrides['description'][$description->getLang()] = $description->getValue(); } diff --git a/tests/testconfig.ttl b/tests/testconfig.ttl index 0d83a3798..923f404ec 100644 --- a/tests/testconfig.ttl +++ b/tests/testconfig.ttl @@ -311,7 +311,7 @@ rdfs:label "Caption"@en, "Luokka"@fi ], [ skosmos:property skos:notation ; rdfs:label "UDC number"@en, "UDC-numero"@fi, "UDC-nummer"@sv ; - schema:description "Class Number"@en ] ; + rdfs:comment "Class Number"@en ] ; skosmos:sparqlGraph . :cbd a skosmos:Vocabulary, void:Dataset ;