summaryrefslogtreecommitdiff
path: root/handler/admin/PostAdd.php
blob: c21a2399b441bf84acb3c24107cd473ed6f3c04b (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace handler\admin;

use csrf;
use posts;
use RedirectResponse;
use Response;

class PostAdd extends AutoAddOrEdit {

    public function get(): Response {
        return $this->_get_postAdd();
    }

    public function post(): Response {
        csrf::check('addpost');

        list($text, $title, $tags, $visible, $short_name)
            = $this->input('text, title, tags, b:visible, short_name');
        $tags = posts::splitStringToTags($tags);

        $error_code = null;

        if (!$title) {
            $error_code = 'no_title';
        } else if (!$text) {
            $error_code = 'no_text';
        } else if (empty($tags)) {
            $error_code = 'no_tags';
        } else if (empty($short_name)) {
            $error_code = 'no_short_name';
        }

        if ($error_code)
            return $this->_get_postAdd(
                text: $text,
                title: $title,
                tags: $tags,
                short_name: $short_name,
                error_code: $error_code
            );

        $id = posts::add([
            'title' => $title,
            'md' => $text,
            'visible' => (int)$visible,
            'short_name' => $short_name,
        ]);

        if (!$id)
            $this->_get_postAdd(
                text: $text,
                title: $title,
                tags: $tags,
                short_name: $short_name,
                error_code: 'db_err'
            );

        // set tags
        $post = posts::get($id);
        $tag_ids = posts::getTagIds($tags);
        $post->setTagIds($tag_ids);

        return new RedirectResponse($post->getUrl());
    }

}