aboutsummaryrefslogtreecommitdiff
path: root/src/model/entities/Node.php
diff options
context:
space:
mode:
authorpolo-pc-greta <ordipolo@gmx.fr>2025-03-27 10:13:03 +0100
committerpolo-pc-greta <ordipolo@gmx.fr>2025-03-27 10:13:03 +0100
commitdf3612ed7e6691530503f79483d2fdbc032d01b8 (patch)
tree56d1c68fdc8625f5dad1937a654299d45142c79a /src/model/entities/Node.php
downloadcms-df3612ed7e6691530503f79483d2fdbc032d01b8.zip
mise en ligne github
Diffstat (limited to 'src/model/entities/Node.php')
-rw-r--r--src/model/entities/Node.php168
1 files changed, 168 insertions, 0 deletions
diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php
new file mode 100644
index 0000000..49e16ba
--- /dev/null
+++ b/src/model/entities/Node.php
@@ -0,0 +1,168 @@
1<?php
2// src/model/entities/Node.php
3
4declare(strict_types=1);
5
6namespace App\Entity;
7
8use Config;
9use Doctrine\ORM\Mapping as ORM;
10
11#[ORM\Entity]
12#[ORM\Table(name: TABLE_PREFIX . "node")]
13class Node
14{
15 #[ORM\Id]
16 #[ORM\GeneratedValue]
17 #[ORM\Column(type: "integer")]
18 private int $id_node;
19
20 #[ORM\Column(type: "string", length: 255)]
21 private string $name_node;
22
23 #[ORM\Column(type: "string", length: 255, unique: true, nullable: true)]
24 private ?string $article_timestamp;
25
26 #[ORM\Column(type: "json", nullable: true)] // type: "json" crée un longtext avec mariadb
27 private ?array $attributes = null;
28
29 #[ORM\Column(type: "integer")]
30 private int $position;
31
32 #[ORM\ManyToOne(targetEntity: self::class)]
33 //#[ORM\ManyToOne(targetEntity: self::class, fetch: 'EAGER')] // À TESTER
34 #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id_node", onDelete: "SET NULL", nullable: true)]
35 private ?self $parent = null;
36
37 #[ORM\ManyToOne(targetEntity: Page::class)]
38 #[ORM\JoinColumn(name: "page_id", referencedColumnName: "id_page", onDelete: "SET DEFAULT", nullable: true)]
39 private ?Page $page;
40
41 #[ORM\ManyToOne(targetEntity: Article::class, cascade: ['persist'])]
42 #[ORM\JoinColumn(name: "article_id", referencedColumnName: "id_article", onDelete: "SET NULL", nullable: true)]
43 private ?Article $article = null;
44
45 // propriété non mappée dans la table "node", la jointure est décrite dans NodeData
46 // elle sert à persister ou supprimer des données par cascade
47 // "mappedBy" permet de cibler $node dans l'autre classe, qui elle possède un "inversedBy"
48 #[ORM\OneToOne(targetEntity: NodeData::class, mappedBy: "node", cascade: ['persist', 'remove'])]
49 private ?NodeData $node_data = null;
50
51
52 // -- fin des attributs destinés à doctrine, début du code utilisateur --
53
54 private array $children = []; // tableau de Node
55 private ?self $temp_child = null; // = "new" est l'enfant de "main" lorsque la page est "article"
56
57 public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null)
58 {
59 $this->name_node = $name;
60 $this->article_timestamp = $article_timestamp;
61 $this->attributes = $attributes;
62 $this->position = $position;
63 $this->parent = $parent;
64 $this->page = $page;
65 $this->article = $article;
66 }
67
68 public function addChild(self $child): void
69 {
70 $this->children[] = $child;
71 $this->sortChildren();
72 }
73
74 // utiliser $position pour afficher les éléments dans l'ordre
75 private function sortChildren(): void
76 {
77 $iteration = count($this->children);
78 while($iteration > 1)
79 {
80 for($i = 0; $i < $iteration - 1; $i++)
81 {
82 //echo '<br>' . $this->children[$i]->getPosition() . ' - ' . $this->children[$i + 1]->getPosition();
83 if($this->children[$i]->getPosition() > $this->children[$i + 1]->getPosition())
84 {
85 $tmp = $this->children[$i];
86 $this->children[$i] = $this->children[$i + 1];
87 $this->children[$i + 1] = $tmp;
88 }
89 }
90 $iteration--;
91 }
92 }
93
94 // pfff...
95 public function getId(): int
96 {
97 return $this->id_node;
98 }
99 public function getName(): string
100 {
101 return $this->name_node;
102 }
103 /*public function setName(string $name): void
104 {
105 $this->name_node = $name;
106 }*/
107 public function getArticleTimestamp(): string
108 {
109 return $this->article_timestamp;
110 }
111 public function getAttributes(): array
112 {
113 return $this->attributes;
114 }
115 /*public function setAttributes(array $attributes): void
116 {
117 $this->attributes = $attributes;
118 }*/
119 public function getParent(): ?self
120 {
121 return $this->parent;
122 }
123 /*public function setParent(?self $parent): void
124 {
125 $this->parent = $parent;
126 }*/
127 public function getPosition(): int
128 {
129 return $this->position;
130 }
131 /*public function setPosition(int $position): void
132 {
133 $this->position = $position;
134 }*/
135 public function getPage(): Page
136 {
137 return $this->page;
138 }
139 /*public function setPage(Page $page): void
140 {
141 $this->page = $page;
142 }*/
143 public function getArticle(): Article
144 {
145 return $this->article;
146 }
147 /*public function setArticle(Article $article): void
148 {
149 $this->article = $article;
150 }*/
151 public function getNodeData(): ?NodeData
152 {
153 return $this->node_data;
154 }
155 public function getChildren(): array
156 {
157 return $this->children;
158 }
159
160 public function getTempChild(): ?self // peut renvoyer null
161 {
162 return $this->temp_child;
163 }
164 public function setTempChild(self $child): void
165 {
166 $this->temp_child = $child;
167 }
168}