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


namespace Micropoly\Handlers;


use Micropoly\Env;
use Micropoly\Handler;
use Micropoly\Models\Attachment;
use Micropoly\Models\Note;
use Micropoly\TemplateModelWrappers\NoteForTemplate;

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);

            if (isset($_FILES['attachments']))
                Attachment::createFromUploads($env->db(), $env->attachmentsPath(), $note, $_FILES['attachments']);
        }

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