Select Git revision
StudIPPlugin.php
Forked from
Stud.IP / Stud.IP
624 commits behind the upstream repository.

Jan-Hendrik Willms authored
use ob_get_clean() instead of ob_get_contents() to avoid double rendering of plugin contents, fixes #5062 Closes #5062 Merge request studip/studip!3784
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
StudIPPlugin.php 7.52 KiB
<?php
/**
* StudIPPlugin.php - generic plugin base class
*
* @author Elmar Ludwig <ludwig@uos.de>
* @copyright 2009 Authors
* @license GPL2 or any later version
*/
abstract class StudIPPlugin
{
use PluginAssetsTrait;
use TranslatablePluginTrait;
/**
* plugin meta data
*/
protected $plugin_info;
/**
* Plugin manifest
*/
protected $manifest = null;
/**
* plugin constructor
*/
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'];
}
/**
* Return the filesystem path to this plugin.
*/
public function getPluginPath()
{
return "plugins_packages/{$this->plugin_info['path']}";
}
/**
* Return the URL of this plugin. Can be used to refer to resources
* (images, style sheets, etc.) inside the installed plugin package.
*/
public function getPluginURL()
{
return $GLOBALS['ABSOLUTE_URI_STUDIP'] . $this->getPluginPath();
}
/**
* Return metadata stored in the manifest of this plugin.
*/
public function getMetadata()
{
if ($this->manifest === null) {
$plugin_manager = PluginManager::getInstance();
$this->manifest = $plugin_manager->getPluginManifest($this->getPluginPath());
}
return $this->manifest;
}
/**
* Returns the description of the plugin either from plugins-table or from the manifest's descriptionlong
* or description attribute.
* @return string|null
*/
public function getPluginDescription()
{
$metadata = $this->getMetadata();
$language = getUserLanguage(User::findCurrent()->id);
if (!empty($metadata['descriptionlong_' . $language])) {
return $metadata['descriptionlong_' . $language];
}
if (!empty($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 $description . $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'];
}
/**
* Returns the version of this plugin as defined in manifest.
* @return string
*/
public function getPluginVersion()
{
return $this->getMetadata()['version'];
}
/**
* Checks if the plugin is a core-plugin. Returns true if this is the case.
*
* @return boolean
*/
public function isCorePlugin()
{
return $this->plugin_info['core'];
}
/**
* Get the activation status of this plugin in the given context.
* This also checks the plugin default activations.
*
* @param $context context range id (optional)
* @param $type type of activation (optional), can be set to 'user'
* in order to point to a homepage plugin
*/
public function isActivated($context = null, $type = 'sem')
{
$plugin_id = $this->getPluginId();
$plugin_manager = PluginManager::getInstance();
/*
* Context can be a Seminar ID or the current user ID if not set.
* Identification is done via the "username" parameter.
*/
if (!isset($context)) {
if ($type === 'user') {
$context = get_userid(Request::username('username', $GLOBALS['user']->username));
} else {
$context = Context::getId();
}
}
if ($type === 'user') {
$activated = $plugin_manager->isPluginActivatedForUser($plugin_id, $context);
} else {
$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;
}
/**
* This method formerly dispatches all actions. Left in place for
* compatibility
* @param string $unconsumed_path part of the dispatch path that was not consumed
*
* @return void
*/
public function perform($unconsumed_path)
{
}
/**
* This method returns callable for slim app
*
* @param string $unconsumed_path part of the dispatch path that was not consumed
*
* @return Closure
* @throws \Trails\Exceptions\UnknownAction
*/
public function getRouteCallable(string $unconsumed_path): Closure
{
$this->perform($unconsumed_path);
$args = explode('/', $unconsumed_path);
$action = $args[0] !== '' ? array_shift($args).'_action' : 'show_action';
if (!method_exists($this, $action)) {
try {
$dispatcher = app(PluginDispatcher::class, ['plugin' => $this]);
return $dispatcher->getRouteCallable($unconsumed_path);
} catch (Trails\Exceptions\UnknownAction $exception) {
if (count($args) > 0) {
throw $exception;
} else {
throw new Exception(_('unbekannte Plugin-Aktion: ') . $unconsumed_path);
}
}
} else {
$that = $this;
return function ($request, $response, array $otherargs) use ($action, $args, $that) {
ob_start();
call_user_func_array([$that, $action], $args);
$content = ob_get_clean();
$response->getBody()->write($content);
return $response;
};
}
}
/**
* 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)
{
}
}