Completed
Push — master ( 535d6b...8f4cdc )
by Henri
02:56
created

WebController::__construct()   D

Complexity

Conditions 17
Paths 32

Size

Total Lines 91
Code Lines 46

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 91
rs 4.8361
cc 17
eloc 46
nc 32
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 11.

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.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
        $sortedVocabs = $this->model->getVocabularyList(false, true);
193
        $langList = $this->model->getLanguages($request->getLang());
194
195
        // render template
196
        echo $template->render(
197
            array(
198
                'sorted_vocabs' => $sortedVocabs,
199
                'category_label' => $categoryLabel,
200
                'languages' => $this->languages,
201
                'lang_list' => $langList,
202
                'request' => $request,
203
            ));
204
    }
205
206
    /**
207
     * Invokes the concept page of a single concept in a specific vocabulary.
208
     */
209
    public function invokeVocabularyConcept($request)
210
    {
211
        $lang = $request->getLang();
212
        $this->setLanguageProperties($lang);
213
        $vocab = $request->getVocab();
214
215
        $langcodes = $vocab->getConfig()->getShowLangCodes();
216
        $uri = $vocab->getConceptURI($request->getUri()); // make sure it's a full URI
217
218
        $results = $vocab->getConceptInfo($uri, $request->getContentLang());
219
        if (!$results) {
220
            $this->invokeGenericErrorPage($request);
221
            return;
222
        }
223
        $template = (in_array('skos:Concept', $results[0]->getType())) ? $this->twig->loadTemplate('concept-info.twig') : $this->twig->loadTemplate('group-contents.twig');
224
        
225
        $crumbs = $vocab->getBreadCrumbs($request->getContentLang(), $uri);
226
        echo $template->render(array(
227
            'search_results' => $results,
228
            'vocab' => $vocab,
229
            'languages' => $this->languages,
230
            'explicit_langcodes' => $langcodes,
231
            'bread_crumbs' => $crumbs['breadcrumbs'],
232
            'combined' => $crumbs['combined'],
233
            'request' => $request)
234
        );
235
    }
236
237
    /**
238
     * Invokes the feedback page with information of the users current vocabulary.
239
     */
240
    public function invokeFeedbackForm($request)
241
    {
242
        $template = $this->twig->loadTemplate('feedback.twig');
243
        $this->setLanguageProperties($request->getLang());
244
        $vocabList = $this->model->getVocabularyList(false);
245
        $vocab = $request->getVocab();
246
247
        $feedback_sent = false;
248
        $feedback_msg = null;
249
        if (filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING)) {
250
            $feedback_sent = true;
251
            $feedback_msg = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
252
        }
253
        $feedback_name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
254
        $feedback_email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
255
        $feedback_vocab = filter_input(INPUT_POST, 'vocab', FILTER_SANITIZE_STRING);
256
        $feedback_vocab_email = ($vocab !== null) ? $vocab->getConfig()->getFeedbackRecipient() : null;
257
258
        // if the hidden field has been set a value we have found a spam bot
259
        // and we do not actually send the message.
260
        if ($feedback_sent && filter_input(INPUT_POST, 'trap', FILTER_SANITIZE_STRING) === '') {
261
            $this->sendFeedback($feedback_msg, $feedback_name, $feedback_email, $feedback_vocab, $feedback_vocab_email);
262
        }
263
264
        echo $template->render(
265
            array(
266
                'languages' => $this->languages,
267
                'vocab' => $vocab,
268
                'vocabList' => $vocabList,
269
                'feedback_sent' => $feedback_sent,
270
                'request' => $request,
271
            ));
272
    }
273
274
    /**
275
     * Sends the user entered message through the php's mailer.
276
     * @param string $message only required parameter is the actual message.
277
     * @param string $fromName senders own name.
278
     * @param string $fromEmail senders email adress.
279
     * @param string $fromVocab which vocabulary is the feedback related to.
280
     */
281
    public function sendFeedback($message, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null)
282
    {
283
        $toAddress = ($toMail) ? $toMail : $this->model->getConfig()->getFeedbackAddress();
284
        if ($fromVocab !== null) {
285
            $message = 'Feedback from vocab: ' . strtoupper($fromVocab) . "<br />" . $message;
286
        }
287
288
        $subject = SERVICE_NAME . " feedback";
289
        $headers = "MIME-Version: 1.0″ . '\r\n";
290
        $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
291
        if ($toMail) {
292
            $headers .= "Cc: " . $this->model->getConfig()->getFeedbackAddress() . "\r\n";
293
        }
294
295
        $headers .= "From: $fromName <$fromEmail>" . "\r\n" . 'X-Mailer: PHP/' . phpversion();
296
        $envelopeSender = FEEDBACK_ENVELOPE_SENDER;
297
        $params = empty($envelopeSender) ? '' : "-f $envelopeSender";
298
299
        // adding some information about the user for debugging purposes.
300
        $agent = (filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING)) ? filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING) : '';
301
        $referer = (filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_STRING)) ? filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_STRING) : '';
302
        $ipAddress = (filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING)) ? filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING) : '';
303
        $timestamp = date(DATE_RFC2822);
304
305
        $message = $message . "<br /><br /> Debugging information:"
306
            . "<br />Timestamp: " . $timestamp
307
            . "<br />User agent: " . $agent
308
            . "<br />IP address: " . $ipAddress
309
            . "<br />Referer: " . $referer;
310
311
        try {
312
            mail($toAddress, $subject, $message, $headers, $params);
313
        } catch (Exception $e) {
314
            header("HTTP/1.0 404 Not Found");
315
            $template = $this->twig->loadTemplate('error-page.twig');
316
            if ($this->model->getConfig()->getLogCaughtExceptions()) {
317
                error_log('Caught exception: ' . $e->getMessage());
318
            }
319
320
            echo $template->render(
321
                array(
322
                    'languages' => $this->languages,
323
                ));
324
325
            return;
326
        }
327
    }
328
329
    /**
330
     * Invokes the about page for the Skosmos service.
331
     */
332 View Code Duplication
    public function invokeAboutPage($request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
333
    {
334
        $template = $this->twig->loadTemplate('about.twig');
335
        $this->setLanguageProperties($request->getLang());
336
        $url = $request->getServerConstant('HTTP_HOST');
337
        $version = $this->model->getVersion();
338
339
        echo $template->render(
340
            array(
341
                'languages' => $this->languages,
342
                'version' => $version,
343
                'server_instance' => $url,
344
                'request' => $request,
345
            ));
346
    }
347
348
    /**
349
     * Invokes the search for concepts in all the availible ontologies.
350
     */
351
    public function invokeGlobalSearch($request)
352
    {
353
        $lang = $request->getLang();
354
        $template = $this->twig->loadTemplate('vocab-search-listing.twig');
355
        $this->setLanguageProperties($lang);
356
357
        $parameters = new ConceptSearchParameters($request, $this->model->getConfig());
358
359
        $vocabs = $request->getQueryParam('vocabs'); # optional
360
        // convert to vocids array to support multi-vocabulary search
361
        $vocids = ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
362
        $vocabObjects = array();
363
        if ($vocids) {
364
            foreach($vocids as $vocid) {
365
                $vocabObjects[] = $this->model->getVocabulary($vocid);
366
            }
367
        }
368
        $parameters->setVocabularies($vocabObjects);
369
370
        try {
371
            $count_and_results = $this->model->searchConceptsAndInfo($parameters);
372
        } catch (Exception $e) {
373
            header("HTTP/1.0 404 Not Found");
374
            if ($this->model->getConfig()->getLogCaughtExceptions()) {
375
                error_log('Caught exception: ' . $e->getMessage());
376
            }
377
            $this->invokeGenericErrorPage($request, $e->getMessage());
378
            return;
379
        }
380
        $counts = $count_and_results['count'];
381
        $search_results = $count_and_results['results'];
382
        $vocabList = $this->model->getVocabularyList();
383
        $sortedVocabs = $this->model->getVocabularyList(false, true);
384
        $langList = $this->model->getLanguages($lang);
385
386
        echo $template->render(
387
            array(
388
                'search_count' => $counts,
389
                'languages' => $this->languages,
390
                'search_results' => $search_results,
391
                'rest' => $parameters->getOffset()>0,
392
                'global_search' => true,
393
                'term' => $request->getQueryParam('q'),
394
                'lang_list' => $langList,
395
                'vocabs' => str_replace(' ', '+', $vocabs),
396
                'vocab_list' => $vocabList,
397
                'sorted_vocabs' => $sortedVocabs,
398
                'request' => $request,
399
            ));
400
    }
401
402
    /**
403
     * Invokes the search for a single vocabs concepts.
404
     */
405
    public function invokeVocabularySearch($request)
406
    {
407
        $template = $this->twig->loadTemplate('vocab-search-listing.twig');
408
        $this->setLanguageProperties($request->getLang());
409
        $vocab = $request->getVocab();
410
        try {
411
            $vocab_types = $this->model->getTypes($request->getVocabid(), $request->getLang());
412
        } catch (Exception $e) {
413
            header("HTTP/1.0 404 Not Found");
414
            if ($this->model->getConfig()->getLogCaughtExceptions()) {
415
                error_log('Caught exception: ' . $e->getMessage());
416
            }
417
418
            echo $template->render(
419
                array(
420
                    'languages' => $this->languages,
421
                ));
422
423
            return;
424
        }
425
426
        $langcodes = $vocab->getConfig()->getShowLangCodes();
427
        $parameters = new ConceptSearchParameters($request, $this->model->getConfig());
428
429
        try {
430
            $count_and_results = $this->model->searchConceptsAndInfo($parameters);
431
            $counts = $count_and_results['count'];
432
            $search_results = $count_and_results['results'];
433
        } catch (Exception $e) {
434
            header("HTTP/1.0 404 Not Found");
435
            if ($this->model->getConfig()->getLogCaughtExceptions()) {
436
                error_log('Caught exception: ' . $e->getMessage());
437
            }
438
439
            echo $template->render(
440
                array(
441
                    'languages' => $this->languages,
442
                    'vocab' => $vocab,
443
                    'term' => $request->getQueryParam('q'),
444
                ));
445
            return;
446
        }
447
        echo $template->render(
448
            array(
449
                'languages' => $this->languages,
450
                'vocab' => $vocab,
451
                'search_results' => $search_results,
452
                'search_count' => $counts,
453
                'rest' => $parameters->getOffset()>0,
454
                'limit_parent' => $parameters->getParentLimit(),
455
                'limit_type' =>  $request->getQueryParam('type') ? explode('+', $request->getQueryParam('type')) : null,
456
                'limit_group' => $parameters->getGroupLimit(),
457
                'limit_scheme' =>  $request->getQueryParam('scheme') ? explode('+', $request->getQueryParam('scheme')) : null,
458
                'group_index' => $vocab->listConceptGroups($request->getContentLang()),
459
                'parameters' => $parameters,
460
                'term' => $request->getQueryParam('q'),
461
                'types' => $vocab_types,
462
                'explicit_langcodes' => $langcodes,
463
                'request' => $request,
464
            ));
465
    }
466
467
    /**
468
     * Invokes the alphabetical listing for a specific vocabulary.
469
     */
470
    public function invokeAlphabeticalIndex($request)
471
    {
472
        $lang = $request->getLang();
473
        $this->setLanguageProperties($lang);
474
        $template = $this->twig->loadTemplate('alphabetical-index.twig');
475
        $vocab = $request->getVocab();
476
477
        $offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0;
478
        if ($request->getQueryParam('limit')) {
479
            $count = $request->getQueryParam('limit');
480
        } else {
481
            $count = ($offset > 0) ? null : 250;
482
        }
483
484
        $content_lang = $request->getContentLang();
485
486
        $all_at_once = $vocab->getConfig()->getAlphabeticalFull();
487
        if (!$all_at_once) {
488
            $search_results = $vocab->searchConceptsAlphabetical($request->getLetter(), $count, $offset, $content_lang);
489
            $letters = $vocab->getAlphabet($content_lang);
490
        } else {
491
            $search_results = $vocab->searchConceptsAlphabetical('*', null, null, $content_lang);
492
            $letters = null;
493
        }
494
495
        $request->setContentLang($content_lang);
496
497
        echo $template->render(
498
            array(
499
                'languages' => $this->languages,
500
                'vocab' => $vocab,
501
                'alpha_results' => $search_results,
502
                'letters' => $letters,
503
                'all_letters' => $all_at_once,
504
                'request' => $request,
505
            ));
506
    }
507
508
    /**
509
     * Invokes the vocabulary group index page template.
510
     * @param boolean $stats set to true to get vocabulary statistics visible.
511
     */
512 View Code Duplication
    public function invokeGroupIndex($request, $stats = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
513
    {
514
        $lang = $request->getLang();
515
        $this->setLanguageProperties($lang);
516
        $template = $this->twig->loadTemplate('group-index.twig');
517
        $vocab = $request->getVocab();
518
519
        echo $template->render(
520
            array(
521
                'languages' => $this->languages,
522
                'stats' => $stats,
523
                'vocab' => $vocab,
524
                'request' => $request,
525
            ));
526
    }
527
528
    /**
529
     * Loads and renders the view containing a specific vocabulary.
530
     */
531
    public function invokeVocabularyHome($request)
532
    {
533
        $lang = $request->getLang();
534
        // set language parameters for gettext
535
        $this->setLanguageProperties($lang);
536
        $vocab = $request->getVocab();
537
538
        $defaultView = $vocab->getConfig()->getDefaultSidebarView();
539
        // load template
540
        if ($defaultView === 'groups') {
541
            $this->invokeGroupIndex($request, true);
542
            return;
543
        }
544
545
        $template = $this->twig->loadTemplate('vocab.twig');
546
547
        echo $template->render(
548
            array(
549
                'languages' => $this->languages,
550
                'vocab' => $vocab,
551
                'search_letter' => 'A',
552
                'active_tab' => $defaultView,
553
                'request' => $request,
554
            ));
555
    }
556
557
    /**
558
     * Invokes a very generic errorpage.
559
     */
560
    public function invokeGenericErrorPage($request, $message = null)
561
    {
562
        $this->setLanguageProperties($request->getLang());
563
        header("HTTP/1.0 404 Not Found");
564
        $template = $this->twig->loadTemplate('error-page.twig');
565
        echo $template->render(
566
            array(
567
                'languages' => $this->languages,
568
                'request' => $request,
569
                'message' => $message,
570
                'requested_page' => filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING),
571
            ));
572
    }
573
574
    /**
575
     * Loads and renders the view containing a list of recent changes in the vocabulary.
576
     * @param Request $request
577
     */
578
    public function invokeChangeList($request, $prop='dc:created')
579
    {
580
        // set language parameters for gettext
581
        $this->setLanguageProperties($request->getLang());
582
        $vocab = $request->getVocab();
583
        $offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0;
584
        $changeList = $vocab->getChangeList($prop, $request->getContentLang(), $request->getLang(), $offset);
585
        // load template
586
        $template = $this->twig->loadTemplate('changes.twig');
587
588
        // render template
589
        echo $template->render(
590
            array(
591
                'vocab' => $vocab,
592
                'languages' => $this->languages,
593
                'request' => $request,
594
                'changeList' => $changeList)
595
            );
596
    }
597
598
}
599