diff options
Diffstat (limited to 'src/controller/Director.php')
| -rw-r--r-- | src/controller/Director.php | 256 |
1 files changed, 0 insertions, 256 deletions
diff --git a/src/controller/Director.php b/src/controller/Director.php deleted file mode 100644 index 9c1c6e3..0000000 --- a/src/controller/Director.php +++ /dev/null | |||
| @@ -1,256 +0,0 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/Director.php | ||
| 3 | |||
| 4 | // => src/model/Model.php serait mieux | ||
| 5 | |||
| 6 | declare(strict_types=1); | ||
| 7 | |||
| 8 | use Doctrine\ORM\EntityManager; | ||
| 9 | use App\Entity\Page; | ||
| 10 | use App\Entity\Node; | ||
| 11 | use Doctrine\ORM\QueryBuilder; | ||
| 12 | use Symfony\Component\HttpFoundation\Request; | ||
| 13 | |||
| 14 | class Director | ||
| 15 | { | ||
| 16 | private EntityManager $entityManager; | ||
| 17 | static public Menu $menu_data; // pour NavBuilder | ||
| 18 | static public ?Path $page_path = null; // pour $current dans NavBuilder et pour BreadcrumbBuilder | ||
| 19 | private Page $page; | ||
| 20 | private ?Node $node; | ||
| 21 | private Node $article; | ||
| 22 | |||
| 23 | public function __construct(EntityManager $entityManager) | ||
| 24 | { | ||
| 25 | $this->entityManager = $entityManager; | ||
| 26 | $this->node = new Node; // instance mère "vide" ne possédant rien d'autre que des enfants | ||
| 27 | } | ||
| 28 | |||
| 29 | // à déplacer dans Path ou un truc comme ça? | ||
| 30 | // couper Director en deux classes NodeModel et PageModel? | ||
| 31 | public function makeMenuAndPaths(): void // lit la table "page" | ||
| 32 | { | ||
| 33 | self::$menu_data = new Menu($this->entityManager); | ||
| 34 | self::$page_path = new Path(); | ||
| 35 | $this->page = self::$page_path->getLast(); | ||
| 36 | } | ||
| 37 | |||
| 38 | public function getNode(): Node | ||
| 39 | { | ||
| 40 | return $this->node; | ||
| 41 | } | ||
| 42 | public function getArticleNode(): Node | ||
| 43 | { | ||
| 44 | return $this->article; | ||
| 45 | } | ||
| 46 | |||
| 47 | // affichage d'une page ordinaire | ||
| 48 | public function getWholePageData(Request $request): void // lit la table "node" + jointures | ||
| 49 | { | ||
| 50 | $id = CURRENT_PAGE === 'article' ? htmlspecialchars($request->query->get('id')) : ''; | ||
| 51 | |||
| 52 | if($id === ''){ // page "normale" | ||
| 53 | // récupérer tous les noeuds sauf les articles | ||
| 54 | $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)"; | ||
| 55 | $bulk_data = $this->entityManager | ||
| 56 | ->createQuery($dql) | ||
| 57 | ->setParameter('page', $this->page) | ||
| 58 | ->getResult(); | ||
| 59 | |||
| 60 | // groupes d'articles triés par bloc, permet de paginer par bloc | ||
| 61 | foreach($bulk_data as $parent_block){ | ||
| 62 | if(Blocks::hasPresentation($parent_block->getName())){ // = post_block ou news_block | ||
| 63 | $bulk_data = array_merge($bulk_data, $this->getNextArticles($parent_block, $request)[0]); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | else{ // page "article" | ||
| 68 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.page = :page OR n.page IS null OR n.id_node = :id'; | ||
| 69 | $bulk_data = $this->entityManager | ||
| 70 | ->createQuery($dql) | ||
| 71 | ->setParameter('page', $this->page) | ||
| 72 | ->setParameter('id', $id) | ||
| 73 | ->getResult(); | ||
| 74 | } | ||
| 75 | |||
| 76 | $this->makeNodeTree($bulk_data); | ||
| 77 | } | ||
| 78 | |||
| 79 | // récupération d'articles | ||
| 80 | public function getNextArticles(Node $parent_block, Request $request): array | ||
| 81 | { | ||
| 82 | $qb = $this->entityManager->createQueryBuilder(); | ||
| 83 | $qb->select('n') | ||
| 84 | ->from('App\Entity\Node', 'n') | ||
| 85 | ->where('n.parent = :parent') | ||
| 86 | ->setParameter('parent', $parent_block); | ||
| 87 | |||
| 88 | if($parent_block->getName() === 'post_block'){ | ||
| 89 | $qb->orderBy('n.position'); | ||
| 90 | } | ||
| 91 | elseif($parent_block->getName() === 'news_block'){ | ||
| 92 | $qb->join('n.article', 'a'); | ||
| 93 | if($parent_block->getNodeData()->getChronoOrder() ?? false){ // ordre antichrono par défaut | ||
| 94 | $qb->orderBy('a.date_time', 'ASC'); | ||
| 95 | } | ||
| 96 | else{ | ||
| 97 | $qb->orderBy('a.date_time', 'DESC'); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | // pagination | ||
| 102 | $limit = $parent_block->getNodeData()->getPaginationLimit(); // = 12 par défaut si = null en BDD | ||
| 103 | $this->paginateWithCursor($qb, $parent_block, $request->query->get('last_article')); | ||
| 104 | $result = $qb->getQuery()->getResult(); | ||
| 105 | |||
| 106 | // il reste des articles à récupérer SI on vient d'en récupérer trop | ||
| 107 | // ET on gère le cas particulier de $limit <= 0 | ||
| 108 | $truncated = false; | ||
| 109 | if(count($result) > $limit && $limit > 0){ // si nb résultat > limit > 0 | ||
| 110 | $truncated = true; | ||
| 111 | array_pop($result); // compenser le $limit + 1 dans paginateWithCursor | ||
| 112 | } | ||
| 113 | |||
| 114 | return [$result, $truncated]; // besoin exceptionnel de retourner deux valeurs | ||
| 115 | } | ||
| 116 | |||
| 117 | private function paginateWithCursor(QueryBuilder $qb, Node $parent_block, ?string $last_article): void | ||
| 118 | { | ||
| 119 | //var_dump($last_article); | ||
| 120 | $limit = $parent_block->getNodeData()->getPaginationLimit(); // = 12 par défaut si = null en BDD | ||
| 121 | |||
| 122 | if($limit > 0){ // si 0 ou moins pas de pagination | ||
| 123 | // nombres de "pages" d'articles | ||
| 124 | $nb_pages = $this->getNumberOfPages($parent_block, $limit); | ||
| 125 | $parent_block->getNodeData()->setNumberOfPages($nb_pages > 1 ? $nb_pages : 1); | ||
| 126 | |||
| 127 | // adaptation de la requête | ||
| 128 | if($parent_block->getName() === 'post_block'){ | ||
| 129 | $qb->andWhere('n.position > :last_position') | ||
| 130 | ->setParameter('last_position', empty($last_article) ? 0 : $last_article) | ||
| 131 | ->setMaxResults($limit + 1); | ||
| 132 | } | ||
| 133 | elseif($parent_block->getName() === 'news_block'){ | ||
| 134 | $cursor_start = $parent_block->getNodeData()->getChronoOrder() ? '1970-01-01' : '9999-12-31'; | ||
| 135 | $qb->andWhere($parent_block->getNodeData()->getChronoOrder() ? 'a.date_time > :date_time' : 'a.date_time < :date_time') | ||
| 136 | ->setParameter('date_time', empty($last_article) ? $cursor_start : $last_article) | ||
| 137 | ->setMaxResults($limit + 1); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | // le Paginator de doctrine le fait aussi si on décidait de s'en servir | ||
| 143 | private function getNumberOfPages(Node $parent_block, int $limit): int | ||
| 144 | { | ||
| 145 | $dql = 'SELECT COUNT(n.id_node) FROM App\Entity\Node n WHERE n.parent = :parent'; | ||
| 146 | $nb_articles = $this->entityManager | ||
| 147 | ->createQuery($dql) | ||
| 148 | ->setParameter('parent', $parent_block) | ||
| 149 | ->getSingleScalarResult(); | ||
| 150 | return (int)ceil($nb_articles / $limit); // que PHP fasse une division non euclidienne (pas comme en C) nous arrange ici | ||
| 151 | } | ||
| 152 | |||
| 153 | private function makeNodeTree(array $bulk_data): void // $bulk_data = tableau de Node | ||
| 154 | { | ||
| 155 | // puis on les range | ||
| 156 | // (attention, risque de disfonctionnement si les noeuds de 1er niveau ne sont pas récupérés en 1er dans la BDD) | ||
| 157 | foreach($bulk_data as $node){ | ||
| 158 | // premier niveau | ||
| 159 | if($node->getParent() == null){ | ||
| 160 | $this->node->addChild($node); | ||
| 161 | |||
| 162 | // spécifique page article | ||
| 163 | if($node->getName() === 'main' && $this->page->getEndOfPath() == 'article'){ | ||
| 164 | $main = $node; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | // autres niveaux | ||
| 168 | else{ | ||
| 169 | $node->getParent()->addChild($node); | ||
| 170 | |||
| 171 | // spécifique page article | ||
| 172 | if($this->page->getEndOfPath() == 'article'){ | ||
| 173 | if($node->getName() === 'new'){ | ||
| 174 | $new = $node; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | if(isset($new)){ | ||
| 180 | $main->setAdoptedChild($new); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | // le basique | ||
| 185 | public function findNodeById(int $id): bool | ||
| 186 | { | ||
| 187 | $this->node = $this->entityManager->find('App\Entity\Node', $id); | ||
| 188 | return $this->node === null ? false : true; | ||
| 189 | } | ||
| 190 | |||
| 191 | // récupération d'un article pour modification | ||
| 192 | public function makeArticleNode(string $id = '', bool $get_section = false): bool | ||
| 193 | { | ||
| 194 | if($get_section){ | ||
| 195 | $dql = 'SELECT n, p FROM App\Entity\Node n LEFT JOIN n.parent p WHERE n.id_node = :id'; | ||
| 196 | } | ||
| 197 | else{ | ||
| 198 | $dql = 'SELECT n FROM App\Entity\Node n WHERE n.id_node = :id'; | ||
| 199 | } | ||
| 200 | // n est l'article et p son $parent | ||
| 201 | $bulk_data = $this->entityManager | ||
| 202 | ->createQuery($dql) | ||
| 203 | ->setParameter('id', $id) | ||
| 204 | ->getResult(); | ||
| 205 | |||
| 206 | if(count($bulk_data) === 0){ | ||
| 207 | return false; | ||
| 208 | } | ||
| 209 | |||
| 210 | if($get_section){ | ||
| 211 | $this->article = $bulk_data[0]; | ||
| 212 | $this->findNodeById($bulk_data[0]->getParent()->getId()); | ||
| 213 | $this->makeSectionNode(); | ||
| 214 | } | ||
| 215 | else{ | ||
| 216 | $this->article = $bulk_data[0]; | ||
| 217 | } | ||
| 218 | |||
| 219 | return true; | ||
| 220 | } | ||
| 221 | |||
| 222 | // récupération des articles d'un bloc <section> à la création d'un article | ||
| 223 | public function makeSectionNode(): bool | ||
| 224 | { | ||
| 225 | $bulk_data = $this->entityManager | ||
| 226 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent') | ||
| 227 | ->setParameter('parent', $this->node) | ||
| 228 | ->getResult(); | ||
| 229 | |||
| 230 | foreach($bulk_data as $article){ | ||
| 231 | $this->node->addChild($article); // pas de flush, on ne va pas écrire dans la BDD à chaque nouvelle page | ||
| 232 | } | ||
| 233 | return true; | ||
| 234 | } | ||
| 235 | |||
| 236 | public function findUniqueNodeByName(string $name): void // = unique en BDD, donc sans "page" associée | ||
| 237 | { | ||
| 238 | $bulk_data = $this->entityManager | ||
| 239 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.name_node = :name') | ||
| 240 | ->setParameter('name', $name) | ||
| 241 | ->getResult(); | ||
| 242 | $this->node = $bulk_data[0]; | ||
| 243 | } | ||
| 244 | |||
| 245 | public function findItsChildren(): void | ||
| 246 | { | ||
| 247 | $bulk_data = $this->entityManager | ||
| 248 | ->createQuery('SELECT n FROM App\Entity\Node n WHERE n.parent = :parent AND n.page = :page') | ||
| 249 | ->setParameter('parent', $this->node) | ||
| 250 | ->setParameter('page', $this->page) | ||
| 251 | ->getResult(); | ||
| 252 | foreach($bulk_data as $child){ | ||
| 253 | $this->node->addChild($child); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
