summaryrefslogtreecommitdiff
path: root/src/controller/Director.php
blob: 896cde17f46feb4d026b81ce98e6f555f866d4e5 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
// src/controller/Director.php

declare(strict_types=1);

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

class Director
{
	private EntityManager $entityManager;
    static public Menu $menu_data; // pour NavBuilder
    static public Path $page_path; // pour BreadcrumbBuilder
	private Page $page;
	private Node $root_node;

	public function __construct(EntityManager $entityManager)
	{
		$this->entityManager = $entityManager;
        self::$menu_data = new Menu($entityManager); // Menu est un modèle mais pas une entité
        self::$page_path = new Path();
        $this->page = self::$page_path->getLast();
        $this->root_node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants
	}

	public function makeRootNode(string $id = ''): void
    {
        // on récupère toutes les entrées
        $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page OR n.page IS null';
        if($id == '')
        {
            $bulk_data = $this->entityManager
                ->createQuery($dql)
                ->setParameter('page', $this->page)
                ->getResult();
        }
        else // avec $_GET['id'] dans l'URL
        {
            $dql .= ' OR n.article_timestamp = :id';
            $bulk_data = $this->entityManager
                ->createQuery($dql)
                ->setParameter('page', $this->page)
                ->setParameter('id', $id)
                ->getResult();
        }
        $this->feedObjects($bulk_data);
    }

    public function makeArticleNode(string $id = ''): bool
    {
        $bulk_data = $this->entityManager
            ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.article_timestamp = :id')
            ->setParameter('id', $id)
            ->getResult();

        if(count($bulk_data) === 0){
            return false;
        }
        
        $this->root_node = $bulk_data[0];
        return true;
    }

    public function feedObjects(array $bulk_data): void // $bulk_data = tableau de Node
    {
        // puis on les range
        // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD)
        foreach($bulk_data as $node)
        {
            // premier niveau
            if($node->getParent() == null)
            {
                $this->root_node->addChild($node);

                // spécifique page article
                if($node->getName() === 'main' && $this->page->getEndOfPath() == 'article'){
                    $main = $node;
                }
            }
            // autres niveaux
            else
            {
                $node->getParent()->addChild($node);

                // spécifique page article
                if($node->getName() === 'new' && $this->page->getEndOfPath() == 'article'){
                    $new = $node;
                }
            }
        }
        if(isset($new)){
            $main->setTempChild($new);
        }
    }

	public function getRootNode(): Node
	{
        return $this->root_node;
    }
}