summaryrefslogtreecommitdiff
path: root/src/controller/post.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller/post.php')
-rw-r--r--src/controller/post.php297
1 files changed, 0 insertions, 297 deletions
diff --git a/src/controller/post.php b/src/controller/post.php
deleted file mode 100644
index 5d9500b..0000000
--- a/src/controller/post.php
+++ /dev/null
@@ -1,297 +0,0 @@
1<?php
2// src/controller/post.php
3
4declare(strict_types=1);
5
6use App\Entity\Node;
7use App\Entity\NodeData;
8use App\Entity\Page;
9use App\Entity\Image;
10use Doctrine\Common\Collections\ArrayCollection;
11
12if(isset($_GET['action']) && $_GET['action'] === 'deconnexion')
13{
14 disconnect($entityManager);
15}
16elseif(isset($_GET['action']) && $_GET['action'] === 'modif_mdp')
17{
18 changePassword($entityManager);
19}
20elseif($_SESSION['admin'] && isset($_GET['page']) && isset($_GET['action']) && $_GET['action'] === 'modif_page'
21 && $_GET['page'] !== 'connexion' && $_GET['page'] !== 'article' && $_GET['page'] !== 'nouvelle_page' && $_GET['page'] !== 'menu_chemins')
22{
23 // les contrôles de la 2è ligne devraient utiliser un tableau
24 MainBuilder::$modif_mode = true;
25}
26
27
28/* -- html form -- */
29if($_SERVER['REQUEST_METHOD'] === 'POST'){
30 // POST ordinaires non admin
31
32 // POST ajax non admin
33 require '../src/controller/ajax.php';
34
35 if($_SESSION['admin'] === true)
36 {
37 /* -- formulaires HTML classiques -- */
38 if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded')
39 {
40 /* -- nouvelle page -- */
41 if(isset($_POST['page_name']) && $_POST['page_name'] !== null
42 && isset($_POST['page_name_path']) && $_POST['page_name_path'] !== null
43 && isset($_POST['page_location']) && $_POST['page_location'] !== null
44 && isset($_POST['page_description']) && $_POST['page_description'] !== null
45 && isset($_POST['new_page_hidden']) && $_POST['new_page_hidden'] === '')
46 {
47 // titre et chemin
48 $director = new Director($entityManager, true);
49 //Director::$menu_data = new Menu($entityManager);
50 $previous_page = Director::$menu_data->findPageById((int)$_POST["page_location"]); // (int) à cause de declare(strict_types=1);
51 $parent = $previous_page->getParent();
52
53 $page = new Page(
54 trim(htmlspecialchars($_POST["page_name"])),
55 trim(htmlspecialchars($_POST["page_name_path"])),
56 true, true, false,
57 $previous_page->getPosition(),
58 $parent); // peut et DOIT être null si on est au 1er niveau
59
60 // on a donné à la nouvelle entrée la même position qu'à la précédente,
61 // addChild l'ajoute à la fin du tableau "children" puis on trie
62 // 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
63 if($parent == null){
64 $parent = Director::$menu_data;
65 }
66 $parent->addChild($page);
67 $parent->reindexPositions();
68
69 $page->setPagePath(ltrim($parent->getPagePath() . '/' . $page->getEndOfPath(), '/'));
70
71 // noeud "head"
72 $node = new Node(
73 'head',
74 null, [],
75 1, // position d'un head = 1
76 null, // pas de parent
77 $page);
78 $node->useDefaultAttributes(); // fichiers CSS et JS
79
80 $data = new NodeData([
81 // pas de titre, il est dans $page
82 'description' => trim(htmlspecialchars($_POST["page_description"]))],
83 $node);
84
85 $bulk_data = $entityManager
86 ->createQuery('SELECT n FROM App\Entity\Image n WHERE n.file_name LIKE :name')
87 ->setParameter('name', '%favicon%')
88 ->getResult();
89 $data->setImages(new ArrayCollection($bulk_data));
90
91 $entityManager->persist($page);
92 $entityManager->persist($node);
93 $entityManager->persist($data);
94 $entityManager->flush();
95
96 // page créée, direction la page en mode modification pour ajouter des blocs
97 header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page']));
98 die;
99 }
100
101 /* -- suppression d'une page -- */
102 elseif(isset($_POST['page_id']) && $_POST['page_id'] !== null
103 && isset($_POST['submit_hidden']) && $_POST['submit_hidden'] === '')
104 {
105 $page = $entityManager->find('App\Entity\Page', (int)$_POST['page_id']);
106 $nodes = $entityManager->getRepository('App\Entity\Node')->findBy(['page' => $page]);
107 $data = [];
108 foreach($nodes as $node){
109 $data[] = $entityManager->getRepository('App\Entity\NodeData')->findOneBy(['node' => $node]);
110 $entityManager->remove($node);
111 }
112 foreach($data as $one_data){
113 $entityManager->remove($one_data);
114 }
115 $entityManager->remove($page); // suppression en BDD
116
117 $entityManager->flush();
118 header("Location: " . new URL);
119 die;
120 }
121
122
123 /* -- mode Modification d'une page -- */
124
125 // modification des titres, chemins et descriptions
126 elseif(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null
127 && isset($_POST['page_id']) && $_POST['page_id'] !== null
128 && isset($_POST['page_name_path_hidden']) && $_POST['page_name_path_hidden'] === '')
129 {
130 $director = new Director($entityManager, true);
131 $page = Director::$page_path->getLast();
132 $path = htmlspecialchars($_POST['page_menu_path']);
133
134 // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores
135 $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_');
136 $page->setEndOfPath($path);
137 foreach(Director::$menu_data->getChildren() as $child){
138 if($child->getEndOfPath() === Director::$page_path->getArray()[0]->getEndOfPath()){
139 $child->fillChildrenPagePath(); // MAJ de $page_path
140 }
141 }
142 $entityManager->flush();
143 header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page']));
144 die;
145 }
146 // ajout d'un bloc dans une page
147 elseif(isset($_POST['bloc_title']) && $_POST['bloc_title'] !== null
148 && isset($_POST['bloc_select']) && $_POST['bloc_select'] !== null
149 && isset($_POST['bloc_title_hidden']) && $_POST['bloc_title_hidden'] === '') // contrôle anti-robot avec input hidden
150 {
151 $director = new Director($entityManager, true); // on a besoin de page_path qui dépend de menu_data
152 $page = Director::$page_path->getLast();
153 $director->findUniqueNodeByName('main');
154 $director->findItsChildren();
155 $main = $director->getNode();
156 $position = count($main->getChildren()) + 1; // position dans la fraterie
157
158 $blocks = ['blog', 'grid', 'calendar', 'galery', 'form']; // même liste dans FormBuilder.php
159 if(!in_array($_POST["bloc_select"], $blocks, true)) // 3è param: contrôle du type
160 {
161 header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'bad_bloc_type']));
162 die;
163 }
164
165 if($_POST["bloc_select"] === 'calendar' || $_POST["bloc_select"] === 'form'){
166 $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page AND n.name_node = :name'; // noeud 'head' de la page
167 $bulk_data = $entityManager
168 ->createQuery($dql)
169 ->setParameter('page', $page)
170 ->setParameter('name', 'head')
171 ->getResult();
172
173 if(count($bulk_data) != 1){ // 1 head par page
174 header("Location: " . new URL(['page' => $_GET['page'], 'error' => 'head_node_not_found']));
175 die;
176 }
177
178 $bulk_data[0]->addAttribute('css_array', $_POST["bloc_select"]);
179 if($_POST["bloc_select"] === 'form'){
180 $bulk_data[0]->addAttribute('js_array', $_POST["bloc_select"]);
181 }
182 $entityManager->persist($bulk_data[0]);
183 }
184
185 $bloc = new Node(
186 $_POST["bloc_select"],
187 null, [],
188 $position,
189 $main,
190 $page);
191 $data = new NodeData(
192 ['title' => trim(htmlspecialchars($_POST["bloc_title"]))],
193 $bloc);
194
195 $entityManager->persist($bloc);
196 $entityManager->persist($data);
197 $entityManager->flush();
198 header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page']));
199 die;
200 }
201 // suppression d'un bloc de page
202 elseif(isset($_POST['delete_bloc_id']) && $_POST['delete_bloc_id'] !== null
203 && isset($_POST['delete_bloc_hidden']) && $_POST['delete_bloc_hidden'] === '') // contrôle anti-robot avec input hidden
204 {
205 $director = new Director($entityManager, true);
206 $director->findUniqueNodeByName('main');
207 $director->findItsChildren();
208 //$director->findNodeById((int)$_POST['delete_bloc_id']);
209 $main = $director->getNode();
210 $bloc;
211 foreach($main->getChildren() as $child){
212 if($child->getId() === (int)$_POST['delete_bloc_id']){
213 $bloc = $child;
214 break;
215 }
216 }
217 $main->removeChild($bloc); // réindex le tableau $children au passage
218 $main->reindexPositions();
219
220 $entityManager->remove($bloc); // suppression en BDD
221 $entityManager->flush();
222 header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page']));
223 die;
224 }
225
226
227 /* -- page Menu et chemins -- */
228
229 // création d'une entrée de menu avec une URL
230 elseif(isset($_POST["label_input"]) && isset($_POST["url_input"]) && isset($_POST["location"])){
231 Director::$menu_data = new Menu($entityManager);
232 $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1);
233 $parent = $previous_page->getParent();
234
235 $page = new Page(
236 trim(htmlspecialchars($_POST["label_input"])),
237 filter_var($_POST["url_input"], FILTER_VALIDATE_URL),
238 true, true, false,
239 $previous_page->getPosition(),
240 $parent); // peut et DOIT être null si on est au 1er niveau
241
242 // on a donné à la nouvelle entrée la même position qu'à la précédente,
243 // addChild l'ajoute à la fin du tableau "children" puis on trie
244 // 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
245 if($parent == null){
246 $parent = Director::$menu_data;
247 }
248 $parent->addChild($page); // true pour réindexer les positions en BDD
249 $parent->reindexPositions();
250
251 $entityManager->persist($page);
252 $entityManager->flush();
253 header("Location: " . new URL(['page' => $_GET['from']]));
254 die;
255 }
256 // suppression d'une entrée de menu avec une URL
257 elseif(isset($_POST['delete']) && isset($_POST['x']) && isset($_POST['y'])){ // 2 params x et y sont là parce qu'on a cliqué sur une image
258 Director::$menu_data = new Menu($entityManager);
259 $page = Director::$menu_data->findPageById((int)$_POST["delete"]);
260 $parent = $page->getParent();
261 if($parent == null){
262 $parent = Director::$menu_data;
263 }
264
265 $parent->removeChild($page); // suppression de $children avant de trier
266 $parent->reindexPositions();
267
268 $entityManager->remove($page); // suppression en BDD
269 $entityManager->flush();
270 header("Location: " . new URL(['page' => $_GET['from']]));
271 die;
272 }
273 elseif(isset($_GET['action']) && $_GET['action'] === 'modif_mdp'
274 && isset($_POST['login']) && isset($_POST['old_password']) && isset($_POST['new_password'])
275 && isset($_POST['modify_password_hidden']) && empty($_POST['modify_password_hidden']))
276 {
277 changePassword($entityManager);
278 header("Location: " . new URL(['page' => $_GET['from']]));
279 die;
280 }
281 else{
282 header("Location: " . new URL(['error' => 'paramètres inconnus']));
283 die;
284 }
285 }
286
287 /* -- requêtes AJAX -- */
288 else{
289 require '../src/controller/ajax_admin.php';
290 }
291
292 require '../src/controller/ajax_calendar_admin.php';
293 }
294}
295elseif($_SERVER['REQUEST_METHOD'] === 'GET'){
296 require '../src/controller/ajax_calendar_visitor.php'; // fullcalendar utilise un GET pour récupérer les données
297} \ No newline at end of file