You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.7 KiB

<?php
/**
* Created by Error202
* Date: 17.08.2018
*/
namespace backend\controllers;
use core\entities\ModuleRecord;
use core\services\ModuleService;
3 years ago
use Yii;
use yii\base\ErrorException;
use yii\db\Exception;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
3 years ago
use yii\web\Response;
/**
* Modules Management
* Class ModuleController
* @package backend\controllers
*/
class ModuleController extends Controller
{
/**
* @var ModuleService Modules management service
*/
3 years ago
private $service;
public function __construct(string $id, $module, ModuleService $service, array $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
}
public function behaviors(): array
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['list', 'disable', 'enable', 'delete', 'search'],
'allow' => true,
'roles' => ['ModuleManagement'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
'disable' => ['POST'],
'enable' => ['POST'],
6 years ago
'install' => ['POST'],
],
],
];
}
/**
* List of modules
* @return string
*/
3 years ago
public function actionList(): string
{
3 years ago
$modules = Yii::$app->moduleManager->getModules();
return $this->render('list', [
'modules' => $modules,
]);
}
3 years ago
/**
* @param $id
* @return Response
* @throws NotFoundHttpException
* @throws ErrorException
* @throws Exception
*/
public function actionDelete($id): Response
{
$module = $this->findModel($id);
3 years ago
$this->service->delete($module);
return $this->redirect(['module/list']);
}
3 years ago
/**
* @param $id
* @return Response
* @throws NotFoundHttpException
*/
public function actionDisable($id): Response
{
$module = $this->findModel($id);
3 years ago
$this->service->disable($module);
return $this->redirect(['module/list']);
}
3 years ago
/**
* @param $id
* @return Response
* @throws NotFoundHttpException
*/
public function actionEnable($id): Response
{
$module = $this->findModel($id);
3 years ago
$this->service->enable($module);
return $this->redirect(['module/list']);
}
3 years ago
public function actionSearch(): string
{
$message = null;
try {
$list = file_get_contents('http://zertex.ru/zxcms/modules.txt');
$links = array_filter(explode('|', $list));
$modules = [];
foreach ($links as $link) {
$manifest = file_get_contents($link);
$manifest = eval(str_replace('<?php', '', $manifest));
if (isset($manifest['enabled']) && $manifest['enabled'] == false) {
continue;
}
3 years ago
$languageFile = str_replace('manifest.php', 'messages/' . Yii::$app->language . '/' . $manifest['name'] . '.php', $link);
try {
$language = file_get_contents($languageFile);
$language = eval(str_replace('<?php', '', $language));
3 years ago
$manifest['locale_name'] = $language[$manifest['name']] ?? $manifest['name'];
$manifest['locale_description'] = $language[$manifest['description']] ?? $manifest['description'];
} catch (\Exception) {
$manifest['locale_name'] = $manifest['name'];
$manifest['locale_description'] = $manifest['description'];
}
$modules[] = $manifest;
}
3 years ago
} catch (\Exception) {
$modules = [];
3 years ago
$message = Yii::t('main', 'Available modules not found');
}
return $this->render('remote-list', [
'modules' => $modules,
'message' => $message,
]);
}
3 years ago
/**
* @param $id
* @return ModuleRecord
* @throws NotFoundHttpException
*/
protected function findModel($id): ModuleRecord
{
if (($model = ModuleRecord::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested module does not exist.');
}
}