aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/PluginController.php
blob: 67fd7f4c121d483b9c60cc15c03ad25b68b06af5 (plain)
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
<?php
/**
 * Copyright (c) 2014  Rasmus Fuhse <fuhse@data-quest.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @property StudIPPlugin $plugin
 * @property callable $_
 * @property callable $_n
 */
class PluginController extends StudipController
{
    public function __construct(PluginDispatcher $dispatcher)
    {
        $this->with_session = false; //session for plugin is always initialized in plugins.php
        parent::__construct($dispatcher);

        $this->plugin = $dispatcher->current_plugin;

        if ($this->plugin->hasTranslation()) {
            // Localization
            $this->_ = function ($string) {
                return call_user_func_array(
                    [$this->plugin, '_'],
                    func_get_args()
                );
            };

            $this->_n = function ($string0, $tring1, $n) {
                return call_user_func_array(
                    [$this->plugin, '_n'],
                    func_get_args()
                );
            };
        }
    }

    /**
     * Creates the body element id for this controller a given action.
     *
     * @param string $unconsumed_path Unconsumed path to extract action from
     * @return string
     */
    protected function getBodyElementIdForControllerAndAction($unconsumed_path)
    {
        $body_id = implode('-', [
            'plugin',
            strtosnakecase(get_class($this->plugin)),
            parent::getBodyElementIdForControllerAndAction($unconsumed_path),
        ]);

        return $body_id;
    }

    /**
     * Intercepts all non-resolvable method calls in order to correctly handle
     * calls to _ and _n.
     *
     * @param string $method
     * @param array  $arguments
     * @return mixed
     */
    public function __call($method, $arguments)
    {
        if (
            isset($this->_template_variables[$method])
            && is_callable($this->_template_variables[$method])
        ) {
            return call_user_func_array($this->_template_variables[$method], $arguments);
        }
        return parent::__call($method, $arguments);
    }
}