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();
}
}