1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Forum\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Anax\Forum\Forum; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Example of FormModel implementation. |
11
|
|
|
*/ |
12
|
|
|
class CreatePostForm extends FormModel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor injects with DI container. |
16
|
|
|
* |
17
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
public function __construct(ContainerInterface $di) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($di); |
22
|
|
|
$user = $this->di->get("session")->get("login"); |
23
|
|
|
$this->form->create( |
24
|
|
|
[ |
25
|
|
|
"id" => __CLASS__, |
26
|
|
|
"legend" => "Create a forum post", |
27
|
|
|
], |
28
|
|
|
[ |
29
|
|
|
"title" => [ |
30
|
|
|
"type" => "text", |
31
|
|
|
], |
32
|
|
|
|
33
|
|
|
"content" => [ |
34
|
|
|
"type" => "textarea", |
35
|
|
|
"placeholder" => "Write the post in markdown to change how it looks.", |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
"tag" => [ |
39
|
|
|
"type" => "text", |
40
|
|
|
], |
41
|
|
|
|
42
|
|
|
"user" => [ |
43
|
|
|
"type" => "text", |
44
|
|
|
"value" => $user, |
45
|
|
|
"readonly" => "readonly", |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
"submit" => [ |
49
|
|
|
"type" => "submit", |
50
|
|
|
"value" => "Skapa inlägg", |
51
|
|
|
"callback" => [$this, "callbackSubmit"] |
52
|
|
|
], |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get details on item to load form with. |
59
|
|
|
* |
60
|
|
|
* @param integer $id get details on item with id. |
61
|
|
|
* |
62
|
|
|
* @return User |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function getItemDetails($id) : object |
65
|
|
|
{ |
66
|
|
|
$user = new \Anax\User\User(); |
67
|
|
|
$user->setDb($this->di->get("dbqb")); |
68
|
|
|
$user->find("id", $id); |
69
|
|
|
return $user; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Callback for submit-button which should return true if it could |
74
|
|
|
* carry out its work and false if something failed. |
75
|
|
|
* |
76
|
|
|
* @return boolean true if okey, false if something went wrong. |
77
|
|
|
*/ |
78
|
|
|
public function callbackSubmit() |
79
|
|
|
{ |
80
|
|
|
// Get values from the submitted form |
81
|
|
|
$login = $this->di->get("session")->get("login"); |
82
|
|
|
$poster = $this->getItemDetails($login); |
83
|
|
|
$title = $this->form->value("title"); |
84
|
|
|
$content = $this->form->value("content"); |
85
|
|
|
$tag = $this->form->value("tag"); |
86
|
|
|
$user = $this->form->value("user"); |
87
|
|
|
|
88
|
|
|
$forum = new Forum(); |
89
|
|
|
$forum->setDb($this->di->get("dbqb")); |
90
|
|
|
$forum->title = $title; |
91
|
|
|
$forum->content = $content; |
92
|
|
|
$forum->tag = $tag; |
93
|
|
|
$forum->user = $user; |
|
|
|
|
94
|
|
|
$poster->username = $poster->username; |
95
|
|
|
$poster->activity += 1; |
96
|
|
|
$poster->save(); |
97
|
|
|
$forum->save(); |
98
|
|
|
|
99
|
|
|
$this->form->addOutput("Post was created."); |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function callbackSuccess() |
104
|
|
|
{ |
105
|
|
|
$this->di->get("response")->redirect("forum")->send(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|