summaryrefslogtreecommitdiff
path: root/src/view/MenuBuilder.php
blob: 42c9273c5741bb4971e6bac6129dcf68d64c8334 (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
<?php
// src/view/MenuBuilder.php
//
// page Menu et chemins en mode admin, fonctionne avec menu.js

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

class MenuBuilder extends AbstractBuilder
{
    //private int $margin_left_multiplier = 29;

    public function __construct(Node $node = null, bool $template = true)
    {
        //parent::__construct($node);
        $viewFile = $node === null ? self::VIEWS_PATH . 'menu.php' : self::VIEWS_PATH . $node->getName() . '.php';
        
        if(file_exists($viewFile))
        {
            /*if(!empty($node->getNodeData()->getData()))
            {
                extract($node->getNodeData()->getData());
            }*/

            if($_SESSION['admin']){
                $this->unfoldMenu(Director::$menu_data/*, 0 - $this->margin_left_multiplier*/);
            }
            else{
                header('Location: ' . new URL);
                die;
            }

            // si faux, n'utilise pas le template
            if($template){
                ob_start();
                require $viewFile; // insertion de $this->html généré par unfoldMenu
                $this->html = ob_get_clean(); // pas de concaténation .= cette fois on écrase
            }
        }
    }

    private function unfoldMenu(Page $menu): void
    {
        $this->html .= '<div class="level">' . "\n";

        foreach($menu->getChildren() as $entry)
        {
            $checked = $entry->isHidden() ? '' : 'checked';
            $this->html .= '<div id="' . $entry->getId() . '" class="menu_edit_entry">
                <img class="move_entry_icon" onclick="moveOneLevelUp(' . $entry->getId() . ')" src="assets/arrow-left.svg">
                <img class="move_entry_icon" onclick="moveOneLevelDown(' . $entry->getId() . ')" src="assets/arrow-right.svg">
                <img class="move_entry_icon" onclick="switchMenuPositions(' . $entry->getId() . ', \'up\')" src="assets/arrow-up.svg">
                <img class="move_entry_icon" onclick="switchMenuPositions(' . $entry->getId() . ', \'down\')" src="assets/arrow-down.svg">
                <span class="menu_entry_checkbox">
                    <input type="checkbox" ' . $checked . ' onclick="checkMenuEntry(' . $entry->getId() . ')">
                </span>
                <button>' . $entry->getPageName() . '</button>';

            if(str_starts_with($entry->getEndOfPath(), 'http')){
                $this->html .= '<span id="edit-i..."><img class="move_entry_icon" src="assets/edit.svg" onclick="openEditor(\'i...\')"></span>
                    <i class="url">' . $entry->getEndOfPath() . '</i>
                    <span id="delete-i..."><img class="move_entry_icon" src="assets/delete-bin.svg" onclick="delete(\'i...\')"></span>';
            }
            else{
                $this->html .= '<i class="path">' . $entry->getPagePath() . '</i>';
            }
            
            if(count($entry->getChildren()) > 0){
                $this->unfoldMenu($entry);
            }
            $this->html .= '</div>' . "\n";
        }
        $this->html .= "</div>\n";
    }
}