Completed
Push — master ( 66b618...d2e9dc )
by Gunvor
01:50
created

CreateCommForm::aForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 25
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 2
crap 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 create an item.
11
 */
12
class CreateCommForm 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, $id, $parentid = null)
20
    {
21
        parent::__construct($di);
22
        $this->aForm($id, $parentid);
23
    }
24
25
26
    /**
27
     * Create the form.
28
     *
29
     */
30
    public function aForm($id, $parentid)
31
    {
32
        $this->form->create(
33
            [
34
                "id" => __CLASS__,
35
                "legend" => "Gör ett inlägg",
36
            ],
37
            [
38
                "title" => [
39
                    "type"  => "text",
40
                    "label" => "Titel",
41
                    "validation" => ["not_empty"],
42
                ],
43
44
                "id" => [
45
                    "type"  => "hidden",
46
                    "value" => $id,
47
                ],
48
49
                "parentid" => [
50
                    "type"  => "hidden",
51
                    "value" => $parentid,
52
                ],
53
54
                "comment" => [
55
                    "type"  => "textarea",
56
                    "label" => "Text",
57
                    "validation" => ["not_empty"],
58
                ],
59
60
                "submit" => [
61
                    "type" => "submit",
62
                    "value" => "Spara",
63
                    "callback" => [$this, "callbackSubmit"]
64
                ],
65
            ]
66
        );
67
    }
68
69
70
71
    /**
72
     * Callback for submit-button which should return true if it could
73
     * carry out its work and false if something failed.
74
     *
75
     * @return boolean true if okey, false if something went wrong.
76
     */
77
    public function callbackSubmit()
78
    {
79
        $textfilter = $this->di->get("textfilter");
80
81
        $userController = $this->di->get("userController");
82
        $userdetails = $userController->getOne($this->form->value("id"));
83
        $parses = ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"];
84
        $comment = $textfilter->parse($this->form->value("comment"), $parses);
85
        $comment->frontmatter['title'] = $this->form->value("title");
86
        $comment = json_encode($comment);
87
88
        $now = date("Y-m-d H:i:s");
89
90
        $comm = new Comm();
91
        $comm->setDb($this->di->get("db"));
92
        $comm->title = $this->form->value("title");
93
        $comm->userid = $this->form->value("id");
94
        $comm->parentid = $this->form->value("parentid");
95
        $comm->comment = $comment;
96
        $comm->email = $userdetails["email"];
97
        $comm->created = $now;
98
        $comm->save();
99
100
        $back = (int)$this->form->value("parentid") > 0 ? "/view-one/" . $this->form->value("parentid") : "";
101
        $pagerender = $this->di->get("pageRender");
102
        $pagerender->redirect("comm" . $back);
103
    }
104
}
105