<?php
// src/Entity/Objet.php
namespace App\Entity;
use App\Repository\ObjetRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ObjetRepository::class)]
#[ORM\Table(name: 'objet')]
#[ORM\UniqueConstraint(
name: 'uniq_objet_type_num',
columns: ['objet_type_id', 'numero']
)]
#[ORM\HasLifecycleCallbacks]
class Objet
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'objets')]
private ?Contact $contact = null;
#[ORM\Column(length: 255)]
private ?string $label = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date_creation = null;
#[ORM\OneToMany(mappedBy: 'objet', targetEntity: Dossier::class, cascade: ['remove'])]
private Collection $dossiers;
#[ORM\OneToMany(mappedBy: 'objet', targetEntity: VariableValue::class)]
private Collection $variableValues;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'objets')]
private ?Objet $objet = null;
#[ORM\OneToMany(mappedBy: 'objet', targetEntity: self::class, cascade: ['remove'])]
private Collection $objets;
#[ORM\ManyToOne(targetEntity: ObjetType::class, inversedBy: 'objets')]
#[ORM\JoinColumn(onDelete: 'CASCADE', nullable: true)]
private ?ObjetType $objet_type = null;
#[ORM\ManyToOne(inversedBy: 'objets')]
private ?User $utilisateur = null;
#[ORM\OneToMany(mappedBy: 'objet', targetEntity: Fichier::class)]
private Collection $fichiers;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $path = null;
#[ORM\Column(type: Types::INTEGER, options: ['default' => 1])]
private int $etat = 1;
#[ORM\Column(type: Types::BIGINT, nullable: true)]
private ?string $numero = null;
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $data = null;
private ?array $groupObjetTypeVariables = null;
private ?array $groupContactVariables = null;
private ?array $ancestors = null;
public function __construct()
{
$this->dossiers = new ArrayCollection();
$this->variableValues = new ArrayCollection();
$this->fichiers = new ArrayCollection();
$this->objets = new ArrayCollection();
}
// ---------------------
// Getters & Setters
// ---------------------
public function getId(): ?int
{
return $this->id;
}
public function getContact(): ?Contact
{
return $this->contact;
}
public function setContact(?Contact $contact): static
{
$this->contact = $contact;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): static
{
$this->label = ucfirst(trim($label));
return $this;
}
public function getDateCreation(): ?\DateTimeInterface
{
return $this->date_creation;
}
public function setDateCreation(\DateTimeInterface $date_creation): static
{
$this->date_creation = $date_creation;
return $this;
}
/**
* @return Collection<int, Dossier>
*/
public function getDossiers(): Collection
{
return $this->dossiers;
}
public function addDossier(Dossier $dossier): static
{
if (!$this->dossiers->contains($dossier)) {
$this->dossiers->add($dossier);
$dossier->setObjet($this);
}
return $this;
}
public function removeDossier(Dossier $dossier): static
{
if ($this->dossiers->removeElement($dossier)) {
if ($dossier->getObjet() === $this) {
$dossier->setObjet(null);
}
}
return $this;
}
/**
* @return Collection<int, VariableValue>
*/
public function getVariableValues(): Collection
{
return $this->variableValues;
}
public function addVariableValue(VariableValue $variableValue): static
{
if (!$this->variableValues->contains($variableValue)) {
$this->variableValues->add($variableValue);
$variableValue->setObjet($this);
}
return $this;
}
public function removeVariableValue(VariableValue $variableValue): static
{
if ($this->variableValues->removeElement($variableValue)) {
if ($variableValue->getObjet() === $this) {
$variableValue->setObjet(null);
}
}
return $this;
}
public function getObjet(): ?self
{
return $this->objet;
}
public function setObjet(?self $objet): static
{
$this->objet = $objet;
return $this;
}
public function setObjets(Collection $objets): static
{
$this->objets = $objets;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getObjets(): Collection
{
return $this->objets;
}
public function addObjet(self $objet): static
{
if (!$this->objets->contains($objet)) {
$this->objets->add($objet);
$objet->setObjet($this);
}
return $this;
}
public function removeObjet(self $objet): static
{
if ($this->objets->removeElement($objet)) {
if ($objet->getObjet() === $this) {
$objet->setObjet(null);
}
}
return $this;
}
public function getObjetType(): ?ObjetType
{
return $this->objet_type;
}
public function setObjetType(?ObjetType $objet_type): static
{
$this->objet_type = $objet_type;
return $this;
}
public function getUtilisateur(): ?User
{
return $this->utilisateur;
}
public function setUtilisateur(?User $utilisateur): static
{
$this->utilisateur = $utilisateur;
return $this;
}
/**
* @return Collection<int, Fichier>
*/
public function getFichiers(): Collection
{
return $this->fichiers;
}
public function addFichier(Fichier $fichier): static
{
if (!$this->fichiers->contains($fichier)) {
$this->fichiers->add($fichier);
$fichier->setObjet($this);
}
return $this;
}
public function removeFichier(Fichier $fichier): static
{
if ($this->fichiers->removeElement($fichier)) {
if ($fichier->getObjet() === $this) {
$fichier->setObjet(null);
}
}
return $this;
}
// ---------------------
// Path logic
// ---------------------
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): static
{
$this->path = $path;
return $this;
}
public function computePath(): void
{
$ids = [];
$parent = $this->getObjet();
while ($parent !== null) {
$ids[] = $parent->getId();
$parent = $parent->getObjet();
}
$ids = array_reverse($ids);
$ids[] = $this->getId();
$this->path = implode(',', $ids);
}
/**
* Set grouped objet type variables
*/
public function setGroupObjetTypeVariables(?array $groupObjetTypeVariables): self
{
$this->groupObjetTypeVariables = $groupObjetTypeVariables;
return $this;
}
/**
* Get grouped objet type variables
* Access in Twig: {{ objet.groupObjetTypeVariables }}
*/
public function getGroupObjetTypeVariables(): ?array
{
return $this->groupObjetTypeVariables;
}
/**
* Set grouped contact variables
*/
public function setGroupContactVariables(?array $groupContactVariables): self
{
$this->groupContactVariables = $groupContactVariables;
return $this;
}
/**
* Get grouped contact variables
* Access in Twig: {{ objet.groupContactVariables }}
*/
public function getGroupContactVariables(): ?array
{
return $this->groupContactVariables;
}
/**
* Set ancestors chain
*/
public function setAncestors(?array $ancestors): self
{
$this->ancestors = $ancestors;
return $this;
}
/**
* Get ancestors
* Access in Twig: {{ objet.ancestors }}
*/
public function getAncestors(): ?array
{
return $this->ancestors;
}
/**
* Group and sort variables by colonne (used by repository)
*/
public function getVariablesGroupedByColonne(array $variablesGroups): array
{
return $this->groupAndSortByColonne($variablesGroups);
}
/**
* Group and sort contact variables by colonne (used by repository)
*/
public function getContactVariablesGroupedByColonne(array $variablesGroups): array
{
return $this->groupAndSortByColonne($variablesGroups);
}
/**
* Helper: Group and sort VariablesGroups by colonne
*/
private function groupAndSortByColonne(array $variablesGroups): array
{
$grouped = [];
foreach ($variablesGroups as $group) {
$colonne = $group->getColonne() ?? 'default';
if (!isset($grouped[$colonne])) {
$grouped[$colonne] = [];
}
$grouped[$colonne][] = $group;
}
// Sort each colonne's groups
foreach ($grouped as &$groups) {
usort($groups, static function ($a, $b) {
return ($a->getOrdre() ?? 0) <=> ($b->getOrdre() ?? 0);
});
}
return $grouped;
}
public function getEtat(): int
{
return $this->etat;
}
public function setEtat(int $etat): static
{
$this->etat = $etat;
return $this;
}
public function getNumero(): ?string
{
return $this->numero;
}
public function setNumero(?string $numero): static
{
$this->numero = $numero;
return $this;
}
public function getData(): ?array
{
return $this->data;
}
public function setData(?array $data): static
{
$this->data = $data;
return $this;
}
}