summaryrefslogtreecommitdiff
path: root/src/model/Path.php
blob: 6faadfde9a03e38ccc8e404a144a3013abd1eb18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
// src/controller/Path.php

declare(strict_types=1);

use Doctrine\ORM\EntityManager;
use App\Entity\Page;

class Path extends Page
{
	private array $current_page = []; // tableau d'objets Page

	public function __construct()
	{
        $path_array = explode('/', CURRENT_PAGE);
        try{
        	$this->findPage(Director::$menu_data, $path_array); // remplit $this->current_page
        }
		catch(Exception $e){}
		/*echo "nb d'autres pages: " . count(Director::$menu_data->getOtherPages()) . '<br>';
		echo 'longueur du chemin: ' . count($this->current_page) . '<br>';
		foreach($this->current_page as $current){
			echo $current->getEndOfPath() . ' ';
		}
		die;*/
	}

	// produit un tableau de Page en comparant le chemin demandé avec les données dans Menu
	// succès => une exception est lancée pour sortir des fonctions imbriquées
	// echec => redirection vers la page erreur 404
	private function findPage(Page|Menu $menu, array $path_array)
	{
		// recherche dans les autres pages
		if($menu instanceof Menu){
			foreach($menu->getOtherPages() as $page)
			{
				if($path_array[0] === $page->getEndOfPath())
				{
					$this->current_page[] = $page;
					throw new Exception();
				}
			}
		}
		// recherche dans le menu
		foreach($menu->getChildren() as $page)
		{
			if($path_array[0] === $page->getEndOfPath())
			{
				$this->current_page[] = $page;
				if(count($path_array) > 1)
				{
					array_shift($path_array); // $this->path_array n'est pas modifié, un tableau PHP est passé à une fonction par copie
					$this->findPage($page, $path_array);
				}
				else{
					throw new Exception(); // sortir de tous les findPage() en même temps
				}
			}
		}
		// rien trouvé
		URL::setPath('erreur404.html');
    	header('Location: '. new URL);
    	die;
	}

	public function getString(): string
    {
    	$path_string = "";
    	foreach($this->current_page as $one_page){
    		$path_string .= $one_page->getEndOfPath() . '/';
    	}
        return rtrim($path_string, '/');
    }
    public function getArray(): array
    {
    	return $this->current_page;
    }

    // c'est là qu'on est quoi
    public function getLast(): Page
    {
    	return $this->current_page[count($this->current_page) - 1];
    }
}