src/Entity/Objet.php line 19

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