summaryrefslogtreecommitdiff
path: root/src/controller/post.php
blob: 631c4ada2ece554850edf83c9883d0c35a683c8a (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
<?php
// src/controller/post.php

declare(strict_types=1);

use App\Entity\Node;
use App\Entity\NodeData;
use App\Entity\Page;

if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SESSION['admin'] === true)
{
    /* -- formulaires HTML classiques -- */
    if($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded')
    {
        /* -- mode Modification d'une page -- */
        if(isset($_POST['page_menu_path']) && $_POST['page_menu_path'] !== null
            && isset($_POST['page_id']) && $_POST['page_id'] !== null
            && isset($_POST['page_name_path_hidden']) && $_POST['page_name_path_hidden'] === '')
        {
            $director = new Director($entityManager, true);
            $page = Director::$page_path->getLast();
            //$page = $entityManager->find('App\Entity\Page', $_POST['page_id']);
            $path = htmlspecialchars($_POST['page_menu_path']);

            // mise en snake_case: filtre caractères non-alphanumériques, minuscule, doublons d'underscore, trim des underscores
            $path = trim(preg_replace('/_+/', '_', strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $path))), '_');
            $page->setEndOfPath($path);
            foreach(Director::$menu_data->getChildren() as $child){
                if($child->getEndOfPath() === Director::$page_path->getArray()[0]->getEndOfPath()){
                    $child->fillChildrenPagePath(); // MAJ de $page_path
                }
            }
            $entityManager->flush();
            header("Location: " . new URL(['page' => $page->getPagePath(), 'action' => 'modif_page']));
            die;
        }
        // ajout d'un bloc dans une page
        elseif(isset($_POST['bloc_title']) && $_POST['bloc_title'] !== null
            && isset($_POST['bloc_select']) && $_POST['bloc_select'] !== null
            && isset($_POST['bloc_title_hidden']) && $_POST['bloc_title_hidden'] === '') // contrôle anti-robot avec input hidden
        {
            $director = new Director($entityManager, true); // on a besoin de page_path qui dépend de menu_data
            $page = Director::$page_path->getLast();
            $director->findUniqueNodeByName('main');
            $director->findItsChildren();
            $main = $director->getNode();
            $position = count($main->getChildren()) + 1; // position dans la fraterie

            $bloc = new Node(
                trim(htmlspecialchars($_POST["bloc_select"])),
                null, [],
                $position,
                $main,
                $page);
            $data = new NodeData(
                ['title' => trim(htmlspecialchars($_POST["bloc_title"]))],
                $bloc);

            $entityManager->persist($bloc);
            $entityManager->persist($data);
            $entityManager->flush();
            header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page']));
            die;
        }
        // suppression d'un bloc de page
        elseif(isset($_POST['delete_bloc_id']) && $_POST['delete_bloc_id'] !== null
            && isset($_POST['delete_bloc_hidden']) && $_POST['delete_bloc_hidden'] === '') // contrôle anti-robot avec input hidden
        {
            $director = new Director($entityManager, true);
            $director->findUniqueNodeByName('main');
            $director->findItsChildren();
            //$director->findNodeById((int)$_POST['delete_bloc_id']);
            $main = $director->getNode();
            $bloc;
            foreach($main->getChildren() as $child){
                if($child->getId() === (int)$_POST['delete_bloc_id']){
                    $bloc = $child;
                    break;
                }
            }
            $main->removeChild($bloc); // réindex le tableau $children au passage
            $main->reindexPositions();

            $entityManager->remove($bloc); // suppression en BDD
            $entityManager->flush();
            header("Location: " . new URL(['page' => $_GET['page'], 'action' => 'modif_page']));
            die;
        }


        /* -- page Menu et chemins -- */

        // création d'une entrée de menu avec une URL
        elseif(isset($_POST["label_input"]) && isset($_POST["url_input"]) && isset($_POST["location"])){
            Director::$menu_data = new Menu($entityManager);
            $previous_page = Director::$menu_data->findPageById((int)$_POST["location"]); // (int) à cause de declare(strict_types=1);
            $parent = $previous_page->getParent();

            $page = new Page(
                trim(htmlspecialchars($_POST["label_input"])),
                filter_var($_POST["url_input"], FILTER_VALIDATE_URL),
                true, true, false,
                $previous_page->getPosition(),
                $parent);

            // on indique pour la nouvelle entrée la même position que la précédente, puis addChild l'ajoute à la fin du tableau "children" avant de déclencher un tri
            // exemple avec 2 comme position demandée: 1 2 3 4 2 devient 1 2 3 4 5 et la nouvelle entrée sera en 3è position
            if($parent == null){
                $parent = Director::$menu_data;
            }
            $parent->addChild($page); // true pour réindexer les positions en BDD
            $parent->reindexPositions();

            $entityManager->persist($page);
            $entityManager->flush();
            header("Location: " . new URL(['page' => $_GET['from']]));
            die;
        }
        // suppression d'une entrée de menu avec une URL
        elseif(isset($_POST['delete']) && isset($_POST['x']) && isset($_POST['y'])){ // 2 params x et y sont là parce qu'on a cliqué sur une image
            Director::$menu_data = new Menu($entityManager);
            $page = Director::$menu_data->findPageById((int)$_POST["delete"]);
            $parent = $page->getParent();
            if($parent == null){
                $parent = Director::$menu_data;
            }

            $parent->removeChild($page); // suppression de $children avant de trier
            $parent->reindexPositions();

            $entityManager->remove($page); // suppression en BDD
            $entityManager->flush();
            header("Location: " . new URL(['page' => $_GET['from']]));
            die;
        }
        else{
            header("Location: " . new URL(['error' => 'paramètres inconnus']));
            die;
        }
    }

    /* -- requêtes AJAX -- */
    else{
        require '../src/controller/ajax.php';
    }
}