CreatePostForm   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 23.68%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 134
ccs 18
cts 76
cp 0.2368
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 1
C createCategory() 0 48 7
B callbackSubmit() 0 37 2
1
<?php
2
3
namespace Radchasay\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\Post;
8
use \Radchasay\User\User;
9
use \Radchasay\Comment\PostCategory;
10
use \Radchasay\Comment\Post2Cat;
11
12
/**
13
 * Example of FormModel implementation.
14
 */
15
class CreatePostForm extends FormModel
16
{
17
    /**
18
     * Constructor injects with DI container.
19
     *
20
     * @param Anax\DI\DIInterface $di a service container
21
     */
22 2
    public function __construct(DIInterface $di)
23
    {
24 2
        parent::__construct($di);
25
26 2
        $this->form->create(
27
            [
28 2
                "id"     => __CLASS__,
29 2
                "legend" => "Create Post",
30
                "class" => "createNewPostForm"
31 2
            ],
32
            [
33
                "title" => [
34 2
                    "type" => "text",
35 2
                ],
36
37
                "text" => [
38 2
                    "type" => "textarea",
39 2
                ],
40
41
                "tags" => [
42 2
                    "type"        => "text",
43 2
                ],
44
45
                "submit" => [
46 2
                    "type"     => "submit",
47 2
                    "value"    => "Create Post",
48 2
                    "callback" => [$this, "callbackSubmit"],
49 2
                ],
50
            ]
51 2
        );
52 2
    }
53
54
55
    /**
56
     * Callback for submit-button which should return true if it could
57
     * carry out its work and false if something failed.
58
     *
59
     * @return boolean true if okey, false if something went wrong.
60
     */
61
    public function callbackSubmit()
62
    {
63
        // Get values from the submitted form
64
        //
65
        $post = new Post();
66
        $post->setDB($this->di->get("db"));
67
        $title = htmlentities($this->form->value("title"));
68
        $data = $this->form->value("text");
69
        $text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]);
70
71
        if (strlen($title) > 100) {
72
            $this->form->addOutput("Title is too long. Please fix!");
73
            return false;
74
        } else {
75
            $post->posttitle = $title;
76
        }
77
78
        $post->posttext = $text;
79
        $post->postname = htmlentities($this->di->get("session")->get("email"));
80
81
        $tags = htmlspecialchars($this->form->value("tags"));
82
83
        $user = new User();
84
        $user->setDb($this->di->get("db"));
85
        $user->getInformation($post->postname);
86
        $user->points += 2;
0 ignored issues
show
Bug introduced by
The property points does not seem to exist in Radchasay\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
87
88
        $user->save();
89
        $post->save();
90
91
92
        $this->createCategory($tags, $post->id);
93
94
        $url = $this->di->get("url")->create("comment/viewAllPosts");
95
        $this->di->get("response")->redirect($url);
96
        return true;
97
    }
98
99
100
    public function createCategory($tags, $postId)
101
    {
102
        $postcat = new PostCategory();
103
        $postcat->setDb($this->di->get("db"));
104
        $allCats = $postcat->findall();
105
106
        $existingCats = [];
107
108
        foreach ($allCats as $ac) {
109
            array_push($existingCats, $ac->category);
110
        }
111
112
        $tags = array_map('trim', explode(',', $tags));
113
114
        $tags = array_map("strtoupper", $tags);
115
116
        $uniqueTags = [];
117
118
        foreach ($tags as $t) {
119
            if (!in_array($t, $existingCats)) {
120
                if (!in_array($t, $uniqueTags)) {
121
                    array_push($uniqueTags, $t);
122
                }
123
            }
124
        }
125
126
        foreach ($uniqueTags as $ut) {
127
            $pc = new PostCategory();
128
            $pc->setDb($this->di->get("db"));
129
            $pc->category = $ut;
130
131
            $pc->save();
132
        }
133
        //
134
        foreach ($tags as $t) {
135
            $pc = new PostCategory();
136
            $pc->setDb($this->di->get("db"));
137
138
            $id = $pc->getId($t);
139
140
            $p2c = new Post2Cat();
141
            $p2c->setDb($this->di->get("db"));
142
            $p2c->catid = $id;
143
            $p2c->postid = $postId;
144
145
            $p2c->save();
146
        }
147
    }
148
}
149