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


namespace Micropoly\Handlers;


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

class NewNote implements Handler
{
    private static function getPostedContent(): ?string
    {
        if (empty($_POST["content"]))
            return null;

        $content = trim((string)$_POST["content"]);
        return empty($content) ? null : $content;
    }

    public function handle(Env $env, array $variables)
    {
        $content = self::getPostedContent();
        if ($content !== null) {
            $note = new Note();
            $note->setContent($content);
            $note->setTags($_POST["tag"]);
            $note->save($env->db());

            $url = $env->documentRoot() . "n/" . $note->getId();
            http_response_code(303);
            header("Location: {$url}");
            echo 'Note created: <a href="' . Esc::e($url) . '">';
        }

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