src/Entity/Category.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  10. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  11. use App\Annotation\CompanyCustomizable;
  12. use App\Annotation\Exclusions;
  13. use App\Repository\CategoryRepository;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Gedmo\Mapping\Annotation as Gedmo;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. /**
  22.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  23.  * @ApiResource(
  24.  *     normalizationContext={"groups"={"category:read"}},
  25.  *     denormalizationContext={"groups"={"category:write"}},
  26.  *     collectionOperations={
  27.  *          "get"={
  28.  *              "security"="is_granted('ROLE_USER')"
  29.  *          },
  30.  *          "homepage_categories"={
  31.  *              "security"="is_granted('ROLE_USER')",
  32.  *              "method"="GET",
  33.  *              "path"="/categories/navigation",
  34.  *              "controller"="App\Controller\Api\CategoryController::navigation",
  35.  *          },
  36.  *          "post"={
  37.  *              "security"="is_granted('ROLE_USER')"
  38.  *          },
  39.  *          "post_update"={
  40.  *              "security"="is_granted('ROLE_ADMIN')",
  41.  *              "path"="/categories/{id}",
  42.  *              "description"="Update an Category with method POST (using content-type: 'multipart')",
  43.  *              "method"="POST",
  44.  *              "controller"="App\Controller\Api\CategoryController::update"
  45.  *          },
  46.  *          "manager_categories"={
  47.  *              "security"="is_granted('ROLE_USER')",
  48.  *              "method"="GET",
  49.  *              "path"="/manager/categories",
  50.  *              "controller"="App\Controller\Api\CategoryController::getCategoriesForManager",
  51.  *          }
  52.  *     },
  53.  *     itemOperations={
  54.  *          "get"={
  55.  *              "security"="is_granted('ROLE_USER')"
  56.  *          }, 
  57.  *          "delete"={
  58.  *              "security"="is_granted('ROLE_ADMIN')"
  59.  *          }, 
  60.  *          "patch"={
  61.  *              "security"="is_granted('ROLE_ADMIN')"
  62.  *          }
  63.  *      }
  64.  * )
  65.  * @ORM\HasLifecycleCallbacks
  66.  * @ApiFilter(OrderFilter::class, properties={"id", "name", "active", "activeMenu"})
  67.  * @ApiFilter(GroupFilter::class, arguments={
  68.  *      "parameterName": "groups", 
  69.  *      "overrideDefaultGroups": true
  70.  * })
  71.  * @ApiFilter(SearchFilter::class, properties={"activeMenu", "company.id"})
  72.  * @ApiFilter(ExistsFilter::class, properties={"company", "parent"})
  73.  * @ApiFilter(PropertyFilter::class, 
  74.  *      arguments={
  75.  *          "parameterName"="fields", 
  76.  *          "overrideDefaultProperties"=true
  77.  *     }
  78.  * )
  79.  * @CompanyCustomizable
  80.  * @Exclusions(exclusionsClass="App\Entity\ExclusionsCategory", property="exclusions_categories")
  81.  */
  82. class Category
  83. {
  84.     /**
  85.      * @ORM\Id
  86.      * @ORM\GeneratedValue
  87.      * @ORM\Column(type="integer")
  88.      * @Groups({"category:read", "category:read:id", "homepage:read", "stats", "channel:read", "search_engine", "slide:read", "category_list", "program_express_list"})
  89.      */
  90.     private $id;
  91.     /**
  92.      * @ORM\Column(type="string", length=255)
  93.      * @Groups({"category:read", "category:read:name", "category:write", "stats", "channel:read", "homepage:read", "search_engine",
  94.      *     "slide:read", "channel_list", "category_list", "program_express_list"})
  95.      * @Assert\NotNull(message="category.name")
  96.      * @Assert\NotBlank(message="category.name")
  97.      */
  98.     private $name;
  99.     /**
  100.      * @ORM\Column(type="text")
  101.      * @Groups({"category:read", "category:read:content", "category:write"})
  102.      * @Assert\NotNull(message="category.content")
  103.      * @Assert\NotBlank(message="category.content")
  104.      */
  105.     private $content;
  106.     /**
  107.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  108.      * @Groups({"category:read", "category:read:picture", "category:write", "stats"})
  109.      */
  110.     private $picture;
  111.     /**
  112.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  113.      * @Groups({"category:read", "category:read:illustration", "category:write"})
  114.      */
  115.     private $illustration;
  116.     /**
  117.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  118.      * @Groups({"category:read", "category:read:appPicture", "category:write"})
  119.      */
  120.     private $appPicture;
  121.     /**
  122.      * @ORM\Column(type="datetime")
  123.      * @Gedmo\Timestampable(on="update")
  124.      * @Groups({"category:read", "category:read:updatedAt"})
  125.      */
  126.     private $updatedAt;
  127.     /**
  128.      * @ORM\Column(type="datetime")
  129.      * @Gedmo\Timestampable(on="create")
  130.      * @Groups({"category:read", "category:read:createdAt"})
  131.      */
  132.     private $createdAt;
  133.     /**
  134.      * @ORM\OneToMany(targetEntity=Channel::class, mappedBy="category", orphanRemoval=true)
  135.      * @Groups({"category:read", "category:read:channels", "homepage:read"})
  136.      * @ApiSubresource(maxDepth=1)
  137.      */
  138.     private $channels;
  139.     /**
  140.      * @ORM\Column(type="boolean", options={"default": "1"})
  141.      * @Groups({"category:read", "category:read:active", "category:write", "category_list"})
  142.      */
  143.     private $active true;
  144.     /**
  145.      * @var int
  146.      * @Groups({"category:read", "category:read:channelsCount", "category_list"})
  147.      */
  148.     private $channelsCount 0;
  149.     /**
  150.      * Indicates to a manager if the category is excluded for them
  151.      * @var bool
  152.      * @Groups({"category:read", "category:read:excluded", "category_list"})
  153.      */
  154.     private $excluded false;
  155.     /**
  156.      * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="categories")
  157.      */
  158.     private $tvCompany;
  159.     /**
  160.      * @ORM\OneToMany(targetEntity=Slide::class, mappedBy="category")
  161.      */
  162.     private $slides;
  163.     /**
  164.      * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="children")
  165.      */
  166.     private $parent;
  167.     /**
  168.      * @ORM\OneToMany(targetEntity=Category::class, mappedBy="parent")
  169.      */
  170.     private $children;
  171.     /**
  172.      * @ORM\Column(type="boolean", options={"default": "0"})
  173.      * @Groups({"category:read", "category:read:activeMenu", "category:write", "stats", "channel:read", "homepage:read", "search_engine", "category_list"})
  174.      */
  175.     private $activeMenu false;
  176.     /**
  177.      * @ORM\OneToMany(targetEntity=ExclusionsCategory::class, mappedBy="category", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  178.      * @Groups({"category:read", "category:read:exclusionsCategories", "category:write"})
  179.      */
  180.     private $exclusionsCategories;
  181.     /**
  182.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="categories")
  183.      * @Groups({"category:read", "category:write"})
  184.      */
  185.     private $company;
  186.     public function __construct()
  187.     {
  188.         $this->channels = new ArrayCollection();
  189.         $this->slides = new ArrayCollection();
  190.         $this->children = new ArrayCollection();
  191.         $this->activeMenu false;
  192.         $this->exclusionsCategories = new ArrayCollection();
  193.     }
  194.     public function getId(): ?int
  195.     {
  196.         return $this->id;
  197.     }
  198.     public function getName(): ?string
  199.     {
  200.         return $this->name;
  201.     }
  202.     public function setName(string $name): self
  203.     {
  204.         $this->name $name;
  205.         return $this;
  206.     }
  207.     public function getContent(): ?string
  208.     {
  209.         return $this->content;
  210.     }
  211.     public function setContent(string $content): self
  212.     {
  213.         $this->content $content;
  214.         return $this;
  215.     }
  216.     public function getPicture(): ?MediaObject
  217.     {
  218.         return $this->picture;
  219.     }
  220.     public function setPicture(?MediaObject $picture): self
  221.     {
  222.         $this->picture $picture;
  223.         return $this;
  224.     }
  225.     /**
  226.      * @Groups({"category:write"})
  227.      */
  228.     public function setPictureFile($file null): self
  229.     {
  230.         if($file instanceof UploadedFile) {
  231.             $picture = empty($this->picture) ? new MediaObject $this->picture;
  232.             $picture->setFile($file);
  233.             $this->setPicture($picture);
  234.         }
  235.         return $this;
  236.     }
  237.     public function getIllustration(): ?MediaObject
  238.     {
  239.         return $this->illustration;
  240.     }
  241.     public function setIllustration(?MediaObject $illustration): self
  242.     {
  243.         $this->illustration $illustration;
  244.         return $this;
  245.     }
  246.     /**
  247.      * @Groups({"category:write"})
  248.      */
  249.     public function setIllustrationFile($file null): self
  250.     {
  251.         if($file instanceof UploadedFile) {
  252.             $illustration = empty($this->illustration) ? new MediaObject $this->illustration;
  253.             $illustration->setFile($file);
  254.             $this->setIllustration($illustration);
  255.         }
  256.         return $this;
  257.     }
  258.     public function getUpdatedAt(): ?\DateTimeInterface
  259.     {
  260.         return $this->updatedAt;
  261.     }
  262.     public function getCreatedAt(): ?\DateTimeInterface
  263.     {
  264.         return $this->createdAt;
  265.     }
  266.     /**
  267.      * @return Collection|Channel[]
  268.      */
  269.     public function getChannels(): Collection
  270.     {
  271.         return $this->channels;
  272.     }
  273.     public function addChannel(Channel $channel): self
  274.     {
  275.         if (!$this->channels->contains($channel)) {
  276.             $this->channels[] = $channel;
  277.             $channel->setCategory($this);
  278.         }
  279.         return $this;
  280.     }
  281.     public function removeChannel(Channel $channel): self
  282.     {
  283.         if ($this->channels->removeElement($channel)) {
  284.             // set the owning side to null (unless already changed)
  285.             if ($channel->getCategory() === $this) {
  286.                 $channel->setCategory(null);
  287.             }
  288.         }
  289.         return $this;
  290.     }
  291.     public function getActive(): ?bool
  292.     {
  293.         return $this->active;
  294.     }
  295.     public function setActive(bool $active): self
  296.     {
  297.         $this->active $active;
  298.         return $this;
  299.     }
  300.     public function getTvCompany(): ?TvCompany
  301.     {
  302.         return $this->tvCompany;
  303.     }
  304.     public function setTvCompany(?TvCompany $tvCompany): self
  305.     {
  306.         $this->tvCompany $tvCompany;
  307.         return $this;
  308.     }
  309.     public function getCompany(): ?Company
  310.     {
  311.         return $this->company;
  312.     }
  313.     public function setCompany(?Company $company): self
  314.     {
  315.         $this->company $company;
  316.         foreach ($this->getChannels() as $channel) {
  317.             $channel->setCompany($company);
  318.         }
  319.         return $this;
  320.     }
  321.     /**
  322.      * @return Collection|Slide[]
  323.      */
  324.     public function getSlides(): Collection
  325.     {
  326.         return $this->slides;
  327.     }
  328.     public function addSlide(Slide $slide): self
  329.     {
  330.         if (!$this->slides->contains($slide)) {
  331.             $this->slides[] = $slide;
  332.             $slide->setCategory($this);
  333.         }
  334.         return $this;
  335.     }
  336.     public function removeSlide(Slide $slide): self
  337.     {
  338.         if ($this->slides->removeElement($slide)) {
  339.             // set the owning side to null (unless already changed)
  340.             if ($slide->getCategory() === $this) {
  341.                 $slide->setCategory(null);
  342.             }
  343.         }
  344.         return $this;
  345.     }
  346.     public function getParent(): ?self
  347.     {
  348.         return $this->parent;
  349.     }
  350.     public function setParent(?self $parent): self
  351.     {
  352.         $this->parent $parent;
  353.         return $this;
  354.     }
  355.     /**
  356.      * @return Collection|self[]
  357.      */
  358.     public function getChildren(): Collection
  359.     {
  360.         return $this->children;
  361.     }
  362.     public function addChild(self $child): self
  363.     {
  364.         if (!$this->children->contains($child)) {
  365.             $this->children[] = $child;
  366.             $child->setParent($this);
  367.         }
  368.         return $this;
  369.     }
  370.     public function removeChild(self $child): self
  371.     {
  372.         if ($this->children->removeElement($child)) {
  373.             // set the owning side to null (unless already changed)
  374.             if ($child->getParent() === $this) {
  375.                 $child->setParent(null);
  376.             }
  377.         }
  378.         return $this;
  379.     }
  380.     public function getActiveMenu(): ?bool
  381.     {
  382.         return $this->activeMenu;
  383.     }
  384.     public function setActiveMenu(bool $activeMenu): self
  385.     {
  386.         $this->activeMenu $activeMenu;
  387.         return $this;
  388.     }
  389.     /**
  390.      * @return Collection|ExclusionsCategory[]
  391.      */
  392.     public function getExclusionsCategories(): Collection
  393.     {
  394.         return $this->exclusionsCategories;
  395.     }
  396.     public function addExclusionsCategory(ExclusionsCategory $exclusionsCategory): self
  397.     {
  398.         if (!$this->exclusionsCategories->contains($exclusionsCategory)) {
  399.             $this->exclusionsCategories[] = $exclusionsCategory;
  400.             $exclusionsCategory->setCategory($this);
  401.             // Ajout des Exclusions de toutes les chaines liées a cette Category
  402.             foreach($this->getChannels()->toArray() as $channel) {
  403.                 // Gestion des ExclusionsChannel
  404.                 $exclusionsChannel = new ExclusionsChannel;
  405.                 $exclusionsChannel
  406.                     ->setChannel($channel)
  407.                     ->setCompany($exclusionsCategory->getCompany())
  408.                     ->setCreatedAt(new \DateTime());
  409.                 // Vérifier si il en existe pas deja un
  410.                 $channel->addExclusionsChannel($exclusionsChannel);
  411.             }
  412.         }
  413.         return $this;
  414.     }
  415.     public function removeExclusionsCategory(ExclusionsCategory $exclusionsCategory): self
  416.     {
  417.         if ($this->exclusionsCategories->removeElement($exclusionsCategory)) {
  418.             // set the owning side to null (unless already changed)
  419.             if ($exclusionsCategory->getCategory() === $this) {
  420.                 $exclusionsCategory->setCategory(null);
  421.                 // Retrait des Exclusion de toutes les chaines liées a cette Category
  422.                 foreach($this->getChannels()->toArray() as $channel) {
  423.                     // Gestion des ExclusionsChannel
  424.                     foreach ($channel->getExclusionsChannels()->toArray() as $exclusionsChannel) {
  425.                         if($exclusionsChannel->getCompany() === $exclusionsCategory->getCompany()) {
  426.                             $channel->removeExclusionsChannel($exclusionsChannel);
  427.                         }
  428.                     }
  429.                 }
  430.             }
  431.         }
  432.         return $this;
  433.     }
  434.     /**
  435.      * @return int
  436.      */
  437.     public function getChannelsCount(): int
  438.     {
  439.         return $this->channelsCount;
  440.     }
  441.     /**
  442.      * @param int $channelsCount
  443.      * @return Category
  444.      */
  445.     public function setChannelsCount(int $channelsCount): Category
  446.     {
  447.         $this->channelsCount $channelsCount;
  448.         return $this;
  449.     }
  450.     /**
  451.      * @param bool $excluded
  452.      * @return Category
  453.      */
  454.     public function setExcluded(bool $excluded): Category
  455.     {
  456.         $this->excluded $excluded;
  457.         return $this;
  458.     }
  459.     /**
  460.      * @return bool
  461.      */
  462.     public function isExcluded(): bool
  463.     {
  464.         return $this->excluded;
  465.     }
  466.     public function getAppPicture(): ?MediaObject
  467.     {
  468.         return $this->appPicture;
  469.     }
  470.     public function setAppPicture(?MediaObject $appPicture): self
  471.     {
  472.         $this->appPicture $appPicture;
  473.         return $this;
  474.     }
  475.     /**
  476.      * @Groups({"category:write"})
  477.      */
  478.     public function setAppPictureFile($file null): self
  479.     {
  480.         if($file instanceof UploadedFile) {
  481.             $appPicture = empty($this->appPicture) ? new MediaObject $this->appPicture;
  482.             $appPicture->setFile($file);
  483.             $this->setAppPicture($appPicture);
  484.         }
  485.         return $this;
  486.     }
  487. }