aboutsummaryrefslogtreecommitdiff
path: root/src/Handlers/NewNote.php
blob: 838c7cbe2c4582fe9b31a2c634c50a228cd4eb75 (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
<?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();
        $templateData = [];
        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();
            if ($_POST["create_and_new"]) {
                $templateData["success"] = true;
            } else {
                http_response_code(303);
                header("Location: {$url}");
                echo 'Note created: <a href="' . Esc::e($url) . '">';
                return;
            }
        }

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