<?php
// api/src/Entity/MediaObject.php
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Delete;
use App\Controller\CreateMediaObjectAction;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\DBAL\Types\Types;
#[Vich\Uploadable]
#[ORM\Entity]
#[ApiResource(
normalizationContext: ['groups' => ['media_object:read']],
types: ['https://schema.org/MediaObject'],
operations: [
new Get(),
new GetCollection(),
new Delete(),
new Post(
controller: CreateMediaObjectAction::class,
deserialize: false,
validationContext: ['groups' => ['Default', 'media_object_create']],
openapiContext: [
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary'
]
]
]
]
]
]
]
)
],
order: ['id' => 'DESC'],
)]
#[ApiFilter(SearchFilter::class, properties: [
'product.id' => 'exact',
'orders.id' => 'exact',
'orders.account.user.id' => 'exact',
])]
#[ORM\HasLifecycleCallbacks]
class MediaObject
{
private const BASE_UPLOAD_DIR = '/media';
#[Groups(['media_object:read', 'product:read', 'user_like:read', 'pre_order_product:read', 'pre_order:read', 'order:read', 'order_product:read','order_product:read','user:read', 'account:read', 'load_invoice:read', 'worker:read'])]
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
private ?int $id = null;
#[ApiProperty(types: ['https://schema.org/contentUrl'])]
#[Groups(['media_object:read', 'product:read', 'cat:read', 'user_like:read', 'pre_order_product:read', 'pre_order:read', 'order:read', 'order_product:read','order_product:read', 'user:read', 'account:read', 'load_invoice:read', 'worker:read'])]
public ?string $contentUrl = null;
#[Vich\UploadableField(mapping: "media_object", fileNameProperty: "filePath")]
#[Assert\NotNull(groups: ['media_object_create'])]
public ?File $file = null;
#[ORM\Column(nullable: true)]
public ?string $filePath = null;
// #[Groups(['media_object:read', 'media_object_create'])]
// #[ORM\ManyToOne(inversedBy: 'mediaObjects')]
// private ?Products $product = null;
// #[Groups(['media_object:read', 'media_object_create'])]
// #[ORM\ManyToOne(inversedBy: 'mediaObjects')]
// private ?Orders $orders = null;
#[Groups(['media_object:read', 'media_object_create'])]
#[ORM\Column(length: 100, nullable: true)]
private ?string $type = null;
#[Groups(['media_object:read', 'media_object_create'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $name = null;
#[Groups(['media_object:read', 'media_object_create'])]
#[ORM\ManyToOne(inversedBy: 'mediaObjects')]
private ?User $users = null;
#[Groups(['media_object:read', 'product:read', 'cat:read', 'user_like:read', 'pre_order_product:read', 'pre_order:read', 'order:read', 'order_product:read','order_product:read', 'user:read', 'account:read', 'load_invoice:read', 'worker:read'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
public $uploadTimestamp;
#[ORM\OneToMany(mappedBy: 'image', targetEntity: Notification::class)]
private Collection $notifications;
// #[Groups(['media_object:read', 'media_object_create'])]
// #[ORM\ManyToOne(inversedBy: 'media')]
// private ?AccountWorker $accountWorker = null;
// #[ORM\ManyToOne(inversedBy: 'media')]
// private ?FormAnswer $formAnswer = null;
public function __construct()
{
$this->notifications = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
// public function getProduct(): ?Products
// {
// return $this->product;
// }
// public function setProduct(?Products $product): self
// {
// $this->product = $product;
// return $this;
// }
// public function getOrders(): ?Orders
// {
// return $this->orders;
// }
// public function setOrders(?Orders $orders): self
// {
// $this->orders = $orders;
// return $this;
// }
public function getFilePath(): ?string
{
return $this->filePath;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getUsers(): ?User
{
return $this->users;
}
public function setUsers(?User $users): self
{
$this->users = $users;
return $this;
}
public function getUploadTimestamp(): ?\DateTimeInterface
{
return $this->uploadTimestamp;
}
#[ORM\PrePersist]
public function setCreatedAtValue(): void
{
$this->uploadTimestamp = new \DateTime();
}
public function getFullPath(): string
{
// Отримуємо рік і місяць з дати завантаження
$year = $this->uploadTimestamp->format('Y');
$month = $this->uploadTimestamp->format('m');
// Формуємо повний шлях
return self::BASE_UPLOAD_DIR . '/' . $year . '/' . $month . '/' . $this->filePath;
}
// public function getAccountWorker(): ?AccountWorker
// {
// return $this->accountWorker;
// }
// public function setAccountWorker(?AccountWorker $accountWorker): self
// {
// $this->accountWorker = $accountWorker;
// return $this;
// }
// public function setFormAnswer(?FormAnswer $formAnswer): self
// {
// $this->formAnswer = $formAnswer;
// return $this;
// }
/**
* @return Collection<int, Notification>
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Notification $notification): static
{
if (!$this->notifications->contains($notification)) {
$this->notifications->add($notification);
$notification->setImage($this);
}
return $this;
}
public function removeNotification(Notification $notification): static
{
if ($this->notifications->removeElement($notification)) {
// set the owning side to null (unless already changed)
if ($notification->getImage() === $this) {
$notification->setImage(null);
}
}
return $this;
}
}