aboutsummaryrefslogtreecommitdiff
path: root/src/Handlers/NewNote.php
blob: 9d4286de76ef79abb1159f6c60d779c34941a53e (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\Esc;
use Micropoly\Handler;
use Micropoly\Models\Attachment;
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());

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

            $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", []);
    }
}