From 6535db259081b02e9db59b905ae922a37d03eddc Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 20 Oct 2025 15:36:59 +0200 Subject: =?UTF-8?q?m=C3=A9thodes=20dans=20Page=20pour=20les=20fichiers=20C?= =?UTF-8?q?SS/JS,=20CCS/JS=20par=20d=C3=A9faut=20supprim=C3=A9=20de=20la?= =?UTF-8?q?=20BDD,=20nettoyage=20quand=20le=20dernier=20bloc=20est=20suppr?= =?UTF-8?q?im=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/PageManagementController.php | 43 +++++++++++++++++++---------- src/installation.php | 2 +- src/model/Menu.php | 2 +- src/model/entities/Page.php | 36 ++++++++++++------------ 4 files changed, 49 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/controller/PageManagementController.php b/src/controller/PageManagementController.php index c45f3f3..ca1f887 100644 --- a/src/controller/PageManagementController.php +++ b/src/controller/PageManagementController.php @@ -68,8 +68,6 @@ class PageManagementController true, true, false, $previous_page->getPosition(), $parent); // peut et DOIT être null si on est au 1er niveau - $page->useDefaultCSS(); - $page->useDefaultJS(); // on a donné à la nouvelle entrée la même position qu'à la précédente, // addChild l'ajoute à la fin du tableau "children" puis on trie @@ -126,11 +124,10 @@ class PageManagementController die; } - if($_POST["bloc_select"] === 'calendar' || $_POST["bloc_select"] === 'form'){ - $page->setCSS(array_merge($page->getCSS(), [$_POST["bloc_select"]])); - + if(in_array($_POST["bloc_select"], ['calendar', 'form'])){ + $page->addCSS($_POST["bloc_select"]); if($_POST["bloc_select"] === 'form'){ - $page->setJS(array_merge($page->getJS(), [$_POST["bloc_select"]])); + $page->addJS($_POST["bloc_select"]); } $entityManager->persist($page); } @@ -146,7 +143,7 @@ class PageManagementController $data->setPresentation('grid'); } elseif($_POST["bloc_select"] === 'galery'){ - $data->setPresentation('mosaic'); // mieux que carousel pour commencer + $data->setPresentation('mosaic'); // un jour on mettra carousel } // else = null par défaut @@ -163,19 +160,37 @@ class PageManagementController $model->makeMenuAndPaths(); $model->findUniqueNodeByName('main'); $model->findItsChildren(); - //$model->findNodeById((int)$_POST['delete_bloc_id']); $main = $model->getNode(); - $bloc = null; + + $block = null; + $type = ''; + $nb_same_type = 0; foreach($main->getChildren() as $child){ if($child->getId() === (int)$_POST['delete_bloc_id']){ - $bloc = $child; - break; + $block = $child; + $type = $block->getName(); + } + if($child->getName() === $type){ + $nb_same_type++; } } - if(!empty($bloc)){ // si $bloc est null c'est que le HTML a été modifié volontairement - $main->removeChild($bloc); // réindex le tableau $children au passage + + // nettoyage fichiers CSS et JS si on retire le derner bloc de ce type + if($nb_same_type === 1 && in_array($block->getName(), ['calendar', 'form'])){ + $page = $block->getPage(); + $page->removeCSS($block->getName()); + if($block->getName() === 'form'){ + $page->removeJS($block->getName()); + } + } + + if(!empty($block)){ // si $block est null c'est que le HTML a été modifié volontairement + $main->removeChild($block); // réindex le tableau $children au passage $main->reindexPositions(); - $entityManager->remove($bloc); // suppression en BDD + if(isset($page)){ + $entityManager->persist($page); + } + $entityManager->remove($block); $entityManager->flush(); } diff --git a/src/installation.php b/src/installation.php index df0f851..5a56f6b 100644 --- a/src/installation.php +++ b/src/installation.php @@ -87,7 +87,7 @@ HTACCESS; // création d'un site minimal avec une page d'accueil à la toute 1ère visite du site // fonctiona appelée après la première requête envoyée en BDD, // en l'occurence dans Menu parce que count($bulk_data) === 0 -function makeStartPage(EntityManager $entityManager){ +function fillStartingDatabase(EntityManager $entityManager){ /* -- table page -- */ // paramètres: name_page, end_of_path, reachable, in_menu, hidden, position, parent $accueil = new Page('Accueil', 'accueil', "Page d'accueil", true, true, false, 1, NULL); diff --git a/src/model/Menu.php b/src/model/Menu.php index 9fb8562..4817b93 100644 --- a/src/model/Menu.php +++ b/src/model/Menu.php @@ -20,7 +20,7 @@ class Menu extends Page ->getResult(); // :array de Page if(count($bulk_data) === 0){ - makeStartPage($entityManager); // => installation.php + fillStartingDatabase($entityManager); // => installation.php } foreach($bulk_data as $first_level_entries){ diff --git a/src/model/entities/Page.php b/src/model/entities/Page.php index 3e90dbc..8d3dd40 100644 --- a/src/model/entities/Page.php +++ b/src/model/entities/Page.php @@ -40,14 +40,6 @@ class Page static private array $default_css = ['body', 'head', 'nav', 'foot']; static private array $default_js = ['main']; - /* remplissage - UPDATE nb_page - JOIN nb_node ON nb_node.page_id = nb_page.id_page - SET nb_page.css = JSON_EXTRACT(nb_node.attributes, '$.css_array'); - UPDATE nb_page - JOIN nb_node ON nb_node.page_id = nb_page.id_page - SET nb_page.js = JSON_EXTRACT(nb_node.attributes, '$.js_array'); */ - #[ORM\Column(type: "boolean")] private bool $reachable; @@ -120,30 +112,38 @@ class Page { $this->description = $description; } + public function getCSS(): array { - return $this->css; + return array_merge(self::$default_css, $this->css ?? []); } - public function useDefaultCSS(): void + public function addCSS(string $css): void { - $this->css = self::$default_css; + if(!in_array($css, $this->css ?? [])){ + $this->css[] = $css; + } } - public function setCSS(array $css): void + public function removeCSS(string $css): void { - $this->css = $css; + // array_diff renvoie une copie du 1er tableau alégée des élements existants aussi dans le 2è, array_values réindexe + $this->css = array_values(array_diff($this->css, [$css])); } public function getJS(): array { - return $this->js; + return array_merge(self::$default_js, $this->js ?? []); + //UPDATE `nb_page` SET `js` = NULL WHERE JSON_EQUALS(`js`, '["main"]'); } - public function useDefaultJS(): void + public function addJS(string $js): void { - $this->js = self::$default_js; + if(!in_array($js, $this->js ?? [])){ + $this->js[] = $js; + } } - public function setJS(array $js): void + public function removeJS(string $js): void { - $this->js = $js; + $this->js = array_values(array_diff($this->js, [$js])); } + public function isReachable(): bool { return $this->reachable; -- cgit v1.2.3