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.      * @var Collection<int, MenuItems>
  98.      */
  99.     #[ORM\OneToMany(mappedBy'media'targetEntityMenuItems::class)]
  100.     private Collection $menuItems;
  101.     
  102.     // #[Groups(['media_object:read', 'media_object_create'])]
  103.     // #[ORM\ManyToOne(inversedBy: 'media')]
  104.     // private ?AccountWorker $accountWorker = null;
  105.     
  106.     // #[ORM\ManyToOne(inversedBy: 'media')]
  107.     // private ?FormAnswer $formAnswer = null;
  108.     public function __construct()
  109.     {
  110.         $this->notifications = new ArrayCollection();
  111.         $this->menuItems = new ArrayCollection();
  112.     }
  113.     public function getId(): ?int
  114.     
  115.         return $this->id;
  116.     }
  117.     // public function getProduct(): ?Products
  118.     // {
  119.     //     return $this->product;
  120.     // }
  121.     // public function setProduct(?Products $product): self
  122.     // {
  123.     //     $this->product = $product;
  124.     //     return $this;
  125.     // }
  126.     // public function getOrders(): ?Orders
  127.     // {
  128.     //     return $this->orders;
  129.     // }
  130.     // public function setOrders(?Orders $orders): self
  131.     // {
  132.     //     $this->orders = $orders;
  133.     //     return $this;
  134.     // }
  135.     public function getFilePath(): ?string
  136.     {
  137.         return $this->filePath;
  138.     }
  139.     public function getType(): ?string
  140.     {
  141.         return $this->type;
  142.     }
  143.     public function setType(string $type): self
  144.     {
  145.         $this->type $type;
  146.         return $this;
  147.     }
  148.     public function getName(): ?string
  149.     {
  150.         return $this->name;
  151.     }
  152.     public function setName(?string $name): self
  153.     {
  154.         $this->name $name;
  155.         return $this;
  156.     }
  157.     public function getUsers(): ?User
  158.     {
  159.         return $this->users;
  160.     }
  161.     public function setUsers(?User $users): self
  162.     {
  163.         $this->users $users;
  164.         return $this;
  165.     }
  166.     public function getUploadTimestamp(): ?\DateTimeInterface
  167.     {
  168.         return $this->uploadTimestamp;
  169.     }
  170.     #[ORM\PrePersist]
  171.     public function setCreatedAtValue(): void
  172.     {
  173.         $this->uploadTimestamp = new \DateTime(); 
  174.     }
  175.     public function getFullPath(): string
  176.     {
  177.         // Отримуємо рік і місяць з дати завантаження
  178.         $year $this->uploadTimestamp->format('Y');
  179.         $month $this->uploadTimestamp->format('m');
  180.         // Формуємо повний шлях
  181.         return self::BASE_UPLOAD_DIR '/' $year '/' $month '/' $this->filePath;
  182.     }
  183.     // public function getAccountWorker(): ?AccountWorker
  184.     // {
  185.     //     return $this->accountWorker;
  186.     // }
  187.     // public function setAccountWorker(?AccountWorker $accountWorker): self
  188.     // {
  189.     //     $this->accountWorker = $accountWorker;
  190.     //     return $this;
  191.     // }
  192.     
  193.     // public function setFormAnswer(?FormAnswer $formAnswer): self
  194.     // {
  195.     //     $this->formAnswer = $formAnswer;
  196.     //     return $this;
  197.     // }
  198.     /**
  199.      * @return Collection<int, Notification>
  200.      */
  201.     public function getNotifications(): Collection
  202.     {
  203.         return $this->notifications;
  204.     }
  205.     public function addNotification(Notification $notification): static
  206.     {
  207.         if (!$this->notifications->contains($notification)) {
  208.             $this->notifications->add($notification);
  209.             $notification->setImage($this);
  210.         }
  211.         return $this;
  212.     }
  213.     public function removeNotification(Notification $notification): static
  214.     {
  215.         if ($this->notifications->removeElement($notification)) {
  216.             // set the owning side to null (unless already changed)
  217.             if ($notification->getImage() === $this) {
  218.                 $notification->setImage(null);
  219.             }
  220.         }
  221.         return $this;
  222.     }
  223.     
  224.     /**
  225.      * @return Collection<int, MenuItems>
  226.      */
  227.     public function getMenuItems(): Collection
  228.     {
  229.         return $this->menuItems;
  230.     }
  231.     public function addMenuItem(MenuItems $menuItem): static
  232.     {
  233.         if (!$this->menuItems->contains($menuItem)) {
  234.             $this->menuItems->add($menuItem);
  235.             $menuItem->setMedia($this);
  236.         }
  237.         return $this;
  238.     }
  239.     public function removeMenuItem(MenuItems $menuItem): static
  240.     {
  241.         if ($this->menuItems->removeElement($menuItem)) {
  242.             // set the owning side to null (unless already changed)
  243.             if ($menuItem->getMedia() === $this) {
  244.                 $menuItem->setMedia(null);
  245.             }
  246.         }
  247.         return $this;
  248.     }
  249. }