1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Core\Container; |
6
|
|
|
use Core\Model; |
7
|
|
|
|
8
|
|
|
class TagModel extends Model |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $tagAssoTbl; |
12
|
|
|
private $tagTbl; |
13
|
|
|
|
14
|
|
|
public function __construct(Container $container) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($container); |
17
|
|
|
$this->tagAssoTbl = $this->getTablePrefix("posts_has_tags"); |
18
|
|
|
$this->tagTbl = $this->getTablePrefix("tags"); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Counts the number of tags in DB |
23
|
|
|
* @return int |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public function countTags(): int |
27
|
|
|
{ |
28
|
|
|
$sql = "SELECT COUNT(*) FROM $this->tagTbl"; |
29
|
|
|
$this->query($sql); |
30
|
|
|
$this->execute(); |
31
|
|
|
return $this->stmt->fetchColumn(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getTagList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$sql = " |
37
|
|
|
SELECT * FROM $this->tagTbl |
38
|
|
|
LIMIT :limit OFFSET :offset |
39
|
|
|
"; |
40
|
|
|
$this->query($sql); |
41
|
|
|
$this->bind(":limit", $limit); |
42
|
|
|
$this->bind(":offset", $offset); |
43
|
|
|
$this->execute(); |
44
|
|
|
return $this->fetchAll(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* check if post has a specific tag |
49
|
|
|
* @param int $postId |
50
|
|
|
* @param int $tagId |
51
|
|
|
* @return bool |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
|
|
private function postHasTag(int $postId, int $tagId): bool |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
$sql = "SELECT * FROM $this->tagAssoTbl WHERE post_idposts = :postId AND tag_idtags = :tagId"; |
58
|
|
|
$this->query($sql); |
59
|
|
|
$this->bind(':postId', $postId); |
60
|
|
|
$this->bind(':tagId', $tagId); |
61
|
|
|
$this->execute(); |
62
|
|
|
|
63
|
|
|
return $this->stmt->rowCount() > 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $tagName the tag to search for |
68
|
|
|
* @return int |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
private function getTagId(string $tagName): int |
72
|
|
|
{ |
73
|
|
|
$sql = "SELECT idtags FROM $this->tagTbl WHERE tag_name = :tagName"; |
74
|
|
|
$this->query($sql); |
75
|
|
|
$this->bind(':tagName', $tagName); |
76
|
|
|
$this->execute(); |
77
|
|
|
//if no rows, return zero |
78
|
|
|
if (!$this->stmt->rowCount() > 0) { |
79
|
|
|
return 0; |
80
|
|
|
} |
81
|
|
|
return $this->stmt->fetchColumn(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a new tag and return it's ID |
86
|
|
|
* @param string $tag tag to insert |
87
|
|
|
* @return int the inserted tag ID |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
private function createNewTag(string $tag): int |
91
|
|
|
{ |
92
|
|
|
$sql = "INSERT INTO $this->tagTbl (tag_name) VALUES (:tag)"; |
93
|
|
|
$this->query($sql); |
94
|
|
|
$this->bind(":tag", $tag); |
95
|
|
|
$this->execute(); |
96
|
|
|
return (int)$this->dbh->lastInsertId(); |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* delete a tag from all posts |
102
|
|
|
* @param int $tagId |
103
|
|
|
* @return bool |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
|
|
private function deleteTagOnAllPosts(int $tagId) |
107
|
|
|
{ |
108
|
|
|
$sql = " |
109
|
|
|
DELETE |
110
|
|
|
FROM $this->tagAssoTbl |
111
|
|
|
WHERE tag_idtags = :tagId |
112
|
|
|
"; |
113
|
|
|
$this->query($sql); |
114
|
|
|
$this->bind(":tagId", $tagId); |
115
|
|
|
return $this->execute(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array the list of all the tags |
120
|
|
|
* @throws \ReflectionException |
121
|
|
|
*/ |
122
|
|
|
public function getTags(): array |
123
|
|
|
{ |
124
|
|
|
return $this->getResultSet('tags'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Add a tag to the post |
129
|
|
|
* @param int $postId the post id |
130
|
|
|
* @param int $tagId the tag id |
131
|
|
|
* @throws \Exception |
132
|
|
|
*/ |
133
|
|
|
public function addTagToPost(int $postId, int $tagId) |
134
|
|
|
{ |
135
|
|
|
//if the post already has the tag, do nothing |
136
|
|
|
if ($this->postHasTag($postId, $tagId)) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$sql = "INSERT INTO $this->tagAssoTbl (post_idposts, tag_idtags) VALUES (:postId, :tagId)"; |
141
|
|
|
$this->query($sql); |
142
|
|
|
$this->bind(':postId', $postId); |
143
|
|
|
$this->bind(':tagId', $tagId); |
144
|
|
|
$this->execute(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Add a new tag to a post |
149
|
|
|
* @param int $postId |
150
|
|
|
* @param string $tag |
151
|
|
|
* @throws \Exception |
152
|
|
|
*/ |
153
|
|
|
public function addNewTagToPost(int $postId, string $tag) |
154
|
|
|
{ |
155
|
|
|
//check if tag doesn't already exist |
156
|
|
|
$tagId = $this->getTagId($tag); |
157
|
|
|
if ($tagId === 0) { |
158
|
|
|
$tagId = $this->createNewTag($tag); |
159
|
|
|
} |
160
|
|
|
$this->addTagToPost($postId, $tagId); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* removes a tag from the post |
165
|
|
|
* @param int $postId the post id |
166
|
|
|
* @param int $tagId the tag id |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
public function removeTagFromPost(int $postId, int $tagId) |
170
|
|
|
{ |
171
|
|
|
//if the tag isn't present, do nothing |
172
|
|
|
if (!$this->postHasTag($postId, $tagId)) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$sql = "DELETE FROM $this->tagAssoTbl WHERE post_idposts = :postId AND tag_idtags = :tagId"; |
177
|
|
|
$this->query($sql); |
178
|
|
|
$this->bind(':postId', $postId); |
179
|
|
|
$this->bind(':tagId', $tagId); |
180
|
|
|
$this->execute(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* get all tags associated to a post |
185
|
|
|
* @param int $postId the post ID |
186
|
|
|
* @return array the associated tags |
187
|
|
|
* @throws \Exception |
188
|
|
|
*/ |
189
|
|
|
public function getTagsOnPost(int $postId) |
190
|
|
|
{ |
191
|
|
|
$sql = "SELECT tag_name, idtags FROM $this->tagTbl |
192
|
|
|
INNER JOIN $this->tagAssoTbl ON $this->tagTbl.idtags = $this->tagAssoTbl.tag_idtags |
193
|
|
|
WHERE post_idposts = :postId |
194
|
|
|
"; |
195
|
|
|
$this->query($sql); |
196
|
|
|
$this->bind(":postId", $postId); |
197
|
|
|
$this->execute(); |
198
|
|
|
|
199
|
|
|
return $this->fetchAll(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Remove all tags from a post |
205
|
|
|
* @param int $postId the post ID |
206
|
|
|
* @throws \Exception |
207
|
|
|
*/ |
208
|
|
|
public function removeTagsOnPost(int $postId) |
209
|
|
|
{ |
210
|
|
|
$sql = " |
211
|
|
|
DELETE FROM $this->tagAssoTbl |
212
|
|
|
WHERE post_idposts = :postId |
213
|
|
|
;"; |
214
|
|
|
$this->query($sql); |
215
|
|
|
$this->bind(":postId", $postId); |
216
|
|
|
$this->execute(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* return all the tag details |
221
|
|
|
* @param int $tagId |
222
|
|
|
* @return array |
223
|
|
|
* @throws \ReflectionException |
224
|
|
|
*/ |
225
|
|
|
public function getTagDetails(int $tagId) |
226
|
|
|
{ |
227
|
|
|
return $this->getRowById($tagId); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* create a new tag |
232
|
|
|
* @param string $tag |
233
|
|
|
* @return bool |
234
|
|
|
* @throws \Exception |
235
|
|
|
*/ |
236
|
|
|
public function new(string $tag) |
237
|
|
|
{ |
238
|
|
|
$tagId = $this->createNewTag($tag); |
239
|
|
|
return is_int($tagId); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Update an existing tag |
244
|
|
|
* @param int $tagId |
245
|
|
|
* @param string $tagName |
246
|
|
|
* @return bool |
247
|
|
|
* @throws \Exception |
248
|
|
|
*/ |
249
|
|
|
public function update(int $tagId, string $tagName) |
250
|
|
|
{ |
251
|
|
|
$sql = " |
252
|
|
|
UPDATE $this->tagTbl |
253
|
|
|
SET |
254
|
|
|
tag_name = :tagName |
255
|
|
|
WHERE |
256
|
|
|
idtags = :tagId |
257
|
|
|
"; |
258
|
|
|
$this->query($sql); |
259
|
|
|
$this->bind(":tagName", $tagName); |
260
|
|
|
$this->bind(":tagId", $tagId); |
261
|
|
|
return $this->execute(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Delete a tag |
266
|
|
|
* @param int $tagId |
267
|
|
|
* @return bool |
268
|
|
|
* @throws \Exception |
269
|
|
|
*/ |
270
|
|
|
public function delete(int $tagId) |
271
|
|
|
{ |
272
|
|
|
$this->deleteTagOnAllPosts($tagId); |
273
|
|
|
$sql = " |
274
|
|
|
DELETE |
275
|
|
|
FROM $this->tagTbl |
276
|
|
|
WHERE idtags = :tagId |
277
|
|
|
"; |
278
|
|
|
$this->query($sql); |
279
|
|
|
$this->bind(":tagId", $tagId); |
280
|
|
|
return $this->execute(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* get tag name from ID |
285
|
|
|
* @param int $tagId |
286
|
|
|
* @return mixed |
287
|
|
|
* @throws \Exception |
288
|
|
|
*/ |
289
|
|
|
public function getNameFromId(int $tagId) |
290
|
|
|
{ |
291
|
|
|
$sql = "SELECT tag_name from $this->tagTbl WHERE idtags = :tagId"; |
292
|
|
|
$this->query($sql); |
293
|
|
|
$this->bind(":tagId", $tagId); |
294
|
|
|
$this->execute(); |
295
|
|
|
return $this->stmt->fetchColumn(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths