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.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  97.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  98.      * @Gedmo\Timestampable(on="create")
  99.      */
  100.     private $createdAt;
  101.     /**
  102.      * @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
  103.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  104.      * @Gedmo\Timestampable(on="update")
  105.      */
  106.     private $updatedAt;
  107.     /**
  108.      * @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="children")
  109.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  110.      */
  111.     private $parent;
  112.     /**
  113.      * @ORM\OneToMany(targetEntity=Segmentation::class, mappedBy="parent")
  114.      * @Groups({"segmentation:read", "company:read"})
  115.      */
  116.     private $children;
  117.     /**
  118.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="segmentation")
  119.      * @Groups({"segmentation:read"})
  120.      */
  121.     private $users;
  122.     /**
  123.      * @ORM\ManyToMany(targetEntity=Event::class, mappedBy="segmentations")
  124.      * @Groups({"segmentation:read", "company:read"})
  125.      */
  126.     private $events;
  127.     /**
  128.      * @ORM\OneToMany(targetEntity=Client::class, mappedBy="segmentation")
  129.      * @Groups({"segmentation:read"})
  130.      */
  131.     private $clients;
  132.     /**
  133.      * @ORM\OneToOne(targetEntity=Department::class, mappedBy="segmentation")
  134.      */
  135.     private $department;
  136.     /**
  137.      * @var bool 
  138.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  139.      */
  140.     private $isParent false;
  141.     
  142.     /**
  143.      * @var bool 
  144.      * @Groups({"segmentation:read", "segmentation:write", "company:read"})
  145.      */
  146.     private $isChild false;
  147.     
  148.     public function __construct()
  149.     {
  150.         $this->children = new ArrayCollection();
  151.         $this->users = new ArrayCollection();
  152.         $this->events = new ArrayCollection();
  153.         $this->clients = new ArrayCollection();
  154.     }
  155.     public function __toString(): string
  156.     {
  157.         return ''.$this->id.' - '.$this->name;
  158.     }
  159.     public function getId(): ?int
  160.     {
  161.         return $this->id;
  162.     }
  163.     public function getCompany(): ?Company
  164.     {
  165.         return $this->company;
  166.     }
  167.     public function setCompany(?Company $company): self
  168.     {
  169.         $this->company $company;
  170.         return $this;
  171.     }
  172.     public function getName(): ?string
  173.     {
  174.         return $this->name;
  175.     }
  176.     public function setName(?string $name): self
  177.     {
  178.         $this->name $name;
  179.         return $this;
  180.     }
  181.     public function getAlone(): ?bool
  182.     {
  183.         return $this->alone;
  184.     }
  185.     public function isAlone(): ?bool
  186.     {
  187.         return $this->alone;
  188.     }
  189.     public function setAlone(?bool $alone): self
  190.     {
  191.         $this->alone $alone;
  192.         return $this;
  193.     }
  194.     public function getActive(): ?bool
  195.     {
  196.         return $this->active;
  197.     }
  198.     public function setActive(?bool $active): self
  199.     {
  200.         $this->active $active;
  201.         return $this;
  202.     }
  203.     public function getCreatedAt(): ?\DateTimeInterface
  204.     {
  205.         return $this->createdAt;
  206.     }
  207.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  208.     {
  209.         $this->createdAt $createdAt;
  210.         return $this;
  211.     }
  212.     public function getUpdatedAt(): ?\DateTimeInterface
  213.     {
  214.         return $this->updatedAt;
  215.     }
  216.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  217.     {
  218.         $this->updatedAt $updatedAt;
  219.         return $this;
  220.     }
  221.     public function getParent(): ?self
  222.     {
  223.         return $this->parent;
  224.     }
  225.     public function setParent(?self $parent): self
  226.     {
  227.         $this->parent $parent;
  228.         return $this;
  229.     }
  230.     /**
  231.      * @return Collection<int, self>
  232.      */
  233.     public function getChildren(): Collection
  234.     {
  235.         return $this->children;
  236.     }
  237.     public function addChild(self $child): self
  238.     {
  239.         if (!$this->children->contains($child)) {
  240.             $this->children[] = $child;
  241.             $child->setParent($this);
  242.         }
  243.         return $this;
  244.     }
  245.     public function removeChild(self $child): self
  246.     {
  247.         if ($this->children->removeElement($child)) {
  248.             // set the owning side to null (unless already changed)
  249.             if ($child->getParent() === $this) {
  250.                 $child->setParent(null);
  251.             }
  252.         }
  253.         return $this;
  254.     }
  255.     public function isParent(): bool
  256.     {
  257.         $this->isParent = ($this->getChildren()->count() > 0) ? true false;
  258.         return $this->isParent;
  259.     }
  260.     public function isChild(): bool
  261.     {
  262.         $this->isChild = (!empty($this->getParent())) ? true false;
  263.         return $this->isChild;
  264.     }
  265.     public function getIsParent(): bool
  266.     {
  267.         return $this->isParent();
  268.     }
  269.     public function getIsChild(): bool
  270.     {
  271.         return $this->isChild();
  272.     }
  273.     /**
  274.      * @return Collection<int, User>
  275.      */
  276.     public function getUsers(): Collection
  277.     {
  278.         return $this->users;
  279.     }
  280.     public function addUser(User $user): self
  281.     {
  282.         if (!$this->users->contains($user)) {
  283.             $this->users[] = $user;
  284.             $user->setSegmentation($this);
  285.         }
  286.         return $this;
  287.     }
  288.     public function removeUser(User $user): self
  289.     {
  290.         if ($this->users->removeElement($user)) {
  291.             // set the owning side to null (unless already changed)
  292.             if ($user->getSegmentation() === $this) {
  293.                 $user->setSegmentation(null);
  294.             }
  295.         }
  296.         return $this;
  297.     }
  298.     /**
  299.      * @return Collection<int, Event>
  300.      */
  301.     public function getEvents(): Collection
  302.     {
  303.         return $this->events;
  304.     }
  305.     public function addEvent(Event $event): self
  306.     {
  307.         if (!$this->events->contains($event)) {
  308.             $this->events[] = $event;
  309.             $event->addSegmentation($this);
  310.         }
  311.         return $this;
  312.     }
  313.     public function removeEvent(Event $event): self
  314.     {
  315.         if ($this->events->removeElement($event)) {
  316.             $event->removeSegmentation($this);
  317.         }
  318.         return $this;
  319.     }
  320.     /**
  321.      * @Groups({"search"})
  322.      * @SerializedName("text")
  323.      * @return string
  324.      */
  325.     public function getText(): string
  326.     {
  327.         return (string)$this;
  328.     }
  329.     /**
  330.      * @return Collection<int, Client>
  331.      */
  332.     public function getClients(): Collection
  333.     {
  334.         return $this->clients;
  335.     }
  336.     public function addClient(Client $client): self
  337.     {
  338.         if (!$this->clients->contains($client)) {
  339.             $this->clients[] = $client;
  340.             $client->setSegmentation($this);
  341.         }
  342.         return $this;
  343.     }
  344.     public function removeClient(Client $client): self
  345.     {
  346.         if ($this->clients->removeElement($client)) {
  347.             // set the owning side to null (unless already changed)
  348.             if ($client->getSegmentation() === $this) {
  349.                 $client->setSegmentation(null);
  350.             }
  351.         }
  352.         return $this;
  353.     }
  354.     /**
  355.      * @return Collection<int, Event>
  356.      */
  357.     public function getActiveEvents(): Collection
  358.     {
  359.         $activeEvents = new ArrayCollection();
  360.         foreach ($this->events as $key => $event) {
  361.             if ($event->getStatus() == Event::STATUS_VALID) {
  362.                 $activeEvents->add($event);
  363.             }
  364.         }
  365.         return $activeEvents;
  366.     }
  367.     public function getDepartment(): ?Department
  368.     {
  369.         return $this->department;
  370.     }
  371.     public function setDepartment(?Department $department): self
  372.     {
  373.         $this->department $department;
  374.         return $this;
  375.     }
  376. }