Passed
Push — Showing-Posts ( 91b09f...1f1a70 )
by Stone
03:15
created

Post::modifyPost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\CategoryModel;
6
use App\Models\PostModel;
7
use App\Models\SlugModel;
8
use App\Models\TagsModel;
9
use Core\AdminController;
10
11
class Post extends AdminController
12
{
13
14
    /**
15
     * page for new post
16
     */
17
    public function new()
18
    {
19
        $this->onlyAdmin();
20
        $categoryModel = new CategoryModel($this->container);
21
        $tagModel = new TagsModel($this->container);
22
        $this->data['categories'] = $categoryModel->getCategories();
23
        $this->data['tags'] = $tagModel->getTags();
24
        $this->renderView('Admin/NewPost');
25
    }
26
27
    /**
28
     * Lists all the posts
29
     */
30
    public function list()
31
    {
32
        $this->onlyAdmin();
33
    }
34
35
    /**
36
     * Shows the post to modify and update
37
     * @throws \ReflectionException
38
     * @throws \Twig_Error_Loader
39
     * @throws \Twig_Error_Runtime
40
     * @throws \Twig_Error_Syntax
41
     * @throws \ErrorException
42
     */
43
    public function modify(string $slug):void
44
    {
45
        $this->onlyAdmin();
46
47
        $categoryModel = new CategoryModel($this->container);
48
        $tagModel = new TagsModel($this->container);
49
        $postModel = new PostModel($this->container);
50
        $slugModel = new SlugModel($this->container);
51
52
        $postId = $slugModel->getIdFromSlug($slug,"posts","posts_slug", "idposts");
53
54
        $this->data['post'] = $postModel->getSinglePost($postId);
55
        $this->data['postTags'] = $tagModel->getTagsOnPost($postId);
56
        $this->data['categories'] = $categoryModel->getCategories();
57
        $this->data['tags'] = $tagModel->getTags();
58
        $this->renderView('Admin/ModifyPost');
59
    }
60
61
    /**
62
     * Create a new post
63
     * @throws \ErrorException
64
     */
65
    public function createNewPost()
66
    {
67
        //Security checks
68
        $this->onlyAdmin();
69
        if (!$this->request->isPost()) {
70
            $this->alertBox->setAlert('Only post messages allowed', 'error');
71
            $this->response->redirect('admin');
72
        }
73
74
        $posts = $this->container->getRequest()->getDataFull();
75
        $userSessionid = $this->container->getSession()->get("user_id");
76
77
78
        $title = trim($posts["newPostTitle"]);
79
        $postImage = $posts["newPostImage"]; //TODO Sanatize the input ? Or will PDO be enough ?
80
        $postSlug = trim($posts["newPostSlug"]); //TODO Check if unique
81
        $article = $posts["newPostTextArea"];
82
        $idCategory = $posts["categorySelector"];
83
        $published = $posts["isPublished"];
84
        $onFrontpage = $posts["isOnFrontPage"];
85
        $idUser = $userSessionid;
86
87
        $slugModel = new SlugModel($this->container);
88
        $tagModel = new TagsModel($this->container);
89
        $postModel = new PostModel($this->container);
90
91
        //security and error checks
92
        $error = false;
93
        if ($title == "") {
94
            $error = true;
95
            $this->alertBox->setAlert("empty title not allowed", "error");
96
        }
97
        if ($postSlug == "") {
98
            $error = true;
99
            $this->alertBox->setAlert("empty slug not allowed", "error");
100
        }
101
        if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) {
102
            $error = true;
103
            $this->alertBox->setAlert("Slug not unique", "error");
104
        }
105
106
        if ($error) {
107
            $this->container->getResponse()->redirect("admin/post/new");
108
        }
109
110
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage,
0 ignored issues
show
Bug introduced by
It seems like $idUser can also be of type null; however, parameter $idUser of App\Models\PostModel::newPost() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, /** @scrutinizer ignore-type */ $idUser, $published, $onFrontpage,
Loading history...
111
            $postSlug);
112
113
        if (isset($posts["tags"])) {
114
            foreach ($posts["tags"] as $tag) {
115
                if (isset($tag["id"])) {
116
                    $tagModel->addTagToPost($postId, $tag["id"]);
117
                    continue;
118
                }
119
                $tagModel->addNewTagToPost($postId, $tag["name"]);
120
            }
121
        }
122
        $this->alertBox->setAlert("Post " . $title . " Created");
123
        $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug);
124
    }
125
126
    /**
127
     * update a post
128
     * @throws \Exception
129
     */
130
    public function modifyPost()
131
    {
132
        //Security checks
133
        $this->onlyAdmin();
134
        if (!$this->request->isPost()) {
135
            $this->alertBox->setAlert('Only post messages allowed', 'error');
136
            $this->response->redirect('admin');
137
        }
138
139
        $posts = $this->container->getRequest()->getDataFull();
140
141
        /*TODO
142
        update the post
143
        add and delete tags -> for that compare list of tags already set (added and missing tags ...) ??
144
145
        */
146
147
        echo "<pre>";
148
        var_dump($posts);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($posts) looks like debug code. Are you sure you do not want to remove it?
Loading history...
149
        die();
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
150
    }
151
}