aboutsummaryrefslogtreecommitdiff
path: root/src/Model/DatabaseSettingUp.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model/DatabaseSettingUp.php')
-rw-r--r--src/Model/DatabaseSettingUp.php157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/Model/DatabaseSettingUp.php b/src/Model/DatabaseSettingUp.php
new file mode 100644
index 0000000..725eaed
--- /dev/null
+++ b/src/Model/DatabaseSettingUp.php
@@ -0,0 +1,157 @@
1<?php
2// src/Model/DatabaseSettingUp.php
3
4declare(strict_types=1);
5
6namespace App\Model;
7
8use Doctrine\ORM\EntityManager;
9use App\Entity\AppMetadata;
10use App\Entity\Page;
11use App\Entity\Node;
12use App\Entity\NodeData;
13
14/* création d'un site minimal avec une page d'accueil à la toute 1ère visite du site, ne doit surtout pas être exécutée une seconde fois */
15class DatabaseSettingUp
16{
17 static public function run(EntityManager $entityManager): void
18 {
19 if(!self::isFirstRun($entityManager)){
20 return;
21 }
22
23 // la BDD n'est pas vierge, on ne touche à rien
24 if(!self::areTablesEmpty($entityManager)){
25 return;
26 }
27
28 self::fillStartingDatabase($entityManager);
29
30 // empêcher la réutilisation de cette fonction
31 self::preventReinstallation($entityManager);
32
33 // recharger la page?
34 //header('Location: ' . new URL);
35 }
36
37 // protection 1 utilisé à chaque requête
38 static private function isFirstRun(EntityManager $entityManager): bool
39 {
40 $metadata = $entityManager->getRepository(AppMetadata::class)->find('installed');
41 return !$metadata || $metadata->getValue() !== '1';
42 }
43
44 // protection 2, qui vérifie vraiment que les tables concernées sont vides
45 static private function areTablesEmpty(EntityManager $entityManager): bool
46 {
47 $empty = true;
48 $entities = ['Page', 'Node', 'NodeData'];
49 foreach($entities as $entity){
50 $entity = 'App\Entity\\' . $entity; // nécéssaire quand on insère le nom avec une variable
51
52 if($entityManager
53 ->createQuery("SELECT e FROM $entity e")
54 ->setMaxResults(1)
55 ->getOneOrNullResult()){
56 $empty = false;
57 }
58 }
59
60 // cas anormal détecté, on remet en place la clé "installed"
61 if(!$empty){
62 self::preventReinstallation($entityManager);
63 }
64
65 return $empty;
66 }
67
68 static private function fillStartingDatabase(EntityManager $entityManager): void
69 {
70 /* -- table page -- */
71 // paramètres: name_page, end_of_path, reachable, in_menu, hidden, position, parent
72 $accueil = new Page('Accueil', 'accueil', "Page d'accueil", true, true, false, 1, NULL);
73 $article = new Page('Article', 'article', "", true, false, false, NULL, NULL);
74 $connection = new Page('Connexion', 'connection', "Connexion", true, false, false, NULL, NULL);
75 $my_account = new Page('Mon compte', 'user_edit', "Mon compte", true, false, false, NULL, NULL);
76 $menu_paths = new Page("Menu et chemins", 'menu_paths', "Menu et chemins", true, false, false, NULL, NULL);
77 $menu_paths->addCSS('menu');
78 $menu_paths->addJS('menu');
79 $new_page = new Page('Nouvelle page', 'new_page', "Nouvelle page", true, false, false, NULL, NULL);
80 $new_page->addCSS('new_page');
81 $new_page->addJS('new_page');
82 $emails = new Page("Courriels", 'emails', "Consulter les courriels en base de données", true, false, false, NULL, NULL);
83 $emails->addCSS('show_emails');
84 $emails->addJS('form');
85 $maintenance = new Page("Maintenance", 'maintenance', "Opérations de maintenance, installation, sauvegarde, restauration", true, false, false, NULL, NULL);
86 $maintenance->addCSS('maintenance');
87 $maintenance->addJS('maintenance');
88
89 /* -- table node -- */
90 // paramètres: name_node, article_timestamp, attributes, position, parent, page, article
91 $head = new Node('head', 1, NULL, NULL, NULL);
92 $header = new Node('header', 2, NULL, NULL, NULL);
93 $nav = new Node('nav', 1, $header, NULL, NULL);
94 $main = new Node('main', 3, NULL, NULL, NULL);
95 $footer = new Node('footer', 4, NULL, NULL, NULL);
96 $breadcrumb = new Node('breadcrumb', 2, $header, NULL, NULL);
97 $login = new Node('login', 1, $main, $connection, NULL);
98 $user_edit = new Node('user_edit', 1, $main, $my_account, NULL);
99 $bloc_edit_menu = new Node('menu', 1, $main, $menu_paths, NULL);
100 $bloc_new_page = new Node('new_page', 1, $main, $new_page, NULL);
101 $bloc_emails = new Node('show_emails', 1, $main, $emails, NULL);
102 $bloc_maintenance = new Node('maintenance', 1, $main, $maintenance, NULL);
103
104 /* -- table node_data -- */
105 // paramètres: data, node, images
106 $head_data = new NodeData([], $head);
107 $header_data = new NodeData([], $header);
108 $footer_data = new NodeData([], $footer);
109 $emails_data = new NodeData([], $bloc_emails);
110
111 /* -- table page -- */
112 $entityManager->persist($accueil);
113 $entityManager->persist($article);
114 $entityManager->persist($connection);
115 $entityManager->persist($my_account);
116 $entityManager->persist($menu_paths);
117 $entityManager->persist($new_page);
118 $entityManager->persist($emails);
119 $entityManager->persist($maintenance);
120
121 /* -- table node -- */
122 $entityManager->persist($head);
123 $entityManager->persist($header);
124 $entityManager->persist($nav);
125 $entityManager->persist($main);
126 $entityManager->persist($footer);
127 $entityManager->persist($breadcrumb);
128 $entityManager->persist($login);
129 $entityManager->persist($user_edit);
130 $entityManager->persist($bloc_edit_menu);
131 $entityManager->persist($bloc_new_page);
132 $entityManager->persist($bloc_emails);
133 $entityManager->persist($bloc_maintenance);
134
135 /* -- table node_data -- */
136 $entityManager->persist($head_data);
137 $entityManager->persist($header_data);
138 $entityManager->persist($footer_data);
139 $entityManager->persist($emails_data);
140
141 $entityManager->flush();
142 }
143
144 // met en place la protection
145 static private function preventReinstallation(EntityManager $entityManager): void
146 {
147 $metadata = $entityManager->getRepository(AppMetadata::class)->find('installed');
148 if($metadata){
149 $metadata->setValue('1');
150 }
151 else{
152 $metadata = new AppMetadata('installed', '1');
153 $entityManager->persist($metadata);
154 }
155 $entityManager->flush();
156 }
157} \ No newline at end of file