summaryrefslogtreecommitdiff
path: root/handler/admin/AutoEdit.php
blob: 9d70c5bda656416e331311fb5d7fc63175a7c818 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

namespace handler\admin;

use csrf;
use pages;
use posts;
use Response;

class AutoEdit extends AutoAddOrEdit {

    public function get(): Response {
        list($short_name, $saved) = $this->input('short_name, b:saved');

        $post = posts::getPostByName($short_name);
        if ($post) {
            $tags = $post->getTags();
            return $this->_get_postEdit($post,
                tags: $post->getTags(),
                saved: $saved,
                title: $post->title,
                text: $post->md,
                visible: $post->visible,
                short_name: $post->shortName,
            );
        }

        $page = pages::getPageByName($short_name);
        if ($page) {
            return $this->_get_pageEdit($page,
                title: $page->title,
                text: $page->md,
                visible: $page->visible,
                saved: $saved,
            );
        }

        throw new \NotFoundException();
    }

    public function post(): Response {
        list($short_name) = $this->input('short_name');

        $post = posts::getPostByName($short_name);
        if ($post) {
            csrf::check('editpost'.$post->id);

            list($text, $title, $tags, $visible, $short_name)
                = $this->input('text, title, tags, b:visible, new_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)
                $this->_get_postEdit($post,
                    text: $text,
                    title: $title,
                    tags: $tags,
                    visible: $visible,
                    short_name: $short_name,
                    error_code: $error_code
                );

            $post->edit([
                'title' => $title,
                'md' => $text,
                'visible' => (int)$visible,
                'short_name' => $short_name
            ]);
            $tag_ids = posts::getTagIds($tags);
            $post->setTagIds($tag_ids);

            return new \RedirectResponse($post->getUrl().'edit/?saved=1');
        }

        $page = pages::getPageByName($short_name);
        if ($page) {
            csrf::check('editpage'.$page->shortName);

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

            $text = trim($text);
            $title = trim($title);
            $error_code = null;

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

            if ($error_code) {
                return $this->_get_pageEdit($page,
                    title: $title,
                    text: $text,
                    visible: $visible,
                    error_code: $error_code
                );
            }

            $page->edit([
                'title' => $title,
                'md' => $text,
                'visible' => (int)$visible,
                'short_name' => $short_name,
            ]);

            return new \RedirectResponse($page->getUrl().'edit/?saved=1');
        }

        throw new \NotFoundException();
    }

}