From 3ef1e7d7e08a2b416d7ace915de2ed01dd873f77 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 29 Nov 2024 13:23:48 +0300 Subject: [PATCH] photo module --- app/modules/photo/PhotoModule.php | 98 +++++++++++++++++ .../photo/controllers/PhotoController.php | 8 ++ app/modules/photo/manifest.json | 13 +++ app/modules/photo/routs/photo.php | 2 + app/modules/tag/TagModule.php | 14 ++- .../photo/controllers/PhotoController.php | 104 ++++++++++++++++++ .../2024_11_28_133527_create_photo_table.php | 30 +++++ kernel/app_modules/photo/models/Photo.php | 44 ++++++++ .../photo/models/form/CreatePhotoForm.php | 19 ++++ kernel/app_modules/photo/routs/photo.php | 20 ++++ .../photo/services/PhotoService.php | 34 ++++++ kernel/app_modules/photo/views/index.php | 41 +++++++ kernel/app_modules/photo/views/view.php | 34 ++++++ .../tag/controllers/TagEntityController.php | 68 ++---------- kernel/app_modules/tag/views/tag/index.php | 2 - kernel/modules/post/views/form.php | 2 +- kernel/services/ModuleService.php | 2 +- 17 files changed, 464 insertions(+), 71 deletions(-) create mode 100644 app/modules/photo/PhotoModule.php create mode 100644 app/modules/photo/controllers/PhotoController.php create mode 100644 app/modules/photo/manifest.json create mode 100644 app/modules/photo/routs/photo.php create mode 100755 kernel/app_modules/photo/controllers/PhotoController.php create mode 100644 kernel/app_modules/photo/migrations/2024_11_28_133527_create_photo_table.php create mode 100755 kernel/app_modules/photo/models/Photo.php create mode 100755 kernel/app_modules/photo/models/form/CreatePhotoForm.php create mode 100755 kernel/app_modules/photo/routs/photo.php create mode 100755 kernel/app_modules/photo/services/PhotoService.php create mode 100755 kernel/app_modules/photo/views/index.php create mode 100755 kernel/app_modules/photo/views/view.php diff --git a/app/modules/photo/PhotoModule.php b/app/modules/photo/PhotoModule.php new file mode 100644 index 0000000..e39f083 --- /dev/null +++ b/app/modules/photo/PhotoModule.php @@ -0,0 +1,98 @@ +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 .= "" . " "; + } + + 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 .= "" . " "; + } + + return substr($photoStr, 0, -1); + } + + public function deleteItems(string $entity, Model $model): void + { + Photo::where("entity", $entity)->where("entity_id", $model->id)->delete(); + } +} \ No newline at end of file diff --git a/app/modules/photo/controllers/PhotoController.php b/app/modules/photo/controllers/PhotoController.php new file mode 100644 index 0000000..4f00aac --- /dev/null +++ b/app/modules/photo/controllers/PhotoController.php @@ -0,0 +1,8 @@ +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(); + } } } diff --git a/kernel/app_modules/photo/controllers/PhotoController.php b/kernel/app_modules/photo/controllers/PhotoController.php new file mode 100755 index 0000000..e241869 --- /dev/null +++ b/kernel/app_modules/photo/controllers/PhotoController.php @@ -0,0 +1,104 @@ +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'); +// } + +} \ No newline at end of file diff --git a/kernel/app_modules/photo/migrations/2024_11_28_133527_create_photo_table.php b/kernel/app_modules/photo/migrations/2024_11_28_133527_create_photo_table.php new file mode 100644 index 0000000..eb30db6 --- /dev/null +++ b/kernel/app_modules/photo/migrations/2024_11_28_133527_create_photo_table.php @@ -0,0 +1,30 @@ +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'); + } +}; diff --git a/kernel/app_modules/photo/models/Photo.php b/kernel/app_modules/photo/models/Photo.php new file mode 100755 index 0000000..daeea66 --- /dev/null +++ b/kernel/app_modules/photo/models/Photo.php @@ -0,0 +1,44 @@ + 'Фото', + '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; + } + +} \ No newline at end of file diff --git a/kernel/app_modules/photo/models/form/CreatePhotoForm.php b/kernel/app_modules/photo/models/form/CreatePhotoForm.php new file mode 100755 index 0000000..164c422 --- /dev/null +++ b/kernel/app_modules/photo/models/form/CreatePhotoForm.php @@ -0,0 +1,19 @@ + 'required', + 'entity' => 'required|string', + 'entity_id' => 'required|integer|min:1', + ]; + } + +} \ No newline at end of file diff --git a/kernel/app_modules/photo/routs/photo.php b/kernel/app_modules/photo/routs/photo.php new file mode 100755 index 0000000..4c74627 --- /dev/null +++ b/kernel/app_modules/photo/routs/photo.php @@ -0,0 +1,20 @@ +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]); + }); + }); +}); \ No newline at end of file diff --git a/kernel/app_modules/photo/services/PhotoService.php b/kernel/app_modules/photo/services/PhotoService.php new file mode 100755 index 0000000..0289979 --- /dev/null +++ b/kernel/app_modules/photo/services/PhotoService.php @@ -0,0 +1,34 @@ +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; + } + +} \ No newline at end of file diff --git a/kernel/app_modules/photo/views/index.php b/kernel/app_modules/photo/views/index.php new file mode 100755 index 0000000..9c42d62 --- /dev/null +++ b/kernel/app_modules/photo/views/index.php @@ -0,0 +1,41 @@ + $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 ? "" : ""; + } +]); + +$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(); \ No newline at end of file diff --git a/kernel/app_modules/photo/views/view.php b/kernel/app_modules/photo/views/view.php new file mode 100755 index 0000000..35b74b9 --- /dev/null +++ b/kernel/app_modules/photo/views/view.php @@ -0,0 +1,34 @@ + ["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 ? "" : ""; + } +]); +$table->create(); +$table->render(); \ No newline at end of file diff --git a/kernel/app_modules/tag/controllers/TagEntityController.php b/kernel/app_modules/tag/controllers/TagEntityController.php index 41152ad..f432e2e 100755 --- a/kernel/app_modules/tag/controllers/TagEntityController.php +++ b/kernel/app_modules/tag/controllers/TagEntityController.php @@ -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/"); -// } - } \ No newline at end of file diff --git a/kernel/app_modules/tag/views/tag/index.php b/kernel/app_modules/tag/views/tag/index.php index 9d46593..bac1430 100755 --- a/kernel/app_modules/tag/views/tag/index.php +++ b/kernel/app_modules/tag/views/tag/index.php @@ -1,6 +1,5 @@ beforePrint(function () { return PrimaryBtn::create("Создать", "/admin/tag/create")->fetch(); - //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); }); $table->columns([ diff --git a/kernel/modules/post/views/form.php b/kernel/modules/post/views/form.php index 568e88a..39648da 100644 --- a/kernel/modules/post/views/form.php +++ b/kernel/modules/post/views/form.php @@ -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", diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index 05a9f93..682e73c 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -451,8 +451,8 @@ class ModuleService return true; } } - return false; + return false; } public function isKernelModule(string $slug): bool