Compare commits
No commits in common. "e29108f21dd97e0909e3935acf9dea34e4965f1b" and "9abfdd2fd9b4d7b2e068f99828c863dedd7ff973" have entirely different histories.
e29108f21d
...
9abfdd2fd9
@ -64,18 +64,4 @@ class ModuleController extends ConsoleController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionUpdateModule(): void
|
||||
{
|
||||
if (!isset($this->argv['path'])) {
|
||||
throw new \Exception('Missing module path "--path" specified');
|
||||
}
|
||||
if (file_exists(ROOT_DIR . $this->argv['path'])) {
|
||||
$moduleService = new ModuleService();
|
||||
$moduleService->updateModule($this->argv['path']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -25,6 +25,5 @@ App::$collector->group(["prefix" => "module"], callback: function (RouteCollecto
|
||||
App::$collector->console('install', [\kernel\console\controllers\ModuleController::class, 'actionInstallModule']);
|
||||
App::$collector->console('uninstall', [\kernel\console\controllers\ModuleController::class, 'actionUninstallModule']);
|
||||
App::$collector->console('pack', [\kernel\console\controllers\ModuleController::class, 'actionPackModule']);
|
||||
App::$collector->console('update', [\kernel\console\controllers\ModuleController::class, 'actionUpdateModule']);
|
||||
});
|
||||
|
||||
|
@ -172,9 +172,6 @@ class ModuleService
|
||||
// $out->out->r('Архив распакован', 'green');
|
||||
// } else {
|
||||
// $this->out->r('Message: Ошибка чтения архива', 'red');
|
||||
} else {
|
||||
$zip->close();
|
||||
throw new \Exception("unable to open zip archive");
|
||||
}
|
||||
|
||||
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
|
||||
@ -195,7 +192,6 @@ class ModuleService
|
||||
|
||||
// $this->out->r("Удаляем временные файлы", 'green');
|
||||
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
var_dump($tmpModuleDirFull);
|
||||
|
||||
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
|
||||
}
|
||||
@ -233,50 +229,7 @@ class ModuleService
|
||||
|
||||
// $this->out->r("Архив создан", 'green');
|
||||
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
|
||||
// $this->out->r("Временная папка удалена", 'green');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function updateModule(string $path): void
|
||||
{
|
||||
$zip = new ZipArchive;
|
||||
$tmpModuleDir = md5(time());
|
||||
$res = $zip->open(ROOT_DIR . $path);
|
||||
if ($res === TRUE) {
|
||||
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
|
||||
$zip->extractTo($tmpModuleDirFull);
|
||||
$zip->close();
|
||||
// $out->out->r('Архив распакован', 'green');
|
||||
// } else {
|
||||
// $this->out->r('Message: Ошибка чтения архива', 'red');
|
||||
} else {
|
||||
$zip->close();
|
||||
throw new \Exception("unable to open zip archive");
|
||||
}
|
||||
|
||||
if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
|
||||
throw new \Exception('manifest.json not found');
|
||||
}
|
||||
|
||||
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
|
||||
$manifest = Manifest::getWithVars($manifestJson);
|
||||
// $this->out->r('manifest.json инициализирован', 'green');
|
||||
|
||||
$fileHelper = new Files();
|
||||
if (isset($manifest['kernel_module_path'])) {
|
||||
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', $manifest['kernel_module_path']);
|
||||
} else {
|
||||
$fileHelper->copy_folder($tmpModuleDirFull . '/kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
|
||||
}
|
||||
|
||||
// $this->out->r("Удаляем временные файлы", 'green');
|
||||
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
|
||||
// $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
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",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("tag");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag\controllers;
|
||||
|
||||
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "Tags",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "tag",
|
||||
"description": "Tags module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"kernel_module_path": "{KERNEL_APP_MODULES}/{slug}",
|
||||
"module_class": "app\\modules\\tag\\TagModule",
|
||||
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
||||
"routs": "routs/tag.php"
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/tag/routs/tag.php";
|
@ -0,0 +1,101 @@
|
||||
<?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\services\TagService;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagController extends AdminController
|
||||
{
|
||||
private TagService $tagService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/";
|
||||
$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/" . $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/" . $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/");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends 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');
|
||||
}
|
||||
};
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $label
|
||||
* @property string $entity
|
||||
* @property string $slug
|
||||
* @property int $status
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'tag';
|
||||
|
||||
protected $fillable = ['label', 'entity', '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 => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?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|min-str-len:1|max-str-len:50',
|
||||
'slug' => '',
|
||||
'status' => ''
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $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('/{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']);
|
||||
});
|
||||
});
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\services;
|
||||
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Slug;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?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\TextInput::class, name: "entity", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Сущность',
|
||||
'value' => $model->entity ?? ''
|
||||
])
|
||||
->setLabel("Сущность")
|
||||
->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();
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @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();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return Tag::getStatus()[$cell];
|
||||
}]
|
||||
]);
|
||||
|
||||
$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();
|
@ -0,0 +1,30 @@
|
||||
<?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();
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
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",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("tag");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag\controllers;
|
||||
|
||||
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "Tags",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "tag",
|
||||
"description": "Tags module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"kernel_module_path": "{KERNEL_APP_MODULES}/{slug}",
|
||||
"module_class": "app\\modules\\tag\\TagModule",
|
||||
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
||||
"routs": "routs/tag.php"
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/tag/routs/tag.php";
|
@ -0,0 +1,101 @@
|
||||
<?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\services\TagService;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagController extends AdminController
|
||||
{
|
||||
private TagService $tagService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/";
|
||||
$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/" . $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/" . $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/");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends 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');
|
||||
}
|
||||
};
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $label
|
||||
* @property string $entity
|
||||
* @property string $slug
|
||||
* @property int $status
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'tag';
|
||||
|
||||
protected $fillable = ['label', 'entity', '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 => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?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|min-str-len:1|max-str-len:50',
|
||||
'slug' => '',
|
||||
'status' => ''
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $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('/{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']);
|
||||
});
|
||||
});
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\services;
|
||||
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Slug;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?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\TextInput::class, name: "entity", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Сущность',
|
||||
'value' => $model->entity ?? ''
|
||||
])
|
||||
->setLabel("Сущность")
|
||||
->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();
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @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();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return Tag::getStatus()[$cell];
|
||||
}]
|
||||
]);
|
||||
|
||||
$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();
|
@ -0,0 +1,30 @@
|
||||
<?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();
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
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",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("tag");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag\controllers;
|
||||
|
||||
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "Tags",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "tag",
|
||||
"description": "Tags module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"kernel_module_path": "{KERNEL_APP_MODULES}/{slug}",
|
||||
"module_class": "app\\modules\\tag\\TagModule",
|
||||
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
||||
"routs": "routs/tag.php"
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/tag/routs/tag.php";
|
@ -0,0 +1,101 @@
|
||||
<?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\services\TagService;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagController extends AdminController
|
||||
{
|
||||
private TagService $tagService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/";
|
||||
$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/" . $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/" . $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/");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends 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');
|
||||
}
|
||||
};
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $label
|
||||
* @property string $entity
|
||||
* @property string $slug
|
||||
* @property int $status
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'tag';
|
||||
|
||||
protected $fillable = ['label', 'entity', '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 => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?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|min-str-len:1|max-str-len:50',
|
||||
'slug' => '',
|
||||
'status' => ''
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $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('/{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']);
|
||||
});
|
||||
});
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\services;
|
||||
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Slug;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?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\TextInput::class, name: "entity", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Сущность',
|
||||
'value' => $model->entity ?? ''
|
||||
])
|
||||
->setLabel("Сущность")
|
||||
->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();
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @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();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return Tag::getStatus()[$cell];
|
||||
}]
|
||||
]);
|
||||
|
||||
$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();
|
@ -0,0 +1,30 @@
|
||||
<?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();
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
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",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("tag");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag\controllers;
|
||||
|
||||
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "Tags",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "tag",
|
||||
"description": "Tags module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"kernel_module_path": "{KERNEL_APP_MODULES}/{slug}",
|
||||
"module_class": "app\\modules\\tag\\TagModule",
|
||||
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
||||
"routs": "routs/tag.php"
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/tag/routs/tag.php";
|
@ -0,0 +1,101 @@
|
||||
<?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\services\TagService;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagController extends AdminController
|
||||
{
|
||||
private TagService $tagService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/";
|
||||
$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/" . $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/" . $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/");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends 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');
|
||||
}
|
||||
};
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $label
|
||||
* @property string $entity
|
||||
* @property string $slug
|
||||
* @property int $status
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'tag';
|
||||
|
||||
protected $fillable = ['label', 'entity', '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 => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?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|min-str-len:1|max-str-len:50',
|
||||
'slug' => '',
|
||||
'status' => ''
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $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('/{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']);
|
||||
});
|
||||
});
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\services;
|
||||
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Slug;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?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\TextInput::class, name: "entity", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Сущность',
|
||||
'value' => $model->entity ?? ''
|
||||
])
|
||||
->setLabel("Сущность")
|
||||
->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();
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @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();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return Tag::getStatus()[$cell];
|
||||
}]
|
||||
]);
|
||||
|
||||
$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();
|
@ -0,0 +1,30 @@
|
||||
<?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();
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
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",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("tag");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\tag\controllers;
|
||||
|
||||
class TagController extends \kernel\app_modules\tag\controllers\TagController
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "Tags",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "tag",
|
||||
"description": "Tags module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"kernel_module_path": "{KERNEL_APP_MODULES}/{slug}",
|
||||
"module_class": "app\\modules\\tag\\TagModule",
|
||||
"module_class_file": "{APP}/modules/tag/TagModule.php",
|
||||
"routs": "routs/tag.php"
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/tag/routs/tag.php";
|
@ -0,0 +1,101 @@
|
||||
<?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\services\TagService;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagController extends AdminController
|
||||
{
|
||||
private TagService $tagService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/";
|
||||
$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/" . $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/" . $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/");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends 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');
|
||||
}
|
||||
};
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $label
|
||||
* @property string $entity
|
||||
* @property string $slug
|
||||
* @property int $status
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'tag';
|
||||
|
||||
protected $fillable = ['label', 'entity', '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 => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?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|min-str-len:1|max-str-len:50',
|
||||
'slug' => '',
|
||||
'status' => ''
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $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('/{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']);
|
||||
});
|
||||
});
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\tag\services;
|
||||
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Slug;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?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\TextInput::class, name: "entity", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Сущность',
|
||||
'value' => $model->entity ?? ''
|
||||
])
|
||||
->setLabel("Сущность")
|
||||
->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();
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @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();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return Tag::getStatus()[$cell];
|
||||
}]
|
||||
]);
|
||||
|
||||
$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();
|
@ -0,0 +1,30 @@
|
||||
<?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();
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user