aboutsummaryrefslogtreecommitdiff
path: root/src/Handlers/NoteHandler.php
blob: afdabb5061aba979bfe358608300a237e50ffbcf (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
<?php


namespace Micropoly\Handlers;


use Micropoly\Env;
use Micropoly\Handler;
use Micropoly\Models\Note;

class NoteHandler implements Handler
{
    public function handle(Env $env, array $variables)
    {
        $db = $env->db();

        $note = Note::byId($db, $variables["id"]);
        if ($note === null) {
            (new NotFoundHandler())->handle($env, []);
            return;
        }

        if ($_SERVER["REQUEST_METHOD"] === "POST") {
            if ($_POST["delete"] === "delete") {
                $note->delete($db);
                http_response_code(303);
                $url = $env->documentRoot();
                header("Location: {$url}");
                return;
            }

            $note->setContent($_POST["content"]);
            $note->setTags($_POST["tag"]);
            $note->save($db);
        }

        echo $env->twig()->render("/note.twig", ["note" => $note]);
    }
}