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
|
|
|
* check if post has a specific tag |
23
|
|
|
* @param int $postId |
24
|
|
|
* @param int $tagId |
25
|
|
|
* @return bool |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
|
|
private function postHasTag(int $postId, int $tagId): bool |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
$sql = "SELECT * FROM $this->tagAssoTbl WHERE post_idposts = :postId AND tag_idtags = :tagId"; |
32
|
|
|
$this->query($sql); |
33
|
|
|
$this->bind(':postId', $postId); |
34
|
|
|
$this->bind(':tagId', $tagId); |
35
|
|
|
$this->execute(); |
36
|
|
|
|
37
|
|
|
return $this->stmt->rowCount() > 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $tagName the tag to search for |
42
|
|
|
* @return int |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
private function getTagId(string $tagName): int |
46
|
|
|
{ |
47
|
|
|
$sql = "SELECT idtags FROM $this->tagTbl WHERE tag_name = :tagName"; |
48
|
|
|
$this->query($sql); |
49
|
|
|
$this->bind(':tagName', $tagName); |
50
|
|
|
$this->execute(); |
51
|
|
|
//if no rows, return zero |
52
|
|
|
if (!$this->stmt->rowCount() > 0) { |
53
|
|
|
return 0; |
54
|
|
|
} |
55
|
|
|
return $this->stmt->fetchColumn(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create a new tag and return it's ID |
60
|
|
|
* @param string $tag tag to insert |
61
|
|
|
* @return int the inserted tag ID |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
private function createNewTag(string $tag): int |
65
|
|
|
{ |
66
|
|
|
$sql = "INSERT INTO $this->tagTbl (tag_name) VALUES (:tag)"; |
67
|
|
|
$this->query($sql); |
68
|
|
|
$this->bind(":tag", $tag); |
69
|
|
|
$this->execute(); |
70
|
|
|
return (int)$this->dbh->lastInsertId(); |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array the list of all the tags |
76
|
|
|
* @throws \ReflectionException |
77
|
|
|
*/ |
78
|
|
|
public function getTags(): array |
79
|
|
|
{ |
80
|
|
|
return $this->getResultSet('tags'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add a tag to the post |
85
|
|
|
* @param int $postId the post id |
86
|
|
|
* @param int $tagId the tag id |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function addTagToPost(int $postId, int $tagId) |
90
|
|
|
{ |
91
|
|
|
//if the post already has the tag, do nothing |
92
|
|
|
if ($this->postHasTag($postId, $tagId)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$sql = "INSERT INTO $this->tagAssoTbl (post_idposts, tag_idtags) VALUES (:postId, :tagId)"; |
97
|
|
|
$this->query($sql); |
98
|
|
|
$this->bind(':postId', $postId); |
99
|
|
|
$this->bind(':tagId', $tagId); |
100
|
|
|
$this->execute(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add a new tag to a post |
105
|
|
|
* @param int $postId |
106
|
|
|
* @param string $tag |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function addNewTagToPost(int $postId, string $tag) |
110
|
|
|
{ |
111
|
|
|
//check if tag doesn't already exist |
112
|
|
|
$tagId = $this->getTagId($tag); |
113
|
|
|
if ($tagId === 0) { |
114
|
|
|
$tagId = $this->createNewTag($tag); |
115
|
|
|
} |
116
|
|
|
$this->addTagToPost($postId, $tagId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* removes a tag from the post |
121
|
|
|
* @param int $postId the post id |
122
|
|
|
* @param int $tagId the tag id |
123
|
|
|
* @throws \Exception |
124
|
|
|
*/ |
125
|
|
|
public function removeTagFromPost(int $postId, int $tagId) |
126
|
|
|
{ |
127
|
|
|
//if the tag isn't present, do nothing |
128
|
|
|
if (!$this->postHasTag($postId, $tagId)) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$sql = "DELETE FROM $this->tagAssoTbl WHERE post_idposts = :postId AND tag_idtags = :tagId"; |
133
|
|
|
$this->query($sql); |
134
|
|
|
$this->bind(':postId', $postId); |
135
|
|
|
$this->bind(':tagId', $tagId); |
136
|
|
|
$this->execute(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* get all tags associated to a post |
141
|
|
|
* @param int $postId the post ID |
142
|
|
|
* @return array the associated tags |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
|
|
public function getTagsOnPost(int $postId) |
146
|
|
|
{ |
147
|
|
|
$sql = "SELECT tag_name, idtags FROM $this->tagTbl |
148
|
|
|
INNER JOIN $this->tagAssoTbl ON $this->tagTbl.idtags = $this->tagAssoTbl.tag_idtags |
149
|
|
|
WHERE post_idposts = :postId |
150
|
|
|
"; |
151
|
|
|
$this->query($sql); |
152
|
|
|
$this->bind(":postId", $postId); |
153
|
|
|
$this->execute(); |
154
|
|
|
|
155
|
|
|
return $this->fetchAll(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Remove all tags from a post |
161
|
|
|
* @param int $postId the post ID |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
public function removeTagsOnPost(int $postId) |
165
|
|
|
{ |
166
|
|
|
$sql = " |
167
|
|
|
DELETE FROM $this->tagAssoTbl |
168
|
|
|
WHERE post_idposts = :postId |
169
|
|
|
;"; |
170
|
|
|
$this->query($sql); |
171
|
|
|
$this->bind(":postId", $postId); |
172
|
|
|
$this->execute(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getTagDetails(int $tagId) |
176
|
|
|
{ |
177
|
|
|
return $this->getRowById($tagId); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |