<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\SegmentationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=SegmentationRepository::class)
* @ApiResource(
* normalizationContext={"groups"={"segmentation:read"}},
* denormalizationContext={"groups"={"segmentation:write"}},
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* }
* )
* @ApiFilter(SearchFilter::class)
* @ApiFilter(OrderFilter::class)
* @ApiFilter(GroupFilter::class, arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true,
* "whitelist": {
* "segmentation:form:read"
* }
* })
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Segmentation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"search", "segmentation:read", "client:read", "company:read", "client:read"})
*/
private $id;
/**
* @Assert\NotBlank
* @Assert\NotNull
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="segmentations")
* @Groups({"segmentation:read", "segmentation:write", "client:read"})
*/
private $company;
/**
* @Assert\NotBlank
* @Assert\NotNull
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"segmentation:read", "segmentation:write", "client:read", "company:read", "client:read"})
*/
private $name;
/**
* @Assert\NotBlank
* @Assert\NotNull
* @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
* @Groups({"segmentation:read", "segmentation:write", "client:read", "company:read"})
*/
private $alone = true;
/**
* @Assert\NotBlank
* @Assert\NotNull
* @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
* @Groups({"segmentation:read", "segmentation:write", "client:read", "company:read", "client:read"})
*/
private $active = true;
/**
* @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
* @Groups({"segmentation:read", "segmentation:write", "company:read"})
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true, options={"default": "CURRENT_TIMESTAMP"})
* @Groups({"segmentation:read", "segmentation:write", "company:read"})
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="children")
* @Groups({"segmentation:read", "segmentation:write", "company:read"})
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=Segmentation::class, mappedBy="parent")
* @Groups({"segmentation:read", "company:read"})
*/
private $children;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="segmentation")
* @Groups({"segmentation:read"})
*/
private $users;
/**
* @ORM\ManyToMany(targetEntity=Event::class, mappedBy="segmentations")
* @Groups({"segmentation:read", "company:read"})
*/
private $events;
/**
* @ORM\OneToMany(targetEntity=Client::class, mappedBy="segmentation")
* @Groups({"segmentation:read"})
*/
private $clients;
/**
* @ORM\OneToOne(targetEntity=Department::class, mappedBy="segmentation")
*/
private $department;
/**
* @var bool
* @Groups({"segmentation:read", "segmentation:write", "company:read"})
*/
private $isParent = false;
/**
* @var bool
* @Groups({"segmentation:read", "segmentation:write", "company:read"})
*/
private $isChild = false;
public function __construct()
{
$this->children = new ArrayCollection();
$this->users = new ArrayCollection();
$this->events = new ArrayCollection();
$this->clients = new ArrayCollection();
}
public function __toString(): string
{
return ''.$this->id.' - '.$this->name;
}
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getAlone(): ?bool
{
return $this->alone;
}
public function isAlone(): ?bool
{
return $this->alone;
}
public function setAlone(?bool $alone): self
{
$this->alone = $alone;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function isParent(): bool
{
$this->isParent = ($this->getChildren()->count() > 0) ? true : false;
return $this->isParent;
}
public function isChild(): bool
{
$this->isChild = (!empty($this->getParent())) ? true : false;
return $this->isChild;
}
public function getIsParent(): bool
{
return $this->isParent();
}
public function getIsChild(): bool
{
return $this->isChild();
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setSegmentation($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getSegmentation() === $this) {
$user->setSegmentation(null);
}
}
return $this;
}
/**
* @return Collection<int, Event>
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events[] = $event;
$event->addSegmentation($this);
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->removeElement($event)) {
$event->removeSegmentation($this);
}
return $this;
}
/**
* @Groups({"search"})
* @SerializedName("text")
* @return string
*/
public function getText(): string
{
return (string)$this;
}
/**
* @return Collection<int, Client>
*/
public function getClients(): Collection
{
return $this->clients;
}
public function addClient(Client $client): self
{
if (!$this->clients->contains($client)) {
$this->clients[] = $client;
$client->setSegmentation($this);
}
return $this;
}
public function removeClient(Client $client): self
{
if ($this->clients->removeElement($client)) {
// set the owning side to null (unless already changed)
if ($client->getSegmentation() === $this) {
$client->setSegmentation(null);
}
}
return $this;
}
/**
* @return Collection<int, Event>
*/
public function getActiveEvents(): Collection
{
$activeEvents = new ArrayCollection();
foreach ($this->events as $key => $event) {
if ($event->getStatus() == Event::STATUS_VALID) {
$activeEvents->add($event);
}
}
return $activeEvents;
}
public function getDepartment(): ?Department
{
return $this->department;
}
public function setDepartment(?Department $department): self
{
$this->department = $department;
return $this;
}
}