1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Comments; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\DIInterface; |
6
|
|
|
use \Anax\Comments\Comm; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Form to update an item. |
10
|
|
|
*/ |
11
|
|
|
class ShowOneService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array $commentitem, the chosen comment. |
15
|
|
|
*/ |
16
|
|
|
protected $comment; |
17
|
|
|
protected $comments; |
18
|
|
|
protected $sess; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor injects with DI container and the id to update. |
23
|
|
|
* |
24
|
|
|
* @param Anax\DI\DIInterface $di a service container |
25
|
|
|
* @param integer $id to show |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct(DIInterface $di, $id) |
28
|
|
|
{ |
29
|
1 |
|
$this->di = $di; |
|
|
|
|
30
|
1 |
|
$this->comment = $this->getItemDetails($id); |
|
|
|
|
31
|
|
|
|
32
|
1 |
|
$where = "parentid = ?"; |
33
|
1 |
|
$params = [$id]; |
34
|
1 |
|
$this->comments = $this->getParentDetails($where, $params); |
35
|
|
|
|
36
|
1 |
|
$session = $this->di->get("session"); |
37
|
1 |
|
$this->sess = $session->get("user"); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get details on item to load form with. |
43
|
|
|
* |
44
|
|
|
* @param integer $id get details on item with id. |
45
|
|
|
* |
46
|
|
|
* @return Comm |
47
|
|
|
*/ |
48
|
1 |
View Code Duplication |
public function getItemDetails($id) |
|
|
|
|
49
|
|
|
{ |
50
|
1 |
|
$comm = new Comm(); |
51
|
1 |
|
$comm->setDb($this->di->get("db")); |
52
|
1 |
|
$comm->find("id", $id); |
53
|
1 |
|
return $comm; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get details on item to load form with. |
59
|
|
|
* |
60
|
|
|
* @param string $where |
61
|
|
|
* @param array $params get details on item with id parentid. |
62
|
|
|
* |
63
|
|
|
* @return Comm |
64
|
|
|
*/ |
65
|
1 |
|
public function getParentDetails($where, $params) |
66
|
|
|
{ |
67
|
1 |
|
$comm = new Comm(); |
68
|
1 |
|
$comm->setDb($this->di->get("db")); |
69
|
1 |
|
return $comm->findAllWhere($where, $params); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets the callable to use for creating routes. |
75
|
|
|
* |
76
|
|
|
* @param callable $urlCreate to create framework urls. |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
1 |
|
public function setUrlCreator($route) |
81
|
|
|
{ |
82
|
1 |
|
$url = $this->di->get("url"); |
83
|
1 |
|
return call_user_func([$url, "create"], $route); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns link for gravatar img |
89
|
|
|
* |
90
|
|
|
* @param object $item |
91
|
|
|
* @return string htmlcode |
92
|
|
|
*/ |
93
|
1 |
|
public function getGravatar($item) |
94
|
|
|
{ |
95
|
1 |
|
$comm = new Comm(); |
96
|
1 |
|
$gravatar = $comm->getGravatar($item); |
97
|
1 |
|
return '<img src="' . $gravatar . '" alt=""/>'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns json_decoded title and text |
103
|
|
|
* If lead text, headline is larger font |
104
|
|
|
* @param object $item |
105
|
|
|
* @return string htmlcode |
106
|
|
|
*/ |
107
|
1 |
|
public function getDecode(Comm $item, $lead = null) |
108
|
|
|
{ |
109
|
1 |
|
$comt = json_decode($item->comment); |
110
|
1 |
|
if ($comt->frontmatter->title) { |
111
|
1 |
|
$til = $comt->frontmatter->title; |
112
|
1 |
|
} else { |
113
|
|
|
$til = $item->title; |
114
|
|
|
} |
115
|
1 |
|
$comt = $comt->text; |
116
|
1 |
|
if ($lead) { |
117
|
|
|
return '<h3>' . $til . '</h3><p>' . $comt . '</p>'; |
118
|
|
|
} |
119
|
1 |
|
return '<h4>' . $til . '</h4><p>' . $comt . '</p>'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* If param met, returns string with edit-links |
125
|
|
|
* @param boolean $isadmin |
126
|
|
|
* @param object $item |
127
|
|
|
* @param string $del |
128
|
|
|
* @return string htmlcode |
129
|
|
|
*/ |
130
|
1 |
|
public function getCanEdit(Comm $item, $isadmin, $del) |
131
|
|
|
{ |
132
|
1 |
|
$canedit = ""; |
133
|
1 |
|
$update = $this->setUrlCreator("comm/update"); |
|
|
|
|
134
|
1 |
|
if ($item->userid == $this->sess['id'] || $isadmin) { |
135
|
1 |
|
$canedit = '<br /><a href="' . $update . '/' . $item->id . '">Redigera</a>'; |
136
|
1 |
|
$canedit .= ' | <a href="' . $del . '/' . $item->id . '">Ta bort inlägget</a></p>'; |
137
|
1 |
|
} else { |
138
|
|
|
$canedit .= '</p>'; |
139
|
|
|
} |
140
|
1 |
|
return $canedit; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* If session contains correct id, returns string with edit-links |
146
|
|
|
* |
147
|
|
|
* @return string htmlcode |
148
|
|
|
*/ |
149
|
|
|
public function getLoginurl() |
150
|
|
|
{ |
151
|
|
|
$loginurl = $this->setUrlCreator("user/login"); |
|
|
|
|
152
|
|
|
$create = $this->setUrlCreator("comm/create"); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$htmlcomment = '<a href="' . $loginurl . '">Logga in om du vill kommentera</a></p>'; |
155
|
|
|
|
156
|
|
|
if ($this->sess && $this->sess['id']) { |
157
|
|
|
$htmlcomment = '<a href="' . $create . '/' . $this->comment->id . '">Kommentera</a>'; |
158
|
|
|
} |
159
|
|
|
return $htmlcomment; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* If loggedin allowed to edit |
165
|
|
|
* |
166
|
|
|
* @param boolean $isadmin |
167
|
|
|
* @param string $userid |
168
|
|
|
* @param string $commid |
169
|
|
|
* @param string $htmlcomment, link |
|
|
|
|
170
|
|
|
* |
171
|
|
|
* @return string htmlcode |
172
|
|
|
*/ |
173
|
|
|
public function getEdit($isadmin, $userid, $commid, $htmlcomment) |
174
|
|
|
{ |
175
|
|
|
$update = $this->setUrlCreator("comm/update"); |
|
|
|
|
176
|
|
|
if ($isadmin || $userid == $this->sess['id']) { |
177
|
|
|
$edit = '<p><a href="' . $update . '/' . $commid . '">Redigera</a> | '; |
178
|
|
|
$edit .= $htmlcomment; |
179
|
|
|
} else { |
180
|
|
|
$edit = "<p>" . $htmlcomment . "</p>"; |
181
|
|
|
} |
182
|
|
|
return $edit; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* If session contains correct id, returns string with edit-links |
188
|
|
|
* |
189
|
|
|
* @return string htmlcode |
190
|
|
|
*/ |
191
|
|
|
public function getDelete($isadmin, $userid, $del, $commid) |
192
|
|
|
{ |
193
|
|
|
$delete = ""; |
194
|
|
|
if ($isadmin || $userid == $this->sess['id']) { |
195
|
|
|
$delete = ' | <a href="' . $del . '/' . $commid . '">Ta bort inlägget</a></p>'; |
196
|
|
|
} |
197
|
|
|
return $delete; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns html for each item |
203
|
|
|
* |
204
|
|
|
* @param object $item |
205
|
|
|
* @param string $can |
206
|
|
|
* |
207
|
|
|
* @return string htmlcod |
208
|
|
|
*/ |
209
|
1 |
|
public function getValHtml(Comm $item, $can) |
210
|
|
|
{ |
211
|
1 |
|
$gravatar = $this->getGravatar($item->email); |
212
|
1 |
|
$updated = isset($item->updated) ? '| Uppdaterades: ' . $item->updated : ""; |
213
|
|
|
|
214
|
1 |
|
$text = $this->getDecode($item); |
215
|
1 |
|
$text .= '<p><span class="smaller">' . $item->email . '</span> ' . $gravatar . '<br />'; |
216
|
1 |
|
$text .= 'Skrevs: ' . $item->created . ' ' . $updated; |
217
|
1 |
|
$text .= $can; |
218
|
1 |
|
$text .= '<hr />'; |
219
|
1 |
|
return $text; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Returns all text for the view |
225
|
|
|
* |
226
|
|
|
* @return string htmlcode |
227
|
|
|
*/ |
228
|
|
|
public function getHTML() |
229
|
|
|
{ |
230
|
|
|
$isadmin = $this->sess['isadmin'] == 1 ? true : false; |
231
|
|
|
|
232
|
|
|
$del = $this->setUrlCreator("comm/delete"); |
|
|
|
|
233
|
|
|
$commpage = $this->setUrlCreator("comm"); |
|
|
|
|
234
|
|
|
|
235
|
|
|
$htmlcomment = $this->getLoginurl(); |
236
|
|
|
$edit = $this->getEdit($isadmin, $this->comment->userid, $this->comment->id, $htmlcomment); |
237
|
|
|
$delete = $this->getDelete($isadmin, $this->comment->userid, $del, $this->comment->id); |
238
|
|
|
|
239
|
|
|
$text = '<h3>Kommentarer</h3>'; |
240
|
|
|
|
241
|
|
|
if ($this->comments) { |
242
|
|
|
foreach ($this->comments as $value) { |
243
|
|
|
$can = $this->getCanEdit($value, $isadmin, $del); |
244
|
|
|
$text .= $this->getValHtml($value, $can); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$html = '<div class="col-sm-12 col-xs-12"><div class="col-lg-4 col-sm-7 col-xs-7 abookimg">'; |
249
|
|
|
$html .= $this->getDecode($this->comment, true); |
|
|
|
|
250
|
|
|
$html .= '<br />' . $edit; |
251
|
|
|
$html .= $delete; |
252
|
|
|
$html .= '<p><a href="' . $commpage . '">Till Alla Inläggen</a></p>'; |
253
|
|
|
$html .= '</div><div class="col-sm-5 col-xs-5">'; |
254
|
|
|
$html .= $text; |
255
|
|
|
$html .= '</div></div>'; |
256
|
|
|
return $html; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: