Browse Source

Init shop module

master
Egorka 6 years ago
parent
commit
53d612276a
  1. 74
      backend/controllers/ModuleController.php
  2. 6
      backend/messages/ru/main.php
  3. 11
      backend/views/module/remote-list.php
  4. 34
      core/helpers/FileHelper.php

74
backend/controllers/ModuleController.php

@ -7,6 +7,7 @@
namespace backend\controllers;
use core\entities\ModuleRecord;
use core\helpers\FileHelper;
use core\services\ModuleService;
use yii\web\Controller;
use yii\filters\VerbFilter;
@ -38,7 +39,7 @@ class ModuleController extends Controller
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['list', 'disable', 'enable', 'delete', 'search'],
'actions' => ['list', 'disable', 'enable', 'delete', 'search', 'install'],
'allow' => true,
'roles' => ['ModuleManagement'],
],
@ -54,6 +55,7 @@ class ModuleController extends Controller
'delete' => ['POST'],
'disable' => ['POST'],
'enable' => ['POST'],
'install' => ['POST'],
],
],
];
@ -98,7 +100,75 @@ class ModuleController extends Controller
public function actionInstall($name)
{
//
$needed_manifest = [];
// check installed extension
if (class_exists('\ZipArchive')) {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'ZipArchive php extension not installed'));
return $this->redirect(['search']);
}
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 ($manifest['name'] == $name) {
$needed_manifest = $manifest;
}
}
} catch (\Exception $e) {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Service not available'));
return $this->redirect(['search']);
}
if (empty($needed_manifest) && !isset($needed_manifest['git_path']) && !isset($needed_manifest['git_nameh'])) {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Module cannot be installed'));
return $this->redirect(['search']);
}
// get git link, download
$download_link = $needed_manifest['git_path'] . '/-/archive/master/' . $needed_manifest['git_name'] . '-master.zip';
if (FileHelper::downloadFile($download_link, \Yii::getAlias('@runtime/module.zip'))) {
// unzip
$zip = new \ZipArchive;
$res = $zip->open(\Yii::getAlias('@runtime/module.zip'));
if ($res === true) {
mkdir(\Yii::getAlias('@runtime/_module'));
$zip->extractTo(\Yii::getAlias('@runtime/_module/'));
$zip->close();
$module_path = \Yii::getAlias('@common/modules/') . $needed_manifest['name'];
if (!file_exists($module_path)) {
mkdir($module_path);
}
FileHelper::copyDirectory(\Yii::getAlias('@runtime/_module'), $module_path);
} else {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Error module archive'));
return $this->redirect(['search']);
}
} else {
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Error while install module'));
return $this->redirect(['search']);
}
// remove zip
$this->cleanInstallationFiles();
\Yii::$app->session->setFlash('success', \Yii::t('main', 'Module installed successful'));
return $this->redirect(['search']);
}
private function cleanInstallationFiles()
{
if (file_exists(\Yii::getAlias('@runtime/_module'))) {
FileHelper::removeDirectory(\Yii::getAlias('@runtime/_module'));
}
if (file_exists(\Yii::getAlias('@runtime/module.zip'))) {
unlink(\Yii::getAlias('@runtime/module.zip'));
}
}
public function actionSearch()

6
backend/messages/ru/main.php

@ -72,4 +72,10 @@ return [
'Maroon' => 'Вишнёвый',
'Available Modules' => 'Доступные модули',
'Installed' => 'Установлен',
'Service not available' => 'Сервис временно недоступен',
'Module cannot be installed' => 'Модуль не может быть установлен',
'Error while install module' => 'При устаноке модуля возникла ошибка',
'ZipArchive php extension not installed' => 'Расширение ZipArchive для PHP не установлено',
'Error module archive' => 'Ошибка архива модуля',
'Module installed successful' => 'Модуль успешно установлен',
];

11
backend/views/module/remote-list.php

@ -26,15 +26,6 @@ $this->params['breadcrumbs'][] = $this->title;
<?php foreach ($modules as $module) : ?>
<?php
/*$color = $module->isEnabled() ? '#00aced' : '#cccccc';
if ($module->isDisabled()) {
ModuleHelper::addModuleAdminTranslation($module->name);
}*/
//$module_name = in_array($module->name, array_keys(Yii::$app->getI18n()->translations)) ? Yii::t($module->name, $module->name) : $module->name;
//$module_description = in_array($module->name, array_keys(Yii::$app->getI18n()->translations)) ? Yii::t($module->name, $module->description) : $module->description;
$color = '#00aced';
$module_name = $module['locale_name'];
@ -54,7 +45,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?php if (Yii::$app->moduleManager->isExists($module['name'])) : ?>
<span class="text-bold text-green"><?= Yii::t('main', 'Installed') ?></span>
<?php else : ?>
<?= Html::a(Yii::t('main', 'Enable'), ['module/enable', 'id' => $module['name']], [
<?= Html::a(Yii::t('main', 'Enable'), ['module/install', 'name' => $module['name']], [
'class' => 'btn btn-default btn-sm',
'data' => [
'method' => 'post',

34
core/helpers/FileHelper.php

@ -0,0 +1,34 @@
<?php
/**
* Created by Error202
* Date: 13.09.2018
*/
namespace core\helpers;
class FileHelper extends \yii\helpers\FileHelper
{
public static function downloadFile($url, $path)
{
$new_file = null;
$new_name = $path;
$file = fopen($url, 'rb');
if ($file) {
$new_file = fopen($new_name, 'wb');
if ($new_file) {
while (!feof($file)) {
fwrite($new_file, fread($file, 1024 * 8), 1024 * 8);
}
}
}
if ($file) {
fclose($file);
}
if ($new_file) {
fclose($new_file);
}
return $new_file ? true : false;
}
}
Loading…
Cancel
Save