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,12 +81,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();
}
}
}