AdminDeleteCommForm   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 53.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 86
ccs 24
cts 45
cp 0.5333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B callbackSubmit() 0 26 4
A __construct() 0 23 1
A getAllPosts() 0 12 2
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