1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Forum\Question\HTMLForm; |
4
|
|
|
|
5
|
|
|
use Anax\HTMLForm\FormModel; |
6
|
|
|
use Forum\Tag\TagQuestion; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Forum\Question\Question; |
9
|
|
|
use Forum\Tag\Tag; |
10
|
|
|
use Forum\User\User; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Form to create an item. |
15
|
|
|
*/ |
16
|
|
|
class CreateForm extends FormModel |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Constructor injects with DI container. |
20
|
|
|
* |
21
|
|
|
* @param Psr\Container\ContainerInterface $di a service container |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function __construct(ContainerInterface $di) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
parent::__construct($di); |
26
|
|
|
$this->form->create( |
27
|
|
|
[ |
28
|
|
|
"id" => __CLASS__, |
29
|
|
|
"legend" => "Skapa fråga", |
30
|
|
|
], |
31
|
|
|
[ |
32
|
|
|
"titel" => [ |
33
|
|
|
"type" => "text", |
34
|
|
|
"validation" => ["not_empty"], |
35
|
|
|
], |
36
|
|
|
|
37
|
|
|
"fråga" => [ |
38
|
|
|
"type" => "textarea", |
39
|
|
|
"validation" => ["not_empty"], |
40
|
|
|
], |
41
|
|
|
|
42
|
|
|
"tags" => [ |
43
|
|
|
"type" => "text", |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
"submit" => [ |
47
|
|
|
"type" => "submit", |
48
|
|
|
"value" => "Skapa fråga", |
49
|
|
|
"callback" => [$this, "callbackSubmit"] |
50
|
|
|
], |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Callback for submit-button which should return true if it could |
59
|
|
|
* carry out its work and false if something failed. |
60
|
|
|
* |
61
|
|
|
* @return bool true if okey, false if something went wrong. |
62
|
|
|
*/ |
63
|
|
|
public function callbackSubmit() : bool |
64
|
|
|
{ |
65
|
|
|
$question = new Question(); |
66
|
|
|
$user_id = $this->di->get("session")->get("user_id"); |
67
|
|
|
$question->setDb($this->di->get("dbqb")); |
68
|
|
|
$question->title = $this->form->value("titel"); |
69
|
|
|
$question->question = $this->form->value("fråga"); |
70
|
|
|
// $question->tag = $tag; |
71
|
|
|
$question->user_id = $user_id; |
72
|
|
|
$question->created = date(DATE_RFC822); |
73
|
|
|
$question->save(); |
74
|
|
|
|
75
|
|
|
$tags = $this->form->value("tags"); |
76
|
|
|
// Split comma separated $tags into $tag_name_list array of strings |
77
|
|
|
$tagNames = explode(",", $tags); |
78
|
|
|
// Put the Tag object into $tag_list, which is an array of Tag objects |
79
|
|
|
$tagObjectsArray = []; |
80
|
|
|
foreach ($tagNames as $tagName) { |
81
|
|
|
$tag = new Tag(); |
82
|
|
|
$tag->setDb($this->di->get("dbqb")); |
83
|
|
|
$tag->find("name", $tagName); |
84
|
|
|
if ($tag->id == null) { |
85
|
|
|
$tag->name = $tagName; |
86
|
|
|
$tag->save(); |
87
|
|
|
} |
88
|
|
|
array_push($tagObjectsArray, $tag); |
89
|
|
|
} |
90
|
|
|
// For each Tag object in $tagObjectsArray |
91
|
|
|
// Create a new TagQuestion object with tag_id and question_id |
92
|
|
|
// Save it to the database |
93
|
|
|
foreach ($tagObjectsArray as $tag) { |
94
|
|
|
$tagQuestion = new TagQuestion(); |
95
|
|
|
$tagQuestion->setDb($this->di->get("dbqb")); |
96
|
|
|
$tagQuestion->tag_id = $tag->id; |
97
|
|
|
$tagQuestion->question_id = $question->id; |
98
|
|
|
$tagQuestion->save(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Callback what to do if the form was successfully submitted, this |
108
|
|
|
* happen when the submit callback method returns true. This method |
109
|
|
|
* can/should be implemented by the subclass for a different behaviour. |
110
|
|
|
*/ |
111
|
|
|
public function callbackSuccess() |
112
|
|
|
{ |
113
|
|
|
$this->di->get("response")->redirect("forum/index")->send(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Callback what to do if the form was unsuccessfully submitted, this |
120
|
|
|
* happen when the submit callback method returns false or if validation |
121
|
|
|
* fails. This method can/should be implemented by the subclass for a |
122
|
|
|
* different behaviour. |
123
|
|
|
*/ |
124
|
|
|
public function callbackFail() |
125
|
|
|
{ |
126
|
|
|
print_r("No go"); |
127
|
|
|
$this->di->get("response")->redirectSelf()->send(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.