Skip to content

Commit

Permalink
linkUrlFilter moved from Controller to its own Twig extension class
Browse files Browse the repository at this point in the history
Moving the code that provides the functionality of the link_url filter used in Twig templates into its own class (LinkUrlExtension.php) makes the code cleaner and enables the extraction of translation messages from Twig templates.
  • Loading branch information
osma authored Aug 21, 2024
1 parent 18feeae commit e6d7dbc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 53 deletions.
51 changes: 0 additions & 51 deletions src/controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,57 +97,6 @@ public function getBaseHref()
return ($this->model->getConfig()->getBaseHref() !== null) ? $this->model->getConfig()->getBaseHref() : $this->guessBaseHref();
}

/**
* Creates Skosmos links from uris.
* @param string $uri
* @param Vocabulary $vocab
* @param string $lang
* @param string $type
* @param string $clang content
* @param string $term
* @throws Exception if the vocabulary ID is not found in configuration
* @return string containing the Skosmos link
*/
public function linkUrlFilter($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null)
{
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object
if ($vocab === null) {
// target vocabulary is unknown, best bet is to link to the plain URI
return $uri;
} elseif (is_string($vocab)) {
$vocid = $vocab;
$vocab = $this->model->getVocabulary($vocid);
} else {
$vocid = $vocab->getId();
}

$params = array();
if (isset($clang) && $clang !== $lang) {
$params['clang'] = $clang;
}

if (isset($term)) {
$params['q'] = $term;
}

// case 1: URI within vocabulary namespace: use only local name
$localname = $vocab->getLocalName($uri);
if ($localname !== $uri && $localname === urlencode($localname)) {
// check that the prefix stripping worked, and there are no problematic chars in localname
$paramstr = count($params) > 0 ? '?' . http_build_query($params) : '';
if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) {
return "$vocid/$lang/$type/$localname" . $paramstr;
}

return "$vocid/$lang/$localname" . $paramstr;
}

// case 2: URI outside vocabulary namespace, or has problematic chars
// pass the full URI as parameter instead
$params['uri'] = $uri;
return "$vocid/$lang/$type/?" . http_build_query($params);
}

/**
* Echos an error message when the request can't be fulfilled.
* @param string $code
Expand Down
67 changes: 67 additions & 0 deletions src/controller/LinkUrlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class LinkUrlExtension extends AbstractExtension
{
private $model;

public function __construct($model)
{
$this->model = $model;
}

public function getFilters()
{
return [
new TwigFilter('link_url', [$this, 'linkUrlFilter']),
];
}

/**
* Creates Skosmos links from uris.
* @param string $uri
* @param Vocabulary $vocab
* @param string $lang
* @param string $type
* @param string $clang content
* @param string $term
* @throws Exception if the vocabulary ID is not found in configuration
* @return string containing the Skosmos link
*/
public function linkUrlFilter($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null)
{
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object
if ($vocab === null) {
return $uri;
} elseif (is_string($vocab)) {
$vocid = $vocab;
$vocab = $this->model->getVocabulary($vocid);
} else {
$vocid = $vocab->getId();
}

$params = [];
if (isset($clang) && $clang !== $lang) {
$params['clang'] = $clang;
}

if (isset($term)) {
$params['q'] = $term;
}

$localname = $vocab->getLocalName($uri);
if ($localname !== $uri && $localname === urlencode($localname)) {
$paramstr = count($params) > 0 ? '?' . http_build_query($params) : '';
if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) {
return "$vocid/$lang/$type/$localname" . $paramstr;
}

return "$vocid/$lang/$localname" . $paramstr;
}

$params['uri'] = $uri;
return "$vocid/$lang/$type/?" . http_build_query($params);
}
}
3 changes: 2 additions & 1 deletion src/controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,10 @@ public function mappings(Request $request)
}

$mappings = [];
$linkUrlExtension = new LinkUrlExtension($this->model);
foreach ($concept->getMappingProperties() as $mappingProperty) {
foreach ($mappingProperty->getValues() as $mappingPropertyValue) {
$hrefLink = $this->linkUrlFilter($mappingPropertyValue->getUri(), $mappingPropertyValue->getExVocab(), $request->getLang(), 'page', $request->getContentLang());
$hrefLink = $linkUrlExtension->linkUrlFilter($mappingPropertyValue->getUri(), $mappingPropertyValue->getExVocab(), $request->getLang(), 'page', $request->getContentLang());
$mappings[] = $mappingPropertyValue->asJskos($queryExVocabs, $request->getLang(), $hrefLink);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct($model)
$this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related'));

// register a Twig filter for generating URLs for vocabulary resources (concepts and groups)
$this->twig->addFilter(new \Twig\TwigFilter('link_url', array($this, 'linkUrlFilter')));
$this->twig->addExtension(new LinkUrlExtension($model));

// register a Twig filter for generating strings from language codes with CLDR
$langFilter = new \Twig\TwigFilter('lang_name', function ($langcode, $lang) {
Expand Down

0 comments on commit e6d7dbc

Please sign in to comment.