AnswerForm::callbackSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anax\Forum\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Anax\Forum\Answer;
8
use Anax\Forum\Forum;
9
10
/**
11
 * Example of FormModel implementation.
12
 */
13
class AnswerForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container and the id to update.
17
     *
18
     * @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...
19
     * @param integer             $id to update
20
     */
21
    public function __construct(ContainerInterface $di, $id)
22
    {
23
        parent::__construct($di);
24
        $forum = $this->getItemDetails($id);
25
        $session = $this->di->get("session");
26
        $user = $session->get("login");
27
        $this->form->create(
28
            [
29
                "id" => __CLASS__,
30
                "legend" => "Answer post nr: $forum->id",
31
            ],
32
            [
33
                "answer" => [
34
                    "type" => "textarea",
35
                    "placeholder" => "Write the answer in markdown to change how it looks.",
36
                ],
37
38
                "question" => [
39
                    "type" => "text",
40
                    "value" => $forum->id,
41
                    "readonly" => "readonly",
42
                ],
43
44
                "user" => [
45
                    "type" => "text",
46
                    "value" => $user,
47
                    "readonly" => "readonly",
48
                ],
49
50
                "submit" => [
51
                    "type" => "submit",
52
                    "value" => "Answer",
53
                    "callback" => [$this, "callbackSubmit"]
54
                ],
55
            ]
56
        );
57
    }
58
59
60
61
    /**
62
     * Get details on item to load form with.
63
     *
64
     * @param integer $id get details on item with id.
65
     *
66
     * @return Forum
67
     */
68
    public function getItemDetails($id) : object
69
    {
70
        $forum = new Forum();
71
        $forum->setDb($this->di->get("dbqb"));
72
        $forum->find("id", $id);
73
        return $forum;
74
    }
75
76
77
78
    /**
79
     * Callback for submit-button which should return true if it could
80
     * carry out its work and false if something failed.
81
     *
82
     * @return bool true if okey, false if something went wrong.
83
     */
84
    public function callbackSubmit()
85
    {
86
        // Get values from the submitted form
87
        $content = $this->form->value("answer");
88
        $question = $this->form->value("question");
89
        $user = $this->form->value("user");
90
        $answer = new Answer();
91
        $answer->setDb($this->di->get("dbqb"));
92
        $answer->content = $content;
93
        $answer->questionId = $question;
94
        $answer->user = $user;
0 ignored issues
show
Bug introduced by
The property user does not seem to exist on Anax\Forum\Answer.
Loading history...
95
        $answer->save();
96
        $this->form->addOutput("The answer has been published.");
97
        return true;
98
    }
99
100
    public function callbackSuccess()
101
    {
102
        $this->di->get("response")->redirect("forum")->send();
103
    }
104
}
105