EditCommentForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
ccs 0
cts 31
cp 0
rs 8.8571
cc 1
eloc 28
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Marcusgsta\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Marcusgsta\Comment\Comment;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class EditCommentForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19
    public function __construct(DIInterface $di, $data)
20
    {
21
        parent::__construct($di);
22
23
        $comment = new Comment();
24
        $comment->setDb($this->di->get("db"));
25
        $page = $data['page'];
26
        $id = $data['commentid'];
27
        $thisComment = $comment->find('id', $id);
28
29
        $commenttext = $thisComment->commenttext;
30
        $acronym = $thisComment->acronym;
31
32
        $this->form->create(
33
            [
34
                "id" => __CLASS__,
35
                "legend" => "Redigera kommentar",
36
            ],
37
            [
38
                "id" => [
39
                    "value"       => $id,
40
                    "type"        => "hidden",
41
                ],
42
                "commenttext" => [
43
                    "type"        => "text",
44
                    "value"       => $commenttext,
45
                ],
46
                "acronym" => [
47
                    "type"        => "hidden",
48
                    "value"       => $acronym,
49
                ],
50
                "page" => [
51
                    "type"        => "hidden",
52
                    "value"       => $page,
53
                ],
54
                "submit" => [
55
                    "type" => "submit",
56
                    "value" => "Skicka",
57
                    "callback" => [$this, "callbackSubmit"]
58
                ],
59
            ]
60
        );
61
    }
62
63
64
    /**
65
    * Callback for submit-button which should return true if it could
66
    * carry out its work and false if something failed.
67
    *
68
    * @return boolean true if okey, false if something went wrong.
69
    */
70
    public function callbackSubmit()
71
    {
72
    // Get values from the submitted form
73
        $id             = $this->form->value("id");
74
        $commenttext    = $this->form->value("commenttext");
75
        $acronym        = $this->form->value("acronym");
76
        $page           = $this->form->value("page");
77
78
        // Save to database
79
        // $db = $this->di->get("db");
80
        // $password = password_hash($password, PASSWORD_DEFAULT);
81
        // $db->connect()
82
        //    ->insert("User", ["acronym", "password"])
83
        //    ->execute([$acronym, $password]);
84
        $comment = new Comment();
85
        $comment->setDb($this->di->get("db"));
86
87
        $comment->id = $id;
88
        $comment->commenttext = $commenttext;
89
        $comment->acronym = $acronym;
90
        $comment->page = $page;
91
92
        $comment->save();
93
94
        $this->form->addOutput("Kommentar uppdaterades.");
95
        return true;
96
    }
97
}
98