CreateCommentForm::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 23
cts 23
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 1
crap 1
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 CreateCommentForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di)
20
    {
21 1
        parent::__construct($di);
22 1
        $page = $this->di->session->get("previous");
23 1
        $acronym = $this->di->session->get("user");
24 1
        $this->form->create(
25
            [
26 1
                "id" => __CLASS__,
27 1
                "legend" => "Lämna en kommentar",
28 1
            ],
29 1
            [
30
                "comment" => [
31 1
                    "type"        => "textarea",
32
                    // "placeholder" => $page,
33 1
                ],
34
                "acronym" => [
35 1
                    "type"        => "hidden",
36 1
                    "value"       => $acronym,
37 1
                ],
38
                "page" => [
39 1
                    "type"        => "hidden",
40 1
                    "value"       => $page,
41 1
                ],
42
                "submit" => [
43 1
                    "type" => "submit",
44 1
                    "value" => "Submit",
45 1
                    "callback" => [$this, "callbackSubmit"]
46 1
                ],
47
            ]
48 1
        );
49 1
    }
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 boolean true if okey, false if something went wrong.
58
     */
59
    public function callbackSubmit()
60
    {
61
        // Get values from the submitted form
62
        $comment       = $this->form->value("comment");
63
        $acronym     = $this->form->value("acronym");
64
        $page       = $this->form->value("page");
65
66
        // Save to database
67
68
        $commentInst = new Comment();
69
        $commentInst->setDb($this->di->get("db"));
70
        $commentInst->commenttext = $comment;
71
        $commentInst->acronym = $acronym;
72
        $createdDate = date("h:i:s M jS Y", time());
73
74
        $commentInst->created = $createdDate;
75
        // $path = $this->di->request->getRoute();
76
        // $path = $this->di->request->getRoute('HTTP_REFERER');
77
        // var_dump($path);
78
        // exit;
79
        $commentInst->page = $page;
80
81
        $commentInst->save();
82
83
        $this->form->addOutput("Comment was posted.");
84
        return true;
85
    }
86
}
87