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