Completed
Branch master (3028d7)
by Markus
06:24 queued 03:12
created

CreatePostForm::callbackSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Anax\Forum\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Anax\Forum\Forum;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreatePostForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Anax\Forum\HTMLForm\Psr\...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $di)
20
    {
21
        parent::__construct($di);
22
        $session = $this->di->get("session");
23
        $user = $session->get("login");
24
        $this->form->create(
25
            [
26
                "id" => __CLASS__,
27
                "legend" => "Create a forum post",
28
            ],
29
            [
30
                "title" => [
31
                    "type"        => "text",
32
                ],
33
34
                "content" => [
35
                    "type"        => "textarea",
36
                    "placeholder" => "Write the post in markdown to change how it looks.",
37
                ],
38
39
                "tag" => [
40
                    "type"        => "text",
41
                ],
42
43
                "user" => [
44
                    "type" => "text",
45
                    "value" => $user,
46
                    "readonly" => "readonly",
47
                ],
48
49
                "submit" => [
50
                    "type" => "submit",
51
                    "value" => "Skapa inlägg",
52
                    "callback" => [$this, "callbackSubmit"]
53
                ],
54
            ]
55
        );
56
    }
57
58
59
60
    /**
61
     * Callback for submit-button which should return true if it could
62
     * carry out its work and false if something failed.
63
     *
64
     * @return boolean true if okey, false if something went wrong.
65
     */
66
     public function callbackSubmit()
67
    {
68
        // Get values from the submitted form
69
        $title = $this->form->value("title");
70
        $content = $this->form->value("content");
71
        $tag = $this->form->value("tag");
72
        $user = $this->form->value("user");
73
74
        $forum = new Forum();
75
        $forum->setDb($this->di->get("dbqb"));
76
        $forum->title = $title;
77
        $forum->content = $content;
78
        $forum->tag = $tag;
79
        $forum->user = $user;
0 ignored issues
show
Bug introduced by
The property user does not seem to exist on Anax\Forum\Forum.
Loading history...
80
        $forum->save();
81
82
        $this->form->addOutput("Post was created.");
83
        return true;
84
    }
85
86
    public function callbackSuccess()
87
    {
88
        $this->di->get("response")->redirect("forum")->send();
89
    }
90
}
91