theme dependence

This commit is contained in:
Билай Станислав 2025-01-21 16:41:48 +03:00
parent f421e0c649
commit 11c99be0f6
26 changed files with 96 additions and 908 deletions

View File

@ -1,8 +0,0 @@
<?php
namespace app\modules\tag;
class TagModule extends \kernel\app_modules\tag\TagModule
{
}

View File

@ -1,8 +0,0 @@
<?php
namespace app\modules\tag\controllers;
class TagController extends \kernel\app_modules\tag\controllers\TagController
{
}

View File

@ -1,13 +0,0 @@
{
"name": "Tags",
"version": "0.1",
"author": "ITGuild",
"slug": "tag",
"type": "additional_property",
"description": "Tags module",
"app_module_path": "{APP}/modules/{slug}",
"module_class": "app\\modules\\tag\\TagModule",
"module_class_file": "{APP}/modules/tag/TagModule.php",
"routs": "routs/tag.php",
"dependence": "menu"
}

View File

@ -1,2 +0,0 @@
<?php
include KERNEL_APP_MODULES_DIR . "/tag/routs/tag.php";

View File

@ -1,124 +0,0 @@
<?php
namespace kernel\app_modules\tag;
use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\SelectBuilder;
use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\models\TagEntity;
use kernel\app_modules\tag\service\TagEntityService;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\modules\option\service\OptionService;
use kernel\Request;
use kernel\services\MigrationService;
class TagModule extends Module
{
public MenuService $menuService;
public MigrationService $migrationService;
public function __construct()
{
$this->menuService = new MenuService();
$this->migrationService = new MigrationService();
}
/**
* @throws \Exception
*/
public function init(): void
{
$this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations");
$this->menuService->createItem([
"label" => "Тэги",
"url" => "/admin/tag",
"slug" => "tag",
]);
$this->menuService->createItem([
"label" => "Тэги",
"url" => "/admin/settings/tag",
"slug" => "tag_settings",
"parent_slug" => "settings"
]);
OptionService::createFromParams("entity_tag_list", "{}", "Список тегов");
}
/**
* @throws \Exception
*/
public function deactivate(): void
{
$this->menuService->removeItemBySlug("tag");
$this->menuService->removeItemBySlug("tag_settings");
OptionService::removeOptionByKey("entity_tag_list");
$this->migrationService->rollbackAtPath("{KERNEL_APP_MODULES}/tag/migrations");
}
public function formInputs(string $entity, Model $model = null): void
{
if (isset($model->id)) {
$value = TagEntityService::getTagsByEntity($entity, $model->id);
}
$input = SelectBuilder::build("tag[]", [
'class' => 'form-control',
'placeholder' => 'Теги',
'value' => $value ?? '',
'multiple' => "multiple",
'options' => Tag::getTagLabelByEntity($entity)
]);
$input->setLabel("Теги");
$input->create()->render();
}
public function saveInputs(string $entity, Model $model, Request $request): void
{
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
$tags = $request->post("tag");
if (is_array($tags)) {
foreach ($tags as $tag) {
$tagEntity = new TagEntity();
$tagEntity->entity = $entity;
$tagEntity->entity_id = $model->id;
$tagEntity->tag_id = $tag;
$tagEntity->save();
}
}
}
public function getItems(string $entity, Model $model): array|string
{
$tags = TagEntity::where("entity", $entity)->where("entity_id", $model->id)->with("tag")->get();
$tagsStr = "";
foreach ($tags as $tag) {
$tagsStr .= $tag->tag->label . ", ";
}
return substr($tagsStr, 0, -2);
}
public function getItem(string $entity, string $entity_id): string
{
$tags = TagEntity::where("entity", $entity)->where("entity_id", $entity_id)->get();
$tagsStr = "";
foreach ($tags as $tag) {
$tagsStr .= $tag->tag->label . ", ";
}
return substr($tagsStr, 0, -2);
}
public function deleteItems(string $entity, Model $model): void
{
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
}
}

View File

@ -1,120 +0,0 @@
<?php
namespace kernel\app_modules\tag\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\tag\models\forms\CreateTagForm;
use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\service\TagService;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\models\Option;
use kernel\modules\menu\service\MenuService;
use kernel\Request;
class TagController extends AdminController
{
private TagService $tagService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag/";
$this->tagService = new TagService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
$tagForm = new CreateTagForm();
$tagForm->load($_REQUEST);
if ($tagForm->validate()){
$tag = $this->tagService->create($tagForm);
if ($tag){
$this->redirect("/admin/tag/view/" . $tag->id);
}
}
$this->redirect("/admin/tag/create");
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$tag = Tag::find($id);
if (!$tag){
throw new Exception(message: "The tag not found");
}
$this->cgView->render("view.php", ['tag' => $tag]);
}
/**
* @throws Exception
*/
public function actionUpdate($id): void
{
$model = Tag::find($id);
if (!$model){
throw new Exception(message: "The tag not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$tag = Tag::find($id);
if (!$tag){
throw new Exception(message: "The tag not found");
}
$tagForm = new CreateTagForm();
$tagService = new TagService();
$tagForm->load($_REQUEST);
if ($tagForm->validate()) {
$tag = $tagService->update($tagForm, $tag);
if ($tag) {
$this->redirect("/admin/tag/view/" . $tag->id);
}
}
$this->redirect("/admin/tag/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
$post = Tag::find($id)->first();
$post->delete();
$this->redirect("/admin/tag/");
}
public function actionSettings(): void
{
$this->cgView->render('settingsForm.php');
}
#[NoReturn] public function actionSaveSettings(): void
{
$request = new Request();
$entities = $request->post('entity');
EntityRelation::configurationEntitiesByProperty($entities, 'tag');
Flash::setMessage("success", "Настройка прошла успешно");
$this->redirect("/admin/settings/tag", 302);
}
}

View File

@ -1,49 +0,0 @@
<?php
namespace kernel\app_modules\tag\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\tag\models\forms\CreateTagForm;
use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\models\TagEntity;
use kernel\app_modules\tag\service\TagEntityService;
use kernel\app_modules\tag\service\TagService;
use kernel\helpers\Debug;
use kernel\modules\menu\service\MenuService;
class TagEntityController extends AdminController
{
/**
* @return void
*/
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag_entity/";
}
/**
* @param $page_number
* @return void
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$tagEntity = TagEntity::find($id);
if (!$tagEntity){
throw new Exception(message: "The tag entity not found");
}
$this->cgView->render("view.php", ['tagEntity' => $tagEntity]);
}
}

View File

@ -1,33 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public string $migration;
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->schema->create('tag', function (Blueprint $table) {
$table->increments('id');
$table->string('label', 255)->nullable(false);
$table->string('entity', 255)->nullable(false);
$table->string('slug', 255)->unique();
$table->integer('status')->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->dropIfExists('tag');
}
};

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public string $migration;
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->schema->create('tag_entity', function (Blueprint $table) {
$table->increments('id');
$table->integer('tag_id')->nullable(false);
$table->string('entity', 255)->nullable(false);
$table->integer('entity_id')->nullable(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->dropIfExists('tag_entity');
}
};

View File

@ -1,61 +0,0 @@
<?php
namespace kernel\app_modules\tag\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $label
* @property string $entity
* @property int $entity_id
* @property string $slug
* @property int $status
*/
class Tag extends Model
{
const DISABLE_STATUS = 0;
const ACTIVE_STATUS = 1;
protected $table = 'tag';
protected $fillable = ['label', 'slug', 'status'];
public static function labels(): array
{
return [
'label' => 'Заголовок',
'entity' => 'Сущность',
'slug' => 'Slug',
'status' => 'Статус',
];
}
/**
* @return string[]
*/
public static function getStatus(): array
{
return [
self::DISABLE_STATUS => "Не активный",
self::ACTIVE_STATUS => "Активный",
];
}
public static function getTagListByEntity(string $entity): array
{
return self::where("entity", $entity)->get()->toArray();
}
public static function getTagLabelByEntity(string $entity): array
{
$result = [];
$tags = self::getTagListByEntity($entity);
foreach ($tags as $tag){
$result[$tag['id']] = $tag['label'];
}
return $result;
}
}

View File

@ -1,32 +0,0 @@
<?php
namespace kernel\app_modules\tag\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property int $tag_id
* @property string $entity
* @property int $entity_id
*/
class TagEntity extends Model
{
protected $table = 'tag_entity';
protected $fillable = ['tag_id', 'entity', 'entity_id'];
public static function labels(): array
{
return [
'tag_id' => 'тег',
'entity' => 'Сущность',
'entity_id' => 'Идентификатор сущности',
];
}
public function tag(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Tag::class);
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace kernel\app_modules\tag\models\forms;
use kernel\FormModel;
class CreateTagEntityForm extends FormModel
{
public function rules(): array
{
return [
'tag_id' => 'required',
'entity' => '',
'entity_id' => '',
];
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace kernel\app_modules\tag\models\forms;
use kernel\FormModel;
class CreateTagForm extends FormModel
{
public function rules(): array
{
return [
'label' => 'required|min-str-len:5|max-str-len:30',
'entity' => 'required',
'slug' => '',
'status' => ''
];
}
}

View File

@ -1,34 +0,0 @@
<?php
use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
App::$collector->group(["prefix" => "tag"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\tag\controllers\TagController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\tag\controllers\TagController::class, 'actionIndex']);
App::$collector->get('/create', [\app\modules\tag\controllers\TagController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\tag\controllers\TagController::class, 'actionAdd']);
App::$collector->get('/view/{id}', [\app\modules\tag\controllers\TagController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\modules\tag\controllers\TagController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\tag\controllers\TagController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\modules\tag\controllers\TagController::class, 'actionDelete']);
});
App::$collector->group(["prefix" => "tag_entity"], function (CGRouteCollector $router) {
App::$collector->get('/', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionIndex']);
App::$collector->get('/create', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionCreate']);
App::$collector->post("/", [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionAdd']);
App::$collector->get('view/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionView']);
App::$collector->any('/update/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\kernel\app_modules\tag\controllers\TagEntityController::class, 'actionDelete']);
});
App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) {
App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']);
App::$collector->post('/tag/update', [\app\modules\tag\controllers\TagController::class, 'actionSaveSettings']);
});
});
});

View File

@ -1,51 +0,0 @@
<?php
namespace kernel\app_modules\tag\service;
use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\models\TagEntity;
use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\helpers\Slug;
class TagEntityService
{
public function create(FormModel $form_model): false|TagEntity
{
$model = new TagEntity();
$model->tag_id = $form_model->getItem('tag_id');
$model->entity = $form_model->getItem('entity');
$model->entity_id = $form_model->getItem('entity_id');
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, TagEntity $tag): false|TagEntity
{
$tag->tag_id = $form_model->getItem('tag_id');
$tag->entity = $form_model->getItem('entity');
$tag->entity_id = $form_model->getItem('entity_id');
if ($tag->save()){
return $tag;
}
return false;
}
public static function getTagsByEntity(string $entity, int $entity_id): array
{
$tags= TagEntity::where("entity_id", $entity_id)->where("entity", $entity)->get();
$value = [];
foreach ($tags as $tag) {
$value[$tag->id] = $tag->tag->label;
}
return $value;
}
}

View File

@ -1,43 +0,0 @@
<?php
namespace kernel\app_modules\tag\service;
use kernel\app_modules\tag\models\Tag;
use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\helpers\Slug;
use kernel\services\ModuleService;
class TagService
{
public function create(FormModel $form_model): false|Tag
{
$model = new Tag();
$model->label = $form_model->getItem('label');
$model->entity = $form_model->getItem('entity');
$model->status = $form_model->getItem('status');
$model->slug = Slug::createSlug($form_model->getItem('label'), Tag::class);
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, Tag $tag): false|Tag
{
if ($tag->label !== $form_model->getItem('label')) {
$tag->slug = Slug::createSlug($form_model->getItem('label'), Tag::class);
}
$tag->label = $form_model->getItem('label');
$tag->entity = $form_model->getItem('entity');
$tag->status = $form_model->getItem('status');
if ($tag->save()){
return $tag;
}
return false;
}
}

View File

@ -1,58 +0,0 @@
<?php
/**
* @var Tag $model
*/
use kernel\app_modules\tag\models\Tag;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/tag/edit/" . $model->id : "/admin/tag");
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "label", params: [
'class' => "form-control",
'placeholder' => 'Заголовок',
'value' => $model->label ?? ''
])
->setLabel("Заголовок")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "entity", params: [
'class' => "form-control",
'value' => $model->entity ?? ''
])
->setLabel("Сущность")
->setOptions(\kernel\EntityRelation::getEntityList())
->render();
$form->field(\itguild\forms\inputs\Select::class, 'status', [
'class' => "form-control",
'value' => $model->status ?? ''
])
->setLabel("Статус")
->setOptions(Tag::getStatus())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -1,40 +0,0 @@
<?php
/**
* @var int $page_number
*/
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\tag\models\Tag;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\models\Menu;
use kernel\modules\menu\table\columns\MenuDeleteActionColumn;
use kernel\modules\menu\table\columns\MenuEditActionColumn;
use kernel\modules\menu\table\columns\MenuViewActionColumn;
$table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tag",
]));
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/tag/create")->fetch();
});
$table->columns([
"status" => [
"value" => function ($cell) {
return Tag::getStatus()[$cell];
}]
]);
\kernel\widgets\TagTabsWidget::create()->run();
$table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class);
$table->create();
$table->render();

View File

@ -1,47 +0,0 @@
<?php
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/admin/settings/tag/update");
//\kernel\helpers\Debug::dd($value);
?>
<div class="row">
<h5>Выберите сущности, к которым хотите прикрепить теги</h5>
</div>
<?php
$form->field(\itguild\forms\inputs\Select::class, "entity[]", [
'class' => "form-control",
'value' => \kernel\EntityRelation::getEntityByProperty('tag') ?? '',
'multiple' => "multiple",
])
->setLabel("Сущности")
->setOptions(\kernel\EntityRelation::getEntityList())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -1,30 +0,0 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $tag
*/
use kernel\modules\user\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($tag, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tag",
]));
$table->beforePrint(function () use ($tag) {
$btn = PrimaryBtn::create("Список", "/admin/tag")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/tag/update/" . $tag->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/tag/delete/" . $tag->id)->fetch();
return $btn;
});
$table->rows([
'status' => (function ($data) {
return \kernel\app_modules\tag\models\Tag::getStatus()[$data];
})
]);
$table->create();
$table->render();

View File

@ -1,34 +0,0 @@
<?php
/**
* @var int $page_number
*/
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\tag\models\Tag;
use kernel\IGTabel\btn\PrimaryBtn;
$table = new ListEloquentTable(new EloquentDataProvider(\kernel\app_modules\tag\models\TagEntity::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tag_entity",
]));
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/tag_entity/create")->fetch();
});
$table->columns([
"tag_id" => [
"value" => function ($data) {
return Tag::find($data)->label;
}]
]);
$table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class);
\kernel\widgets\TagTabsWidget::create()->run();
$table->create();
$table->render();

View File

@ -1,30 +0,0 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $tagEntity
*/
use kernel\modules\user\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($tagEntity, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tag",
]));
$table->beforePrint(function () use ($tagEntity) {
$btn = PrimaryBtn::create("Список", "/admin/tag_entity")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/tag_entity/update/" . $tagEntity->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/tag_entity/delete/" . $tagEntity->id)->fetch();
return $btn;
});
$table->rows([
'tag_id' => (function ($data) {
return \kernel\app_modules\tag\models\Tag::find($data)->label;
})
]);
$table->create();
$table->render();

View File

@ -3,6 +3,7 @@
namespace kernel\services;
use DirectoryIterator;
use GuzzleHttp\Exception\GuzzleException;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
@ -19,6 +20,12 @@ class ModuleService
protected null|bool $serverAvailable = null;
public ModuleShopService $moduleShopService;
public function __construct()
{
$this->moduleShopService = new ModuleShopService();
}
/**
* @param string $module
* @return false|array|string
@ -458,12 +465,13 @@ class ModuleService
return false;
}
/**
* @throws GuzzleException
*/
public function isLastVersion(string $slug): bool
{
if ($this->isServerAvailable()) {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$modules_info = $this->moduleShopService->getGroupedBySlugModules();
$mod_info = $this->getModuleInfoBySlug($slug);
@ -491,16 +499,18 @@ class ModuleService
return false;
}
/**
* @throws GuzzleException
*/
public function isShopModule(string $slug): bool
{
if ($this->isServerAvailable()) {
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = $this->moduleShopService->getGroupedBySlugModules();
if (!$this->issetModuleShopToken()) {
return false;
}
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
if (isset($modules_info)) {
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {

View File

@ -3,7 +3,11 @@
namespace kernel\services;
use GuzzleHttp\Exception\GuzzleException;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\RESTClient;
use kernel\Request;
class ModuleShopService
{
@ -17,20 +21,71 @@ class ModuleShopService
}
/**
* @param string $email
* @return mixed
* @throws GuzzleException
*/
public function email_auth(string $email)
public function email_auth(string $email): mixed
{
$request = RESTClient::post($this->url . "/api/secure/email_auth", ['email' => $email], false);
return json_decode($request->getBody()->getContents(), true);
}
public function code_check(string $code)
/**
* @param string $code
* @return mixed
* @throws GuzzleException
*/
public function code_check(string $code): mixed
{
$request = RESTClient::post($this->url . "/api/secure/code_check", ['code' => $code], false);
return json_decode($request->getBody()->getContents(), true);
}
/**
* @return mixed
* @throws GuzzleException
*/
public function getGroupedBySlugModules(): mixed
{
$modulesInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
return json_decode($modulesInfo->getBody()->getContents(), true);
}
/**
* @param string $slug
* @return void
* @throws GuzzleException
*/
public function installModule(string $slug): void
{
$moduleInfo = $this->getModuleInfoBySlug($slug);
$moduleInfo = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $moduleInfo['id']);
$moduleInfo = json_decode($moduleInfo->getBody()->getContents(), true);
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $moduleInfo['path_to_archive'], RESOURCES_DIR . "/tmp/modules");
(new ModuleService())->installModule('/resources/tmp/modules/' . basename($moduleInfo['path_to_archive']));
}
/**
* @param string $slug
* @return false|mixed
* @throws GuzzleException
*/
public function getModuleInfoBySlug(string $slug): mixed
{
$modulesInfo = $this->getGroupedBySlugModules();
foreach ($modulesInfo as $module) {
if ($module['slug'] === $slug) {
return $module;
}
}
return false;
}
}

View File

@ -3,6 +3,7 @@
namespace kernel\services;
use DirectoryIterator;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
@ -19,12 +20,15 @@ class ThemeService
protected ModuleService $moduleService;
protected ModuleShopService $moduleShopService;
public function __construct()
{
$this->option = new Option();
$this->findActiveTheme();
$this->moduleService = new ModuleService();
$this->moduleShopService = new ModuleShopService();
}
/**
@ -55,7 +59,12 @@ class ThemeService
return $this->active_theme;
}
public function setActiveTheme(string $theme): void
/**
* @param string $theme
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function setActiveTheme(string $theme): bool
{
$activeTheme = $this->option::where("key", "active_theme")->first();
@ -63,21 +72,25 @@ class ThemeService
if (isset($themeInfo['dependence'])) {
$dependence_array = explode(',', $themeInfo['dependence']);
foreach ($dependence_array as $depend) {
$this->moduleService->runInitScript($depend);
}
}
$currentThemeInfo = $this->getActiveThemeInfo();
if (isset($currentThemeInfo['dependence'])) {
$dependence_array = explode(',', $currentThemeInfo['dependence']);
foreach ($dependence_array as $depend) {
$this->moduleService->runDeactivateScript($depend);
if (!$this->moduleService->isInstall($depend)) {
if ($this->moduleService->isShopModule($depend)) {
$this->moduleShopService->installModule($depend);
} else {
Flash::setMessage("error", "Модуль не найден в IT Guild Framework Shop.");
return false;
}
} else {
if (!$this->moduleService->isActive($depend)) {
$this->moduleService->setActiveModule($depend);
}
}
}
}
$activeTheme->value = getConst($theme);
$activeTheme->save();
return true;
}
public function getActiveThemeInfo(): false|array|string

View File

@ -79,7 +79,5 @@ if ($moduleService->isActive('module_shop_client')) {
ModuleTabsWidget::create()->run();
}
$table->create();
$table->render();