|
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 ShowAllService |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array $comments, all comments. |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $comments; |
|
17
|
|
|
protected $sess; |
|
18
|
|
|
protected $users; |
|
19
|
|
|
protected $user; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor injects with DI container and the id to update. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(DIInterface $di) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->di = $di; |
|
|
|
|
|
|
29
|
|
|
$this->comments = $this->getAll(); |
|
30
|
|
|
$session = $this->di->get("session"); |
|
31
|
|
|
$this->sess = $session->get("user"); |
|
32
|
|
|
$addsess = isset($this->sess) ? $this->sess : null; |
|
33
|
|
|
$this->sess = $addsess; |
|
34
|
|
|
$userController = $this->di->get("userController"); |
|
35
|
|
|
$this->users = $userController->getAllUsers(); |
|
36
|
|
|
$this->user = $userController->getOne($this->sess['id']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get details on all comments. |
|
41
|
|
|
* |
|
42
|
|
|
* @return Comm |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getAll() |
|
45
|
|
|
{ |
|
46
|
|
|
$comm = new Comm(); |
|
47
|
|
|
$comm->setDb($this->di->get("db")); |
|
48
|
|
|
return $comm->findAll(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets the callable to use for creating routes. |
|
54
|
|
|
* |
|
55
|
|
|
* @param callable $urlCreate to create framework urls. |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setUrlCreator($route) |
|
60
|
|
|
{ |
|
61
|
|
|
$url = $this->di->get("url"); |
|
62
|
|
|
return call_user_func([$url, "create"], $route); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns link for gravatar img |
|
68
|
|
|
* |
|
69
|
|
|
* @param object $item |
|
70
|
|
|
* |
|
71
|
|
|
* @return string htmlcode |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getGravatar($item) |
|
74
|
|
|
{ |
|
75
|
|
|
$comm = new Comm(); |
|
76
|
|
|
$gravatar = $comm->getGravatar($item); |
|
77
|
|
|
return '<img src="' . $gravatar . '" alt=""/>'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns text if updated |
|
83
|
|
|
* |
|
84
|
|
|
* @param object $item |
|
85
|
|
|
* @return string htmlcode |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getExtra($item) |
|
88
|
|
|
{ |
|
89
|
|
|
$extra = ""; |
|
90
|
|
|
if ($item) { |
|
91
|
|
|
$extra .= '<br />Uppdaterades: ' . $item; |
|
92
|
|
|
} |
|
93
|
|
|
return $extra; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns correct loginlink |
|
99
|
|
|
* |
|
100
|
|
|
* @param boolean $isadmin |
|
101
|
|
|
* @param string $create |
|
102
|
|
|
* @param string $del |
|
103
|
|
|
* |
|
104
|
|
|
* @return string htmlcode |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getLoginLink($isadmin, $create, $del) |
|
107
|
|
|
{ |
|
108
|
|
|
$loggedin = '<a href="user/login">Logga in om du vill kommentera</a>'; |
|
109
|
|
|
if ($this->sess['id']) { |
|
110
|
|
|
$loggedin = ' <a href="' . $create .'">Skriv ett inlägg</a>'; |
|
111
|
|
|
if ($isadmin === true) { |
|
112
|
|
|
$loggedin .= ' | <a href="' . $del . '">Ta bort ett inlägg</a>'; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
return $loggedin; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns html for each item |
|
121
|
|
|
* |
|
122
|
|
|
* @param object $item |
|
123
|
|
|
* @param boolean $isadmin |
|
124
|
|
|
* @param string $viewone |
|
125
|
|
|
* |
|
126
|
|
|
* @return string htmlcode |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getValHtml($item, $isadmin, $viewone) |
|
129
|
|
|
{ |
|
130
|
|
|
$gravatar = $this->getGravatar($item->email); |
|
131
|
|
|
$extra = $this->getExtra($item->updated); |
|
132
|
|
|
if ($isadmin === true) { |
|
133
|
|
|
$showid = '(' . $item->id . '): '; |
|
134
|
|
|
} |
|
135
|
|
|
$html = '<h4><a href="' . $viewone . '/' . $item->id . '">'; |
|
136
|
|
|
$html .= $showid . ' ' . $item->title . '</a></h4><p>'; |
|
|
|
|
|
|
137
|
|
|
$html .= $item->created . ' ' . $item->email . ' ' . $gravatar . ' ' . $extra . '</p><hr />'; |
|
138
|
|
|
return $html; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns all text for the view |
|
144
|
|
|
* |
|
145
|
|
|
* @return string htmlcode |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getHTML() |
|
148
|
|
|
{ |
|
149
|
|
|
$loggedin = ""; |
|
|
|
|
|
|
150
|
|
|
$showid = ""; |
|
|
|
|
|
|
151
|
|
|
$gravatar = ""; |
|
|
|
|
|
|
152
|
|
|
$extra = ""; |
|
|
|
|
|
|
153
|
|
|
$html = ""; |
|
154
|
|
|
|
|
155
|
|
|
$isadmin = $this->sess['isadmin'] === 1 ? true : false; |
|
156
|
|
|
|
|
157
|
|
|
$create = $this->setUrlCreator("comm/create"); |
|
|
|
|
|
|
158
|
|
|
$del = $this->setUrlCreator("comm/admindelete"); |
|
|
|
|
|
|
159
|
|
|
$viewone = $this->setUrlCreator("comm/view-one"); |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
$loggedin = $this->getLoginLink($isadmin, $create, $del); |
|
162
|
|
|
|
|
163
|
|
|
$html .= '<div class="col-sm-12 col-xs-12"> |
|
164
|
|
|
<div class="col-lg-6 col-sm-7 col-xs-7"> |
|
165
|
|
|
<h3>Gruppinlägg <span class="small">' . $loggedin . '</span></h3> |
|
166
|
|
|
<hr />'; |
|
167
|
|
|
|
|
168
|
|
|
foreach ($this->comments as $value) { |
|
169
|
|
|
if ((int)$value->parentid > 0) { |
|
170
|
|
|
continue; |
|
171
|
|
|
} |
|
172
|
|
|
$html .= $this->getValHtml($value, $isadmin, $viewone); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$html .= '</div></div>'; |
|
176
|
|
|
return $html; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
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: