diff options
Diffstat (limited to 'src/controller/Director.php')
-rw-r--r-- | src/controller/Director.php | 79 |
1 files changed, 71 insertions, 8 deletions
diff --git a/src/controller/Director.php b/src/controller/Director.php index 8be9b59..4b9293c 100644 --- a/src/controller/Director.php +++ b/src/controller/Director.php | |||
@@ -6,6 +6,8 @@ declare(strict_types=1); | |||
6 | use Doctrine\ORM\EntityManager; | 6 | use Doctrine\ORM\EntityManager; |
7 | use App\Entity\Page; | 7 | use App\Entity\Page; |
8 | use App\Entity\Node; | 8 | use App\Entity\Node; |
9 | //use Doctrine\ORM\QueryBuilder; | ||
10 | use Symfony\Component\HttpFoundation\Request; | ||
9 | 11 | ||
10 | class Director | 12 | class Director |
11 | { | 13 | { |
@@ -37,30 +39,91 @@ class Director | |||
37 | } | 39 | } |
38 | 40 | ||
39 | // affichage d'une page ordinaire | 41 | // affichage d'une page ordinaire |
40 | public function makeRootNode(string $id = ''): void | 42 | public function getWholePageData(Request $request): void |
41 | { | 43 | { |
42 | // on récupère toutes les entrées | 44 | $id = CURRENT_PAGE === 'article' ? htmlspecialchars($request->query->get('id')) : ''; |
43 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page OR n.page IS null'; | 45 | |
44 | if($id == '') | 46 | if($id === '') // page "normale" |
45 | { | 47 | { |
48 | // tous les noeuds sauf les articles, tri par page | ||
49 | $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)"; | ||
46 | $bulk_data = $this->entityManager | 50 | $bulk_data = $this->entityManager |
47 | ->createQuery($dql) | 51 | ->createQuery($dql) |
48 | ->setParameter('page', $this->page) | 52 | ->setParameter('page', $this->page) |
49 | ->getResult(); | 53 | ->getResult(); |
54 | |||
55 | // groupes d'articles triés par bloc, permet de paginer par bloc | ||
56 | foreach($bulk_data as $parent_block){ | ||
57 | if(Blocks::hasPresentation($parent_block->getName())){ // = post_block ou news_block | ||
58 | $qb = $this->entityManager->createQueryBuilder(); | ||
59 | $qb->select('n') | ||
60 | ->from('App\Entity\Node', 'n') | ||
61 | ->where('n.parent = :parent') | ||
62 | ->setParameter('parent', $parent_block); | ||
63 | |||
64 | if($parent_block->getName() === 'post_block'){ | ||
65 | $qb->orderBy('n.position'); | ||
66 | } | ||
67 | elseif($parent_block->getName() === 'news_block'){ | ||
68 | $qb->join('n.article', 'a'); | ||
69 | if($parent_block->getNodeData()->getChronoOrder() ?? false){ // ordre antichrono par défaut | ||
70 | $qb->orderBy('a.date_time', 'ASC'); | ||
71 | } | ||
72 | else{ | ||
73 | $qb->orderBy('a.date_time', 'DESC'); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | // pagination | ||
78 | $limit = $parent_block->getNodeData()->getPaginationLimit() ?? 0; // 0 par défaut = pas de pagination, sinon 12 rend bien avec des grilles de 2, 3 ou 4 colonnes | ||
79 | if($limit > 0){ | ||
80 | //$this->paginateWithCursor($qb, $request->query->get('last_position') ?? 0, $limit); | ||
81 | $qb->andWhere('n.position > :last_position') | ||
82 | ->setParameter('last_position', $request->query->get('last_position') ?? 0) | ||
83 | ->setMaxResults($limit); | ||
84 | |||
85 | $nb_pages = $this->getNumberOfPages($parent_block, $limit); // nombres de "pages" d'articles | ||
86 | if($nb_pages > 1){ | ||
87 | //$parent_block->setNumberOfPages($nb_pages); // => navigation en HTML | ||
88 | } | ||
89 | } | ||
90 | |||
91 | $bulk_data = array_merge($bulk_data, $qb->getQuery()->getResult()); | ||
92 | } | ||
93 | } | ||
50 | } | 94 | } |
51 | else // avec $_GET['id'] dans l'URL | 95 | else // page "article" |
52 | { | 96 | { |
53 | $dql .= ' OR n.id_node = :id'; | 97 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page OR n.page IS null OR n.id_node = :id'; |
54 | $bulk_data = $this->entityManager | 98 | $bulk_data = $this->entityManager |
55 | ->createQuery($dql) | 99 | ->createQuery($dql) |
56 | ->setParameter('page', $this->page) | 100 | ->setParameter('page', $this->page) |
57 | ->setParameter('id', $id) | 101 | ->setParameter('id', $id) |
58 | ->getResult(); | 102 | ->getResult(); |
59 | } | 103 | } |
60 | $this->feedRootNodeObjects($bulk_data); | 104 | $this->makeNodeTree($bulk_data); |
105 | } | ||
106 | |||
107 | /*private function paginateWithCursor(QueryBuilder $qb, int $last_position = 0, int $limit = 0): void | ||
108 | { | ||
109 | $qb->andWhere('n.position > :last_position') | ||
110 | ->setParameter('last_position', $last_position) | ||
111 | ->setMaxResults($limit); | ||
112 | }*/ | ||
113 | |||
114 | // requête à part n'alimentant pas $bulk_data | ||
115 | // fonctionnalité offerte par le Paginator de doctrine si on décidait de s'en servir | ||
116 | private function getNumberOfPages(Node $parent_block, int $limit): int | ||
117 | { | ||
118 | $dql = 'SELECT COUNT(n.id_node) FROM App\Entity\Node n WHERE n.parent = :parent'; | ||
119 | $nb_articles = $this->entityManager | ||
120 | ->createQuery($dql) | ||
121 | ->setParameter('parent', $parent_block) | ||
122 | ->getSingleScalarResult(); | ||
123 | return (int)ceil($nb_articles / $limit); // que PHP fasse une division non euclidienne (pas comme en C) nous arrange ici | ||
61 | } | 124 | } |
62 | 125 | ||
63 | private function feedRootNodeObjects(array $bulk_data): void // $bulk_data = tableau de Node | 126 | private function makeNodeTree(array $bulk_data): void // $bulk_data = tableau de Node |
64 | { | 127 | { |
65 | // puis on les range | 128 | // puis on les range |
66 | // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD) | 129 | // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD) |