forked from AKSW/imagehelper.ontowiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagehelperModule.php
102 lines (89 loc) · 3.15 KB
/
ImagehelperModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* OntoWiki image helper module
*
* Rewrites a given value of a resource to an image URL and displays this image in a module
*
* @category OntoWiki
* @package Extensions_Map
* @author Natanael Arndt <[email protected]>
* @copyright Copyright (c) 2014, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
class ImagehelperModule extends OntoWiki_Module
{
public function getTitle()
{
return $this->_owApp->translate->_('Image Helper');
}
public function shouldShow()
{
$query = new Erfurt_Sparql_SimpleQuery();
$query->setProloguePart('SELECT DISTINCT ?type')
->setWherePart(
'WHERE {<' . (string)$this->_owApp->selectedResource . '> a ?type.}'
);
if ($results = $this->_owApp->selectedModel->sparqlQuery($query)) {
foreach ($results as $result) {
if (in_array('http://purl.org/ontology/bibo/Periodical', $result)) {
return true;
}
}
}
return false;
}
/**
* Get the map content
*/
public function getContents()
{
if (!isset($this->_owApp->selectedResource)) {
$this->_owApp->logger->debug(
'ImagehelperModule/getContents: no selectedResource available.'
);
return;
}
$resourceUri = $this->_owApp->selectedResource;
$properties = array();
if (isset($this->_privateConfig->properties)) {
$properties = $this->_privateConfig->properties;
}
$this->view->imageUrl = array();
if (count($properties) > 0) {
$sparql = 'SELECT ?property ?value WHERE {';
$sparql .= ' <' . $resourceUri . '> ?property ?value .';
$sparql .= ' FILTER(';
foreach ($properties as $setup) {
$sparql .= ' sameTerm(?property, <' . $setup->property . '>) ||';
}
$sparql = substr($sparql, 0, -2);
$sparql .= ' )';
$sparql .= '}';
$results = $this->_owApp->selectedModel->sparqlQuery($sparql);
$properties = $properties->toArray();
$properties = $this->_array_index($properties, 'property');
foreach ($results as $row) {
$value = $row['value'];
$property = $row['property'];
$ruleSpec = $properties[$property];
$this->view->imageUrl[] = preg_replace($ruleSpec['pattern'], $ruleSpec['replacement'], $value);
}
}
return $this->render('imagehelper');
}
/**
* This method takes a 2D array and puts the value of the given key of each sub array as key in the super array
*
* @param $array the array to process
* @param $key a string giving the key of the sub array to use as key for the super array
* @return array
*/
private function _array_index($array, $key)
{
$out = array();
foreach ($array as $subArray) {
$out[$subArray[$key]] = $subArray;
}
return $out;
}
}