|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nicklas\Comment\HTMLForm\Comment; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
|
6
|
|
|
use \Anax\DI\DIInterface; |
|
7
|
|
|
use \Nicklas\Comment\Modules\User; |
|
8
|
|
|
use \Nicklas\Comment\Modules\Comment; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Example of FormModel implementation. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CreateQuestionForm extends FormModel |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Constructor injects with DI container. |
|
17
|
|
|
* |
|
18
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(DIInterface $di) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct($di); |
|
23
|
|
|
$this->form->create( |
|
24
|
|
|
[ |
|
25
|
|
|
"id" => __CLASS__, |
|
26
|
|
|
], |
|
27
|
|
|
[ |
|
28
|
|
|
"title" => [ |
|
29
|
|
|
"type" => "text", |
|
30
|
|
|
"label" => "En bra titel ökar chansen att någon svarar", |
|
31
|
|
|
"placeholder" => "Titel" |
|
32
|
|
|
], |
|
33
|
|
|
"tags" => [ |
|
34
|
|
|
"type" => "text", |
|
35
|
|
|
"label" => "Du kan ange fler än en tag. Men du behöver ange minst en.", |
|
36
|
|
|
"placeholder" => "#tags" |
|
37
|
|
|
], |
|
38
|
|
|
"question" => [ |
|
39
|
|
|
"type" => "textarea", |
|
40
|
|
|
"label" => "Här kan du skriva din fråga", |
|
41
|
|
|
"placeholder" => "Din fråga" |
|
42
|
|
|
], |
|
43
|
|
|
|
|
44
|
|
|
"submit" => [ |
|
45
|
|
|
"type" => "submit", |
|
46
|
|
|
"value" => "Skicka", |
|
47
|
|
|
"callback" => [$this, "callbackSubmit"] |
|
48
|
|
|
], |
|
49
|
|
|
] |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public function getHashtags($string) |
|
56
|
|
|
{ |
|
57
|
|
|
preg_match_all("/(#\w+)/u", $string, $matches); |
|
58
|
|
|
if ($matches) { |
|
59
|
|
|
$hashtagsArray = array_count_values($matches[0]); |
|
60
|
|
|
$hashtags = array_keys($hashtagsArray); |
|
61
|
|
|
$tags = implode(",", $hashtags); |
|
62
|
|
|
return $tags; |
|
63
|
|
|
} |
|
64
|
|
|
$tags = implode(",", $hashtags); |
|
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
/** |
|
68
|
|
|
* Callback for submit-button which should return true if it could |
|
69
|
|
|
* carry out its work and false if something failed. |
|
70
|
|
|
* |
|
71
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function callbackSubmit() |
|
74
|
|
|
{ |
|
75
|
|
|
// Get values from the submitted form |
|
76
|
|
|
$title = $this->form->value("title"); |
|
77
|
|
|
$tags = $this->getHashtags($this->form->value("tags")); |
|
78
|
|
|
$question = $this->form->value("question"); |
|
79
|
|
|
|
|
80
|
|
|
$comment = new Comment(); |
|
81
|
|
|
$comment->setDb($this->di->get("db")); |
|
82
|
|
|
|
|
83
|
|
|
if (!$this->di->get('session')->has("user")) { |
|
84
|
|
|
$this->form->addOutput("Du behöver logga in"); |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!$tags) { |
|
|
|
|
|
|
89
|
|
|
$this->form->addOutput(" |
|
90
|
|
|
<p>Inga taggar hittades, du behöver ange minst en.</p> |
|
91
|
|
|
<p>Du taggar genom att göra en hashtag, #mintag.</p> |
|
92
|
|
|
<p>#hej på dig #kaffe <--- kommer att bli #hej #kaffe</p> |
|
93
|
|
|
"); |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$user = $this->di->get('session')->get("user"); # get user name |
|
98
|
|
|
|
|
99
|
|
|
$comment->user = $user; |
|
|
|
|
|
|
100
|
|
|
$comment->title = $title; |
|
101
|
|
|
$comment->tags = $tags; |
|
102
|
|
|
$comment->text = $question; |
|
103
|
|
|
$comment->type = "question"; |
|
104
|
|
|
$comment->save(); |
|
105
|
|
|
|
|
106
|
|
|
$this->form->addOutput("Du skapade en fråga! Du bör se den nedan"); |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.