src/Entity/Objet.php line 20

Open in your IDE?
  1. <?php
  2. // src/Entity/Objet.php
  3. namespace App\Entity;
  4. use App\Repository\ObjetRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassObjetRepository::class)]
  10. #[ORM\Table(name'objet')]
  11. #[ORM\Index(name'idx_objet_type_etat_date'columns: ['objet_type_id''etat''date_creation'])]
  12. #[ORM\UniqueConstraint(
  13.     name'uniq_objet_type_num',
  14.     columns: ['objet_type_id''numero']
  15. )]
  16. #[ORM\HasLifecycleCallbacks]
  17. class Objet
  18. {
  19.     #[ORM\Id]
  20.     #[ORM\GeneratedValue]
  21.     #[ORM\Column]
  22.     private ?int $id null;
  23.     #[ORM\ManyToOne(inversedBy'objets')]
  24.     private ?Contact $contact null;
  25.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  26.     private ?\DateTimeInterface $date_creation null;
  27.     #[ORM\Column(name'date_modification'typeTypes::DATETIME_MUTABLEnullabletrue)]
  28.     private ?\DateTimeInterface $datemodification null;
  29.     #[ORM\OneToMany(mappedBy'objet'targetEntityDossier::class, cascade: ['remove'])]
  30.     private Collection $dossiers;
  31.     #[ORM\OneToMany(mappedBy'objet'targetEntityVariableValue::class)]
  32.     private Collection $variableValues;
  33.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'objets')]
  34.     private ?Objet $objet null;
  35.     #[ORM\OneToMany(mappedBy'objet'targetEntityself::class, cascade: ['remove'])]
  36.     private Collection $objets;
  37.     #[ORM\ManyToOne(targetEntityObjetType::class, inversedBy'objets')]
  38.     #[ORM\JoinColumn(onDelete'CASCADE'nullabletrue)]
  39.     private ?ObjetType $objet_type null;
  40.     #[ORM\ManyToOne(inversedBy'objets')]
  41.     private ?User $utilisateur null;
  42.     #[ORM\OneToMany(mappedBy'objet'targetEntityFichier::class)]
  43.     private Collection $fichiers;
  44.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  45.     private ?string $path null;
  46.     #[ORM\Column(typeTypes::INTEGERoptions: ['default' => 1])]
  47.     private int $etat 1;
  48.     #[ORM\Column(typeTypes::BIGINTnullabletrue)]
  49.     private ?string $numero null;
  50.     #[ORM\Column(typeTypes::JSONnullabletrue)]
  51.     private ?array $data null;
  52.     private ?array $groupObjetTypeVariables null;
  53.     private ?array $groupContactVariables null;
  54.     private ?array $ancestors null;
  55.     public function __construct()
  56.     {
  57.         $this->dossiers = new ArrayCollection();
  58.         $this->variableValues = new ArrayCollection();
  59.         $this->fichiers = new ArrayCollection();
  60.         $this->objets = new ArrayCollection();
  61.     }
  62.     // ---------------------
  63.     // Getters & Setters
  64.     // ---------------------
  65.     public function getId(): ?int
  66.     {
  67.         return $this->id;
  68.     }
  69.     public function getContact(): ?Contact
  70.     {
  71.         return $this->contact;
  72.     }
  73.     public function setContact(?Contact $contact): static
  74.     {
  75.         $this->contact $contact;
  76.         return $this;
  77.     }
  78.     public function getDateCreation(): ?\DateTimeInterface
  79.     {
  80.         return $this->date_creation;
  81.     }
  82.     public function setDateCreation(\DateTimeInterface $date_creation): static
  83.     {
  84.         $this->date_creation $date_creation;
  85.         return $this;
  86.     }
  87.     public function getDatemodification(): ?\DateTimeInterface
  88.     {
  89.         return $this->datemodification;
  90.     }
  91.     public function setDatemodification(?\DateTimeInterface $datemodification): static
  92.     {
  93.         $this->datemodification $datemodification;
  94.         return $this;
  95.     }
  96.     /**
  97.      * @return Collection<int, Dossier>
  98.      */
  99.     public function getDossiers(): Collection
  100.     {
  101.         return $this->dossiers;
  102.     }
  103.     public function addDossier(Dossier $dossier): static
  104.     {
  105.         if (!$this->dossiers->contains($dossier)) {
  106.             $this->dossiers->add($dossier);
  107.             $dossier->setObjet($this);
  108.         }
  109.         return $this;
  110.     }
  111.     public function removeDossier(Dossier $dossier): static
  112.     {
  113.         if ($this->dossiers->removeElement($dossier)) {
  114.             if ($dossier->getObjet() === $this) {
  115.                 $dossier->setObjet(null);
  116.             }
  117.         }
  118.         return $this;
  119.     }
  120.     /**
  121.      * @return Collection<int, VariableValue>
  122.      */
  123.     public function getVariableValues(): Collection
  124.     {
  125.         return $this->variableValues;
  126.     }
  127.     public function addVariableValue(VariableValue $variableValue): static
  128.     {
  129.         if (!$this->variableValues->contains($variableValue)) {
  130.             $this->variableValues->add($variableValue);
  131.             $variableValue->setObjet($this);
  132.         }
  133.         return $this;
  134.     }
  135.     public function removeVariableValue(VariableValue $variableValue): static
  136.     {
  137.         if ($this->variableValues->removeElement($variableValue)) {
  138.             if ($variableValue->getObjet() === $this) {
  139.                 $variableValue->setObjet(null);
  140.             }
  141.         }
  142.         return $this;
  143.     }
  144.     public function getObjet(): ?self
  145.     {
  146.         return $this->objet;
  147.     }
  148.     public function setObjet(?self $objet): static
  149.     {
  150.         $this->objet $objet;
  151.         return $this;
  152.     }
  153.     public function setObjets(Collection $objets): static
  154.     {
  155.         $this->objets $objets;
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return Collection<int, self>
  160.      */
  161.     public function getObjets(): Collection
  162.     {
  163.         return $this->objets;
  164.     }
  165.     public function addObjet(self $objet): static
  166.     {
  167.         if (!$this->objets->contains($objet)) {
  168.             $this->objets->add($objet);
  169.             $objet->setObjet($this);
  170.         }
  171.         return $this;
  172.     }
  173.     public function removeObjet(self $objet): static
  174.     {
  175.         if ($this->objets->removeElement($objet)) {
  176.             if ($objet->getObjet() === $this) {
  177.                 $objet->setObjet(null);
  178.             }
  179.         }
  180.         return $this;
  181.     }
  182.     public function getObjetType(): ?ObjetType
  183.     {
  184.         return $this->objet_type;
  185.     }
  186.     public function setObjetType(?ObjetType $objet_type): static
  187.     {
  188.         $this->objet_type $objet_type;
  189.         return $this;
  190.     }
  191.     public function getUtilisateur(): ?User
  192.     {
  193.         return $this->utilisateur;
  194.     }
  195.     public function setUtilisateur(?User $utilisateur): static
  196.     {
  197.         $this->utilisateur $utilisateur;
  198.         return $this;
  199.     }
  200.     
  201.     /**
  202.      * @return Collection<int, Fichier>
  203.      */
  204.     public function getFichiers(): Collection
  205.     {
  206.         return $this->fichiers;
  207.     }
  208.     public function addFichier(Fichier $fichier): static
  209.     {
  210.         if (!$this->fichiers->contains($fichier)) {
  211.             $this->fichiers->add($fichier);
  212.             $fichier->setObjet($this);
  213.         }
  214.         return $this;
  215.     }
  216.     public function removeFichier(Fichier $fichier): static
  217.     {
  218.         if ($this->fichiers->removeElement($fichier)) {
  219.             if ($fichier->getObjet() === $this) {
  220.                 $fichier->setObjet(null);
  221.             }
  222.         }
  223.         return $this;
  224.     }
  225.     // ---------------------
  226.     // Path logic
  227.     // ---------------------
  228.     public function getPath(): ?string
  229.     {
  230.         return $this->path;
  231.     }
  232.     public function setPath(?string $path): static
  233.     {
  234.         $this->path $path;
  235.         return $this;
  236.     }
  237.     public function computePath(): void
  238.     {
  239.         $ids = [];
  240.         $parent $this->getObjet();
  241.         while ($parent !== null) {
  242.             $ids[] = $parent->getId();
  243.             $parent $parent->getObjet();
  244.         }
  245.         $ids array_reverse($ids);
  246.         $ids[] = $this->getId();
  247.         $this->path implode(','$ids);
  248.     }
  249.     /**
  250.      * Set grouped objet type variables
  251.      */
  252.     public function setGroupObjetTypeVariables(?array $groupObjetTypeVariables): self
  253.     {
  254.         $this->groupObjetTypeVariables $groupObjetTypeVariables;
  255.         return $this;
  256.     }
  257.     /**
  258.      * Get grouped objet type variables
  259.      * Access in Twig: {{ objet.groupObjetTypeVariables }}
  260.      * Lazily computes from objetType if not already set
  261.      */
  262.     public function getGroupObjetTypeVariables(): ?array
  263.     {
  264.         if ($this->groupObjetTypeVariables === null && $this->objet_type !== null) {
  265.             $liaisons $this->objet_type->getAttributGroupeLiaisons();
  266.             if ($liaisons !== null && !$liaisons->isEmpty()) {
  267.                 $this->groupObjetTypeVariables $this->groupAndSortByColonneFromLiaisons($liaisons->toArray());
  268.             }
  269.         }
  270.         return $this->groupObjetTypeVariables;
  271.     }
  272.     /**
  273.      * Set grouped contact variables
  274.      */
  275.     public function setGroupContactVariables(?array $groupContactVariables): self
  276.     {
  277.         $this->groupContactVariables $groupContactVariables;
  278.         return $this;
  279.     }
  280.     /**
  281.      * Get grouped contact variables
  282.      * Access in Twig: {{ objet.groupContactVariables }}
  283.      */
  284.     public function getGroupContactVariables(): ?array
  285.     {
  286.         return $this->groupContactVariables;
  287.     }
  288.     /**
  289.      * Set ancestors chain
  290.      */
  291.     public function setAncestors(?array $ancestors): self
  292.     {
  293.         $this->ancestors $ancestors;
  294.         return $this;
  295.     }
  296.     /**
  297.      * Get ancestors
  298.      * Access in Twig: {{ objet.ancestors }}
  299.      */
  300.     public function getAncestors(): ?array
  301.     {
  302.         return $this->ancestors;
  303.     }
  304.     /**
  305.      * Group and sort variables by colonne from liaisons (used by repository)
  306.      */
  307.     public function getVariablesGroupedByColonneFromLiaisons(array $liaisons): array
  308.     {
  309.         return $this->groupAndSortByColonneFromLiaisons($liaisons);
  310.     }
  311.     /**
  312.      * Group and sort contact variables by colonne from liaisons (used by repository)
  313.      */
  314.     public function getContactVariablesGroupedByColonneFromLiaisons(array $liaisons): array
  315.     {
  316.         return $this->groupAndSortByColonneFromLiaisons($liaisons);
  317.     }
  318.     /**
  319.      * Group and sort variables by colonne (legacy - used by repository)
  320.      */
  321.     public function getVariablesGroupedByColonne(array $variablesGroups): array
  322.     {
  323.         return $this->groupAndSortByColonne($variablesGroups);
  324.     }
  325.     /**
  326.      * Group and sort contact variables by colonne (legacy - used by repository)
  327.      */
  328.     public function getContactVariablesGroupedByColonne(array $variablesGroups): array
  329.     {
  330.         return $this->groupAndSortByColonne($variablesGroups);
  331.     }
  332.     /**
  333.      * Helper: Group and sort VariablesGroups by colonne using liaisons
  334.      */
  335.     private function groupAndSortByColonneFromLiaisons(array $liaisons): array
  336.     {
  337.         $grouped = [];
  338.         foreach ($liaisons as $liaison) {
  339.             $colonne $liaison->getColonne() ?? 1;
  340.             if (!isset($grouped[$colonne])) {
  341.                 $grouped[$colonne] = [];
  342.             }
  343.             $grouped[$colonne][] = $liaison->getAttributGroupe();
  344.         }
  345.         // Liaisons are already sorted by ordre through the OrderBy annotation
  346.         return $grouped;
  347.     }
  348.     /**
  349.      * Helper: Group and sort VariablesGroups by colonne (legacy - kept for compatibility)
  350.      */
  351.     private function groupAndSortByColonne(array $variablesGroups): array
  352.     {
  353.         // For variables that don't come through liaisons, group them all in colonne 1
  354.         return [=> $variablesGroups];
  355.     }
  356.     public function getEtat(): int
  357.     {
  358.         return $this->etat;
  359.     }
  360.     public function setEtat(int $etat): static
  361.     {
  362.         $this->etat $etat;
  363.         return $this;
  364.     }
  365.     public function getNumero(): ?string
  366.     {
  367.         return $this->numero;
  368.     }
  369.     public function setNumero(?string $numero): static
  370.     {
  371.         $this->numero $numero;
  372.         return $this;
  373.     }
  374.     public function getData(): ?array
  375.     {
  376.         return $this->data;
  377.     }
  378.     public function setData(?array $data): static
  379.     {
  380.         $this->data $data;
  381.         return $this;
  382.     }
  383.     /**
  384.      * Get the public URL for this object
  385.      *
  386.      * @param string|null $baseUrl Base URL for the application
  387.      * @return string Full URL to object show page
  388.      */
  389.     public function getLienUrl(?string $baseUrl null): string
  390.     {
  391.         if ($baseUrl === null) {
  392.             // Auto-detect base URL from request if available
  393.             if (isset($_SERVER['HTTP_HOST'])) {
  394.                 $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' 'https' 'http';
  395.                 $baseUrl $scheme '://' $_SERVER['HTTP_HOST'];
  396.             } else {
  397.                 // Fallback to environment-based URL
  398.                 $env $_ENV['APP_ENV'] ?? 'dev';
  399.                 $baseUrl $env === 'prod'
  400.                     ? ($_ENV['APP_URL_PROD'] ?? 'https://evapi.click')
  401.                     : ($_ENV['APP_URL_DEV'] ?? 'http://localhost');
  402.             }
  403.         }
  404.         return rtrim($baseUrl'/') . '/objet/' $this->id;
  405.     }
  406.     /**
  407.      * Get QR code as base64 encoded PNG image
  408.      *
  409.      * @param int $size Image size in pixels (default: 150)
  410.      * @return string Base64 encoded PNG image with data URI prefix
  411.      */
  412.     public function getQrCode(int $size 150): string
  413.     {
  414.         $qrCodeGenerator = new \App\Service\QRCodeGenerator();
  415.         return $qrCodeGenerator->generateBase64($this->getLienUrl(), $size);
  416.     }
  417. }