/
PluginsController.php
224 lines (194 loc) · 7.46 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
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 __get($property)
{
return $this->properties[$property];
}
public function __set($property, $value)
{
$this->properties[$property] = $value;
}
public function _init()
{
parent::_init();
Yii::app()->bootstrap->init(); // Make sure bootstrap css is rendered in time
}
public function accessRules()
{
$aRules = array(
array('allow', 'roles' => array('administrator')),
array('allow', 'actions' => array('direct')),
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($aRules, parent::accessRules());
}
public function actionActivate($id)
{
$oPlugin = Plugin::model()->findByPk($id);
if (!is_null($oPlugin))
{
$iStatus = $oPlugin->active;
if ($iStatus == 0)
{
// Load the plugin:
App()->getPluginManager()->loadPlugin($oPlugin->name, $id);
$result = App()->getPluginManager()->dispatchEvent(new PluginEvent('beforeActivate', $this), $oPlugin->name);
if ($result->get('success', true))
{
$iStatus = 1;
} else
{
$sMessage = $result->get('message', gT('Failed to activate the plugin.'));
App()->user->setFlash('pluginActivation', $sMessage);
$this->redirect(array('plugins/'));
}
}
$oPlugin->active = $iStatus;
$oPlugin->save();
}
$this->redirect(array('plugins/'));
}
public function actionConfigure($id)
{
$arPlugin = Plugin::model()->findByPk($id)->attributes;
$oPluginObject = App()->getPluginManager()->loadPlugin($arPlugin['name'], $arPlugin['id']);
if ($arPlugin === null)
{
Yii::app()->user->setFlash('pluginmanager', 'Plugin not found');
$this->redirect(array('plugins/'));
}
// If post handle data, yt0 seems to be the submit button
if (App()->request->isPostRequest)
{
$aSettings = $oPluginObject->getPluginSettings(false);
$aSave = array();
foreach ($aSettings as $name => $setting)
{
$aSave[$name] = App()->request->getPost($name, null);
}
$oPluginObject->saveSettings($aSave);
if(App()->request->getPost('redirect'))
{
Yii::app()->user->setFlash('pluginmanager', 'Settings saved');
$this->forward('plugins/index', true);
}
}
// Prepare settings to be send to the view.
$aSettings = $oPluginObject->getPluginSettings();
if (empty($aSettings))
{
// And show a message
Yii::app()->user->setFlash('pluginmanager', 'This plugin has no settings');
$this->forward('plugins/index', true);
}
// Send to view plugin porperties: name and description
$aPluginProp = App()->getPluginManager()->getPluginInfo($arPlugin['name']);
$this->render('/plugins/configure', array('settings' => $aSettings, 'plugin' => $arPlugin, 'properties' => $aPluginProp));
}
public function actionDeactivate($id)
{
$oPlugin = Plugin::model()->findByPk($id);
if (!is_null($oPlugin))
{
$iStatus = $oPlugin->active;
if ($iStatus == 1)
{
$result = App()->getPluginManager()->dispatchEvent(new PluginEvent('beforeDeactivate', $this), $oPlugin->name);
if ($result->get('success', true))
{
$iStatus = 0;
} else
{
$message = $result->get('message', gT('Failed to deactivate the plugin.'));
App()->user->setFlash('pluginActivation', $message);
$this->redirect(array('plugins/'));
}
}
$oPlugin->active = $iStatus;
$oPlugin->save();
}
$this->redirect(array('plugins/'));
}
public function actionDirect($plugin, $function)
{
$oEvent = new PluginEvent('newDirectRequest');
// The intended target of the call.
$oEvent->set('target', $plugin);
// The name of the function.
$oEvent->set('function', $function);
$oEvent->set('request', App()->request);
App()->getPluginManager()->dispatchEvent($oEvent);
$sOutput = '';
foreach ($oEvent->getAllContent() as $content)
{
$sOutput .= $content->getContent();
}
if (!empty($sOutput))
{
$this->renderText($sOutput);
}
}
public function actionIndex()
{
$oPluginManager = App()->getPluginManager();
// Scan the plugins folder.
$aDiscoveredPlugins = $oPluginManager->scanPlugins();
$aInstalledPlugins = $oPluginManager->getInstalledPlugins();
$aInstalledNames = array_map(function ($installedPlugin) {
return $installedPlugin->name;
}, $aInstalledPlugins);
// Install newly discovered plugins.
foreach ($aDiscoveredPlugins as $discoveredPlugin)
{
if (!in_array($discoveredPlugin['pluginClass'], $aInstalledNames))
{
$oPlugin = new Plugin();
$oPlugin->name = $discoveredPlugin['pluginClass'];
$oPlugin->active = 0;
$oPlugin->save();
}
}
$aoPlugins = Plugin::model()->findAll();
$data = array();
foreach ($aoPlugins as $oPlugin)
{
/* @var $plugin Plugin */
if (array_key_exists($oPlugin->name, $aDiscoveredPlugins))
{
$aPluginSettings = App()->getPluginManager()->loadPlugin($oPlugin->name, $oPlugin->id)->getPluginSettings(false);
$data[] = array(
'id' => $oPlugin->id,
'name' => $aDiscoveredPlugins[$oPlugin->name]['pluginName'],
'description' => $aDiscoveredPlugins[$oPlugin->name]['description'],
'active' => $oPlugin->active,
'settings' => $aPluginSettings,
'new' => !in_array($oPlugin->name, $aInstalledNames)
);
} else
{
// This plugin is missing, maybe the files were deleted but the record was not removed from the database
// Now delete this record. Depending on the plugin the settings will be preserved
App()->user->setFlash('pluginDelete' . $oPlugin->id, sprintf(gT("Plugin '%s' was missing and is removed from the database."), $oPlugin->name));
$oPlugin->delete();
}
}
echo $this->render('/plugins/index', compact('data'));
}
public function filters()
{
$aFilters = array(
'accessControl'
);
return array_merge(parent::filters(), $aFilters);
}
}
?>