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