CreateForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 9.472
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Forum\Answer\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Forum\Answer\Answer;
7
use Forum\User\User;
8
use Psr\Container\ContainerInterface;
9
10
11
/**
12
 * Form to create an item.
13
 */
14
class CreateForm extends FormModel
15
{
16
    /**
17
     * Constructor injects with DI container.
18
     *
19
     * @param ContainerInterface $di a service container
20
     * @param string $questionId the id of the question
21
     */
22
    public function __construct(ContainerInterface $di, string $questionId)
23
    {
24
        error_log("constructor called");
25
        parent::__construct($di);
26
        $this->form->create(
27
            [
28
                "id" => __CLASS__,
29
                "legend" => "Skapa svar",
30
            ],
31
            [
32
                "svar" => [
33
                    "type" => "textarea",
34
                    "validation" => ["not_empty"],
35
                ],
36
37
                "question_id" => [
38
                    "type"        => "hidden",
39
                    "value"       => $questionId,
40
                ],
41
42
                "submit" => [
43
                    "type" => "submit",
44
                    "value" => "Skapa svar",
45
                    "callback" => [$this, "callbackSubmit"]
46
                ],
47
            ]
48
        );
49
    }
50
51
52
53
    /**
54
     * Callback for submit-button which should return true if it could
55
     * carry out its work and false if something failed.
56
     *
57
     * @return bool true if okey, false if something went wrong.
58
     */
59
    public function callbackSubmit() : bool
60
    {
61
        error_log("callbacksubmit");
62
        $answer = new Answer();
63
        $answer->setDb($this->di->get("dbqb"));
64
        $answer->answer = $this->form->value("svar");
65
        $answer->question_id = $this->form->value("question_id");
66
        $answer->user_id = $this->di->get("session")->get("user_id");
67
        var_dump($answer->user_id);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($answer->user_id); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
68
        $answer->save();
69
        return true;
70
    }
71
72
73
74
    /**
75
     * Callback what to do if the form was successfully submitted, this
76
     * happen when the submit callback method returns true. This method
77
     * can/should be implemented by the subclass for a different behaviour.
78
     */
79
    public function callbackSuccess()
80
    {
81
        $this->di->get("response")->redirect("question/read/" . $this->form->value("question_id"))->send();
82
    }
83
84
85
86
     /**
87
      * Callback what to do if the form was unsuccessfully submitted, this
88
      * happen when the submit callback method returns false or if validation
89
      * fails. This method can/should be implemented by the subclass for a
90
      * different behaviour.
91
      */
92
     public function callbackFail()
93
     {
94
         error_log("No go");
95
         $this->di->get("response")->redirectSelf()->send();
96
     }
97
}
98