/
PluginsController.php
186 lines (165 loc) · 5.67 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
<?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('superadmin')),
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)
{
$pluginConfig = \ls\pluginmanager\PluginConfig::findAll(false)[$id];
$plugin = App()->pluginManager->loadPlugin($pluginConfig);
if (App()->request->isPostRequest) {
$plugin->saveSettings(App()->request->getPost($plugin->id));
}
// var_dump($settings);
// var_dump($plugin);
// die();
// 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);
// App()->user->setFlash('pluginmanager', 'Settings saved');
// if(!is_null(App()->request->getPost('redirect')))
// {
// $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('configure', ['plugin' => $plugin]);
}
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()
{
$plugins = new CArrayDataProvider(App()->pluginManager->scanPlugins());
return $this->render('index', ['plugins' => $plugins]);
}
public function filters()
{
$aFilters = array(
'accessControl'
);
return array_merge(parent::filters(), $aFilters);
}
}
?>