Skip to content

Commit

Permalink
New feature: Support for config.json plugin file and active-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Aug 22, 2016
1 parent a91c572 commit 2a065c2
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
9 changes: 8 additions & 1 deletion application/core/LSUserIdentity.php
Expand Up @@ -159,6 +159,13 @@ protected function postLogin()

Yii::app()->session['adminlang'] = $sLanguage;
App()->setLanguage($sLanguage);

// Read all plugin config files if superadmin logged in
if (Permission::model()->hasGlobalPermission('superadmin'))
{
$pm = Yii::app()->getPluginManager();
$pm->readConfigFiles();
}
}

public function setPlugin($name) {
Expand All @@ -168,4 +175,4 @@ public function setPlugin($name) {
public function setConfig($config) {
$this->config = $config;
}
}
}
8 changes: 6 additions & 2 deletions application/helpers/update/updatedb_helper.php
Expand Up @@ -1450,16 +1450,20 @@ function db_upgrade_all($iOldDBVersion, $bSilent=false) {
}

/**
* Cint db version. Cint plugin is activated by default.
* Plugin JSON config file
* @since 2016-08-22
* @author Olle Haerstedt
*/
if ($iOldDBVersion < 261)
{
/*
$oDB->createCommand()->insert('{{plugins}}', array(
'name' => 'CintLink',
'active' => 1
));
Yii::import('application.core.plugins.CintLink.CintLink');
CintLink::createDatabase();
*/
addColumn('{{plugins}}', 'version', 'string(32)');

$oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>261),"stg_name='DBVersion'");
}
Expand Down
81 changes: 81 additions & 0 deletions application/libraries/PluginManager/PluginBase.php
Expand Up @@ -33,6 +33,12 @@ abstract class PluginBase implements iPlugin {
*/
protected $pluginManager;

/**
* If plugin has a config.json file, it will be parsed into this variable.
* @var array
*/
protected $config = null;

/**
* Constructor for the plugin
* @todo Add proper type hint in 3.0
Expand Down Expand Up @@ -372,4 +378,79 @@ public function log($message, $level = \CLogger::LEVEL_TRACE)
\Yii::log($message, $level, 'plugin.' . $category);
}

/**
* Read JSON config file and store it in $this->config
* Assumes config file is config.json and in plugin root folder.
* @return void
*/
public function readConfigFile()
{
$file = $this->getDir() . DIRECTORY_SEPARATOR . 'config.json';
if (file_exists($file))
{
$json = file_get_contents($file);
$this->config = json_decode($json);
$this->checkActive();
}
else
{
$this->log('Found no config file');
}
}

/**
* Check if config field active is 1. If yes and then version differs, activate the plugin.
* This is the 'active-by-default' feature.
* @return void
*/
protected function checkActive()
{
// No config? Do nothing.
if ($this->config === null)
{
$this->log('Tried to run active-by-default, but found no config');
return;
}

$pluginModel = \Plugin::model()->findByPk($this->id);

// "Impossible"
if (empty($pluginModel))
{
throw new Exception('Internal error: Found no database entry for plugin id ' . $this->id);
}

// Only activate if plugin version in db diff from config
$newVersion = $pluginModel->version !== $this->config->version;
$activeByDefault = $this->config->active == 1;
if ($newVersion && $activeByDefault)
{
// Activate plugin
$result = App()->getPluginManager()->dispatchEvent(
new PluginEvent('beforeActivate', App()->getController()),
$this->getName()
);

if ($result->get('success') !== false)
{
$pluginModel->version = $this->config->version;
$pluginModel->active = 1;
$pluginModel->update();
}
else
{
// Failed. Popup error message.
$not = new \Notification(array(
'user_id' => App()->user->id,
'title' => gT('Plugin error'),
'message' =>
'<span class="fa fa-exclamation-circle text-warning"></span>&nbsp;' .
gT('Could not activate plugin ' . $this->getName()) . '. ' .
gT('Reason:') . ' ' . $result->get('message'),
'importance' => \Notification::HIGH_IMPORTANCE
));
$not->save();
}
}
}
}
31 changes: 30 additions & 1 deletion application/libraries/PluginManager/PluginManager.php
Expand Up @@ -18,6 +18,9 @@ class PluginManager extends \PluginManager {
*/
protected $guidToQuestion = array();

/**
* @var ?
*/
protected $plugins = array();

protected $pluginDirs = array(
Expand Down Expand Up @@ -322,7 +325,7 @@ public function loadPlugin($pluginName, $id = null)
*/
public function loadPlugins()
{
// If DB version is less than 165 : plugins table don't exist. 175 update it (boolean to integer for active)
// If DB version is less than 165 : plugins table don't exist. 175 update it (boolean to integer for active).
$dbVersion=\SettingGlobal::model()->find("stg_name=:name",array(':name'=>'DBVersion'));// Need table SettingGlobal, but settings from DB is set only in controller, not in App, see #11294
if($dbVersion && $dbVersion->stg_value >= 165)
{
Expand All @@ -341,6 +344,19 @@ public function loadPlugins()
$this->dispatchEvent(new PluginEvent('afterPluginLoad', $this)); // Alow plugins to do stuff after all plugins are loaded
}

/**
* Load ALL plugins, active and non-active
* @return void
*/
public function loadAllPlugins()
{
$records = Plugin::model()->findAll();
foreach ($records as $record)
{
$this->loadPlugin($record->name, $record->id);
}
}

/**
* Get a list of question objects and load some information about them.
* This registers the question object classes with Yii.
Expand Down Expand Up @@ -399,5 +415,18 @@ public function constructQuestionFromGUID($guid, $questionId = null, $responseId
}
}

/**
* Read all plugin config files and updates information
* in database
* @return void
*/
public function readConfigFiles()
{
$this->loadAllPlugins();
foreach ($this->plugins as $plugin) {
$plugin->readConfigFile();
}
$this->loadPlugins();
}

}
3 changes: 2 additions & 1 deletion application/models/Plugin.php
Expand Up @@ -29,4 +29,5 @@ public static function model($className = __CLASS__) {
public function tableName() {
return '{{plugins}}';
}
}

}

0 comments on commit 2a065c2

Please sign in to comment.