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

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