<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\String\Slugger\AsciiSlugger;
use App\Entity\SubDomain;
use App\Repository\DomainRepository;
/**
* @ORM\Entity(repositoryClass=DomainRepository::class)
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"slug"}, message="Slug deja utilisé")
*/
class Domain
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* Titre du domaine (obligatoire)
* @ORM\Column(type="string", length=150, nullable=false)
* @Assert\NotBlank(message="Le titre est obligatoire.")
*/
private ?string $title = null;
/**
* Slug unique (auto-généré à partir du titre si vide)
* @ORM\Column(type="string", length=255, unique=true, nullable=false)
* @Assert\NotBlank(message="Le slug est obligatoire.")
*/
private ?string $slug = null;
/**
* Activé (switch)
* @ORM\Column(type="boolean", options={"default": false})
*/
private bool $isActivated = false;
/**
* Visible en Front Office (switch)
* @ORM\Column(type="boolean", options={"default": false})
*/
private bool $isPublic = false;
/**
* Visible en Front Office (liste des domaines)
* @ORM\Column(type="boolean", options={"default": true})
*/
private bool $isVisible = true;
/**
* Couleur de fond (nouvelle API)
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $backgroundColor = '#000000';
/**
* Couleur du titre (noir/blanc selon contraste)
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $titleColor = '#FFFFFF';
/**
* Rôles associés (sélecteur lorsque le switch n'est pas sur "tous")
* @ORM\ManyToMany(targetEntity=Roles::class)
*/
private Collection $roles;
/**
* Tous les rôles autorisés (switch)
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $hasAllRoles = null;
/**
* Critère 1 – tous (switch)
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $hasAllCriteria1 = null;
/**
* Critère 1 – éléments sélectionnés
* @ORM\ManyToMany(targetEntity=Criteria1Item::class)
*/
private Collection $criteria1Items;
/**
* Critère 2 – tous (switch)
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $hasAllCriteria2 = null;
/**
* Critère 2 – éléments sélectionnés
* @ORM\ManyToMany(targetEntity=Criteria2Item::class)
*/
private Collection $criteria2Items;
/**
* Critère 3 – tous (switch)
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $hasAllCriteria3 = null;
/**
* Critère 3 – éléments sélectionnés
* @ORM\ManyToMany(targetEntity=Criteria3Item::class)
*/
private Collection $criteria3Items;
/**
* Critère 4 – tous (switch)
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $hasAllCriteria4 = null;
/**
* Critère 4 – éléments sélectionnés
* @ORM\ManyToMany(targetEntity=Criteria4Item::class)
*/
private Collection $criteria4Items;
/**
* Critère 5 – tous (switch)
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $hasAllCriteria5 = null;
/**
* Critère 5 – éléments sélectionnés
* @ORM\ManyToMany(targetEntity=Criteria5Item::class)
*/
private Collection $criteria5Items;
/**
* Organisations (Instances) – obligatoire si multi-organisation
* @ORM\ManyToMany(targetEntity=Instance::class)
*/
private Collection $instances;
/**
* Sous-domaines rattachés
* @ORM\OneToMany(targetEntity=SubDomain::class, mappedBy="domain")
*/
private Collection $subDomains;
/**
* Métadatas (dates)
* @ORM\Column(type="datetime")
*/
private \DateTime $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private \DateTime $updatedAt;
public function __construct()
{
$this->roles = new ArrayCollection();
$this->criteria1Items = new ArrayCollection();
$this->criteria2Items = new ArrayCollection();
$this->criteria3Items = new ArrayCollection();
$this->criteria4Items = new ArrayCollection();
$this->criteria5Items = new ArrayCollection();
$this->instances = new ArrayCollection();
$this->subDomains = new ArrayCollection();
$this->createdAt = new \DateTime('now');
$this->updatedAt = new \DateTime('now');
}
public function __toString(): string
{
return $this->getTitle() ?? '';
}
/**
* @ORM\PrePersist
*/
public function onPrePersist(): void
{
$this->updatedAt = new \DateTime('now');
if (!$this->slug && $this->title) {
$slugger = new AsciiSlugger();
$this->slug = strtolower($slugger->slug($this->title)->toString());
}
}
/**
* @ORM\PreUpdate
*/
public function onPreUpdate(): void
{
$this->updatedAt = new \DateTime('now');
if (!$this->slug && $this->title) {
$slugger = new AsciiSlugger();
$this->slug = strtolower($slugger->slug($this->title)->toString());
}
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): self
{
$this->slug = $slug;
return $this;
}
public function isActivated(): bool
{
return $this->isActivated;
}
public function setIsActivated(bool $isActivated): self
{
$this->isActivated = $isActivated;
return $this;
}
public function isPublic(): bool
{
return $this->isPublic;
}
public function setIsPublic(bool $isPublic): self
{
$this->isPublic = $isPublic;
return $this;
}
public function isVisible(): bool
{
return $this->isVisible;
}
public function setIsVisible(bool $isVisible): self
{
$this->isVisible = $isVisible;
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
public function getBackgroundColor(): ?string
{
return $this->backgroundColor;
}
public function setBackgroundColor(?string $backgroundColor): self
{
$this->backgroundColor = $backgroundColor;
if ($backgroundColor && preg_match('/^#?[0-9a-fA-F]{6}$/', $backgroundColor)) {
$hex = ltrim($backgroundColor, '#');
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
$luminance = (0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255;
$this->titleColor = $luminance > 0.6 ? '#000000' : '#FFFFFF';
}
return $this;
}
public function getTitleColor(): ?string
{
return $this->titleColor;
}
public function setTitleColor(?string $titleColor): self
{
$this->titleColor = $titleColor;
return $this;
}
/**
* @return Collection<int, Roles>
*/
public function getRoles(): Collection
{
return $this->roles;
}
public function addRole(Roles $role): self
{
if (!$this->roles->contains($role)) {
$this->roles[] = $role;
}
return $this;
}
public function removeRole(Roles $role): self
{
$this->roles->removeElement($role);
return $this;
}
public function getHasAllRoles(): ?bool
{
return $this->hasAllRoles;
}
public function setHasAllRoles(?bool $hasAllRoles): self
{
$this->hasAllRoles = $hasAllRoles;
return $this;
}
public function isHasAllCriteria1(): ?bool
{
return $this->hasAllCriteria1;
}
public function setHasAllCriteria1(?bool $hasAllCriteria1): self
{
$this->hasAllCriteria1 = $hasAllCriteria1;
return $this;
}
/**
* @return Collection<int, Criteria1Item>
*/
public function getCriteria1Items(): Collection
{
return $this->criteria1Items;
}
public function addCriteria1Item(Criteria1Item $item): self
{
if (!$this->criteria1Items->contains($item)) {
$this->criteria1Items[] = $item;
}
return $this;
}
public function removeCriteria1Item(Criteria1Item $item): self
{
$this->criteria1Items->removeElement($item);
return $this;
}
public function isHasAllCriteria2(): ?bool
{
return $this->hasAllCriteria2;
}
public function setHasAllCriteria2(?bool $hasAllCriteria2): self
{
$this->hasAllCriteria2 = $hasAllCriteria2;
return $this;
}
/**
* @return Collection<int, Criteria2Item>
*/
public function getCriteria2Items(): Collection
{
return $this->criteria2Items;
}
public function addCriteria2Item(Criteria2Item $item): self
{
if (!$this->criteria2Items->contains($item)) {
$this->criteria2Items[] = $item;
}
return $this;
}
public function removeCriteria2Item(Criteria2Item $item): self
{
$this->criteria2Items->removeElement($item);
return $this;
}
public function isHasAllCriteria3(): ?bool
{
return $this->hasAllCriteria3;
}
public function setHasAllCriteria3(?bool $hasAllCriteria3): self
{
$this->hasAllCriteria3 = $hasAllCriteria3;
return $this;
}
/**
* @return Collection<int, Criteria3Item>
*/
public function getCriteria3Items(): Collection
{
return $this->criteria3Items;
}
public function addCriteria3Item(Criteria3Item $item): self
{
if (!$this->criteria3Items->contains($item)) {
$this->criteria3Items[] = $item;
}
return $this;
}
public function removeCriteria3Item(Criteria3Item $item): self
{
$this->criteria3Items->removeElement($item);
return $this;
}
public function isHasAllCriteria4(): ?bool
{
return $this->hasAllCriteria4;
}
public function setHasAllCriteria4(?bool $hasAllCriteria4): self
{
$this->hasAllCriteria4 = $hasAllCriteria4;
return $this;
}
/**
* @return Collection<int, Criteria4Item>
*/
public function getCriteria4Items(): Collection
{
return $this->criteria4Items;
}
public function addCriteria4Item(Criteria4Item $item): self
{
if (!$this->criteria4Items->contains($item)) {
$this->criteria4Items[] = $item;
}
return $this;
}
public function removeCriteria4Item(Criteria4Item $item): self
{
$this->criteria4Items->removeElement($item);
return $this;
}
public function isHasAllCriteria5(): ?bool
{
return $this->hasAllCriteria5;
}
public function setHasAllCriteria5(?bool $hasAllCriteria5): self
{
$this->hasAllCriteria5 = $hasAllCriteria5;
return $this;
}
/**
* @return Collection<int, Criteria5Item>
*/
public function getCriteria5Items(): Collection
{
return $this->criteria5Items;
}
public function addCriteria5Item(Criteria5Item $item): self
{
if (!$this->criteria5Items->contains($item)) {
$this->criteria5Items[] = $item;
}
return $this;
}
public function removeCriteria5Item(Criteria5Item $item): self
{
$this->criteria5Items->removeElement($item);
return $this;
}
/**
* @return Collection<int, Instance>
*/
public function getInstances(): Collection
{
return $this->instances;
}
public function addInstance(Instance $instance): self
{
if (!$this->instances->contains($instance)) {
$this->instances[] = $instance;
}
return $this;
}
public function removeInstance(Instance $instance): self
{
$this->instances->removeElement($instance);
return $this;
}
/**
* @return Collection<int, SubDomain>
*/
public function getSubDomains(): Collection
{
return $this->subDomains;
}
public function addSubDomain(SubDomain $subDomain): self
{
if (!$this->subDomains->contains($subDomain)) {
$this->subDomains[] = $subDomain;
}
return $this;
}
public function removeSubDomain(SubDomain $subDomain): self
{
$this->subDomains->removeElement($subDomain);
return $this;
}
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTime $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}