Completed
Push — master ( cdee41...f70a2b )
by Gunvor
01:59
created

CreateCommForm::aForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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
            ["id" => __CLASS__, "legend" => "Gör ett inlägg",],
34
            [
35
                "title" => ["type"  => "text", "label" => "Titel","validation" => ["not_empty"]],
36
                "id" => ["type"  => "hidden", "value" => $id],
37
                "parentid" => ["type"  => "hidden", "value" => $parentid],
38
                "comment" => ["type"  => "textarea","label" => "Text","validation" => ["not_empty"]],
39
                "submit" => ["type" => "submit", "value" => "Spara", "callback" => [$this, "callbackSubmit"]],
40
            ]
41
        );
42
    }
43
44
45
46
    /**
47
     * Callback for submit-button which should return true if it could
48
     * carry out its work and false if something failed.
49
     *
50
     * @return boolean true if okey, false if something went wrong.
51
     */
52
    public function callbackSubmit()
53
    {
54
        $textfilter = $this->di->get("textfilter");
55
56
        $userController = $this->di->get("userController");
57
        $userdetails = $userController->getOne($this->form->value("id"));
58
        $parses = ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"];
59
        $comment = $textfilter->parse($this->form->value("comment"), $parses);
60
        $comment->frontmatter['title'] = $this->form->value("title");
61
        $comment = json_encode($comment);
62
63
        $now = date("Y-m-d H:i:s");
64
65
        $comm = new Comm();
66
        $comm->setDb($this->di->get("db"));
67
        $comm->title = $this->form->value("title");
68
        $comm->userid = $this->form->value("id");
69
        $comm->parentid = $this->form->value("parentid");
70
        $comm->comment = $comment;
71
        $comm->email = $userdetails["email"];
72
        $comm->created = $now;
73
        $comm->save();
74
75
        $back = (int)$this->form->value("parentid") > 0 ? "/view-one/" . $this->form->value("parentid") : "";
76
        $pagerender = $this->di->get("pageRender");
77
        $pagerender->redirect("comm" . $back);
78
    }
79
}
80