<?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(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date_creation = null;
#[ORM\Column(name: 'date_modification', type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $datemodification = 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 getDateCreation(): ?\DateTimeInterface
{
return $this->date_creation;
}
public function setDateCreation(\DateTimeInterface $date_creation): static
{
$this->date_creation = $date_creation;
return $this;
}
public function getDatemodification(): ?\DateTimeInterface
{
return $this->datemodification;
}
public function setDatemodification(?\DateTimeInterface $datemodification): static
{
$this->datemodification = $datemodification;
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 }}
* Lazily computes from objetType if not already set
*/
public function getGroupObjetTypeVariables(): ?array
{
if ($this->groupObjetTypeVariables === null && $this->objet_type !== null) {
$liaisons = $this->objet_type->getAttributGroupeLiaisons();
if ($liaisons !== null && !$liaisons->isEmpty()) {
$this->groupObjetTypeVariables = $this->groupAndSortByColonneFromLiaisons($liaisons->toArray());
}
}
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 from liaisons (used by repository)
*/
public function getVariablesGroupedByColonneFromLiaisons(array $liaisons): array
{
return $this->groupAndSortByColonneFromLiaisons($liaisons);
}
/**
* Group and sort contact variables by colonne from liaisons (used by repository)
*/
public function getContactVariablesGroupedByColonneFromLiaisons(array $liaisons): array
{
return $this->groupAndSortByColonneFromLiaisons($liaisons);
}
/**
* Group and sort variables by colonne (legacy - used by repository)
*/
public function getVariablesGroupedByColonne(array $variablesGroups): array
{
return $this->groupAndSortByColonne($variablesGroups);
}
/**
* Group and sort contact variables by colonne (legacy - used by repository)
*/
public function getContactVariablesGroupedByColonne(array $variablesGroups): array
{
return $this->groupAndSortByColonne($variablesGroups);
}
/**
* Helper: Group and sort VariablesGroups by colonne using liaisons
*/
private function groupAndSortByColonneFromLiaisons(array $liaisons): array
{
$grouped = [];
foreach ($liaisons as $liaison) {
$colonne = $liaison->getColonne() ?? 1;
if (!isset($grouped[$colonne])) {
$grouped[$colonne] = [];
}
$grouped[$colonne][] = $liaison->getAttributGroupe();
}
// Liaisons are already sorted by ordre through the OrderBy annotation
return $grouped;
}
/**
* Helper: Group and sort VariablesGroups by colonne (legacy - kept for compatibility)
*/
private function groupAndSortByColonne(array $variablesGroups): array
{
// For variables that don't come through liaisons, group them all in colonne 1
return [1 => $variablesGroups];
}
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;
}
/**
* Get the public URL for this object
*
* @param string|null $baseUrl Base URL for the application
* @return string Full URL to object show page
*/
public function getLienUrl(?string $baseUrl = null): string
{
if ($baseUrl === null) {
// Auto-detect base URL from request if available
if (isset($_SERVER['HTTP_HOST'])) {
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$baseUrl = $scheme . '://' . $_SERVER['HTTP_HOST'];
} else {
// Fallback to environment-based URL
$env = $_ENV['APP_ENV'] ?? 'dev';
$baseUrl = $env === 'prod'
? ($_ENV['APP_URL_PROD'] ?? 'https://evapi.click')
: ($_ENV['APP_URL_DEV'] ?? 'http://localhost');
}
}
return rtrim($baseUrl, '/') . '/objet/' . $this->id;
}
/**
* Get QR code as base64 encoded PNG image
*
* @param int $size Image size in pixels (default: 150)
* @return string Base64 encoded PNG image with data URI prefix
*/
public function getQrCode(int $size = 150): string
{
$qrCodeGenerator = new \App\Service\QRCodeGenerator();
return $qrCodeGenerator->generateBase64($this->getLienUrl(), $size);
}
}