|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2012-2013 Aalto University and University of Helsinki |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* see LICENSE.txt for more information |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Importing the dependencies. |
|
10
|
|
|
*/ |
|
11
|
|
|
require_once 'controller/Controller.php'; |
|
12
|
|
|
use \Punic\Language; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* WebController is an extension of the Controller that handles all |
|
16
|
|
|
* the requests originating from the view of the website. |
|
17
|
|
|
*/ |
|
18
|
|
|
class WebController extends Controller |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Provides access to the templating engine. |
|
22
|
|
|
* @property object $twig the twig templating engine. |
|
23
|
|
|
*/ |
|
24
|
|
|
public $twig; |
|
25
|
|
|
|
|
26
|
|
|
public $base_href; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor for the WebController. |
|
30
|
|
|
* @param Model $model |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($model) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($model); |
|
35
|
|
|
|
|
36
|
|
|
// initialize Twig templates |
|
37
|
|
|
$tmp_dir = $model->getConfig()->getTemplateCache(); |
|
38
|
|
|
|
|
39
|
|
|
// check if the cache pointed by config.inc exists, if not we create it. |
|
40
|
|
|
if (!file_exists($tmp_dir)) { |
|
41
|
|
|
mkdir($tmp_dir); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// specify where to look for templates and cache |
|
45
|
|
|
$loader = new Twig_Loader_Filesystem('view'); |
|
46
|
|
|
// initialize Twig environment |
|
47
|
|
|
$this->twig = new Twig_Environment($loader, array('cache' => $tmp_dir, |
|
48
|
|
|
'auto_reload' => true, 'debug' => true)); |
|
49
|
|
|
$this->twig->addExtension(new Twig_Extensions_Extension_I18n()); |
|
50
|
|
|
//ENABLES DUMP() method for easy and fun debugging! |
|
51
|
|
|
$this->twig->addExtension(new Twig_Extension_Debug()); |
|
52
|
|
|
// used for setting the base href for the relative urls |
|
53
|
|
|
$this->base_href = ($this->model->getConfig()->getBaseHref() !== null) ? $this->model->getConfig()->getBaseHref() : $this->guessBaseHref(); |
|
54
|
|
|
$this->twig->addGlobal("BaseHref", $this->base_href); |
|
55
|
|
|
// setting the service name string from the config.inc |
|
56
|
|
|
$this->twig->addGlobal("ServiceName", $this->model->getConfig()->getServiceName()); |
|
57
|
|
|
// setting the service logo location from the config.inc |
|
58
|
|
|
if ($this->model->getConfig()->getServiceLogo() !== null) { |
|
59
|
|
|
$this->twig->addGlobal("ServiceLogo", $this->model->getConfig()->getServiceLogo()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// setting the service custom css file from the config.inc |
|
63
|
|
|
if ($this->model->getConfig()->getCustomCss() !== null) { |
|
64
|
|
|
$this->twig->addGlobal("ServiceCustomCss", $this->model->getConfig()->getCustomCss()); |
|
65
|
|
|
} |
|
66
|
|
|
// used for displaying the ui language selection as a dropdown |
|
67
|
|
|
if ($this->model->getConfig()->getUiLanguageDropdown() !== null) { |
|
68
|
|
|
$this->twig->addGlobal("LanguageDropdown", $this->model->getConfig()->getUiLanguageDropdown()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// setting the list of properties to be displayed in the search results |
|
72
|
|
|
$this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related')); |
|
73
|
|
|
|
|
74
|
|
|
// register a Twig filter for generating URLs for vocabulary resources (concepts and groups) |
|
75
|
|
|
$controller = $this; // for use by anonymous function below |
|
76
|
|
|
$urlFilter = new Twig_SimpleFilter('link_url', function ($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null) use ($controller) { |
|
77
|
|
|
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object |
|
78
|
|
|
if ($vocab === null) { |
|
79
|
|
|
// target vocabulary is unknown, best bet is to link to the plain URI |
|
80
|
|
|
return $uri; |
|
81
|
|
|
} elseif (is_string($vocab)) { |
|
82
|
|
|
$vocid = $vocab; |
|
83
|
|
|
$vocab = $controller->model->getVocabulary($vocid); |
|
84
|
|
|
} else { |
|
85
|
|
|
$vocid = $vocab->getId(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$params = array(); |
|
89
|
|
|
if (isset($clang) && $clang !== $lang) { |
|
90
|
|
|
$params['clang'] = $clang; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (isset($term)) { |
|
94
|
|
|
$params['q'] = $term; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// case 1: URI within vocabulary namespace: use only local name |
|
98
|
|
|
$localname = $vocab->getLocalName($uri); |
|
99
|
|
|
if ($localname !== $uri && $localname === urlencode($localname)) { |
|
100
|
|
|
// check that the prefix stripping worked, and there are no problematic chars in localname |
|
101
|
|
|
$paramstr = sizeof($params) > 0 ? '?' . http_build_query($params) : ''; |
|
102
|
|
|
if ($type && $type !== '' && $type !== 'vocab') { |
|
103
|
|
|
return "$vocid/$lang/$type/$localname" . $paramstr; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return "$vocid/$lang/$localname" . $paramstr; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// case 2: URI outside vocabulary namespace, or has problematic chars |
|
110
|
|
|
// pass the full URI as parameter instead |
|
111
|
|
|
$params['uri'] = $uri; |
|
112
|
|
|
return "$vocid/$lang/$type/?" . http_build_query($params); |
|
113
|
|
|
}); |
|
114
|
|
|
$this->twig->addFilter($urlFilter); |
|
115
|
|
|
|
|
116
|
|
|
// register a Twig filter for generating strings from language codes with CLDR |
|
117
|
|
|
$langFilter = new Twig_SimpleFilter('lang_name', function ($langcode, $lang) { |
|
118
|
|
|
return Language::getName($langcode, $lang); |
|
119
|
|
|
}); |
|
120
|
|
|
$this->twig->addFilter($langFilter); |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
private function guessBaseHref() |
|
125
|
|
|
{ |
|
126
|
|
|
$script_name = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_STRING); |
|
127
|
|
|
$script_filename = filter_input(INPUT_SERVER, 'SCRIPT_FILENAME', FILTER_SANITIZE_STRING); |
|
128
|
|
|
$script_filename = realpath($script_filename); // resolve any symlinks (see #274) |
|
129
|
|
|
$script_filename = str_replace("\\", "/", $script_filename); // fixing windows paths with \ (see #309) |
|
130
|
|
|
$base_dir = __DIR__; // Absolute path to your installation, ex: /var/www/mywebsite |
|
131
|
|
|
$base_dir = str_replace("\\", "/", $base_dir); // fixing windows paths with \ (see #309) |
|
132
|
|
|
$doc_root = preg_replace("!{$script_name}$!", '', $script_filename); |
|
133
|
|
|
$base_url = preg_replace("!^{$doc_root}!", '', $base_dir); |
|
134
|
|
|
$base_url = str_replace('/controller', '/', $base_url); |
|
135
|
|
|
$protocol = filter_input(INPUT_SERVER, 'HTTPS', FILTER_SANITIZE_STRING) === null ? 'http' : 'https'; |
|
136
|
|
|
$port = filter_input(INPUT_SERVER, 'SERVER_PORT', FILTER_SANITIZE_STRING); |
|
137
|
|
|
$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port"; |
|
138
|
|
|
$domain = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING); |
|
139
|
|
|
$full_url = "$protocol://{$domain}{$disp_port}{$base_url}"; |
|
140
|
|
|
return $full_url; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Guess the language of the user. Return a language string that is one |
|
145
|
|
|
* of the supported languages defined in the $LANGUAGES setting, e.g. "fi". |
|
146
|
|
|
* @param string $vocab_id identifier for the vocabulary eg. 'yso'. |
|
147
|
|
|
* @return string returns the language choice as a numeric string value |
|
148
|
|
|
*/ |
|
149
|
|
|
public function guessLanguage($vocab_id = null) |
|
150
|
|
|
{ |
|
151
|
|
|
// 1. select language based on SKOSMOS_LANGUAGE cookie |
|
152
|
|
|
if (filter_input(INPUT_COOKIE, 'SKOSMOS_LANGUAGE', FILTER_SANITIZE_STRING)) { |
|
153
|
|
|
return filter_input(INPUT_COOKIE, 'SKOSMOS_LANGUAGE', FILTER_SANITIZE_STRING); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// 2. if vocabulary given, select based on the default language of the vocabulary |
|
157
|
|
|
if ($vocab_id !== null && $vocab_id !== '') { |
|
158
|
|
|
try { |
|
159
|
|
|
$vocab = $this->model->getVocabulary($vocab_id); |
|
160
|
|
|
return $vocab->getConfig()->getDefaultLanguage(); |
|
161
|
|
|
} catch (Exception $e) { |
|
162
|
|
|
// vocabulary id not found, move on to the next selection method |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// 3. select language based on Accept-Language header |
|
167
|
|
|
header('Vary: Accept-Language'); // inform caches that a decision was made based on Accept header |
|
168
|
|
|
$this->negotiator = new \Negotiation\LanguageNegotiator(); |
|
169
|
|
|
$langcodes = array_keys($this->languages); |
|
170
|
|
|
$acceptLanguage = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING) ? filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING) : ''; |
|
171
|
|
|
$bestLang = $this->negotiator->getBest($acceptLanguage, $langcodes); |
|
172
|
|
|
if (isset($bestLang) && in_array($bestLang, $langcodes)) { |
|
173
|
|
|
return $bestLang->getValue(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// show default site or prompt for language |
|
177
|
|
|
return $langcodes[0]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Loads and renders the view containing all the vocabularies. |
|
182
|
|
|
* @param Request $request |
|
183
|
|
|
*/ |
|
184
|
|
|
public function invokeVocabularies($request) |
|
185
|
|
|
{ |
|
186
|
|
|
// set language parameters for gettext |
|
187
|
|
|
$this->setLanguageProperties($request->getLang()); |
|
188
|
|
|
// load template |
|
189
|
|
|
$template = $this->twig->loadTemplate('light.twig'); |
|
190
|
|
|
// set template variables |
|
191
|
|
|
$categoryLabel = $this->model->getClassificationLabel($request->getLang()); |
|
192
|
|
|
$vocabList = $this->model->getVocabularyList(); |
|
193
|
|
|
$sortedVocabs = $this->model->getVocabularyList(false, true); |
|
194
|
|
|
$langList = $this->model->getLanguages($request->getLang()); |
|
195
|
|
|
|
|
196
|
|
|
// render template |
|
197
|
|
|
echo $template->render( |
|
198
|
|
|
array( |
|
199
|
|
|
'vocab_list' => $vocabList, |
|
200
|
|
|
'sorted_vocabs' => $sortedVocabs, |
|
201
|
|
|
'category_label' => $categoryLabel, |
|
202
|
|
|
'languages' => $this->languages, |
|
203
|
|
|
'lang_list' => $langList, |
|
204
|
|
|
'request' => $request, |
|
205
|
|
|
)); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Invokes the concept page of a single concept in a specific vocabulary. |
|
210
|
|
|
*/ |
|
211
|
|
|
public function invokeVocabularyConcept($request) |
|
212
|
|
|
{ |
|
213
|
|
|
$lang = $request->getLang(); |
|
214
|
|
|
$this->setLanguageProperties($lang); |
|
215
|
|
|
$template = $this->twig->loadTemplate('concept-info.twig'); |
|
216
|
|
|
$vocab = $request->getVocab(); |
|
217
|
|
|
|
|
218
|
|
|
$langcodes = $vocab->getConfig()->getShowLangCodes(); |
|
219
|
|
|
$uri = $vocab->getConceptURI($request->getUri()); // make sure it's a full URI |
|
220
|
|
|
|
|
221
|
|
|
$results = $vocab->getConceptInfo($uri, $request->getContentLang()); |
|
222
|
|
|
$crumbs = $vocab->getBreadCrumbs($request->getContentLang(), $uri); |
|
223
|
|
|
echo $template->render(array( |
|
224
|
|
|
'search_results' => $results, |
|
225
|
|
|
'vocab' => $vocab, |
|
226
|
|
|
'languages' => $this->languages, |
|
227
|
|
|
'explicit_langcodes' => $langcodes, |
|
228
|
|
|
'bread_crumbs' => $crumbs['breadcrumbs'], |
|
229
|
|
|
'combined' => $crumbs['combined'], |
|
230
|
|
|
'request' => $request) |
|
231
|
|
|
); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Invokes the feedback page with information of the users current vocabulary. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function invokeFeedbackForm($request) |
|
238
|
|
|
{ |
|
239
|
|
|
$template = $this->twig->loadTemplate('feedback.twig'); |
|
240
|
|
|
$this->setLanguageProperties($request->getLang()); |
|
241
|
|
|
$vocabList = $this->model->getVocabularyList(false); |
|
242
|
|
|
$vocab = $request->getVocab(); |
|
243
|
|
|
|
|
244
|
|
|
$feedback_sent = false; |
|
245
|
|
|
$feedback_msg = null; |
|
246
|
|
|
if (filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING)) { |
|
247
|
|
|
$feedback_sent = true; |
|
248
|
|
|
$feedback_msg = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING); |
|
249
|
|
|
} |
|
250
|
|
|
$feedback_name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); |
|
251
|
|
|
$feedback_email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING); |
|
252
|
|
|
$feedback_vocab = filter_input(INPUT_POST, 'vocab', FILTER_SANITIZE_STRING); |
|
253
|
|
|
$feedback_vocab_email = ($vocab !== null) ? $vocab->getConfig()->getFeedbackRecipient() : null; |
|
254
|
|
|
|
|
255
|
|
|
// if the hidden field has been set a value we have found a spam bot |
|
256
|
|
|
// and we do not actually send the message. |
|
257
|
|
|
if ($feedback_sent && filter_input(INPUT_POST, 'trap', FILTER_SANITIZE_STRING) === '') { |
|
258
|
|
|
$this->sendFeedback($feedback_msg, $feedback_name, $feedback_email, $feedback_vocab, $feedback_vocab_email); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
echo $template->render( |
|
262
|
|
|
array( |
|
263
|
|
|
'languages' => $this->languages, |
|
264
|
|
|
'vocab' => $vocab, |
|
265
|
|
|
'vocabList' => $vocabList, |
|
266
|
|
|
'feedback_sent' => $feedback_sent, |
|
267
|
|
|
'request' => $request, |
|
268
|
|
|
)); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Sends the user entered message through the php's mailer. |
|
273
|
|
|
* @param string $message only required parameter is the actual message. |
|
274
|
|
|
* @param string $fromName senders own name. |
|
275
|
|
|
* @param string $fromEmail senders email adress. |
|
276
|
|
|
* @param string $fromVocab which vocabulary is the feedback related to. |
|
277
|
|
|
*/ |
|
278
|
|
|
public function sendFeedback($message, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null) |
|
279
|
|
|
{ |
|
280
|
|
|
$toAddress = ($toMail) ? $toMail : $this->model->getConfig()->getFeedbackAddress(); |
|
281
|
|
|
if ($fromVocab !== null) { |
|
282
|
|
|
$message = 'Feedback from vocab: ' . strtoupper($fromVocab) . "<br />" . $message; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$subject = SERVICE_NAME . " feedback"; |
|
286
|
|
|
$headers = "MIME-Version: 1.0″ . '\r\n"; |
|
287
|
|
|
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n"; |
|
288
|
|
|
if ($toMail) { |
|
289
|
|
|
$headers .= "Cc: " . $this->model->getConfig()->getFeedbackAddress() . "\r\n"; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
$headers .= "From: $fromName <$fromEmail>" . "\r\n" . 'X-Mailer: PHP/' . phpversion(); |
|
293
|
|
|
$envelopeSender = FEEDBACK_ENVELOPE_SENDER; |
|
294
|
|
|
$params = empty($envelopeSender) ? '' : "-f $envelopeSender"; |
|
295
|
|
|
|
|
296
|
|
|
// adding some information about the user for debugging purposes. |
|
297
|
|
|
$agent = (filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING)) ? filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING) : ''; |
|
298
|
|
|
$referer = (filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_STRING)) ? filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_STRING) : ''; |
|
299
|
|
|
$ipAddress = (filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING)) ? filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING) : ''; |
|
300
|
|
|
$timestamp = date(DATE_RFC2822); |
|
301
|
|
|
|
|
302
|
|
|
$message = $message . "<br /><br /> Debugging information:" |
|
303
|
|
|
. "<br />Timestamp: " . $timestamp |
|
304
|
|
|
. "<br />User agent: " . $agent |
|
305
|
|
|
. "<br />IP address: " . $ipAddress |
|
306
|
|
|
. "<br />Referer: " . $referer; |
|
307
|
|
|
|
|
308
|
|
|
try { |
|
309
|
|
|
mail($toAddress, $subject, $message, $headers, $params); |
|
310
|
|
|
} catch (Exception $e) { |
|
311
|
|
|
header("HTTP/1.0 404 Not Found"); |
|
312
|
|
|
$template = $this->twig->loadTemplate('error-page.twig'); |
|
313
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
|
314
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
echo $template->render( |
|
318
|
|
|
array( |
|
319
|
|
|
'languages' => $this->languages, |
|
320
|
|
|
)); |
|
321
|
|
|
|
|
322
|
|
|
return; |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Invokes the about page for the Skosmos service. |
|
328
|
|
|
*/ |
|
329
|
|
View Code Duplication |
public function invokeAboutPage($request) |
|
|
|
|
|
|
330
|
|
|
{ |
|
331
|
|
|
$template = $this->twig->loadTemplate('about.twig'); |
|
332
|
|
|
$this->setLanguageProperties($request->getLang()); |
|
333
|
|
|
$url = $request->getServerConstant('HTTP_HOST'); |
|
334
|
|
|
$version = $this->model->getVersion(); |
|
335
|
|
|
|
|
336
|
|
|
echo $template->render( |
|
337
|
|
|
array( |
|
338
|
|
|
'languages' => $this->languages, |
|
339
|
|
|
'version' => $version, |
|
340
|
|
|
'server_instance' => $url, |
|
341
|
|
|
'request' => $request, |
|
342
|
|
|
)); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Invokes the search for concepts in all the availible ontologies. |
|
347
|
|
|
*/ |
|
348
|
|
|
public function invokeGlobalSearch($request) |
|
349
|
|
|
{ |
|
350
|
|
|
$lang = $request->getLang(); |
|
351
|
|
|
$template = $this->twig->loadTemplate('vocab-search-listing.twig'); |
|
352
|
|
|
$this->setLanguageProperties($lang); |
|
353
|
|
|
|
|
354
|
|
|
$parameters = new ConceptSearchParameters($request, $this->model->getConfig()); |
|
355
|
|
|
|
|
356
|
|
|
$vocabs = $request->getQueryParam('vocabs'); # optional |
|
357
|
|
|
// convert to vocids array to support multi-vocabulary search |
|
358
|
|
|
$vocids = ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null; |
|
359
|
|
|
$vocabObjects = array(); |
|
360
|
|
|
foreach($vocids as $vocid) { |
|
|
|
|
|
|
361
|
|
|
$vocabObjects[] = $this->model->getVocabulary($vocid); |
|
362
|
|
|
} |
|
363
|
|
|
$parameters->setVocabularies($vocabObjects); |
|
364
|
|
|
|
|
365
|
|
|
try { |
|
366
|
|
|
$count_and_results = $this->model->searchConceptsAndInfo($parameters); |
|
367
|
|
|
} catch (Exception $e) { |
|
368
|
|
|
header("HTTP/1.0 404 Not Found"); |
|
369
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
|
370
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
|
371
|
|
|
} |
|
372
|
|
|
$this->invokeGenericErrorPage($request, $e->getMessage()); |
|
373
|
|
|
return; |
|
374
|
|
|
} |
|
375
|
|
|
$counts = $count_and_results['count']; |
|
376
|
|
|
$search_results = $count_and_results['results']; |
|
377
|
|
|
$vocabList = $this->model->getVocabularyList(); |
|
378
|
|
|
$sortedVocabs = $this->model->getVocabularyList(false, true); |
|
379
|
|
|
$langList = $this->model->getLanguages($lang); |
|
380
|
|
|
|
|
381
|
|
|
echo $template->render( |
|
382
|
|
|
array( |
|
383
|
|
|
'search_count' => $counts, |
|
384
|
|
|
'languages' => $this->languages, |
|
385
|
|
|
'search_results' => $search_results, |
|
386
|
|
|
'rest' => $parameters->getOffset()>0, |
|
387
|
|
|
'global_search' => true, |
|
388
|
|
|
'term' => $request->getQueryParam('q'), |
|
389
|
|
|
'lang_list' => $langList, |
|
390
|
|
|
'vocabs' => $vocabs, |
|
391
|
|
|
'vocab_list' => $vocabList, |
|
392
|
|
|
'sorted_vocabs' => $sortedVocabs, |
|
393
|
|
|
'request' => $request, |
|
394
|
|
|
)); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Invokes the search for a single vocabs concepts. |
|
399
|
|
|
*/ |
|
400
|
|
|
public function invokeVocabularySearch($request) |
|
401
|
|
|
{ |
|
402
|
|
|
$template = $this->twig->loadTemplate('vocab-search-listing.twig'); |
|
403
|
|
|
$this->setLanguageProperties($request->getLang()); |
|
404
|
|
|
$vocab = $request->getVocab(); |
|
405
|
|
|
try { |
|
406
|
|
|
$vocab_types = $this->model->getTypes($request->getVocabid(), $request->getLang()); |
|
407
|
|
|
} catch (Exception $e) { |
|
408
|
|
|
header("HTTP/1.0 404 Not Found"); |
|
409
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
|
410
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
echo $template->render( |
|
414
|
|
|
array( |
|
415
|
|
|
'languages' => $this->languages, |
|
416
|
|
|
)); |
|
417
|
|
|
|
|
418
|
|
|
return; |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
$langcodes = $vocab->getConfig()->getShowLangCodes(); |
|
422
|
|
|
$parameters = new ConceptSearchParameters($request, $this->model->getConfig()); |
|
423
|
|
|
|
|
424
|
|
|
try { |
|
425
|
|
|
$count_and_results = $this->model->searchConceptsAndInfo($parameters); |
|
426
|
|
|
$counts = $count_and_results['count']; |
|
427
|
|
|
$search_results = $count_and_results['results']; |
|
428
|
|
|
} catch (Exception $e) { |
|
429
|
|
|
header("HTTP/1.0 404 Not Found"); |
|
430
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
|
431
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
echo $template->render( |
|
435
|
|
|
array( |
|
436
|
|
|
'languages' => $this->languages, |
|
437
|
|
|
'vocab' => $vocab, |
|
438
|
|
|
'term' => $request->getQueryParam('q'), |
|
439
|
|
|
'rest' => $rest, |
|
|
|
|
|
|
440
|
|
|
)); |
|
441
|
|
|
return; |
|
442
|
|
|
} |
|
443
|
|
|
echo $template->render( |
|
444
|
|
|
array( |
|
445
|
|
|
'languages' => $this->languages, |
|
446
|
|
|
'vocab' => $vocab, |
|
447
|
|
|
'search_results' => $search_results, |
|
448
|
|
|
'search_count' => $counts, |
|
449
|
|
|
'rest' => $parameters->getOffset()>0, |
|
450
|
|
|
'limit_parent' => $parameters->getParentLimit(), |
|
451
|
|
|
'limit_type' => $request->getQueryParam('type'), |
|
452
|
|
|
'limit_group' => $parameters->getGroupLimit(), |
|
453
|
|
|
'group_index' => $vocab->listConceptGroups($request->getContentLang()), |
|
454
|
|
|
'parameters' => $parameters, |
|
455
|
|
|
'term' => $request->getQueryParam('q'), |
|
456
|
|
|
'types' => $vocab_types, |
|
457
|
|
|
'explicit_langcodes' => $langcodes, |
|
458
|
|
|
'request' => $request, |
|
459
|
|
|
)); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* Invokes the alphabetical listing for a specific vocabulary. |
|
464
|
|
|
*/ |
|
465
|
|
|
public function invokeAlphabeticalIndex($request) |
|
466
|
|
|
{ |
|
467
|
|
|
$lang = $request->getLang(); |
|
468
|
|
|
$this->setLanguageProperties($lang); |
|
469
|
|
|
$template = $this->twig->loadTemplate('alphabetical-index.twig'); |
|
470
|
|
|
$vocab = $request->getVocab(); |
|
471
|
|
|
|
|
472
|
|
|
$offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0; |
|
473
|
|
|
if ($request->getQueryParam('limit')) { |
|
474
|
|
|
$count = $request->getQueryParam('limit'); |
|
475
|
|
|
} else { |
|
476
|
|
|
$count = ($offset > 0) ? null : 250; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
$content_lang = $request->getContentLang(); |
|
480
|
|
|
|
|
481
|
|
|
$all_at_once = $vocab->getConfig()->getAlphabeticalFull(); |
|
482
|
|
|
if (!$all_at_once) { |
|
483
|
|
|
$search_results = $vocab->searchConceptsAlphabetical($request->getLetter(), $count, $offset, $content_lang); |
|
484
|
|
|
$letters = $vocab->getAlphabet($content_lang); |
|
485
|
|
|
} else { |
|
486
|
|
|
$search_results = $vocab->searchConceptsAlphabetical('*', null, null, $content_lang); |
|
487
|
|
|
$letters = null; |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
$request->setContentLang($content_lang); |
|
491
|
|
|
|
|
492
|
|
|
echo $template->render( |
|
493
|
|
|
array( |
|
494
|
|
|
'languages' => $this->languages, |
|
495
|
|
|
'vocab' => $vocab, |
|
496
|
|
|
'alpha_results' => $search_results, |
|
497
|
|
|
'letters' => $letters, |
|
498
|
|
|
'all_letters' => $all_at_once, |
|
499
|
|
|
'request' => $request, |
|
500
|
|
|
)); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* Invokes the vocabulary group index page template. |
|
505
|
|
|
* @param boolean $stats set to true to get vocabulary statistics visible. |
|
506
|
|
|
*/ |
|
507
|
|
View Code Duplication |
public function invokeGroupIndex($request, $stats = false) |
|
|
|
|
|
|
508
|
|
|
{ |
|
509
|
|
|
$lang = $request->getLang(); |
|
510
|
|
|
$this->setLanguageProperties($lang); |
|
511
|
|
|
$template = $this->twig->loadTemplate('group-index.twig'); |
|
512
|
|
|
$vocab = $request->getVocab(); |
|
513
|
|
|
|
|
514
|
|
|
echo $template->render( |
|
515
|
|
|
array( |
|
516
|
|
|
'languages' => $this->languages, |
|
517
|
|
|
'stats' => $stats, |
|
518
|
|
|
'vocab' => $vocab, |
|
519
|
|
|
'request' => $request, |
|
520
|
|
|
)); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* Invokes the vocabulary group contents page template. |
|
525
|
|
|
*/ |
|
526
|
|
|
public function invokeGroupContents($request) |
|
527
|
|
|
{ |
|
528
|
|
|
$lang = $request->getLang(); |
|
529
|
|
|
$this->setLanguageProperties($lang); |
|
530
|
|
|
$template = $this->twig->loadTemplate('group-contents.twig'); |
|
531
|
|
|
$vocab = $request->getVocab(); |
|
532
|
|
|
|
|
533
|
|
|
$uri = $vocab->getConceptURI($request->getUri()); // make sure it's a full URI |
|
534
|
|
|
$results = $vocab->getConceptInfo($uri, $request->getContentLang()); |
|
535
|
|
|
|
|
536
|
|
|
echo $template->render( |
|
537
|
|
|
array( |
|
538
|
|
|
'languages' => $this->languages, |
|
539
|
|
|
'vocab' => $vocab, |
|
540
|
|
|
'search_results' => $results, |
|
541
|
|
|
'request' => $request, |
|
542
|
|
|
)); |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
/** |
|
546
|
|
|
* Loads and renders the view containing a specific vocabulary. |
|
547
|
|
|
*/ |
|
548
|
|
|
public function invokeVocabularyHome($request) |
|
549
|
|
|
{ |
|
550
|
|
|
$lang = $request->getLang(); |
|
551
|
|
|
// set language parameters for gettext |
|
552
|
|
|
$this->setLanguageProperties($lang); |
|
553
|
|
|
$vocab = $request->getVocab(); |
|
554
|
|
|
|
|
555
|
|
|
$defaultView = $vocab->getConfig()->getDefaultSidebarView(); |
|
556
|
|
|
// load template |
|
557
|
|
|
if ($defaultView === 'groups') { |
|
558
|
|
|
$this->invokeGroupIndex($request, true); |
|
559
|
|
|
return; |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
$template = $this->twig->loadTemplate('vocab.twig'); |
|
563
|
|
|
|
|
564
|
|
|
echo $template->render( |
|
565
|
|
|
array( |
|
566
|
|
|
'languages' => $this->languages, |
|
567
|
|
|
'vocab' => $vocab, |
|
568
|
|
|
'search_letter' => 'A', |
|
569
|
|
|
'active_tab' => $defaultView, |
|
570
|
|
|
'request' => $request, |
|
571
|
|
|
)); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Invokes a very generic errorpage. |
|
576
|
|
|
*/ |
|
577
|
|
|
public function invokeGenericErrorPage($request, $message = null) |
|
578
|
|
|
{ |
|
579
|
|
|
$this->setLanguageProperties($request->getLang()); |
|
580
|
|
|
header("HTTP/1.0 404 Not Found"); |
|
581
|
|
|
$template = $this->twig->loadTemplate('error-page.twig'); |
|
582
|
|
|
echo $template->render( |
|
583
|
|
|
array( |
|
584
|
|
|
'languages' => $this->languages, |
|
585
|
|
|
'request' => $request, |
|
586
|
|
|
'message' => $message, |
|
587
|
|
|
'requested_page' => filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING), |
|
588
|
|
|
)); |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* Loads and renders the view containing a list of recent changes in the vocabulary. |
|
593
|
|
|
* @param Request $request |
|
594
|
|
|
*/ |
|
595
|
|
|
public function invokeChangeList($request, $prop='dc:created') |
|
596
|
|
|
{ |
|
597
|
|
|
// set language parameters for gettext |
|
598
|
|
|
$this->setLanguageProperties($request->getLang()); |
|
599
|
|
|
$vocab = $request->getVocab(); |
|
600
|
|
|
$offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0; |
|
601
|
|
|
$changeList = $vocab->getChangeList($prop, $request->getContentLang(), $request->getLang(), $offset); |
|
602
|
|
|
// load template |
|
603
|
|
|
$template = $this->twig->loadTemplate('changes.twig'); |
|
604
|
|
|
|
|
605
|
|
|
// render template |
|
606
|
|
|
echo $template->render( |
|
607
|
|
|
array( |
|
608
|
|
|
'vocab' => $vocab, |
|
609
|
|
|
'languages' => $this->languages, |
|
610
|
|
|
'request' => $request, |
|
611
|
|
|
'changeList' => $changeList) |
|
612
|
|
|
); |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
} |
|
616
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.