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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
<?php
// src/model/Model.php
declare(strict_types=1);
use Doctrine\ORM\EntityManager;
use App\Entity\Page;
use App\Entity\Node;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\Request;
class Model
{
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)
{
$this->entityManager = $entityManager;
$this->node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants
}
// à déplacer dans Path ou un truc comme ça?
// couper Model en deux classes NodeModel et PageModel?
public function makeMenuAndPaths(): void // lit la table "page"
{
self::$menu_data = new Menu($this->entityManager);
self::$page_path = new Path();
$this->page = self::$page_path->getLast();
}
public function getNode(): Node
{
return $this->node;
}
public function getArticleNode(): Node
{
return $this->article;
}
// affichage d'une page ordinaire
public function getWholePageData(Request $request): void // lit la table "node" + jointures
{
$id = CURRENT_PAGE === 'article' ? htmlspecialchars($request->query->get('id')) : '';
if($id === ''){ // page "normale"
// récupérer tous les noeuds sauf les articles
$dql = "SELECT n FROM App\Entity\Node n WHERE n.name_node != 'new' AND n.name_node != 'post' AND (n.page = :page OR n.page IS null)";
$bulk_data = $this->entityManager
->createQuery($dql)
->setParameter('page', $this->page)
->getResult();
// groupes d'articles triés par bloc, permet de paginer par bloc
foreach($bulk_data as $parent_block){
if(Blocks::hasPresentation($parent_block->getName())){ // = post_block ou news_block
$bulk_data = array_merge($bulk_data, $this->getNextArticles($parent_block, $request)[0]);
}
}
}
else{ // page "article"
$dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page OR n.page IS null OR n.id_node = :id';
$bulk_data = $this->entityManager
->createQuery($dql)
->setParameter('page', $this->page)
->setParameter('id', $id)
->getResult();
}
$this->makeNodeTree($bulk_data);
}
// récupération d'articles
public function getNextArticles(Node $parent_block, Request $request): array
{
$qb = $this->entityManager->createQueryBuilder();
$qb->select('n')
->from('App\Entity\Node', 'n')
->where('n.parent = :parent')
->setParameter('parent', $parent_block);
if($parent_block->getName() === 'post_block'){
$qb->orderBy('n.position');
}
elseif($parent_block->getName() === 'news_block'){
$qb->join('n.article', 'a');
if($parent_block->getNodeData()->getChronoOrder() ?? false){ // ordre antichrono par défaut
$qb->orderBy('a.date_time', 'ASC');
}
else{
$qb->orderBy('a.date_time', 'DESC');
}
}
// pagination
$limit = $parent_block->getNodeData()->getPaginationLimit(); // = 12 par défaut si = null en BDD
$this->paginateWithCursor($qb, $parent_block, $request->query->get('last_article'));
$result = $qb->getQuery()->getResult();
// il reste des articles à récupérer SI on vient d'en récupérer trop
// ET on gère le cas particulier de $limit <= 0
$truncated = false;
if(count($result) > $limit && $limit > 0){ // si nb résultat > limit > 0
$truncated = true;
array_pop($result); // compenser le $limit + 1 dans paginateWithCursor
}
return [$result, $truncated]; // besoin exceptionnel de retourner deux valeurs
}
private function paginateWithCursor(QueryBuilder $qb, Node $parent_block, ?string $last_article): void
{
//var_dump($last_article);
$limit = $parent_block->getNodeData()->getPaginationLimit(); // = 12 par défaut si = null en BDD
if($limit > 0){ // si 0 ou moins pas de pagination
// nombres de "pages" d'articles
$nb_pages = $this->getNumberOfPages($parent_block, $limit);
$parent_block->getNodeData()->setNumberOfPages($nb_pages > 1 ? $nb_pages : 1);
// adaptation de la requête
if($parent_block->getName() === 'post_block'){
$qb->andWhere('n.position > :last_position')
->setParameter('last_position', empty($last_article) ? 0 : $last_article)
->setMaxResults($limit + 1);
}
elseif($parent_block->getName() === 'news_block'){
$cursor_start = $parent_block->getNodeData()->getChronoOrder() ? '1970-01-01' : '9999-12-31';
$qb->andWhere($parent_block->getNodeData()->getChronoOrder() ? 'a.date_time > :date_time' : 'a.date_time < :date_time')
->setParameter('date_time', empty($last_article) ? $cursor_start : $last_article)
->setMaxResults($limit + 1);
}
}
}
// le Paginator de doctrine le fait aussi si on décidait de s'en servir
private function getNumberOfPages(Node $parent_block, int $limit): int
{
$dql = 'SELECT COUNT(n.id_node) FROM App\Entity\Node n WHERE n.parent = :parent';
$nb_articles = $this->entityManager
->createQuery($dql)
->setParameter('parent', $parent_block)
->getSingleScalarResult();
return (int)ceil($nb_articles / $limit); // que PHP fasse une division non euclidienne (pas comme en C) nous arrange ici
}
private function makeNodeTree(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($this->page->getEndOfPath() == 'article'){
if($node->getName() === 'new'){
$new = $node;
}
}
}
}
if(isset($new)){
$main->setAdoptedChild($new);
}
}
// le basique
public function findNodeById(int $id): bool
{
$this->node = $this->entityManager->find('App\Entity\Node', $id);
return $this->node === null ? false : true;
}
// 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.id_node = :id';
}
else{
$dql = 'SELECT n FROM App\Entity\Node n WHERE n.id_node = :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);
}
}
}
|