summaryrefslogtreecommitdiff
path: root/src/controller/Director.php
blob: b154432b11e516ce00bd08bc812d1d6add167ef5 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?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 = null; // pour $current dans NavBuilder et pour BreadcrumbBuilder
	private Page $page;
	private Node $node;
    private Node $article;

	public function __construct(EntityManager $entityManager, bool $get_menu = false)
	{
		$this->entityManager = $entityManager;
        if($get_menu){
            self::$menu_data = new Menu($entityManager);
            self::$page_path = new Path();
            $this->page = self::$page_path->getLast();
        }
        $this->node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants
	}

    public function getNode(): Node
    {
        return $this->node;
    }
    public function getArticleNode(): Node
    {
        return $this->article;
    }

    // affichage d'une page ordinaire
	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->feedRootNodeObjects($bulk_data);
    }

    private function feedRootNodeObjects(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->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->setAdoptedChild($new);
        }
    }

    // le basique
    public function findNodeById(int $id): void
    {
        $this->node = $this->entityManager->find('App\Entity\Node', $id);
    }

    // récupération d'un article pour modification
    public function makeArticleNode(string $id = '', bool $get_section = false): bool
    {
        if($get_section){
            $dql = 'SELECT n, p FROM App\Entity\Node n LEFT JOIN n.parent p WHERE n.article_timestamp = :id';
        }
        else{
            $dql = 'SELECT n FROM App\Entity\Node n WHERE n.article_timestamp = :id';
        }
        // n est l'article et p son $parent
        $bulk_data = $this->entityManager
            ->createQuery($dql)
            ->setParameter('id', $id)
            ->getResult();

        if(count($bulk_data) === 0){
            return false;
        }

        if($get_section){
            $this->article = $bulk_data[0];
            $this->findNodeById($bulk_data[0]->getParent()->getId());
            $this->makeSectionNode();
        }
        else{
            $this->article = $bulk_data[0];
        }

        return true;
    }

    // récupération des articles d'un bloc <section> à la création d'un article
    public function makeSectionNode(): bool
    {
        $bulk_data = $this->entityManager
            ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent')
            ->setParameter('parent', $this->node)
            ->getResult();

        foreach($bulk_data as $article){
            $this->node->addChild($article); // pas de flush, on ne va pas écrire dans la BDD à chaque nouvelle page
        }
        return true;
    }

    public function findUniqueNodeByName(string $name): void // = unique en BDD, donc sans "page" associée
    {
        $bulk_data = $this->entityManager
            ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.name_node = :name')
            ->setParameter('name', $name)
            ->getResult();
        $this->node = $bulk_data[0];
    }

    public function findItsChildren(): void
    {
        $bulk_data = $this->entityManager
            ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent AND n.page = :page')
            ->setParameter('parent', $this->node)
            ->setParameter('page', $this->page)
            ->getResult();
        foreach($bulk_data as $child){
            $this->node->addChild($child);
        }
    }
}