summaryrefslogtreecommitdiff
path: root/src/model/entities/Node.php
blob: a52a7e64059d101ea8ae23134c86f06cba5d3b9b (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
// src/model/entities/Node.php

declare(strict_types=1);

namespace App\Entity;

use Config;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: TABLE_PREFIX . "node")]
class Node
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: "integer")]
    private int $id_node;

    #[ORM\Column(type: "string", length: 255)]
    private string $name_node;

    #[ORM\Column(type: "string", length: 255, unique: true, nullable: true)]
    private ?string $article_timestamp;

    #[ORM\Column(type: "json", nullable: true)] // type: "json" crée un longtext avec mariadb
    private ?array $attributes = null;

    #[ORM\Column(type: "integer")]
    private int $position;

    #[ORM\ManyToOne(targetEntity: self::class)]
    //#[ORM\ManyToOne(targetEntity: self::class, fetch: 'EAGER')] // À TESTER
    #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id_node", onDelete: "SET NULL", nullable: true)]
    private ?self $parent = null;

    #[ORM\ManyToOne(targetEntity: Page::class)]
    #[ORM\JoinColumn(name: "page_id", referencedColumnName: "id_page", onDelete: "SET DEFAULT", nullable: true)]
    private ?Page $page;

    #[ORM\ManyToOne(targetEntity: Article::class, cascade: ['persist'])]
    #[ORM\JoinColumn(name: "article_id", referencedColumnName: "id_article", onDelete: "SET NULL", nullable: true)]
    private ?Article $article = null;

    // propriété non mappée dans la table "node", la jointure est décrite dans NodeData
    // elle sert à persister ou supprimer des données par cascade
    // "mappedBy" permet de cibler $node dans l'autre classe, qui elle possède un "inversedBy"
    #[ORM\OneToOne(targetEntity: NodeData::class, mappedBy: "node", cascade: ['persist', 'remove'])]
    private ?NodeData $node_data = null;


    // -- fin des attributs destinés à doctrine, début du code utilisateur --
    
    private array $children = []; // tableau de Node
    private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article"

    public function __construct(string $name = '', ?string $article_timestamp = null, array $attributes = [], int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null)
    {
        $this->name_node = $name;
        $this->article_timestamp = $article_timestamp;
        $this->attributes = $attributes;
        $this->position = $position;
        $this->parent = $parent;
        $this->page = $page;
        $this->article = $article;
    }

    // pfff...
    public function getId(): int
    {
        return $this->id_node;
    }
    public function getName(): string
    {
        return $this->name_node;
    }
    /*public function setName(string $name): void
    {
        $this->name_node = $name;
    }*/
    public function getArticleTimestamp(): string
    {
        return $this->article_timestamp;
    }
    public function getAttributes(): array
    {
        return $this->attributes;
    }
    /*public function setAttributes(array $attributes): void
    {
        $this->attributes = $attributes;
    }*/
    public function getParent(): ?self
    {
        return $this->parent;
    }
    /*public function setParent(?self $parent): void
    {
        $this->parent = $parent;
    }*/
    public function getPosition(): int
    {
        return $this->position;
    }
    public function setPosition(int $position): void
    {
        $this->position = $position;
    }
    public function getPage(): Page
    {
        return $this->page;
    }
    /*public function setPage(Page $page): void
    {
        $this->page = $page;
    }*/
    public function getArticle(): Article
    {
        return $this->article;
    }
    /*public function setArticle(Article $article): void
    {
        $this->article = $article;
    }*/
    public function getNodeData(): ?NodeData
    {
        return $this->node_data;
    }
    public function getChildren(): array
    {
        return $this->children;
    }
    public function addChild(self $child): void
    {
        $this->children[] = $child;
        $this->sortChildren(false);
    }
    // utiliser $position pour afficher les éléments dans l'ordre
    public function sortChildren(bool $reposition = false): void
    {
        // ordre du tableau des enfants
        // inefficace quand des noeuds ont la même position
        
        // tri par insertion
        for($i = 1; $i < count($this->children); $i++)
        {
            $tmp = $this->children[$i];
            $j = $i - 1;

            // Déplacez les éléments du tableau qui sont plus grands que la clé
            // à une position devant leur position actuelle
            while ($j >= 0 && $this->children[$j]->getPosition() > $tmp->getPosition()) {
                $this->children[$j + 1] = $this->children[$j];
                $j = $j - 1;
            }
            $this->children[$j + 1] = $tmp;
        }

        // nouvelles positions
        if($reposition){
            $i = 1;
            foreach($this->children as $child){
                $child->setPosition($i);
                $i++;
            }
        }
    }
    public function removeChild(self $child): void
    {
        foreach($this->children as $key => $object){
            if($object->getId() === $child->getId()){
                unset($this->children[$key]);
            }
            break;
        }
        $this->children = array_values($this->children); // réindexer pour supprimer la case vide
    }

    public function getAdoptedChild(): ?self // peut renvoyer null
    {
        return $this->adopted;
    }
    public function setAdoptedChild(self $child): void
    {
        $this->adopted = $child;
    }
}