|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pamo\Question\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Pamo\Question\Question; |
|
8
|
|
|
use Pamo\Tag\Tag; |
|
9
|
|
|
use Pamo\Tag\TagQuestion; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Form to create an item. |
|
13
|
|
|
*/ |
|
14
|
|
|
class CreateQuestionForm extends FormModel |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor injects with DI container. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
|
|
|
|
|
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function __construct(ContainerInterface $di) |
|
22
|
|
|
{ |
|
23
|
1 |
|
parent::__construct($di); |
|
24
|
1 |
|
$this->user = $di->get("session")->get("user", null); |
|
|
|
|
|
|
25
|
1 |
|
$this->form->create( |
|
26
|
|
|
[ |
|
27
|
1 |
|
"id" => __CLASS__, |
|
28
|
|
|
"legend" => "Details of the item", |
|
29
|
|
|
], |
|
30
|
|
|
[ |
|
31
|
1 |
|
"user" => [ |
|
32
|
1 |
|
"type" => "hidden", |
|
33
|
|
|
"validation" => ["not_empty"], |
|
34
|
1 |
|
"value" => $this->user["username"], |
|
35
|
|
|
], |
|
36
|
|
|
|
|
37
|
|
|
"title" => [ |
|
38
|
|
|
"type" => "text", |
|
39
|
|
|
"validation" => ["not_empty"], |
|
40
|
|
|
], |
|
41
|
|
|
|
|
42
|
|
|
"question" => [ |
|
43
|
|
|
"type" => "text", |
|
44
|
|
|
"validation" => ["not_empty"], |
|
45
|
|
|
], |
|
46
|
|
|
|
|
47
|
|
|
"tags" => [ |
|
48
|
|
|
"type" => "text" |
|
49
|
|
|
], |
|
50
|
|
|
|
|
51
|
|
|
"submit" => [ |
|
52
|
1 |
|
"type" => "submit", |
|
53
|
1 |
|
"value" => "Submit", |
|
54
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
|
55
|
|
|
], |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Callback for submit-button which should return true if it could |
|
64
|
|
|
* carry out its work and false if something failed. |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool true if okey, false if something went wrong. |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function callbackSubmit() : bool |
|
69
|
|
|
{ |
|
70
|
1 |
|
$question = new Question(); |
|
71
|
1 |
|
$question->setDb($this->di->get("dbqb")); |
|
72
|
1 |
|
$question->title = $this->form->value("title"); |
|
73
|
1 |
|
$question->text = $this->form->value("question"); |
|
74
|
1 |
|
$question->user = $this->form->value("user"); |
|
75
|
1 |
|
$question->vote = 0; |
|
76
|
1 |
|
$question->save(); |
|
77
|
|
|
|
|
78
|
1 |
|
$tags = $this->form->value("tags"); |
|
79
|
|
|
|
|
80
|
1 |
|
if ($tags) { |
|
81
|
1 |
|
$questionTag = explode(",", $tags); |
|
82
|
1 |
|
foreach ($questionTag as $formTag) { |
|
83
|
1 |
|
$this->addTag($formTag, $question->id); |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
1 |
|
$this->addTag("General", $question->id); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
$game = $this->di->get("game"); |
|
90
|
1 |
|
$game->updateRank($question->user, 5); |
|
91
|
|
|
|
|
92
|
1 |
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add a new tag. |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function addTag($tagName, $question) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$tag = new Tag(); |
|
103
|
1 |
|
$tag->setDb($this->di->get("dbqb")); |
|
104
|
1 |
|
$tagQuestion = new TagQuestion(); |
|
105
|
1 |
|
$tagQuestion->setDb($this->di->get("dbqb")); |
|
106
|
1 |
|
$match = $tag->findAllWhere("name = ?", $tagName); |
|
107
|
|
|
|
|
108
|
1 |
|
if (count($match) == 0) { |
|
109
|
1 |
|
$tag->name = $tagName; |
|
110
|
1 |
|
$tag->save(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
$tagQuestion->questionid = $question; |
|
114
|
1 |
|
$tagQuestion->tagname = $tagName; |
|
115
|
1 |
|
$tagQuestion->save(); |
|
116
|
1 |
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Callback what to do if the form was successfully submitted, this |
|
123
|
|
|
* happen when the submit callback method returns true. This method |
|
124
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function callbackSuccess() |
|
127
|
|
|
{ |
|
128
|
1 |
|
$this->di->get("response")->redirect("question")->send(); |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
// /** |
|
134
|
|
|
// * Callback what to do if the form was unsuccessfully submitted, this |
|
135
|
|
|
// * happen when the submit callback method returns false or if validation |
|
136
|
|
|
// * fails. This method can/should be implemented by the subclass for a |
|
137
|
|
|
// * different behaviour. |
|
138
|
|
|
// */ |
|
139
|
|
|
// public function callbackFail() |
|
140
|
|
|
// { |
|
141
|
|
|
// $this->di->get("response")->redirectSelf()->send(); |
|
142
|
|
|
// } |
|
143
|
|
|
} |
|
144
|
|
|
|