src/Entity/Segmentation.php line 61

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\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  8. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  9. use App\Repository\SegmentationRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Gedmo\Mapping\Annotation as Gedmo;
  14. use Symfony\Component\Serializer\Annotation\SerializedName;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ORM\Entity(repositoryClass=SegmentationRepository::class)
  19.  * @ApiResource(
  20.  *      normalizationContext={"groups"={"segmentation:read"}},
  21.  *      denormalizationContext={"groups"={"segmentation:write"}},
  22.  *      itemOperations={
  23.  *          "get"={
  24.  *              "security"="is_granted('ROLE_USER')"
  25.  *          },
  26.  *          "patch"={
  27.  *              "security"="is_granted('ROLE_ADMIN')"
  28.  *          },
  29.  *          "delete"={
  30.  *              "security"="is_granted('ROLE_ADMIN')"
  31.  *          }
  32.  *      },
  33.  *      collectionOperations={
  34.  *          "get"={
  35.  *              "security"="is_granted('ROLE_USER')"
  36.  *          },
  37.  *          "post"={
  38.  *              "security"="is_granted('ROLE_ADMIN')"
  39.  *          }
  40.  *      }
  41.  * )
  42.  * @ApiFilter(SearchFilter::class)
  43.  * @ApiFilter(OrderFilter::class)
  44.  * @ApiFilter(GroupFilter::class, arguments={
  45.  *      "parameterName": "groups", 
  46.  *      "overrideDefaultGroups": true, 
  47.  *      "whitelist": {
  48.  *          "segmentation:form:read"
  49.  *      }
  50.  * }) 
  51.  * @ApiFilter(PropertyFilter::class, 
  52.  *      arguments={
  53.  *          "parameterName"="fields", 
  54.  *          "overrideDefaultProperties"=true
  55.  *     }
  56.  * )
  57.  */
  58. class Segmentation
  59. {
  60.     /**
  61.      * @ORM\Id
  62.      * @ORM\GeneratedValue
  63.      * @ORM\Column(type="integer")
  64.      * @Groups({"search", "segmentation:read", "client:read", "company:read", "client:read"})
  65.      */
  66.     private $id;
  67.     /**
  68.      * @Assert\NotBlank
  69.      * @Assert\NotNull
  70.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="segmentations")
  71.      * @Groups({"segmentation:read", "segmentation:write", "client:read"})
  72.      */
  73.     private $company;
  74.     /**
  75.      * @Assert\NotBlank
  76.      * @Assert\NotNull
  77.      * @ORM\Column(type="string", length=255, nullable=true)
  78.      * @Groups({"segmentation:read", "segmentation:write", "client:read", "company:read", "client:read"})
  79.      */
  80.     private $name;
  81.     /**
  82.      * @Assert\NotBlank
  83.      * @Assert\NotNull
  84.      * @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
  85.      * @Groups({"segmentation:read", "segmentation:write", "client:read", "company:read"})
  86.      */
  87.     private $alone = true;
  88.     /**
  89.      * @Assert\NotBlank
  90.      * @Assert\NotNull
  91.      * @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
  92.      * @Groups({"segmentation:read", "segmentation:write", "client:read", "company:read", "client:read"})
  93.      */
  94.     private $active = true;
  95.     /**
  96.      * Axe générant des classements challenge (pertinent uniquement sur les segmentations parentes)
  97.      * @ORM\Column(type="boolean", options={"default": 0})
  98.      * @Groups({"segmentation:read", "segmentation:write", "company:read", "client:read"})
  99.      */
  100.     private $ranked = false;
  101.     /**
  102.      * Ordre de l'axe dans le questionnaire de segmentation présenté à l'utilisateur.
  103.      * null par défaut ; les axes avec un rank défini sont demandés en premier, triés croissant.
  104.      * (`rank` est un mot réservé MySQL, d'où le name échappé)
  105.      * @ORM\Column(name="`rank`", type="integer", nullable=true)
  106.      * @Groups({"segmentation:read", "segmentation:write", "company:read", "client:read"})
  107.      */
  108.     private $rank = null;
  109.     /**
  110.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  111.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  112.      * @Gedmo\Timestampable(on="create")
  113.      */
  114.     private $createdAt;
  115.     /**
  116.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  117.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  118.      * @Gedmo\Timestampable(on="update")
  119.      */
  120.     private $updatedAt;
  121.     /**
  122.      * @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="children")
  123.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  124.      */
  125.     private $parent;
  126.     /**
  127.      * @ORM\OneToMany(targetEntity=Segmentation::class, mappedBy="parent")
  128.      * @Groups({"segmentation:read", "company:read"})
  129.      */
  130.     private $children;
  131.     /**
  132.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="segmentation")
  133.      * @Groups({"segmentation:read"})
  134.      */
  135.     private $users;
  136.     /**
  137.      * @ORM\ManyToMany(targetEntity=Event::class, mappedBy="segmentations")
  138.      * @Groups({"segmentation:read", "company:read"})
  139.      */
  140.     private $events;
  141.     /**
  142.      * @ORM\OneToMany(targetEntity=Client::class, mappedBy="segmentation")
  143.      * @Groups({"segmentation:read"})
  144.      */
  145.     private $clients;
  146.     /**
  147.      * @ORM\OneToOne(targetEntity=Department::class, mappedBy="segmentation")
  148.      */
  149.     private $department;
  150.     /**
  151.      * @var bool 
  152.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  153.      */
  154.     private $isParent = false;
  155.     
  156.     /**
  157.      * @var bool 
  158.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  159.      */
  160.     private $isChild = false;
  161.     
  162.     public function __construct()
  163.     {
  164.         $this->children = new ArrayCollection();
  165.         $this->users = new ArrayCollection();
  166.         $this->events = new ArrayCollection();
  167.         $this->clients = new ArrayCollection();
  168.     }
  169.     public function __toString(): string
  170.     {
  171.         return ''.$this->id.' - '.$this->name;
  172.     }
  173.     public function getId(): ?int
  174.     {
  175.         return $this->id;
  176.     }
  177.     public function getCompany(): ?Company
  178.     {
  179.         return $this->company;
  180.     }
  181.     public function setCompany(?Company $company): self
  182.     {
  183.         $this->company = $company;
  184.         return $this;
  185.     }
  186.     public function getName(): ?string
  187.     {
  188.         return $this->name;
  189.     }
  190.     public function setName(?string $name): self
  191.     {
  192.         $this->name = $name;
  193.         return $this;
  194.     }
  195.     public function getAlone(): ?bool
  196.     {
  197.         return $this->alone;
  198.     }
  199.     public function isAlone(): ?bool
  200.     {
  201.         return $this->alone;
  202.     }
  203.     public function setAlone(?bool $alone): self
  204.     {
  205.         $this->alone = $alone;
  206.         return $this;
  207.     }
  208.     public function getActive(): ?bool
  209.     {
  210.         return $this->active;
  211.     }
  212.     public function setActive(?bool $active): self
  213.     {
  214.         $this->active = $active;
  215.         return $this;
  216.     }
  217.     public function isRanked(): bool
  218.     {
  219.         return $this->ranked;
  220.     }
  221.     public function getRanked(): bool
  222.     {
  223.         return $this->ranked;
  224.     }
  225.     public function setRanked(bool $ranked): self
  226.     {
  227.         $this->ranked = $ranked;
  228.         return $this;
  229.     }
  230.     public function getRank(): ?int
  231.     {
  232.         return $this->rank;
  233.     }
  234.     public function setRank(?int $rank): self
  235.     {
  236.         $this->rank = $rank;
  237.         return $this;
  238.     }
  239.     public function getCreatedAt(): ?\DateTimeInterface
  240.     {
  241.         return $this->createdAt;
  242.     }
  243.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  244.     {
  245.         $this->createdAt = $createdAt;
  246.         return $this;
  247.     }
  248.     public function getUpdatedAt(): ?\DateTimeInterface
  249.     {
  250.         return $this->updatedAt;
  251.     }
  252.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  253.     {
  254.         $this->updatedAt = $updatedAt;
  255.         return $this;
  256.     }
  257.     public function getParent(): ?self
  258.     {
  259.         return $this->parent;
  260.     }
  261.     public function setParent(?self $parent): self
  262.     {
  263.         $this->parent = $parent;
  264.         return $this;
  265.     }
  266.     /**
  267.      * @return Collection<int, self>
  268.      */
  269.     public function getChildren(): Collection
  270.     {
  271.         return $this->children;
  272.     }
  273.     public function addChild(self $child): self
  274.     {
  275.         if (!$this->children->contains($child)) {
  276.             $this->children[] = $child;
  277.             $child->setParent($this);
  278.         }
  279.         return $this;
  280.     }
  281.     public function removeChild(self $child): self
  282.     {
  283.         if ($this->children->removeElement($child)) {
  284.             // set the owning side to null (unless already changed)
  285.             if ($child->getParent() === $this) {
  286.                 $child->setParent(null);
  287.             }
  288.         }
  289.         return $this;
  290.     }
  291.     public function isParent(): bool
  292.     {
  293.         $this->isParent = ($this->getChildren()->count() > 0) ? true : false;
  294.         return $this->isParent;
  295.     }
  296.     public function isChild(): bool
  297.     {
  298.         $this->isChild = (!empty($this->getParent())) ? true : false;
  299.         return $this->isChild;
  300.     }
  301.     public function getIsParent(): bool
  302.     {
  303.         return $this->isParent();
  304.     }
  305.     public function getIsChild(): bool
  306.     {
  307.         return $this->isChild();
  308.     }
  309.     /**
  310.      * @return Collection<int, User>
  311.      */
  312.     public function getUsers(): Collection
  313.     {
  314.         return $this->users;
  315.     }
  316.     public function addUser(User $user): self
  317.     {
  318.         if (!$this->users->contains($user)) {
  319.             $this->users[] = $user;
  320.             $user->setSegmentation($this);
  321.         }
  322.         return $this;
  323.     }
  324.     public function removeUser(User $user): self
  325.     {
  326.         if ($this->users->removeElement($user)) {
  327.             // set the owning side to null (unless already changed)
  328.             if ($user->getSegmentation() === $this) {
  329.                 $user->setSegmentation(null);
  330.             }
  331.         }
  332.         return $this;
  333.     }
  334.     /**
  335.      * @return Collection<int, Event>
  336.      */
  337.     public function getEvents(): Collection
  338.     {
  339.         return $this->events;
  340.     }
  341.     public function addEvent(Event $event): self
  342.     {
  343.         if (!$this->events->contains($event)) {
  344.             $this->events[] = $event;
  345.             $event->addSegmentation($this);
  346.         }
  347.         return $this;
  348.     }
  349.     public function removeEvent(Event $event): self
  350.     {
  351.         if ($this->events->removeElement($event)) {
  352.             $event->removeSegmentation($this);
  353.         }
  354.         return $this;
  355.     }
  356.     /**
  357.      * @Groups({"search"})
  358.      * @SerializedName("text")
  359.      * @return string
  360.      */
  361.     public function getText(): string
  362.     {
  363.         return (string)$this;
  364.     }
  365.     /**
  366.      * @return Collection<int, Client>
  367.      */
  368.     public function getClients(): Collection
  369.     {
  370.         return $this->clients;
  371.     }
  372.     public function addClient(Client $client): self
  373.     {
  374.         if (!$this->clients->contains($client)) {
  375.             $this->clients[] = $client;
  376.             $client->setSegmentation($this);
  377.         }
  378.         return $this;
  379.     }
  380.     public function removeClient(Client $client): self
  381.     {
  382.         if ($this->clients->removeElement($client)) {
  383.             // set the owning side to null (unless already changed)
  384.             if ($client->getSegmentation() === $this) {
  385.                 $client->setSegmentation(null);
  386.             }
  387.         }
  388.         return $this;
  389.     }
  390.     /**
  391.      * @return Collection<int, Event>
  392.      */
  393.     public function getActiveEvents(): Collection
  394.     {
  395.         $activeEvents = new ArrayCollection();
  396.         foreach ($this->events as $key => $event) {
  397.             if ($event->getStatus() == Event::STATUS_VALID) {
  398.                 $activeEvents->add($event);
  399.             }
  400.         }
  401.         return $activeEvents;
  402.     }
  403.     public function getDepartment(): ?Department
  404.     {
  405.         return $this->department;
  406.     }
  407.     public function setDepartment(?Department $department): self
  408.     {
  409.         $this->department = $department;
  410.         return $this;
  411.     }
  412. }