photo module

This commit is contained in:
Билай Станислав 2024-11-29 13:23:48 +03:00
parent e7a20d9b97
commit 3ef1e7d7e0
17 changed files with 464 additions and 71 deletions

View File

@ -0,0 +1,98 @@
<?php
namespace app\modules\photo;
use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\FileBuilder;
use kernel\app_modules\photo\models\Photo;
use kernel\FileUpload;
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",
]);
}
public function deactivate(): void
{
$this->menuService->removeItemBySlug("photo");
}
public function formInputs(string $entity, Model $model = null): void
{
$input = FileBuilder::build("image", [
'class' => 'form-control',
'value' => $model->image ?? '',
]);
$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

@ -81,6 +81,7 @@ class TagModule extends Module
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
$tags = $request->post("tag"); $tags = $request->post("tag");
if (is_array($tags)) {
foreach ($tags as $tag) { foreach ($tags as $tag) {
$tagEntity = new TagEntity(); $tagEntity = new TagEntity();
$tagEntity->entity = $entity; $tagEntity->entity = $entity;
@ -89,6 +90,7 @@ class TagModule extends Module
$tagEntity->save(); $tagEntity->save();
} }
} }
}
public function getItems(string $entity, Model $model): array|string public function getItems(string $entity, Model $model): array|string

View File

@ -0,0 +1,104 @@
<?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\helpers\Debug;
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]);
}
// /**
// * @throws Exception
// */
// public function actionUpdate($id): void
// {
// $model = Tag::find($id);
// if (!$model){
// throw new Exception(message: "The tag not found");
// }
//
// $this->cgView->render("form.php", ['model' => $model]);
// }
//
// /**
// * @throws Exception
// */
// public function actionEdit($id): void
// {
// $tag = Tag::find($id);
// if (!$tag){
// throw new Exception(message: "The tag not found");
// }
// $tagForm = new CreateTagForm();
// $tagService = new TagService();
// $tagForm->load($_REQUEST);
// if ($tagForm->validate()) {
// $tag = $tagService->update($tagForm, $tag);
// if ($tag) {
// $this->redirect("/admin/tag/view/" . $tag->id);
// }
// }
// $this->redirect("/admin/tag/update/" . $id);
// }
//
// #[NoReturn] public function actionDelete($id): void
// {
// $post = Tag::find($id)->first();
// $post->delete();
// $this->redirect("/admin/tag/");
// }
//
// public function actionSettings(): void
// {
// $this->cgView->render('form.php');
// }
}

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,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" => "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]);
});
});
});

View File

@ -0,0 +1,34 @@
<?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;
}
}

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,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

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

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

View File

@ -6,7 +6,7 @@
use kernel\modules\post\models\Post; use kernel\modules\post\models\Post;
$form = new \itguild\forms\ActiveForm(); $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', [ $form->field(\itguild\forms\inputs\TextInput::class, 'title', [
'class' => "form-control", 'class' => "form-control",

View File

@ -451,8 +451,8 @@ class ModuleService
return true; return true;
} }
} }
return false;
return false;
} }
public function isKernelModule(string $slug): bool public function isKernelModule(string $slug): bool