summaryrefslogtreecommitdiff
path: root/src/view/NavBuilder.php
blob: a9cf49cf1018d52b0ef7e2271011b4ff20ca0ac6 (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
<?php
// src/view/NavBuilder.php
//
// menu principal

declare(strict_types=1);

use App\Entity\Node;
use App\Entity\Page;

class NavBuilder extends AbstractBuilder
{
    public function __construct(Node $node = null)
    {
        $this->html .= '<nav class="nav_main"><ul>';
        $this->html .= $this->navMainHTML(
            Director::$menu_data,
            // param nullable, ça retire une dépendance stricte entre NavBuilder et Director
            Director::$page_path != null ? Director::$page_path->getArray() : []);
        $this->html .= '</ul></nav>';
    }

    private function navMainHTML(Page $nav_data, array $current): string
    {
        $nav_html = '';
        static $level = 0;

        foreach($nav_data->getChildren() as $data)
        {
            if(!$data->isHidden()){
                $li_class = '';
                if(isset($current[$level]) && $data->getEndOfPath() === $current[$level]->getEndOfPath()){
                    $li_class = 'current ';
                }

                $link = '';
                if($data->isReachable()) // titre de catégorie du menu non clicable
                {
                    if(str_starts_with($data->getEndOfPath(), 'http')) // lien vers autre site
                    {
                        $link .= '<a href="' . $data->getEndOfPath() . '" target="_blank">';
                    }
                    elseif($data->getEndOfPath() != '') // lien relatif
                    {
                        $link .= '<a href="' . new URL(['page' => $data->getPagePath()]) . '">';
                    }
                }
                else{
                    $link .= '<a>';
                }
                
                if(count($data->getChildren()) > 0) // titre de catégorie
                {
                    $li_class .= $data->getParent() == null ? 'drop-down' : 'drop-right';
                    
                    $nav_html .= '<li class="'. $li_class . '">' . $link . '<p id="m_' . $data->getId() . '">' . $data->getPageName() . '</p></a>
                            <button class="sub-menu-toggle" aria-label="Ouvrir le sous-menu">▼</button>
                        <ul class="sub-menu">' . "\n";
                    $level++;
                    $nav_html .= $this->navMainHTML($data, $current);
                    $level--;
                    $nav_html .= '</ul></li>' . "\n";
                }
                else
                {
                    $nav_html .= '<li class="'. $li_class . '">' . $link . '<p id="m_' . $data->getId() . '">' . $data->getPageName() . '</p></a></li>' . "\n";
                }
            }
        }
        return $nav_html;
    }
}