summaryrefslogtreecommitdiff
path: root/src/model/Path.php
diff options
context:
space:
mode:
authorpolo-pc-greta <ordipolo@gmx.fr>2025-03-27 10:13:03 +0100
committerpolo-pc-greta <ordipolo@gmx.fr>2025-03-27 10:13:03 +0100
commitdf3612ed7e6691530503f79483d2fdbc032d01b8 (patch)
tree56d1c68fdc8625f5dad1937a654299d45142c79a /src/model/Path.php
downloadcms-df3612ed7e6691530503f79483d2fdbc032d01b8.zip
mise en ligne github
Diffstat (limited to 'src/model/Path.php')
-rw-r--r--src/model/Path.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/model/Path.php b/src/model/Path.php
new file mode 100644
index 0000000..6faadfd
--- /dev/null
+++ b/src/model/Path.php
@@ -0,0 +1,84 @@
1<?php
2// src/controller/Path.php
3
4declare(strict_types=1);
5
6use Doctrine\ORM\EntityManager;
7use App\Entity\Page;
8
9class Path extends Page
10{
11 private array $current_page = []; // tableau d'objets Page
12
13 public function __construct()
14 {
15 $path_array = explode('/', CURRENT_PAGE);
16 try{
17 $this->findPage(Director::$menu_data, $path_array); // remplit $this->current_page
18 }
19 catch(Exception $e){}
20 /*echo "nb d'autres pages: " . count(Director::$menu_data->getOtherPages()) . '<br>';
21 echo 'longueur du chemin: ' . count($this->current_page) . '<br>';
22 foreach($this->current_page as $current){
23 echo $current->getEndOfPath() . ' ';
24 }
25 die;*/
26 }
27
28 // produit un tableau de Page en comparant le chemin demandé avec les données dans Menu
29 // succès => une exception est lancée pour sortir des fonctions imbriquées
30 // echec => redirection vers la page erreur 404
31 private function findPage(Page|Menu $menu, array $path_array)
32 {
33 // recherche dans les autres pages
34 if($menu instanceof Menu){
35 foreach($menu->getOtherPages() as $page)
36 {
37 if($path_array[0] === $page->getEndOfPath())
38 {
39 $this->current_page[] = $page;
40 throw new Exception();
41 }
42 }
43 }
44 // recherche dans le menu
45 foreach($menu->getChildren() as $page)
46 {
47 if($path_array[0] === $page->getEndOfPath())
48 {
49 $this->current_page[] = $page;
50 if(count($path_array) > 1)
51 {
52 array_shift($path_array); // $this->path_array n'est pas modifié, un tableau PHP est passé à une fonction par copie
53 $this->findPage($page, $path_array);
54 }
55 else{
56 throw new Exception(); // sortir de tous les findPage() en même temps
57 }
58 }
59 }
60 // rien trouvé
61 URL::setPath('erreur404.html');
62 header('Location: '. new URL);
63 die;
64 }
65
66 public function getString(): string
67 {
68 $path_string = "";
69 foreach($this->current_page as $one_page){
70 $path_string .= $one_page->getEndOfPath() . '/';
71 }
72 return rtrim($path_string, '/');
73 }
74 public function getArray(): array
75 {
76 return $this->current_page;
77 }
78
79 // c'est là qu'on est quoi
80 public function getLast(): Page
81 {
82 return $this->current_page[count($this->current_page) - 1];
83 }
84} \ No newline at end of file