123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace backend\controllers;
- use backend\models\ModuleConfig;
- use common\models\Module;
- use Yii;
- use yii\base\Model;
- use yii\data\ArrayDataProvider;
- use yii\filters\VerbFilter;
- use yii\helpers\Json;
- use yii\web\Controller;
- /**
- * ModuleController implements the CRUD actions for Module model.
- */
- class PluginController extends Controller
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'open' => ['post'],
- 'close' => ['post'],
- 'install' => ['post'],
- 'uninstall' => ['post']
- ],
- ],
- ];
- }
- /**
- * Lists all Module models.
- * @return mixed
- */
- public function actionIndex()
- {
- $plugins = Yii::$app->pluginManager->findAll();
- $dataProvider = new ArrayDataProvider([
- 'allModels' => $plugins
- ]);
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- ]);
- }
- // 安装
- public function actionInstall()
- {
- $id = Yii::$app->request->post('id');
- $pluginManager = Yii::$app->pluginManager;
- $plugin = $pluginManager->findOne($id);
- if(!$pluginManager->install($plugin)){
- Yii::$app->session->setFlash('error', '插件安装失败');
- } else {
- Yii::$app->session->setFlash('success', '插件安装成功');
- }
- return $this->redirect(['index']);
- }
- //卸载
- public function actionUninstall()
- {
- $id = Yii::$app->request->post('id');
- $pluginManager = Yii::$app->pluginManager;
- $plugin = $pluginManager->findOne($id);
- if(!$pluginManager->uninstall($plugin)){
- Yii::$app->session->setFlash('error', '插件卸载失败');
- } else {
- Yii::$app->session->setFlash('success', '插件卸载成功');
- }
- return $this->redirect(['index']);
- }
- // 开启
- public function actionOpen()
- {
- $id = Yii::$app->request->post('id');
- $pluginManager = Yii::$app->pluginManager;
- $plugin = $pluginManager->findOne($id);
- if(!$plugin->install){
- Yii::$app->session->setFlash('error', '插件没安装');
- }
- if(!$pluginManager->open($plugin)){
- Yii::$app->session->setFlash('error', '插件打开失败');
- } else {
- Yii::$app->session->setFlash('success', '插件打开成功');
- }
- return $this->redirect(['index']);
- }
- // 关闭
- public function actionClose()
- {
- $id = Yii::$app->request->post('id');
- $pluginManager = Yii::$app->pluginManager;
- $plugin = $pluginManager->findOne($id);
- if(!$plugin->install){
- Yii::$app->session->setFlash('error', '插件没安装');
- }
- if(!$pluginManager->close($plugin)){
- Yii::$app->session->setFlash('error', '插件关闭失败');
- } else {
- Yii::$app->session->setFlash('success', '插件关闭成功');
- }
- return $this->redirect(['index']);
- }
- /**
- * 插件配置
- * @param $id
- * @return string|\yii\web\Response
- * @throws \yii\base\InvalidConfigException
- */
- public function actionConfig($id)
- {
- $pluginManager = Yii::$app->pluginManager;
- $plugin = $pluginManager->findOne($id);
- if(!$plugin->install){
- Yii::$app->session->setFlash('error', '插件没安装');
- return $this->redirect(['index']);
- }
- $configs = Json::decode($plugin->getModel()->config);
- $configModels = [];
- if (!empty($configs)) {
- foreach ($configs as $k => $config) {
- $configModel = new ModuleConfig();
- $configModel->scenario = 'init';
- $configModel->attributes = $config;
- $configModels[$k] = $configModel;
- }
- }
- $model = $plugin->getModel();
- if (\Yii::$app->request->isPost && Model::loadMultiple($configModels, \Yii::$app->request->post()) && Model::validateMultiple($configModels)) {
- $configs = Json::encode($configModels);
- $model->config = $configs;
- $model->save();
- return $this->redirect(['index']);
- }
- return $this->render('config', [
- 'model' => $model,
- 'configModels' => $configModels
- ]);
- }
- }
|