From 9419626485f422756a28270ddb887f1147c034e5 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 9 Dec 2024 16:35:43 +0300 Subject: [PATCH] app modules classes move to kerel app modules --- app/modules/photo/PhotoModule.php | 109 +-------------------- app/modules/tag/TagModule.php | 117 +--------------------- app/modules/tag/manifest.json | 2 +- kernel/app_modules/photo/PhotoModule.php | 112 +++++++++++++++++++++ kernel/app_modules/tag/TagModule.php | 119 +++++++++++++++++++++++ 5 files changed, 234 insertions(+), 225 deletions(-) create mode 100644 kernel/app_modules/photo/PhotoModule.php create mode 100644 kernel/app_modules/tag/TagModule.php diff --git a/app/modules/photo/PhotoModule.php b/app/modules/photo/PhotoModule.php index 1ead103..ff86533 100644 --- a/app/modules/photo/PhotoModule.php +++ b/app/modules/photo/PhotoModule.php @@ -2,114 +2,7 @@ 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 +class PhotoModule extends \kernel\app_modules\photo\PhotoModule { - 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 .= "" . " "; - } - - 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/tag/TagModule.php b/app/modules/tag/TagModule.php index 36d07e8..ff826c6 100644 --- a/app/modules/tag/TagModule.php +++ b/app/modules/tag/TagModule.php @@ -2,122 +2,7 @@ namespace app\modules\tag; -use Illuminate\Database\Eloquent\Model; -use itguild\forms\builders\SelectBuilder; -use itguild\forms\builders\TextInputBuilder; -use itguild\forms\inputs\Select; -use kernel\app_modules\tag\models\Tag; -use kernel\app_modules\tag\models\TagEntity; -use kernel\app_modules\tag\services\TagEntityService; -use kernel\helpers\Debug; -use kernel\helpers\Slug; -use kernel\Module; -use kernel\modules\menu\service\MenuService; -use kernel\modules\option\service\OptionService; -use kernel\Request; -use kernel\services\MigrationService; - -class TagModule extends Module +class TagModule extends \kernel\app_modules\tag\TagModule { - 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/tag"); - $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag_entity"); - - $this->menuService->createItem([ - "label" => "Тэги", - "url" => "/admin/tag", - "slug" => "tag", - ]); - - $this->menuService->createItem([ - "label" => "Тэги", - "url" => "/admin/settings/tag", - "slug" => "tag_settings", - "parent_slug" => "settings" - ]); - - OptionService::createFromParams("entity_tag_list", "{}", "Список тегов"); - } - - public function deactivate(): void - { - $this->menuService->removeItemBySlug("tag"); - $this->menuService->removeItemBySlug("tag_settings"); - OptionService::removeOptionByKey("entity_tag_list"); - } - - public function formInputs(string $entity, Model $model = null): void - { - if (isset($model->id)) { - $value = TagEntityService::getTagsByEntity($entity, $model->id); - } - - $input = SelectBuilder::build("tag[]", [ - 'class' => 'form-control', - 'placeholder' => 'Теги', - 'value' => $value ?? '', - 'multiple' => "multiple", - 'options' => Tag::getTagLabelByEntity($entity) - ]); - $input->setLabel("Теги"); - $input->create()->render(); - } - - public function saveInputs(string $entity, Model $model, Request $request): void - { - TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); - - $tags = $request->post("tag"); - if (is_array($tags)) { - foreach ($tags as $tag) { - $tagEntity = new TagEntity(); - $tagEntity->entity = $entity; - $tagEntity->entity_id = $model->id; - $tagEntity->tag_id = $tag; - $tagEntity->save(); - } - } - } - - - public function getItems(string $entity, Model $model): array|string - { - $tags = TagEntity::where("entity", $entity)->where("entity_id", $model->id)->with("tag")->get(); - $tagsStr = ""; - foreach ($tags as $tag) { - $tagsStr .= $tag->tag->label . ", "; - } - - return substr($tagsStr, 0, -2); - } - - public function getItem(string $entity, string $entity_id): string - { - $tags = TagEntity::where("entity", $entity)->where("entity_id", $entity_id)->get(); - $tagsStr = ""; - foreach ($tags as $tag) { - $tagsStr .= $tag->tag->label . ", "; - } - - return substr($tagsStr, 0, -2); - } - - public function deleteItems(string $entity, Model $model): void - { - TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); - } } \ No newline at end of file diff --git a/app/modules/tag/manifest.json b/app/modules/tag/manifest.json index b164b3e..f746569 100644 --- a/app/modules/tag/manifest.json +++ b/app/modules/tag/manifest.json @@ -1,6 +1,6 @@ { "name": "Tags", - "version": "0.2", + "version": "0.1", "author": "ITGuild", "slug": "tag", "type": "additional_property", diff --git a/kernel/app_modules/photo/PhotoModule.php b/kernel/app_modules/photo/PhotoModule.php new file mode 100644 index 0000000..a944c08 --- /dev/null +++ b/kernel/app_modules/photo/PhotoModule.php @@ -0,0 +1,112 @@ +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 .= "" . " "; + } + + 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/kernel/app_modules/tag/TagModule.php b/kernel/app_modules/tag/TagModule.php new file mode 100644 index 0000000..9872e89 --- /dev/null +++ b/kernel/app_modules/tag/TagModule.php @@ -0,0 +1,119 @@ +menuService = new MenuService(); + $this->migrationService = new MigrationService(); + } + + /** + * @throws \Exception + */ + public function init(): void + { + $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag"); + $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag_entity"); + + $this->menuService->createItem([ + "label" => "Тэги", + "url" => "/admin/tag", + "slug" => "tag", + ]); + + $this->menuService->createItem([ + "label" => "Тэги", + "url" => "/admin/settings/tag", + "slug" => "tag_settings", + "parent_slug" => "settings" + ]); + + OptionService::createFromParams("entity_tag_list", "{}", "Список тегов"); + } + + public function deactivate(): void + { + $this->menuService->removeItemBySlug("tag"); + $this->menuService->removeItemBySlug("tag_settings"); + OptionService::removeOptionByKey("entity_tag_list"); + } + + public function formInputs(string $entity, Model $model = null): void + { + if (isset($model->id)) { + $value = TagEntityService::getTagsByEntity($entity, $model->id); + } + + $input = SelectBuilder::build("tag[]", [ + 'class' => 'form-control', + 'placeholder' => 'Теги', + 'value' => $value ?? '', + 'multiple' => "multiple", + 'options' => Tag::getTagLabelByEntity($entity) + ]); + $input->setLabel("Теги"); + $input->create()->render(); + } + + public function saveInputs(string $entity, Model $model, Request $request): void + { + TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); + + $tags = $request->post("tag"); + if (is_array($tags)) { + foreach ($tags as $tag) { + $tagEntity = new TagEntity(); + $tagEntity->entity = $entity; + $tagEntity->entity_id = $model->id; + $tagEntity->tag_id = $tag; + $tagEntity->save(); + } + } + } + + + public function getItems(string $entity, Model $model): array|string + { + $tags = TagEntity::where("entity", $entity)->where("entity_id", $model->id)->with("tag")->get(); + $tagsStr = ""; + foreach ($tags as $tag) { + $tagsStr .= $tag->tag->label . ", "; + } + + return substr($tagsStr, 0, -2); + } + + public function getItem(string $entity, string $entity_id): string + { + $tags = TagEntity::where("entity", $entity)->where("entity_id", $entity_id)->get(); + $tagsStr = ""; + foreach ($tags as $tag) { + $tagsStr .= $tag->tag->label . ", "; + } + + return substr($tagsStr, 0, -2); + } + + public function deleteItems(string $entity, Model $model): void + { + TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete(); + } +} \ No newline at end of file