-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateModule.php
203 lines (168 loc) · 6.09 KB
/
CreateModule.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* This file is part of the {@link http://amsl.technology amsl} project.
*
* @author Norman Radtke
* @copyright Copyright (c) 2015, {@link http://ub.uni-leipzig.de Leipzig University Library}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/**
* Create resource module for the linkandcreate extension
*
* @category OntoWiki
* @package OntoWiki_extensions_linkandcreate
*/
class CreateModule extends OntoWiki_Module
{
/*
* The module has two options:
* 1. Enable the module according to the type of the selected resource or
* 2. Enable the module without checking types (default)
*/
private $_types = array();
private $_hideProperties = array();
private $_useWithoutTypeCheck = true;
/**
* Constructor
*/
public function init()
{
$config = $this->_privateConfig;
if (isset($config->useModuleWithoutTypeCheck)) {
$this->_useWithoutTypeCheck = (boolean)$config->useModuleWithoutTypeCheck;
}
if ($this->_useWithoutTypeCheck === false && isset($config->enableForTypes)) {
$this->_types = $config->enableForTypes->toArray();
}
if (isset($config->hideProperties)) {
$this->_hideProperties = $config->hideProperties->toArray();
}
}
public function getTitle()
{
return $this->_owApp->translate->_('Link and Create a resource from here');
}
public function shouldShow()
{
// show only if type matches
return $this->_checkClass();
}
public function getContents()
{
require_once('LinkandcreateController.php');
$selectedResource = $this->_owApp->selectedResource;
$data = array();
$data['resourceUri'] = $selectedResource->getUri();
$event = new Erfurt_Event('onResourceShowRanges');
$event->resource = $selectedResource;
$event->hideProperties = $this->_hideProperties;
$data['linkData'] = $this->showRanges($event);
require_once('LinkandcreateController.php');
return $this->render('linkandcreate/linkandcreate', $data);
}
public function showRanges($event)
{
$owApp = OntoWiki::getInstance();
$this->_model = $owApp->selectedModel;
$resourceObject = $event->resource;
$rModel = $resourceObject->getMemoryModel();
$titleHelper = new OntoWiki_Model_TitleHelper();
$hideProperties = $event->hideProperties;
$temp = array();
foreach ($hideProperties as $name) {
$temp[$name['propertyUri']] = '';
}
$hideProperties = $temp;
$values = $rModel->getValues($resourceObject->getUri(), EF_RDF_TYPE);
$data = array();
if (count($values) === 1) {
$class = $values[0]['value'];
} else {
return $data;
}
$query = 'SELECT DISTINCT ?p ?range ?oneOf WHERE ' . PHP_EOL;
$query.= ' ' . PHP_EOL;
$query.= '{ ' . PHP_EOL;
$query.= ' ?s a <' . $class . '> . ' . PHP_EOL;
$query.= ' ?s ?p ?o . ' . PHP_EOL;
$query.= ' ?p <' . EF_RDFS_RANGE . '> ?range . ' . PHP_EOL;
$query.= ' OPTIONAL ' . PHP_EOL;
$query.= ' { ' . PHP_EOL;
$query.= ' ?range <' . EF_OWL_ONEOF . '> ?oneOf . ' . PHP_EOL;
$query.= ' } ' . PHP_EOL;
$query.= '} ' . PHP_EOL;
$results = $this->_model->sparqlQuery($query);
if (count($results) === 0) {
return $data;
}
foreach ($results as $result) {
if (strpos($result['range'], 'XMLSchema#') === false
&& (!isset($result['oneOf']) || $result['oneOf'] === '' || is_null($result['oneOf']))
&& $result['p'] !== EF_RDF_TYPE
&& !isset($hideProperties[$result['p']])
) {
$data[] = array(
'property' => $result['p'],
'propertyLabel' => $titleHelper->getTitle($result['p']),
'class' => $result['range'],
'classLabel' => $titleHelper->getTitle($result['range'])
);
}
}
$delete = array();
foreach($data as $key => $range) {
$query = 'SELECT ?testCollection WHERE ';
$query .= '{ ' . PHP_EOL;
$query .= ' <' . $range['class'] . '> ?p ?testCollection . ' . PHP_EOL;
$query .= ' ?testCollection <' . EF_RDF_FIRST . '> ?test . ' . PHP_EOL;
$query .= '} ' . PHP_EOL;
$ranges = $this->_model->sparqlQuery($query);
if (count($ranges) > 0) {
$delete[] = $key;
$this->_getCollection($ranges[0]['testCollection']);
foreach ($this->_ranges as $foundRange) {
$data[] = array(
'property' => $range['property'],
'propertyLabel' => $titleHelper->getTitle($range['property']),
'class' => $foundRange,
'classLabel' => $titleHelper->getTitle($foundRange)
);
}
}
}
foreach ($delete as $key) {
unset($data[$key]);
}
return $data;
}
/*
* checks the resource types agains the configured patterns
*/
private function _checkClass()
{
if ($this->_useWithoutTypeCheck === true) {
return true;
}
$resource = $this->_owApp->selectedResource;
$rModel = $resource->getMemoryModel();
// search with each expression using the preg matchtype
foreach ($this->_types as $type) {
if (isset($type['classUri'])) {
$classUri = $type['classUri'];
} else {
continue;
}
if (
$rModel->hasSPvalue(
(string) $resource,
EF_RDF_TYPE,
$classUri
)
) {
return true;
}
}
// type does not match to one of the expressions
return false;
}
}