UpdateForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 9.216
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Vihd14\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Vihd14\Comment\Comment;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20 1
    public function __construct(DIInterface $di, $id)
21
    {
22 1
        parent::__construct($di);
23 1
        $comment = $this->getItemDetails($id);
24 1
        $this->form->create(
25
            [
26 1
                "id" => __CLASS__,
27
                // "legend" => "Update details of the item",
28
            ],
29
            [
30 1
                "id" => [
31 1
                    "type" => "text",
32
                    "validation" => ["not_empty"],
33
                    "readonly" => true,
34 1
                    "value" => $comment->id,
35
                ],
36
37
                "email" => [
38 1
                    "type" => "text",
39 1
                    "label" => "E-mail:",
40
                    "validation" => ["not_empty"],
41 1
                    "value" => $comment->email,
42
                ],
43
44
                "text" => [
45 1
                    "type" => "textarea",
46 1
                    "label" => "Comment:",
47
                    "validation" => ["not_empty"],
48 1
                    "value" => $comment->text,
49
                ],
50
51
                "submit" => [
52 1
                    "type" => "submit",
53 1
                    "value" => "Save",
54 1
                    "callback" => [$this, "callbackSubmit"]
55
                ],
56
57
                "reset" => [
58
                    "type"      => "reset",
59
                    "value"     => "Reset"
60
                ],
61
            ]
62
        );
63 1
    }
64
65
66
67
    /**
68
     * Get details on item to load form with.
69
     *
70
     * @param integer $id get details on item with id.
71
     *
72
     * @return Comment
73
     */
74 1
    public function getItemDetails($id)
75
    {
76 1
        $comment = new Comment();
77 1
        $comment->setDb($this->di->get("db"));
78 1
        $comment->find("id", $id);
79 1
        return $comment;
80
    }
81
82
83
84
    /**
85
     * Callback for submit-button which should return true if it could
86
     * carry out its work and false if something failed.
87
     *
88
     * @return boolean true if okey, false if something went wrong.
89
     */
90
    public function callbackSubmit()
91
    {
92
        $comment = new Comment();
93
        $comment->setDb($this->di->get("db"));
94
        $comment->find("id", $this->form->value("id"));
95
        $comment->email = $this->form->value("email");
96
        $comment->text = $this->form->value("text");
97
        $comment->save();
98
        $this->di->get("response")->redirect("comments");
99
    }
100
}
101