src/Entity/MediaObject.php line 66

Open in your IDE?
  1. <?php
  2. // api/src/Entity/MediaObject.php
  3. namespace App\Entity;
  4. use ApiPlatform\Metadata\ApiProperty;
  5. use ApiPlatform\Metadata\ApiResource;
  6. use ApiPlatform\Metadata\Get;
  7. use ApiPlatform\Metadata\GetCollection;
  8. use ApiPlatform\Metadata\Post;
  9. use ApiPlatform\Metadata\Delete;
  10. use App\Controller\CreateMediaObjectAction;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\HttpFoundation\File\File;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  18. use ApiPlatform\Metadata\ApiFilter;
  19. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  20. use Doctrine\DBAL\Types\Types;
  21. #[Vich\Uploadable]
  22. #[ORM\Entity]
  23. #[ApiResource(
  24.     normalizationContext: ['groups' => ['media_object:read']], 
  25.     types: ['https://schema.org/MediaObject'],
  26.     operations: [
  27.         new Get(),
  28.         new GetCollection(),
  29.         new Delete(),
  30.         new Post(
  31.             controllerCreateMediaObjectAction::class, 
  32.             deserializefalse
  33.             validationContext: ['groups' => ['Default''media_object_create']], 
  34.             openapiContext: [
  35.                 'requestBody' => [
  36.                     'content' => [
  37.                         'multipart/form-data' => [
  38.                             'schema' => [
  39.                                 'type' => 'object'
  40.                                 'properties' => [
  41.                                     'file' => [
  42.                                         'type' => 'string'
  43.                                         'format' => 'binary'
  44.                                     ]
  45.                                 ]
  46.                             ]
  47.                         ]
  48.                     ]
  49.                 ]
  50.             ]
  51.         )
  52.     ],
  53.     order: ['id' => 'DESC'],
  54. )]
  55. #[ApiFilter(SearchFilter::class, properties: [
  56.     'product.id' => 'exact'
  57.     'orders.id' => 'exact',
  58.     'orders.account.user.id' => 'exact',
  59. ])]  
  60. #[ORM\HasLifecycleCallbacks]
  61. class MediaObject
  62. {
  63.     private const BASE_UPLOAD_DIR '/media';
  64.     #[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'])]
  65.     #[ORM\IdORM\ColumnORM\GeneratedValue]
  66.     private ?int $id null;
  67.     #[ApiProperty(types: ['https://schema.org/contentUrl'])]
  68.     #[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'])]
  69.     public ?string $contentUrl null;
  70.     #[Vich\UploadableField(mapping"media_object"fileNameProperty"filePath")]
  71.     #[Assert\NotNull(groups: ['media_object_create'])]
  72.     public ?File $file null;
  73.     #[ORM\Column(nullabletrue)] 
  74.     public ?string $filePath null;
  75.     
  76.     // #[Groups(['media_object:read', 'media_object_create'])]
  77.     // #[ORM\ManyToOne(inversedBy: 'mediaObjects')]
  78.     // private ?Products $product = null;
  79.     // #[Groups(['media_object:read', 'media_object_create'])]
  80.     // #[ORM\ManyToOne(inversedBy: 'mediaObjects')]
  81.     // private ?Orders $orders = null;
  82.     #[Groups(['media_object:read''media_object_create'])]
  83.     #[ORM\Column(length100nullabletrue)]
  84.     private ?string $type null;
  85.     #[Groups(['media_object:read''media_object_create'])]
  86.     #[ORM\Column(length255nullabletrue)]
  87.     private ?string $name null;
  88.     #[Groups(['media_object:read''media_object_create'])]
  89.     #[ORM\ManyToOne(inversedBy'mediaObjects')]
  90.     private ?User $users null;
  91.     #[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'])]
  92.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  93.     public $uploadTimestamp;
  94.     #[ORM\OneToMany(mappedBy'image'targetEntityNotification::class)]
  95.     private Collection $notifications;
  96.     
  97.     // #[Groups(['media_object:read', 'media_object_create'])]
  98.     // #[ORM\ManyToOne(inversedBy: 'media')]
  99.     // private ?AccountWorker $accountWorker = null;
  100.     
  101.     // #[ORM\ManyToOne(inversedBy: 'media')]
  102.     // private ?FormAnswer $formAnswer = null;
  103.     public function __construct()
  104.     {
  105.         $this->notifications = new ArrayCollection();
  106.     }
  107.     public function getId(): ?int
  108.     
  109.         return $this->id;
  110.     }
  111.     // public function getProduct(): ?Products
  112.     // {
  113.     //     return $this->product;
  114.     // }
  115.     // public function setProduct(?Products $product): self
  116.     // {
  117.     //     $this->product = $product;
  118.     //     return $this;
  119.     // }
  120.     // public function getOrders(): ?Orders
  121.     // {
  122.     //     return $this->orders;
  123.     // }
  124.     // public function setOrders(?Orders $orders): self
  125.     // {
  126.     //     $this->orders = $orders;
  127.     //     return $this;
  128.     // }
  129.     public function getFilePath(): ?string
  130.     {
  131.         return $this->filePath;
  132.     }
  133.     public function getType(): ?string
  134.     {
  135.         return $this->type;
  136.     }
  137.     public function setType(string $type): self
  138.     {
  139.         $this->type $type;
  140.         return $this;
  141.     }
  142.     public function getName(): ?string
  143.     {
  144.         return $this->name;
  145.     }
  146.     public function setName(?string $name): self
  147.     {
  148.         $this->name $name;
  149.         return $this;
  150.     }
  151.     public function getUsers(): ?User
  152.     {
  153.         return $this->users;
  154.     }
  155.     public function setUsers(?User $users): self
  156.     {
  157.         $this->users $users;
  158.         return $this;
  159.     }
  160.     public function getUploadTimestamp(): ?\DateTimeInterface
  161.     {
  162.         return $this->uploadTimestamp;
  163.     }
  164.     #[ORM\PrePersist]
  165.     public function setCreatedAtValue(): void
  166.     {
  167.         $this->uploadTimestamp = new \DateTime(); 
  168.     }
  169.     public function getFullPath(): string
  170.     {
  171.         // Отримуємо рік і місяць з дати завантаження
  172.         $year $this->uploadTimestamp->format('Y');
  173.         $month $this->uploadTimestamp->format('m');
  174.         // Формуємо повний шлях
  175.         return self::BASE_UPLOAD_DIR '/' $year '/' $month '/' $this->filePath;
  176.     }
  177.     // public function getAccountWorker(): ?AccountWorker
  178.     // {
  179.     //     return $this->accountWorker;
  180.     // }
  181.     // public function setAccountWorker(?AccountWorker $accountWorker): self
  182.     // {
  183.     //     $this->accountWorker = $accountWorker;
  184.     //     return $this;
  185.     // }
  186.     
  187.     // public function setFormAnswer(?FormAnswer $formAnswer): self
  188.     // {
  189.     //     $this->formAnswer = $formAnswer;
  190.     //     return $this;
  191.     // }
  192.     /**
  193.      * @return Collection<int, Notification>
  194.      */
  195.     public function getNotifications(): Collection
  196.     {
  197.         return $this->notifications;
  198.     }
  199.     public function addNotification(Notification $notification): static
  200.     {
  201.         if (!$this->notifications->contains($notification)) {
  202.             $this->notifications->add($notification);
  203.             $notification->setImage($this);
  204.         }
  205.         return $this;
  206.     }
  207.     public function removeNotification(Notification $notification): static
  208.     {
  209.         if ($this->notifications->removeElement($notification)) {
  210.             // set the owning side to null (unless already changed)
  211.             if ($notification->getImage() === $this) {
  212.                 $notification->setImage(null);
  213.             }
  214.         }
  215.         return $this;
  216.     }
  217. }