/
PluginsController.php
157 lines (137 loc) · 5.78 KB
/
PluginsController.php
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
<?php
/**
* @property PluginSettingsHelper $PluginSettings
*/
class PluginsController extends LSYii_Controller
{
public $layout = 'main';
/**
* Stored dynamic properties set and unset via __get and __set.
* @var array of mixed.
*/
protected $properties = array();
public function accessRules()
{
$rules = array(
array('allow', 'roles' => array('administrator')),
array('deny')
);
// Note the order; rules are numerically indexed and we want to
// parents rules to be executed only if ours dont apply.
return array_merge($rules, parent::accessRules());
}
public function actionIndex()
{
// Scan the plugins folder.
$discoveredPlugins = App()->getPluginManager()->scanPlugins();
$installedPlugins = Plugin::model()->findAll();
$installedNames = array_map(function ($installedPlugin) { return $installedPlugin->name; }, $installedPlugins);
// Install newly discovered plugins.
foreach ($discoveredPlugins as $discoveredPlugin)
{
if (!in_array($discoveredPlugin['name'], $installedNames))
{
$plugin = new Plugin();
$plugin->name = $discoveredPlugin['name'];
$plugin->active = 0;
$plugin->save();
}
}
$plugins = Plugin::model()->findAll();
$data = array();
foreach ($plugins as $plugin)
{
$data[] = array(
'id' => $plugin->id,
'name' => $plugin->name,
'description' => $discoveredPlugins[$plugin->name]['description'],
'active' => $plugin->active,
'new' => !in_array($plugin->name, $installedNames)
);
}
echo $this->render('/plugins/index', compact('data'));
}
public function actionActivate($id)
{
$plugin = Plugin::model()->findByPk($id);
if (!is_null($plugin)) {
$status = $plugin->active;
if ($status == 1) {
$result = App()->getPluginManager()->dispatchEvent(new PluginEvent('beforeDeactivate', $this), $plugin->name);
if ($result->get('success', true)) {
$status = 0;
} else {
$message = $result->get('message', gT('Failed to deactivate the plugin.'));
App()->user->setFlash('pluginActivation', $message);
$this->redirect(array('plugins/'));
}
} else {
// Load the plugin:
App()->getPluginManager()->loadPlugin($plugin->name, $id);
$result = App()->getPluginManager()->dispatchEvent(new PluginEvent('beforeActivate', $this), $plugin->name);
if ($result->get('success', true)) {
$status = 1;
} else {
$message = $result->get('message', gT('Failed to activate the plugin.'));
App()->user->setFlash('pluginActivation', $message);
$this->redirect(array('plugins/'));
}
}
$plugin->active = $status;
$plugin->save();
}
$this->redirect(array('plugins/'));
}
public function actionConfigure($id)
{
$plugin = Plugin::model()->findByPk($id)->attributes;
$pluginObject = App()->getPluginManager()->loadPlugin($plugin['name'], $plugin['id']);
if ($plugin === null)
{
/**
* @todo Add flash message "Plugin not found".
*/
$this->redirect(array('plugins/'));
}
// If post handle data.
if (App()->request->isPostRequest)
{
if (!is_null(App()->request->getPost('ok'))) {
$settings = $pluginObject->getPluginSettings(false);
$save = array();
foreach ($settings as $name => $setting)
{
$save[$name] = App()->request->getPost($name, null);
}
$pluginObject->saveSettings($save);
Yii::app()->user->setFlash('pluginmanager', 'Settings saved');
} else {
// Ok buttons was not pressed, assume cancel
}
$this->forward('plugins/index', true);
}
$settings = $pluginObject->getPluginSettings();
if (empty($settings)) {
// And show a message
Yii::app()->user->setFlash('pluginmanager', 'This plugin has no settings');
$this->forward('plugins/index', true);
}
$this->render('/plugins/configure', compact('settings', 'plugin'));
}
public function filters()
{
$filters = array(
'accessControl'
);
return array_merge(parent::filters(), $filters);
}
public function __get($property)
{
return $this->properties[$property];
}
public function __set($property, $value)
{
$this->properties[$property] = $value;
}
}
?>