aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins/core/CorePlugin.php
blob: 0bba2897b13a0c6310ea9d5ddd4a3838111bc884 (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
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
<?php
/**
 * CorePlugin.class.php - base class
 *
 * @author    André Noack <noack@data-quest.de>
 * @copyright 2021 Authors
 * @license   GPL2 or any later version
 */
abstract class CorePlugin
{

    /**
     * plugin meta data
     */
    protected $plugin_info;

    /**
     * plugin constructor
     * TODO bindtextdomain()
     */
    public function __construct()
    {
        $plugin_manager = PluginManager::getInstance();
        $this->plugin_info = $plugin_manager->getPluginInfo(static::class);
    }

    /**
     * Return the ID of this plugin.
     */
    public function getPluginId()
    {
        return $this->plugin_info['id'];
    }

    public function isEnabled()
    {
        return $this->plugin_info['enabled'];
    }

    /**
     * Return the name of this plugin.
     */
    public function getPluginName()
    {
        return $this->plugin_info['name'];
    }


    public function getPluginURL()
    {
        return $GLOBALS['ABSOLUTE_URI_STUDIP'];
    }

    /**
     * Returns the version of this plugin as defined in manifest.
     * @return string
     */
    public function getPluginVersion()
    {
        return '';
    }

    public function getPluginDescription()
    {
        $metadata = $this->getMetadata();
        $language = getUserLanguage(User::findCurrent()->id);
        if ($metadata['descriptionlong_' . $language]) {
            return $metadata['descriptionlong_' . $language];
        }
        if ($metadata['description_' . $language]) {
            return $metadata['description_' . $language];
        }
        $description = $metadata['descriptionlong'] ?? $metadata['description'];

        if ($this->plugin_info['description_mode'] === 'override_description') {
            return $this->plugin_info['description'];
        } else {
            return '<!-- HTML --><div>' . $description . '</div>' . $this->plugin_info['description'];
        }
    }

    public function getDescriptionMode()
    {
        return $this->plugin_info['description_mode'];
    }

    public function isHighlighted()
    {
        return $this->plugin_info['highlight_until'] > time();
    }

    public function getHighlightText()
    {
        return $this->plugin_info['highlight_text'];
    }


    /**
     * Checks if the plugin is a core-plugin. Returns true if this is the case.
     *
     * @return boolean
     */
    public function isCorePlugin()
    {
        return true;
    }

    /**
     * Get the activation status of this plugin in the given context.
     * This also checks the plugin default activations.
     *
     * @param $context   context range id (optional)
     */
    public function isActivated($context = null)
    {
        $plugin_id = $this->getPluginId();
        $plugin_manager = PluginManager::getInstance();

        if (!isset($context)) {
            $context = Context::getId();
        }
        $activated = $plugin_manager->isPluginActivated($plugin_id, $context);
        return $activated;
    }

    /**
     * Returns whether the plugin may be activated in a certain context.
     *
     * @param Range $context
     * @return bool
     */
    public function isActivatableForContext(Range $context)
    {
        return true;
    }

    /**
     * Callback function called after enabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just enabled.
     */
    public static function onEnable($plugin_id)
    {
    }

    /**
     * Callback function called after disabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just disabled.
     */
    public static function onDisable($plugin_id)
    {
    }

    /**
     * Callback function called after enabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just enabled.
     */
    public static function onActivation($plugin_id, $range_id)
    {
    }

    /**
     * Callback function called after disabling a plugin.
     * The plugin's ID is transmitted for convenience.
     *
     * @param $plugin_id string The ID of the plugin just disabled.
     */
    public static function onDeactivation($plugin_id, $range_id)
    {
    }

    /**
     * @param $range_id string
     * @return bool
     */
    public static function checkActivation($range_id)
    {
        $core_plugin = PluginEngine::getPlugin(static::class);
        return $core_plugin && $core_plugin->isActivated($range_id);
    }
}