Compare commits

...

10 Commits

Author SHA1 Message Date
6c124e9bb7 slider 2024-12-03 16:16:36 +03:00
aff394ae72 some fix 2024-12-03 12:13:18 +03:00
c0bedcb5ee add additional property to user 2024-12-03 10:51:22 +03:00
159b3933fb some fix 2024-12-02 16:50:44 +03:00
13978449a2 is server available 2024-12-02 16:38:05 +03:00
a1bed2d9f2 photo settings 2024-12-02 16:25:28 +03:00
567ab8544d tag settings 2024-12-02 14:15:01 +03:00
b981ff0c44 settings for tag 2024-11-29 16:54:35 +03:00
0ed97877fd fix photo module 2024-11-29 14:07:55 +03:00
3ef1e7d7e0 photo module 2024-11-29 13:23:48 +03:00
39 changed files with 1025 additions and 93 deletions

View File

@ -0,0 +1,115 @@
<?php
namespace app\modules\photo;
use Cassandra\Decimal;
use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\FileBuilder;
use kernel\app_modules\photo\models\Photo;
use kernel\app_modules\photo\services\PhotoService;
use kernel\app_modules\tag\services\TagEntityService;
use kernel\FileUpload;
use kernel\helpers\Debug;
use kernel\helpers\Html;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\Request;
use kernel\services\MigrationService;
class PhotoModule 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}/photo/migrations");
$this->menuService->createItem([
"label" => "Фото",
"url" => "/admin/photo",
"slug" => "photo",
]);
$this->menuService->createItem([
"label" => "Фото",
"url" => "/admin/settings/photo",
"slug" => "photo_settings",
"parent_slug" => "settings"
]);
}
public function deactivate(): void
{
$this->menuService->removeItemBySlug("photo");
$this->menuService->removeItemBySlug("photo_settings");
}
public function formInputs(string $entity, Model $model = null): void
{
if (isset($model->id)) {
$value = PhotoService::getByEntity($entity, $model->id);
echo Html::img($value, ['width' => '200px']);
}
$input = FileBuilder::build("image", [
'class' => 'form-control',
'value' => $value ?? '',
]);
$input->setLabel("Фото");
$input->create()->render();
}
public function saveInputs(string $entity, Model $model, Request $request): void
{
Photo::where("entity", $entity)->where("entity_id", $model->id)->delete();
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$file = new FileUpload($_FILES['image'], ['jpg', 'jpeg', 'png']);
$file->upload();
$image = $file->getUploadFile();
$photo = new Photo();
$photo->entity = $entity;
$photo->entity_id = $model->id;
$photo->image = $image;
$photo->save();
}
}
public function getItems(string $entity, Model $model): array|string
{
$photos = Photo::where("entity", $entity)->where("entity_id", $model->id)->get();
$photoStr = "";
foreach ($photos as $photo) {
$photoStr .= "<img src='$photo->image' width='150px'>" . " ";
}
return substr($photoStr, 0, -1);
}
public function getItem(string $entity, string $entity_id): string
{
$photos = Photo::where("entity", $entity)->where("entity_id", $entity_id)->get();
$photoStr = "";
foreach ($photos as $photo) {
$photoStr .= "<img src='$photo->image' width='150px'>" . " ";
}
return substr($photoStr, 0, -1);
}
public function deleteItems(string $entity, Model $model): void
{
Photo::where("entity", $entity)->where("entity_id", $model->id)->delete();
}
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,38 @@
<?php
namespace app\modules\slider;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\services\MigrationService;
class SliderModule 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}/slider/migrations");
$this->menuService->createItem([
"label" => "Слайдер",
"url" => "/admin/slider",
"slug" => "slider",
]);
}
public function deactivate(): void
{
$this->menuService->removeItemBySlug("slider");
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace app\modules\slider\controllers;
use app\modules\slider\models\Slider;
use Exception;
use kernel\AdminController;
class SliderController extends AdminController
{
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/photo/views/";
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$slide = Slider::find($id);
if (!$slide){
throw new Exception(message: "The slide not found");
}
$this->cgView->render("view.php", ['slider' => $slide]);
}
}

View File

@ -0,0 +1,13 @@
{
"name": "Slider",
"version": "0.1",
"author": "ITGuild",
"slug": "slider",
"type": "entity",
"description": "Slider module",
"app_module_path": "{APP}/modules/{slug}",
"module_class": "app\\modules\\slider\\SliderModule",
"module_class_file": "{APP}/modules/slider/SliderModule.php",
"routs": "routs/slider.php",
"dependence": "menu, user"
}

View File

@ -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('slider', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 255)->nullable(false);
$table->string('additional_information', 255)->nullable(false);
$table->string('content', 255)->nullable(false);
$table->integer('status')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->dropIfExists('slider');
}
};

View File

@ -0,0 +1,39 @@
<?php
namespace app\modules\slider\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $title
* @property string $additionalInformation
* @property string $content
*/
class Slider extends Model
{
const int DISABLE_STATUS = 0;
const int ACTIVE_STATUS = 1;
protected $table = "slider";
protected $fillable = ['title', 'additional_information', 'content'];
public static function labels()
{
return [
'title' => 'Заголовок',
'additional_information' => 'Дополнительная информация',
'content' => 'Контент'
];
}
public static function getStatus(): array
{
return [
self::DISABLE_STATUS => "Не активный",
self::ACTIVE_STATUS => "Активный",
];
}
}

View File

@ -0,0 +1,20 @@
<?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" => "slider"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\slider\controllers\SliderController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\slider\controllers\SliderController::class, 'actionIndex']);
App::$collector->get('/create', [\app\modules\slider\controllers\SliderController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\slider\controllers\SliderController::class, 'actionAdd']);
App::$collector->get('/view/{id}', [\app\modules\slider\controllers\SliderController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\modules\slider\controllers\SliderController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\slider\controllers\SliderController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\modules\slider\controllers\SliderController::class]);
});
});
});

View File

@ -0,0 +1,43 @@
<?php
/**
* @var int $page_number
*/
use app\modules\slider\models\Slider;
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\photo\models\Photo;
use kernel\widgets\IconBtn\IconBtnCreateWidget;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnViewWidget;
$table = new ListEloquentTable(new EloquentDataProvider(Slider::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/photo",
]));
$table->beforePrint(function () {
return IconBtnCreateWidget::create(['url' => '/admin/slider/create'])->run();
});
$table->columns([
"status" => [
"value" => function ($cell) {
return Slider::getStatus()[$cell];
}]
]);
$table->addAction(function($row) {
return IconBtnViewWidget::create(['url' => '/admin/slider/view/' . $row['id']])->run();
});
$table->addAction(function($row) {
return IconBtnEditWidget::create(['url' => '/admin/slider/update/' . $row['id']])->run();
});
$table->addAction(function($row) {
return IconBtnDeleteWidget::create(['url' => '/admin/slider/delete/' . $row['id']])->run();
});
$table->create();
$table->render();

View File

@ -0,0 +1,29 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $slider
*/
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($slider, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/slider",
]));
$table->beforePrint(function () use ($slider) {
$btn = PrimaryBtn::create("Список", "/admin/slider")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/slider/update/" . $slider->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/slider/delete/" . $slider->id)->fetch();
return $btn;
});
$table->rows([
'status' => (function ($data) {
return \app\modules\slider\models\Slider::getStatus()[$data];
})
]);
$table->create();
$table->render();

View File

@ -65,6 +65,7 @@ class TagModule extends Module
if (isset($model->id)) {
$value = TagEntityService::getTagsByEntity($entity, $model->id);
}
$input = SelectBuilder::build("tag[]", [
'class' => 'form-control',
'placeholder' => 'Теги',
@ -81,12 +82,14 @@ class TagModule extends Module
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
$tags = $request->post("tag");
foreach ($tags as $tag) {
$tagEntity = new TagEntity();
$tagEntity->entity = $entity;
$tagEntity->entity_id = $model->id;
$tagEntity->tag_id = $tag;
$tagEntity->save();
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();
}
}
}

View File

@ -25,7 +25,7 @@ class EntityRelation
$activeModules = $moduleService->getActiveModules();
foreach ($activeModules as $module) {
if (isset($module['type']) and $module['type'] === "entity") {
$list[$module['slug']] = $module['name'];
$list[$module['slug']] = $module['slug'];
}
}
@ -45,7 +45,7 @@ class EntityRelation
return $list;
}
public function getEntitiesRelations(): array|bool
public static function getEntitiesRelations(): array|bool
{
$entity_relations = OptionService::getItem("entity_relations");
if ($entity_relations) {
@ -61,8 +61,13 @@ class EntityRelation
if ($entity_relations_info) {
$entity_relations = json_decode($entity_relations_info->value, true);
if ($entity_relations[$entity]) {
if ($entity_relations[$entity][$property]) {
unset($entity_relations[$entity][$property]);
$propertyKey = array_search($property, $entity_relations[$entity]);
if ($entity_relations[$entity][$propertyKey] === $property) {
unset($entity_relations[$entity][$propertyKey]);
$entity_relations[$entity] = array_values($entity_relations[$entity]);
if (empty($entity_relations[$entity])) {
unset($entity_relations[$entity]);
}
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
$entity_relations_info->save();
return true;
@ -118,10 +123,10 @@ class EntityRelation
}
}
return false;
return [];
}
public function addEntityRelation(string $entity, string $property): bool
public static function addEntityRelation(string $entity, string $property): bool
{
$entity_relations_info = Option::where("key", "entity_relations")->first();
if ($entity_relations_info) {
@ -129,7 +134,7 @@ class EntityRelation
if (isset($entity_relations[$entity])) {
$entity_relations[$entity][] = $property;
} else {
$entity_relations[$entity] = $property;
$entity_relations[$entity][] = $property;
}
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
$entity_relations_info->save();
@ -231,4 +236,44 @@ class EntityRelation
}
}
}
public static function getEntityByProperty(string $data): array
{
$entityRelations = self::getEntitiesRelations();
$entities = [];
foreach ($entityRelations as $entity => $property) {
if (in_array($data, $property)) {
$entities[] = $entity;
}
}
return $entities;
}
public static function configurationEntitiesByProperty(array|null $entities, string $property): void
{
$entityRelations = self::getEntitiesRelations();
if (isset($entities)) {
foreach ($entities as $entity) {
if (!isset($entityRelations[$entity])) {
EntityRelation::addEntityRelation($entity, $property);
}
}
foreach ($entityRelations as $entity => $additionalProperty) {
if (in_array($entity, $entities)) {
if (!in_array($property, $additionalProperty)) {
EntityRelation::addEntityRelation($entity, $property);
}
} else {
if (in_array($property, $additionalProperty)) {
EntityRelation::removePropertyFromEntityRelations($entity, $property);
}
}
}
} else {
foreach ($entityRelations as $entity => $additionalProperty) {
EntityRelation::removePropertyFromEntityRelations($entity, $property);
}
}
}
}

View File

@ -59,6 +59,16 @@ class FormModel
return false;
}
public function validateForUpdate(): bool
{
$res = $this->validator->validate($this->data, $this->rulesForUpdate());
if (!$res) {
return true;
}
return false;
}
public function getErrors(): array
{
return $this->validator->getProcessedErrors();

View File

@ -0,0 +1,77 @@
<?php
namespace kernel\app_modules\photo\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\photo\models\form\CreatePhotoForm;
use kernel\app_modules\photo\models\Photo;
use kernel\app_modules\photo\services\PhotoService;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\Request;
class PhotoController extends AdminController
{
private PhotoService $photoService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/photo/views/";
$this->photoService = new PhotoService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
$photoForm = new CreatePhotoForm();
$photoForm->load($_REQUEST);
if ($photoForm->validate()){
$photo = $this->photoService->create($photoForm);
if ($photo){
$this->redirect("/admin/photo/view/" . $photo->id);
}
}
$this->redirect("/admin/photo/create");
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$photo = Photo::find($id);
if (!$photo){
throw new Exception(message: "The photo not found");
}
$this->cgView->render("view.php", ['photo' => $photo]);
}
public function actionSettings(): void
{
$this->cgView->render('settingsForm.php');
}
#[NoReturn] public function actionSaveSettings(): void
{
$request = new Request();
$entities = $request->post('entity');
EntityRelation::configurationEntitiesByProperty($entities, 'photo');
Flash::setMessage("success", "Настройка прошла успешно");
$this->redirect("/admin/settings/photo", 302);
}
}

View File

@ -0,0 +1,30 @@
<?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('photo', function (Blueprint $table) {
$table->increments('id');
$table->string('image', 255)->nullable(false);
$table->string('entity', 255)->nullable(false);
$table->integer('entity_id')->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->dropIfExists('photo');
}
};

View File

@ -0,0 +1,44 @@
<?php
namespace kernel\app_modules\photo\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $image
* @property string $entity
* @property int $entity_id
*/
class Photo extends Model
{
protected $table = 'photo';
protected $fillable = ['image', 'entity', 'entity_id'];
public static function labels(): array
{
return [
'image' => 'Фото',
'entity' => 'Сущность',
'entity_id' => 'Идентификатор сущности',
];
}
public static function getPhotoListByEntity(string $entity): array
{
return self::where("entity", $entity)->get()->toArray();
}
public static function getPhotoByEntity(string $entity): array
{
$result = [];
$photos = self::getPhotoListByEntity($entity);
foreach ($photos as $photo) {
$result[$photo['id']] = $photo['label'];
}
return $result;
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace kernel\app_modules\photo\models\form;
use kernel\FormModel;
class CreatePhotoForm extends FormModel
{
public function rules(): array
{
return [
'image' => 'required',
'entity' => 'required|string',
'entity_id' => 'required|integer|min:1',
];
}
}

View File

@ -0,0 +1,24 @@
<?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" => "photo"], function (CGRouteCollector $router) {
App::$collector->get('/', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionIndex']);
App::$collector->get('/create', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionCreate']);
App::$collector->post("/", [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionAdd']);
App::$collector->get('/view/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionView']);
App::$collector->any('/update/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class]);
});
App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) {
App::$collector->get('/photo', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionSettings']);
App::$collector->post('/photo/update', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionSaveSettings']);
});
});
});

View File

@ -0,0 +1,40 @@
<?php
namespace kernel\app_modules\photo\services;
use kernel\app_modules\photo\models\Photo;
use kernel\FormModel;
use kernel\helpers\Debug;
class PhotoService
{
public function create(FormModel $form_model): false|Photo
{
$model = new Photo();
$model->image = $form_model->getItem('image');
$model->entity = $form_model->getItem('entity');
$model->entity_id = $form_model->getItem('entity_id');
return false;
}
public function update(FormModel $form_model, Photo $photo): false|Photo
{
$photo->image = $form_model->getItem('image');
$photo->entity = $form_model->getItem('entity');
$photo->entity_id = $form_model->getItem('entity_id');
if ($photo->save()){
return $photo;
}
return false;
}
public static function getByEntity(string $entity, int $entity_id): string
{
$photo = Photo::where("entity", $entity)->where("entity_id", $entity_id)->first();
return $photo->image ?? "";
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @var int $page_number
*/
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\photo\models\Photo;
use kernel\widgets\IconBtn\IconBtnCreateWidget;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnViewWidget;
$table = new ListEloquentTable(new EloquentDataProvider(Photo::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/photo",
]));
//$table->beforePrint(function () {
// return IconBtnCreateWidget::create(['url' => '/admin/photo/create'])->run();
//});
$table->columns([
'image' => function ($data) {
return $data ? "<img src='$data' width='150px'>" : "";
}
]);
$table->addAction(function($row) {
return IconBtnViewWidget::create(['url' => '/admin/photo/view/' . $row['id']])->run();
});
//$table->addAction(function($row) {
// return IconBtnEditWidget::create(['url' => '/admin/photo/update/' . $row['id']])->run();
//});
//$table->addAction(function($row) {
// return IconBtnDeleteWidget::create(['url' => '/admin/photo/delete/' . $row['id']])->run();
//});
$table->create();
$table->render();

View File

@ -0,0 +1,46 @@
<?php
$form = new \itguild\forms\ActiveForm();
$form->beginForm("/admin/settings/photo/update");
?>
<div class="row">
<h5>Выберите сущности, к которым хотите прикрепить фото</h5>
</div>
<?php
$form->field(\itguild\forms\inputs\Select::class, "entity[]", [
'class' => "form-control",
'value' => \kernel\EntityRelation::getEntityByProperty('photo') ?? '',
'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

@ -0,0 +1,34 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $photo
*/
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;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnListWidget;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($photo, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/photo",
]));
$table->beforePrint(function () use ($photo) {
$btn = IconBtnListWidget::create(['url' => '/admin/photo'])->run();
// $btn .= IconBtnEditWidget::create(['url' => '/admin/photo/update/' . $photo->id])->run();
// $btn .= IconBtnDeleteWidget::create(['url' => '/admin/photo/delete/' . $photo->id])->run();
return $btn;
});
$table->rows([
'image' => function ($data) {
return $data ? "<img src='$data' width='300px'>" : "";
}
]);
$table->create();
$table->render();

View File

@ -8,8 +8,12 @@ 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\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
{
@ -100,7 +104,17 @@ class TagController extends AdminController
public function actionSettings(): void
{
$this->cgView->render('form.php');
$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

@ -15,32 +15,19 @@ use kernel\modules\menu\service\MenuService;
class TagEntityController extends AdminController
{
private TagEntityService $tagEntityService;
/**
* @return void
*/
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag_entity/";
$this->tagEntityService = new TagEntityService();
}
// 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->tagEntityService->create($tagForm);
// if ($tag){
// $this->redirect("/admin/tag/" . $tag->id);
// }
// }
// $this->redirect("/admin/tag/create");
// }
/**
* @param $page_number
* @return void
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
@ -59,45 +46,4 @@ class TagEntityController extends AdminController
$this->cgView->render("view.php", ['tagEntity' => $tagEntity]);
}
// /**
// * @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/");
// }
}

View File

@ -28,6 +28,7 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
});
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,6 +1,5 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $menuItem
* @var int $page_number
*/
@ -22,7 +21,6 @@ $table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/tag/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
});
$table->columns([

View File

@ -0,0 +1,47 @@
<?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();

30
kernel/helpers/Html.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace kernel\helpers;
class Html
{
public static function img(string $src, array $params = [])
{
$paramsStr = self::createParams($params);
return "<img src='$src' $paramsStr>";
}
/**
* @param array $data
* @return string
*/
public static function createParams(array $data = []): string
{
$paramsString = "";
foreach($data as $key => $param){
if(is_string($param)){
$paramsString .= $key . "='" . $param . "'";
}
}
return $paramsString;
}
}

View File

@ -6,7 +6,7 @@
use kernel\modules\post\models\Post;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post");
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post", 'multipart/form-data');
$form->field(\itguild\forms\inputs\TextInput::class, 'title', [
'class' => "form-control",

View File

@ -5,9 +5,11 @@ namespace kernel\modules\user\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\EntityRelation;
use kernel\modules\user\models\forms\CreateUserForm;
use kernel\modules\user\models\User;
use kernel\modules\user\service\UserService;
use kernel\Request;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
@ -35,6 +37,11 @@ class UserController extends AdminController
$userForm->load($_REQUEST);
if ($userForm->validate()){
$user = $this->userService->create($userForm);
$entityRelation = new EntityRelation();
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
if ($user){
$this->redirect("/admin/user/view/" . $user->id);
}
@ -91,8 +98,12 @@ class UserController extends AdminController
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
if ($userForm->validateForUpdate()){
$user = $userService->update($userForm, $user);
$entityRelation = new EntityRelation();
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
if ($user){
$this->redirect("/admin/user/view/" . $user->id);
}
@ -100,9 +111,20 @@ class UserController extends AdminController
$this->redirect("/admin/user/update/" . $id);
}
/**
* @throws Exception
*/
#[NoReturn] public function actionDelete($id): void
{
User::find($id)->delete();
$user = User::find($id)->first();
if (!$user){
throw new Exception(message: "The user not found");
}
$entityRelation = new EntityRelation();
$entityRelation->deleteEntityRelation(entity: "user", model: $user);
$user->delete();
$this->redirect("/admin/user/");
}

View File

@ -16,4 +16,13 @@ class CreateUserForm extends FormModel
];
}
public function rulesForUpdate(): array
{
return [
'username' => 'required|min-str-len:5|max-str-len:30',
'password' => '',
'email' => 'required|email'
];
}
}

View File

@ -3,6 +3,7 @@
namespace kernel\modules\user\service;
use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\modules\user\models\User;
class UserService
@ -25,7 +26,9 @@ class UserService
{
$user->username = $form_model->getItem('username');
$user->email = $form_model->getItem('email');
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
if ($form_model->getItem('password')) {
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
}
if ($user->save()){
return $user;
}

View File

@ -6,7 +6,7 @@
use kernel\modules\user\models\User;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user");
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user", enctype: 'multipart/form-data');
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [
'class' => "form-control",
@ -32,6 +32,11 @@ $form->field(class: \itguild\forms\inputs\TextInput::class, name: "email", param
->setLabel("Email")
->render();
$entityRelations = new \kernel\EntityRelation();
if (!isset($model)) {
$model = new User();
}
$entityRelations->renderEntityAdditionalPropertyFormBySlug("user", $model);
?>
<div class="row">
<div class="col-sm-2">

View File

@ -24,6 +24,16 @@ $table = new ListEloquentTable(new EloquentDataProvider(User::class, [
'baseUrl' => "/admin/user",
'filters' => ['email'],
]));
$entityRelation = new \kernel\EntityRelation();
$additionals = $entityRelation->getEntityRelationsBySlug("user");
foreach ($additionals as $additional) {
$table->addColumn($additional, $additional, function ($id) use ($entityRelation, $additional) {
return $entityRelation->getAdditionalPropertyByEntityId("user", $id, $additional);
});
}
$table->columns([
'username' => [
"filter" => [

View File

@ -25,6 +25,15 @@ $table->beforePrint(function () use ($user) {
return $btn;
});
$entityRelation = new \kernel\EntityRelation();
$additionals = $entityRelation->getEntityAdditionalProperty("user", $user);
foreach ($additionals as $key => $additional) {
$table->addRow($key, function () use ($additional) {
return $additional;
}, ['after' => 'email']);
}
$table->rows([
'created_at' => function ($data) {
if (!$data){

View File

@ -4,17 +4,21 @@ namespace kernel\services;
use DirectoryIterator;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option;
use MongoDB\Driver\Session;
use ZipArchive;
class ModuleService
{
protected array $errors = [];
protected null|bool $serverAvailable = null;
/**
* @param string $module
* @return false|array|string
@ -359,8 +363,6 @@ class ModuleService
mkdir(RESOURCES_DIR . '/tmp/modules', 0777, true);
}
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.igm');
//$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
}
/**
@ -442,17 +444,19 @@ class ModuleService
public function isLastVersion(string $slug): bool
{
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
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);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
return true;
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
return true;
}
}
}
return false;
return false;
}
public function isKernelModule(string $slug): bool
@ -467,6 +471,23 @@ class ModuleService
return false;
}
public function isShopModule(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);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug']) {
return true;
}
}
}
return false;
}
public function getKernelModules(): array
{
$modules_info = [];
@ -478,4 +499,21 @@ class ModuleService
return $modules_info;
}
public function isServerAvailable(): bool
{
if (null !== $this->serverAvailable){
return $this->serverAvailable;
}
try {
RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$this->serverAvailable = true;
return true;
} catch (\Exception $e) {
$this->serverAvailable = false;
return false;
}
}
}

View File

@ -7,6 +7,7 @@
* @var \kernel\services\ModuleService $moduleService
*/
use kernel\Flash;
use kernel\widgets\IconBtn\IconBtnActivateWidget;
use kernel\widgets\IconBtn\IconBtnDeactivateWidget;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
@ -53,7 +54,7 @@ $table->addAction(function ($row, $url) use ($moduleService) {
});
$table->addAction(function ($row, $url) use ($moduleService){
if (!$moduleService->isKernelModule($row['slug'])){
if ($moduleService->isShopModule($row['slug'])){
if (!$moduleService->isLastVersion($row['slug'])) {
$url = "$url/update/?slug=". $row['slug'];
@ -74,7 +75,7 @@ $table->addAction(function ($row) use ($moduleService){
return false;
});
if ($moduleService->isActive('module_shop_client')) {
if ($moduleService->isActive('module_shop_client') && $moduleService->isServerAvailable()) {
ModuleTabsWidget::create()->run();
}