1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Twig\Extension\AbstractExtension; |
4
|
|
|
use Twig\TwigFilter; |
5
|
|
|
|
6
|
|
|
class LinkUrlExtension extends AbstractExtension |
7
|
|
|
{ |
8
|
|
|
private $model; |
9
|
|
|
|
10
|
|
|
public function __construct($model) |
11
|
|
|
{ |
12
|
|
|
$this->model = $model; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function getFilters() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
new TwigFilter('link_url', [$this, 'linkUrlFilter']), |
19
|
|
|
]; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Creates Skosmos links from uris. |
24
|
|
|
* @param string $uri |
25
|
|
|
* @param Vocabulary $vocab |
26
|
|
|
* @param string $lang |
27
|
|
|
* @param string $type |
28
|
|
|
* @param string $clang content |
29
|
|
|
* @param string $term |
30
|
|
|
* @throws Exception if the vocabulary ID is not found in configuration |
31
|
|
|
* @return string containing the Skosmos link |
32
|
|
|
*/ |
33
|
|
|
public function linkUrlFilter($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null) |
34
|
|
|
{ |
35
|
|
|
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object |
36
|
|
|
if ($vocab === null) { |
37
|
|
|
return $uri; |
38
|
|
|
} elseif (is_string($vocab)) { |
|
|
|
|
39
|
|
|
$vocid = $vocab; |
40
|
|
|
$vocab = $this->model->getVocabulary($vocid); |
41
|
|
|
} else { |
42
|
|
|
$vocid = $vocab->getId(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$params = []; |
46
|
|
|
if (isset($clang) && $clang !== $lang) { |
47
|
|
|
$params['clang'] = $clang; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (isset($term)) { |
51
|
|
|
$params['q'] = $term; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$localname = $vocab->getLocalName($uri); |
55
|
|
|
if ($localname !== $uri && $localname === urlencode($localname)) { |
56
|
|
|
$paramstr = count($params) > 0 ? '?' . http_build_query($params) : ''; |
57
|
|
|
if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) { |
58
|
|
|
return "$vocid/$lang/$type/$localname" . $paramstr; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return "$vocid/$lang/$localname" . $paramstr; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$params['uri'] = $uri; |
65
|
|
|
return "$vocid/$lang/$type/?" . http_build_query($params); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|