MicroFrameWork/app/modules/photo/PhotoModule.php

107 lines
3.1 KiB
PHP
Raw Normal View History

2024-11-29 13:23:48 +03:00
<?php
namespace app\modules\photo;
2024-11-29 14:07:55 +03:00
use Cassandra\Decimal;
2024-11-29 13:23:48 +03:00
use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\FileBuilder;
use kernel\app_modules\photo\models\Photo;
2024-11-29 14:07:55 +03:00
use kernel\app_modules\photo\services\PhotoService;
use kernel\app_modules\tag\services\TagEntityService;
2024-11-29 13:23:48 +03:00
use kernel\FileUpload;
2024-11-29 14:07:55 +03:00
use kernel\helpers\Debug;
use kernel\helpers\Html;
2024-11-29 13:23:48 +03:00
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
{
2024-11-29 14:07:55 +03:00
if (isset($model->id)) {
$value = PhotoService::getByEntity($entity, $model->id);
2024-11-29 16:54:35 +03:00
echo Html::img($value, ['width' => '200px']);
2024-11-29 14:07:55 +03:00
}
2024-11-29 13:23:48 +03:00
$input = FileBuilder::build("image", [
'class' => 'form-control',
2024-11-29 16:54:35 +03:00
'value' => $value ?? '',
2024-11-29 13:23:48 +03:00
]);
$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();
}
}