src/Entity/BlogPostCategory.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BlogPostCategoryRepository;
  4. use App\Repository\BlogPostRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use App\Entity\Instance;
  10. use App\Entity\Domain;
  11. use App\Entity\SubDomain;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  14. /**
  15.  * @ORM\Entity(repositoryClass=BlogPostCategoryRepository::class)
  16.  * @UniqueEntity(fields={"slug"}, message="Slug deja utilisé")
  17.  */
  18. class BlogPostCategory implements WithQRCodeInterfacePositionedInterface
  19. {
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @ORM\Column(type="string", length=100)
  28.      */
  29.     private $title;
  30.     /**
  31.      * @ORM\Column(type="string", length=255, unique=true)
  32.      */
  33.     private $slug;
  34.     /**
  35.      * @ORM\Column(type="boolean")
  36.      */
  37.     private $isActivated false;
  38.     /**
  39.      * @ORM\Column(type="datetime")
  40.      */
  41.     private $createdAt;
  42.     /**
  43.      * @ORM\Column(type="datetime")
  44.      */
  45.     private $updatedAt;
  46.     /**
  47.      * @ORM\Column(type="integer")
  48.      */
  49.     private $position;
  50.     /**
  51.      * @ORM\Column(type="string", length=255, nullable=true)
  52.      */
  53.     private $qrCode;
  54.     /**
  55.      * @ORM\Column(type="boolean")
  56.      */
  57.     private $isPublic;
  58.     /**
  59.      * @ORM\OneToOne(targetEntity=Seo::class, cascade={"persist", "remove"})
  60.      */
  61.     private $seo;
  62.     /**
  63.      * @ORM\OneToMany(targetEntity=BlogPost::class, mappedBy="blogPostCategory")
  64.      */
  65.     private $blogPosts;
  66.     /**
  67.      * @ORM\ManyToMany(targetEntity=User::class, mappedBy="blogPostCategories")
  68.      */
  69.     private $users;
  70.     /**
  71.      * @ORM\ManyToMany(targetEntity=Roles::class)
  72.      */
  73.     private $roles;
  74.     /**
  75.      * @ORM\Column(type="boolean", nullable=true)
  76.      */
  77.     private $hasAllCriteria1;
  78.     /**
  79.      * @ORM\ManyToMany(targetEntity=Criteria1Item::class, inversedBy="blogPostCategories")
  80.      */
  81.     private $criteria1Items;
  82.     /**
  83.      * @ORM\Column(type="boolean", nullable=true)
  84.      */
  85.     private $hasAllCriteria2;
  86.     /**
  87.      * @ORM\ManyToMany(targetEntity=Criteria2Item::class, inversedBy="blogPostCategories")
  88.      */
  89.     private $criteria2Items;
  90.     /**
  91.      * @ORM\Column(type="boolean", nullable=true)
  92.      */
  93.     private $hasAllCriteria3;
  94.     /**
  95.      * @ORM\ManyToMany(targetEntity=Criteria3Item::class, inversedBy="blogPostCategories")
  96.      */
  97.     private $criteria3Items;
  98.     /**
  99.      * @ORM\Column(type="boolean", nullable=true)
  100.      */
  101.     private $hasAllCriteria4;
  102.     /**
  103.      * @ORM\ManyToMany(targetEntity=Criteria4Item::class, inversedBy="blogPostCategories")
  104.      * @ORM\JoinTable(name="blog_post_category_criteria4_item")
  105.      */
  106.     private $criteria4Items;
  107.     /**
  108.      * @ORM\Column(type="boolean", nullable=true)
  109.      */
  110.     private $hasAllCriteria5;
  111.     /**
  112.      * @ORM\ManyToMany(targetEntity=Criteria5Item::class, inversedBy="blogPostCategories")
  113.      * @ORM\JoinTable(name="blog_post_category_criteria5_item")
  114.      */
  115.     private $criteria5Items;
  116.     /**
  117.      * @ORM\ManyToMany(targetEntity=Instance::class, inversedBy="blogPostCategories")
  118.      */
  119.     private Collection $instances;
  120.     /**
  121.      * @ORM\Column(type="boolean", options={"default": false})
  122.      */
  123.     private $isHome false;
  124.     /**
  125.      * @ORM\Column(type="integer", options={"default": 0})
  126.      */
  127.     private int $subDomainPosition 0;
  128.     /**
  129.      * @ORM\Column(type="integer", options={"default": 0})
  130.      */
  131.     private int $homePosition 0;
  132.     /**
  133.      * Domaine parent (facilite les filtres & colonnes)
  134.      *
  135.      * @Assert\NotNull(message="Un domaine est obligatoire.")
  136.      *
  137.      * @ORM\ManyToOne(targetEntity=Domain::class)
  138.      * @ORM\JoinColumn(nullable=false)
  139.      */
  140.     private $domain;
  141.     /**
  142.      * Sous-domaine parent
  143.      *
  144.      * @Assert\NotNull(message="Un sous-domaine est obligatoire.")
  145.      *
  146.      * @ORM\ManyToOne(targetEntity=SubDomain::class)
  147.      * @ORM\JoinColumn(nullable=false)
  148.      */
  149.     private $subDomain;
  150.     /**
  151.      * BlogPostCategory constructor.
  152.      */
  153.     public function __construct()
  154.     {
  155.         $this->blogPosts = new ArrayCollection();
  156.         $this->createdAt = new \DateTime();
  157.         $this->updatedAt = new \DateTime();
  158.         $this->users = new ArrayCollection();
  159.         $this->roles = new ArrayCollection();
  160.         $this->criteria1Items = new ArrayCollection();
  161.         $this->criteria2Items = new ArrayCollection();
  162.         $this->criteria3Items = new ArrayCollection();
  163.         $this->criteria4Items = new ArrayCollection();
  164.         $this->criteria5Items = new ArrayCollection();
  165.         $this->instances = new ArrayCollection();
  166.     }
  167.     /**
  168.      * @return int|null
  169.      */
  170.     public function getId(): ?int
  171.     {
  172.         return $this->id;
  173.     }
  174.     /**
  175.      * @return string|null
  176.      */
  177.     public function getTitle(): ?string
  178.     {
  179.         return $this->title;
  180.     }
  181.     /**
  182.      * @param string $title
  183.      * @return $this
  184.      */
  185.     public function setTitle(string $title): self
  186.     {
  187.         $this->title $title;
  188.         return $this;
  189.     }
  190.     /**
  191.      * @return string|null
  192.      */
  193.     public function getSlug(): ?string
  194.     {
  195.         return $this->slug;
  196.     }
  197.     /**
  198.      * @param string $slug
  199.      * @return $this
  200.      */
  201.     public function setSlug(string $slug): self
  202.     {
  203.         $this->slug $slug;
  204.         return $this;
  205.     }
  206.     /**
  207.      * @return bool|null
  208.      */
  209.     public function getIsActivated(): ?bool
  210.     {
  211.         return $this->isActivated;
  212.     }
  213.     /**
  214.      * @param bool $isActivated
  215.      * @return $this
  216.      */
  217.     public function setIsActivated(bool $isActivated): self
  218.     {
  219.         $this->isActivated $isActivated;
  220.         return $this;
  221.     }
  222.     /**
  223.      * @return \DateTimeInterface|null
  224.      */
  225.     public function getCreatedAt(): ?\DateTimeInterface
  226.     {
  227.         return $this->createdAt;
  228.     }
  229.     /**
  230.      * @param \DateTimeInterface $createdAt
  231.      * @return $this
  232.      */
  233.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  234.     {
  235.         $this->createdAt $createdAt;
  236.         return $this;
  237.     }
  238.     /**
  239.      * @return \DateTimeInterface|null
  240.      */
  241.     public function getUpdatedAt(): ?\DateTimeInterface
  242.     {
  243.         return $this->updatedAt;
  244.     }
  245.     /**
  246.      * @param \DateTimeInterface $updatedAt
  247.      * @return $this
  248.      */
  249.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  250.     {
  251.         $this->updatedAt $updatedAt;
  252.         return $this;
  253.     }
  254.     /**
  255.      * @return Collection<int, BlogPost>
  256.      */
  257.     public function getBlogPosts(): Collection
  258.     {
  259.         return $this->blogPosts;
  260.     }
  261.     public function addBlogPost(BlogPost $blogPost): self
  262.     {
  263.         if (!$this->blogPosts->contains($blogPost)) {
  264.             $this->blogPosts[] = $blogPost;
  265.             $blogPost->setBlogPostCategory($this);
  266.         }
  267.         return $this;
  268.     }
  269.     public function removeBlogPost(BlogPost $blogPost): self
  270.     {
  271.         if ($this->blogPosts->removeElement($blogPost)) {
  272.             // set the owning side to null (unless already changed)
  273.             if ($blogPost->getBlogPostCategory() === $this) {
  274.                 $blogPost->setBlogPostCategory(null);
  275.             }
  276.         }
  277.         return $this;
  278.     }
  279.     public function getPosition(): ?int
  280.     {
  281.         return $this->position;
  282.     }
  283.     public function setPosition(int $position): self
  284.     {
  285.         $this->position $position;
  286.         return $this;
  287.     }
  288.     public function getQrCode(): ?string
  289.     {
  290.         return $this->qrCode;
  291.     }
  292.     public function setQrCode(string $qrCode): self
  293.     {
  294.         $this->qrCode $qrCode;
  295.         return $this;
  296.     }
  297.     public function getQrCodeRouteName(): string
  298.     {
  299.         return 'categorie';
  300.     }
  301.     public function getQrCodeRouteParams(): array
  302.     {
  303.         return [
  304.             'slug' => $this->getSlug(),
  305.         ];
  306.     }
  307.     public function getIsPublic(): ?bool
  308.     {
  309.         return $this->isPublic;
  310.     }
  311.     public function setIsPublic(bool $isPublic): self
  312.     {
  313.         $this->isPublic $isPublic;
  314.         return $this;
  315.     }
  316.     /**
  317.      * @return Collection<int, User>
  318.      */
  319.     public function getUsers(): Collection
  320.     {
  321.         return $this->users;
  322.     }
  323.     public function addUser(User $user): self
  324.     {
  325.         if (!$this->users->contains($user)) {
  326.             $this->users[] = $user;
  327.         }
  328.         return $this;
  329.     }
  330.     public function removeUser(User $user): self
  331.     {
  332.         if ($this->users->contains($user)) {
  333.             $this->users->removeElement($user);
  334.         }
  335.         return $this;
  336.     }
  337.     public function getSeo(): ?Seo
  338.     {
  339.         return $this->seo;
  340.     }
  341.     public function setSeo(?Seo $seo): self
  342.     {
  343.         $this->seo $seo;
  344.         return $this;
  345.     }
  346.     /**
  347.      * @return Collection<int, Roles>
  348.      */
  349.     public function getRoles(): Collection
  350.     {
  351.         return $this->roles;
  352.     }
  353.     public function addRole(Roles $role): self
  354.     {
  355.         if (!$this->roles->contains($role)) {
  356.             $this->roles[] = $role;
  357.         }
  358.         return $this;
  359.     }
  360.     public function removeRole(Roles $role): self
  361.     {
  362.         $this->roles->removeElement($role);
  363.         return $this;
  364.     }
  365.     public function isHasAllCriteria1(): ?bool
  366.     {
  367.         return $this->hasAllCriteria1;
  368.     }
  369.     public function setHasAllCriteria1(?bool $hasAllCriteria1): self
  370.     {
  371.         $this->hasAllCriteria1 $hasAllCriteria1;
  372.         return $this;
  373.     }
  374.     /**
  375.      * @return Collection<int, Criteria1Item>
  376.      */
  377.     public function getCriteria1Items(): Collection
  378.     {
  379.         return $this->criteria1Items;
  380.     }
  381.     public function addCriteria1Item(Criteria1Item $criteria1Item): self
  382.     {
  383.         if (!$this->criteria1Items->contains($criteria1Item)) {
  384.             $this->criteria1Items[] = $criteria1Item;
  385.         }
  386.         return $this;
  387.     }
  388.     public function removeCriteria1Item(Criteria1Item $criteria1Item): self
  389.     {
  390.         $this->criteria1Items->removeElement($criteria1Item);
  391.         return $this;
  392.     }
  393.     public function isHasAllCriteria2(): ?bool
  394.     {
  395.         return $this->hasAllCriteria2;
  396.     }
  397.     public function setHasAllCriteria2(?bool $hasAllCriteria2): self
  398.     {
  399.         $this->hasAllCriteria2 $hasAllCriteria2;
  400.         return $this;
  401.     }
  402.     /**
  403.      * @return Collection<int, Criteria2Item>
  404.      */
  405.     public function getCriteria2Items(): Collection
  406.     {
  407.         return $this->criteria2Items;
  408.     }
  409.     public function addCriteria2Item(Criteria2Item $criteria2Item): self
  410.     {
  411.         if (!$this->criteria2Items->contains($criteria2Item)) {
  412.             $this->criteria2Items[] = $criteria2Item;
  413.         }
  414.         return $this;
  415.     }
  416.     public function removeCriteria2Item(Criteria2Item $criteria2Item): self
  417.     {
  418.         $this->criteria2Items->removeElement($criteria2Item);
  419.         return $this;
  420.     }
  421.     public function isHasAllCriteria3(): ?bool
  422.     {
  423.         return $this->hasAllCriteria3;
  424.     }
  425.     public function setHasAllCriteria3(?bool $hasAllCriteria3): self
  426.     {
  427.         $this->hasAllCriteria3 $hasAllCriteria3;
  428.         return $this;
  429.     }
  430.     /**
  431.      * @return Collection<int, Criteria3Item>
  432.      */
  433.     public function getCriteria3Items(): Collection
  434.     {
  435.         return $this->criteria3Items;
  436.     }
  437.     public function addCriteria3Item(Criteria3Item $criteria3Item): self
  438.     {
  439.         if (!$this->criteria3Items->contains($criteria3Item)) {
  440.             $this->criteria3Items[] = $criteria3Item;
  441.         }
  442.         return $this;
  443.     }
  444.     public function removeCriteria3Item(Criteria3Item $criteria3Item): self
  445.     {
  446.         $this->criteria3Items->removeElement($criteria3Item);
  447.         return $this;
  448.     }
  449.     public function isHasAllCriteria4(): ?bool
  450.     {
  451.         return $this->hasAllCriteria4;
  452.     }
  453.     public function setHasAllCriteria4(?bool $hasAllCriteria4): self
  454.     {
  455.         $this->hasAllCriteria4 $hasAllCriteria4;
  456.         return $this;
  457.     }
  458.     /**
  459.      * @return Collection<int, Criteria4Item>
  460.      */
  461.     public function getCriteria4Items(): Collection
  462.     {
  463.         return $this->criteria4Items;
  464.     }
  465.     public function addCriteria4Item(Criteria4Item $criteria4Item): self
  466.     {
  467.         if (!$this->criteria4Items->contains($criteria4Item)) {
  468.             $this->criteria4Items[] = $criteria4Item;
  469.         }
  470.         return $this;
  471.     }
  472.     public function removeCriteria4Item(Criteria4Item $criteria4Item): self
  473.     {
  474.         $this->criteria4Items->removeElement($criteria4Item);
  475.         return $this;
  476.     }
  477.     public function isHasAllCriteria5(): ?bool
  478.     {
  479.         return $this->hasAllCriteria5;
  480.     }
  481.     public function setHasAllCriteria5(?bool $hasAllCriteria5): self
  482.     {
  483.         $this->hasAllCriteria5 $hasAllCriteria5;
  484.         return $this;
  485.     }
  486.     /**
  487.      * @return Collection<int, Criteria5Item>
  488.      */
  489.     public function getCriteria5Items(): Collection
  490.     {
  491.         return $this->criteria5Items;
  492.     }
  493.     public function addCriteria5Item(Criteria5Item $criteria5Item): self
  494.     {
  495.         if (!$this->criteria5Items->contains($criteria5Item)) {
  496.             $this->criteria5Items[] = $criteria5Item;
  497.         }
  498.         return $this;
  499.     }
  500.     public function removeCriteria5Item(Criteria5Item $criteria5Item): self
  501.     {
  502.         $this->criteria5Items->removeElement($criteria5Item);
  503.         return $this;
  504.     }
  505.     public function __toString(): string
  506.     {
  507.         return $this->getTitle() ?? '';
  508.     }
  509.     /**
  510.      * @return Collection<int, Instance>
  511.      */
  512.     public function getInstances(): Collection
  513.     {
  514.         return $this->instances;
  515.     }
  516.     public function addInstance(Instance $instance): self
  517.     {
  518.         if (!$this->instances->contains($instance)) {
  519.             $this->instances[] = $instance;
  520.         }
  521.         return $this;
  522.     }
  523.     public function removeInstance(Instance $instance): self
  524.     {
  525.         $this->instances->removeElement($instance);
  526.         return $this;
  527.     }
  528.     public function getInstancesList(): string
  529.     {
  530.         return implode(', '$this->instances->toArray());
  531.     }
  532.     public function isHome(): bool
  533.     {
  534.         return (bool) $this->isHome;
  535.     }
  536.     public function setIsHome(bool $isHome): self
  537.     {
  538.         $this->isHome $isHome;
  539.         return $this;
  540.     }
  541.     public function getBlogPostsCount(): int
  542.     {
  543.         return $this->blogPosts $this->blogPosts->count() : 0;
  544.     }
  545.     public function getSubDomainPosition(): int
  546.     {
  547.         return $this->subDomainPosition;
  548.     }
  549.     public function setSubDomainPosition(int $subDomainPosition): self
  550.     {
  551.         $this->subDomainPosition $subDomainPosition;
  552.         return $this;
  553.     }
  554.     public function getHomePosition(): int
  555.     {
  556.         return $this->homePosition;
  557.     }
  558.     public function setHomePosition(int $homePosition): self
  559.     {
  560.         $this->homePosition $homePosition;
  561.         return $this;
  562.     }
  563.     public function getDomain(): ?Domain
  564.     {
  565.         return $this->domain;
  566.     }
  567.     public function setDomain(?Domain $domain): self
  568.     {
  569.         $this->domain $domain;
  570.         // Si l'admin change le domaine et que le sous-domaine courant n'appartient plus à ce domaine,
  571.         // on le vide pour forcer une re-sélection (et donc bloquer la sauvegarde).
  572.         if (null !== $domain && null !== $this->subDomain) {
  573.             $sdDomain $this->subDomain->getDomain();
  574.             if (null !== $sdDomain && $sdDomain->getId() !== $domain->getId()) {
  575.                 $this->subDomain null;
  576.             }
  577.         }
  578.         return $this;
  579.     }
  580.     public function getSubDomain(): ?SubDomain
  581.     {
  582.         return $this->subDomain;
  583.     }
  584.     public function setSubDomain(?SubDomain $subDomain): self
  585.     {
  586.         $this->subDomain $subDomain;
  587.         // Si un sous-domaine est défini, on synchronise le domaine parent
  588.         if ($subDomain && $subDomain->getDomain()) {
  589.             $this->domain $subDomain->getDomain();
  590.         }
  591.         return $this;
  592.     }
  593.     /**
  594.      * @Assert\Callback
  595.      */
  596.     public function validateDiffusionConsistency(ExecutionContextInterface $context): void
  597.     {
  598.         if ($this->getIsPublic() === false && $this->getRoles()->count() === 0) {
  599.             $context->buildViolation('Selectionnez au moins un role ou activez "Tout Public".')
  600.                 ->atPath('roles')
  601.                 ->addViolation();
  602.         }
  603.         for ($i 1$i <= 5; ++$i) {
  604.             $hasAllGetter sprintf('isHasAllCriteria%d'$i);
  605.             $itemsGetter sprintf('getCriteria%dItems'$i);
  606.             $itemsPath sprintf('criteria%dItems'$i);
  607.             if ($this->{$hasAllGetter}() === false && $this->{$itemsGetter}()->count() === 0) {
  608.                 $context->buildViolation('Selectionnez au moins une valeur ou activez le mode "Tout".')
  609.                     ->atPath($itemsPath)
  610.                     ->addViolation();
  611.             }
  612.         }
  613.     }
  614. }