diff options
Diffstat (limited to 'src/controller/PageManagementController.php')
-rw-r--r-- | src/controller/PageManagementController.php | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/src/controller/PageManagementController.php b/src/controller/PageManagementController.php new file mode 100644 index 0000000..f84c528 --- /dev/null +++ b/src/controller/PageManagementController.php | |||
@@ -0,0 +1,264 @@ | |||
1 | <?php | ||
2 | // src/controller/PageManagementController.php | ||
3 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | use App\Entity\Page; | ||
7 | use App\Entity\Node; | ||
8 | use App\Entity\NodeData; | ||
9 | //use App\Entity\Image; | ||
10 | use Doctrine\Common\Collections\ArrayCollection; | ||
11 | use Doctrine\ORM\EntityManager; | ||
12 | |||
13 | class PageManagementController | ||
14 | { | ||
15 | /* -- partie page -- */ | ||
16 | static public function setPageTitle(EntityManager $entityManager, array $json): void | ||
17 | { | ||
18 | $page = $entityManager->find('App\Entity\Page', $json['page_id']); | ||
19 | $page->setPageName(htmlspecialchars($json['title'])); | ||
20 | $entityManager->flush(); | ||
21 | echo json_encode(['success' => true, 'title' => $page->getPageName()]); | ||
22 | die; | ||
23 | } | ||
24 | |||
25 | static public function updatePageMenuPath(EntityManager $entityManager): void | ||
26 | { | ||
27 | Director::$menu_data = new Menu($entityManager); | ||
28 | Director::$page_path = new Path(); | ||
29 | $page = Director::$page_path->getLast(); | ||
30 | $path = htmlspecialchars($_POST['page_menu_path']); | ||
31 | |||
32 | // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores | ||
33 | $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_'); | ||
34 | $page->setEndOfPath($path); | ||
35 | foreach(Director::$menu_data->getChildren() as $child){ | ||
36 | if($child->getEndOfPath() === Director::$page_path->getArray()[0]->getEndOfPath()){ | ||
37 | $child->fillChildrenPagePath(); // MAJ de $page_path | ||
38 | } | ||
39 | } | ||
40 | $entityManager->flush(); | ||
41 | header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); | ||
42 | die; | ||
43 | } | ||
44 | |||
45 | static public function setPageDescription(EntityManager $entityManager, array $json): void | ||
46 | { | ||
47 | $node_data = $entityManager->find('App\Entity\NodeData', $json['node_data_id']); | ||
48 | $node_data->updateData('description', htmlspecialchars($json['description'])); | ||
49 | $entityManager->flush(); | ||
50 | echo json_encode(['success' => true, 'description' => $node_data->getData()['description']]); | ||
51 | die; | ||
52 | } | ||
53 | |||
54 | static public function newPage(EntityManager $entityManager): void | ||
55 | { | ||
56 | // titre et chemin | ||
57 | $director = new Director($entityManager, true); | ||
58 | //Director::$menu_data = new Menu($entityManager); | ||
59 | $previous_page = Director::$menu_data->findPageById((int)$_POST["page_location"]); // (int) à cause de declare(strict_types=1); | ||
60 | $parent = $previous_page->getParent(); | ||
61 | |||
62 | $page = new Page( | ||
63 | trim(htmlspecialchars($_POST["page_name"])), | ||
64 | trim(htmlspecialchars($_POST["page_name_path"])), | ||
65 | true, true, false, | ||
66 | $previous_page->getPosition(), | ||
67 | $parent); // peut et DOIT être null si on est au 1er niveau | ||
68 | |||
69 | // on a donné à la nouvelle entrée la même position qu'à la précédente, | ||
70 | // addChild l'ajoute à la fin du tableau "children" puis on trie | ||
71 | // exemple avec 2 comme position demandée: 1 2 3 4 2 devient 1 2 3 4 5 et la nouvelle entrée sera en 3è position | ||
72 | if($parent == null){ | ||
73 | $parent = Director::$menu_data; | ||
74 | } | ||
75 | $parent->addChild($page); | ||
76 | $parent->reindexPositions(); | ||
77 | |||
78 | $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/')); | ||
79 | |||
80 | // noeud "head" | ||
81 | $node = new Node( | ||
82 | 'head', | ||
83 | null, [], | ||
84 | 1, // position d'un head = 1 | ||
85 | null, // pas de parent | ||
86 | $page); | ||
87 | $node->useDefaultAttributes(); // fichiers CSS et JS | ||
88 | |||
89 | $data = new NodeData([ | ||
90 | // pas de titre, il est dans $page | ||
91 | 'description' => trim(htmlspecialchars($_POST["page_description"]))], | ||
92 | $node); | ||
93 | |||
94 | $bulk_data = $entityManager | ||
95 | ->createQuery('SELECT n FROM App\Entity\Image n WHERE n.file_name LIKE :name') | ||
96 | ->setParameter('name', '%favicon%') | ||
97 | ->getResult(); | ||
98 | $data->setImages(new ArrayCollection($bulk_data)); | ||
99 | |||
100 | $entityManager->persist($page); | ||
101 | $entityManager->persist($node); | ||
102 | $entityManager->persist($data); | ||
103 | $entityManager->flush(); | ||
104 | |||
105 | // page créée, direction la page en mode modification pour ajouter des blocs | ||
106 | header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page'])); | ||
107 | die; | ||
108 | } | ||
109 | |||
110 | static public function deletePage(EntityManager $entityManager): void | ||
111 | { | ||
112 | $page = $entityManager->find('App\Entity\Page', (int)$_POST['page_id']); | ||
113 | $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]); | ||
114 | $data = []; | ||
115 | foreach($nodes as $node){ | ||
116 | $data[] = $entityManager->getRepository('App\Entity\NodeData')->findOneBy(['node' => $node]); | ||
117 | $entityManager->remove($node); | ||
118 | } | ||
119 | foreach($data as $one_data){ | ||
120 | $entityManager->remove($one_data); | ||
121 | } | ||
122 | $entityManager->remove($page); // suppression en BDD | ||
123 | |||
124 | $entityManager->flush(); | ||
125 | header("Location: " . new URL); | ||
126 | die; | ||
127 | } | ||
128 | |||
129 | /* partie "blocs" */ | ||
130 | static public function addBloc(EntityManager $entityManager): void | ||
131 | { | ||
132 | $director = new Director($entityManager, true); // on a besoin de page_path qui dépend de menu_data | ||
133 | $page = Director::$page_path->getLast(); | ||
134 | $director->findUniqueNodeByName('main'); | ||
135 | $director->findItsChildren(); | ||
136 | $main = $director->getNode(); | ||
137 | $position = count($main->getChildren()) + 1; // position dans la fraterie | ||
138 | |||
139 | $blocks = ['blog', 'grid', 'calendar', 'galery', 'form']; // même liste dans FormBuilder.php | ||
140 | if(!in_array($_POST["bloc_select"], $blocks, true)) // 3è param: contrôle du type | ||
141 | { | ||
142 | header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'bad_bloc_type'])); | ||
143 | die; | ||
144 | } | ||
145 | |||
146 | if($_POST["bloc_select"] === 'calendar' || $_POST["bloc_select"] === 'form'){ | ||
147 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page AND n.name_node = :name'; // noeud 'head' de la page | ||
148 | $bulk_data = $entityManager | ||
149 | ->createQuery($dql) | ||
150 | ->setParameter('page', $page) | ||
151 | ->setParameter('name', 'head') | ||
152 | ->getResult(); | ||
153 | |||
154 | if(count($bulk_data) != 1){ // 1 head par page | ||
155 | header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'head_node_not_found'])); | ||
156 | die; | ||
157 | } | ||
158 | |||
159 | $bulk_data[0]->addAttribute('css_array', $_POST["bloc_select"]); | ||
160 | if($_POST["bloc_select"] === 'form'){ | ||
161 | $bulk_data[0]->addAttribute('js_array', $_POST["bloc_select"]); | ||
162 | } | ||
163 | $entityManager->persist($bulk_data[0]); | ||
164 | } | ||
165 | |||
166 | $bloc = new Node( | ||
167 | $_POST["bloc_select"], | ||
168 | null, [], | ||
169 | $position, | ||
170 | $main, | ||
171 | $page); | ||
172 | $data = new NodeData( | ||
173 | ['title' => trim(htmlspecialchars($_POST["bloc_title"]))], | ||
174 | $bloc); | ||
175 | |||
176 | $entityManager->persist($bloc); | ||
177 | $entityManager->persist($data); | ||
178 | $entityManager->flush(); | ||
179 | header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page'])); | ||
180 | die; | ||
181 | } | ||
182 | |||
183 | static public function deleteBloc(EntityManager $entityManager): void | ||
184 | { | ||
185 | $director = new Director($entityManager, true); | ||
186 | $director->findUniqueNodeByName('main'); | ||
187 | $director->findItsChildren(); | ||
188 | //$director->findNodeById((int)$_POST['delete_bloc_id']); | ||
189 | $main = $director->getNode(); | ||
190 | $bloc = null; | ||
191 | foreach($main->getChildren() as $child){ | ||
192 | if($child->getId() === (int)$_POST['delete_bloc_id']){ | ||
193 | $bloc = $child; | ||
194 | break; | ||
195 | } | ||
196 | } | ||
197 | if(!empty($bloc)){ // si $bloc est null c'est que le HTML a été modifié volontairement | ||
198 | $main->removeChild($bloc); // réindex le tableau $children au passage | ||
199 | $main->reindexPositions(); | ||
200 | $entityManager->remove($bloc); // suppression en BDD | ||
201 | $entityManager->flush(); | ||
202 | } | ||
203 | header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page'])); | ||
204 | die; | ||
205 | } | ||
206 | |||
207 | static public function renameBloc(EntityManager $entityManager, array $json): void | ||
208 | { | ||
209 | if(isset($json['bloc_title']) && $json['bloc_title'] !== null && isset($json['bloc_id']) && is_int($json['bloc_id'])){ | ||
210 | $director = new Director($entityManager); | ||
211 | $director->findNodeById($json['bloc_id']); | ||
212 | |||
213 | // le titre (du JSON en BDD) est récupéré sous forme de tableau, modifié et renvoyé | ||
214 | $data = $director->getNode()->getNodeData()->getData(); | ||
215 | $data['title'] = htmlspecialchars($json['bloc_title']); | ||
216 | $director->getNode()->getNodeData()->updateData('title', htmlspecialchars($json['bloc_title'])); | ||
217 | |||
218 | $entityManager->flush(); | ||
219 | echo json_encode(['success' => true, 'title' => $data['title']]); | ||
220 | } | ||
221 | else{ | ||
222 | echo json_encode(['success' => false]); | ||
223 | } | ||
224 | die; | ||
225 | } | ||
226 | |||
227 | static public function SwitchBlocsPositions(EntityManager $entityManager, array $json): void | ||
228 | { | ||
229 | if(isset($json['id1']) && is_int($json['id1']) && isset($json['id2']) && is_int($json['id2']) && isset($_GET['page'])){ | ||
230 | $director = new Director($entityManager, true); | ||
231 | $director->findUniqueNodeByName('main'); | ||
232 | $director->findItsChildren(); | ||
233 | $main = $director->getNode(); | ||
234 | $main->sortChildren(true); // régénère les positions avant inversion | ||
235 | |||
236 | $bloc1 = null; | ||
237 | $bloc2 = null; | ||
238 | foreach($main->getChildren() as $child){ | ||
239 | if($child->getId() === $json['id1']){ | ||
240 | $bloc1 = $child; | ||
241 | break; | ||
242 | } | ||
243 | } | ||
244 | foreach($main->getChildren() as $child){ | ||
245 | if($child->getId() === $json['id2']){ | ||
246 | $bloc2 = $child; | ||
247 | break; | ||
248 | } | ||
249 | } | ||
250 | |||
251 | // inversion | ||
252 | $tmp = $bloc1->getPosition(); | ||
253 | $bloc1->setPosition($bloc2->getPosition()); | ||
254 | $bloc2->setPosition($tmp); | ||
255 | |||
256 | $entityManager->flush(); | ||
257 | echo json_encode(['success' => true]); | ||
258 | } | ||
259 | else{ | ||
260 | echo json_encode(['success' => false]); | ||
261 | } | ||
262 | die; | ||
263 | } | ||
264 | } \ No newline at end of file | ||