Test Failed
Push — master ( 72a473...678733 )
by Gunvor
01:37
created

AdminDeleteCommForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Anax\Comments\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\Comments\Comm;
8
9
/**
10
 * Form to delete an item.
11
 */
12
class AdminDeleteCommForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 2
    public function __construct(DIInterface $di)
20
    {
21 2
        parent::__construct($di);
22 2
        $this->form->create(
23
            [
24 2
                "id" => __CLASS__,
25 2
                "legend" => "Alla inlägg:",
26 2
            ],
27
            [
28
                "select" => [
29 2
                    "type"        => "select",
30 2
                    "label"       => "Poster:",
31 2
                    "options"     => $this->getAllPosts(),
32 2
                ],
33
34
                "submit" => [
35 2
                    "type" => "submit",
36 2
                    "value" => "Ta bort",
37 2
                    "callback" => [$this, "callbackSubmit"]
38 2
                ],
39
            ]
40 2
        );
41 2
    }
42
43
44
45
    /**
46
     * Get all items as array suitable for display in select option dropdown.
47
     *
48
     * @return array with key value of all items.
49
     */
50 2
    public function getAllPosts()
51
    {
52 2
        $comm = new Comm();
53 2
        $comm->setDb($this->di->get("db"));
54
55 2
        $posts = ["-1" => "Välj en text..."];
56 2
        foreach ($comm->findAll() as $obj) {
57 2
            $posts[$obj->id] = "{$obj->title} ({$obj->id})";
58 2
        }
59
60 2
        return $posts;
61
    }
62
63
64
65
    /**
66
     * Callback for submit-button which should return true if it could
67
     * carry out its work and false if something failed.
68
     *
69
     * @return boolean true if okey, false if something went wrong.
70
     */
71
    public function callbackSubmit()
72
    {
73
        $comm = new Comm();
74
        $comm->setDb($this->di->get("db"));
75
        $one = $this->form->value("select");
76
77
        $all = $comm->findAll();
78
        $objects = "";
79
80
        foreach ($all as $obj) {
81
            if ($obj->parentid && $obj->parentid == $one) {
82
                echo 'Ja: ' . $obj->parentid . ' kastar ' . $obj->id . '<br />';
83
                $comm->find("id", $obj->id);
84
                $objects .= $comm->title . ", ";
85
                $comm->delete();
86
            }
87
        }
88
89
        $comm->find("id", $this->form->value("select"));
90
        $titles = $objects . $comm->title;
91
        $comm->delete();
92
        $this->form->addOutput($titles . ": kastad.");
93
94
        $pagerender = $this->di->get("pageRender");
95
        $pagerender->redirect("comm");
96
    }
97
}
98