@@ -13,158 +13,158 @@ discard block |
||
13 | 13 | class Api |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Constant for photo type |
|
18 | - */ |
|
19 | - const string TYPE_PHOTO = "PHOTO"; |
|
20 | - |
|
21 | - /** |
|
22 | - * Constant for video type |
|
23 | - */ |
|
24 | - const string TYPE_VIDEO = "VIDEO"; |
|
25 | - |
|
26 | - /** |
|
27 | - * Type map, mapping the ObjectID prefixes to our constants |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - private array $typeMap = [ |
|
16 | + /** |
|
17 | + * Constant for photo type |
|
18 | + */ |
|
19 | + const string TYPE_PHOTO = "PHOTO"; |
|
20 | + |
|
21 | + /** |
|
22 | + * Constant for video type |
|
23 | + */ |
|
24 | + const string TYPE_VIDEO = "VIDEO"; |
|
25 | + |
|
26 | + /** |
|
27 | + * Type map, mapping the ObjectID prefixes to our constants |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + private array $typeMap = [ |
|
32 | 32 | self::TYPE_PHOTO => 'thumb', |
33 | 33 | self::TYPE_VIDEO => 'video-' |
34 | - ]; |
|
35 | - |
|
36 | - /** |
|
37 | - * database |
|
38 | - * |
|
39 | - * @var \PDO |
|
40 | - */ |
|
41 | - private \PDO $db; |
|
42 | - |
|
43 | - /** |
|
44 | - * Local cache array |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - private ?array $tagMap = null; |
|
49 | - |
|
50 | - /** |
|
51 | - * Api constructor. |
|
52 | - * |
|
53 | - * @param string $dbPath Path to sqlite file |
|
54 | - * |
|
55 | - * @throws \Exception |
|
56 | - */ |
|
57 | - public function __construct($dbPath) |
|
58 | - { |
|
34 | + ]; |
|
35 | + |
|
36 | + /** |
|
37 | + * database |
|
38 | + * |
|
39 | + * @var \PDO |
|
40 | + */ |
|
41 | + private \PDO $db; |
|
42 | + |
|
43 | + /** |
|
44 | + * Local cache array |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + private ?array $tagMap = null; |
|
49 | + |
|
50 | + /** |
|
51 | + * Api constructor. |
|
52 | + * |
|
53 | + * @param string $dbPath Path to sqlite file |
|
54 | + * |
|
55 | + * @throws \Exception |
|
56 | + */ |
|
57 | + public function __construct($dbPath) |
|
58 | + { |
|
59 | 59 | if (!file_exists($dbPath) || !is_readable($dbPath)) { |
60 | - throw new \Exception("Database with path '" . $dbPath ."' doesn't exist or isn't readable!"); |
|
60 | + throw new \Exception("Database with path '" . $dbPath ."' doesn't exist or isn't readable!"); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $this->db = new \PDO('sqlite:' . $dbPath); |
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * returns the PDO instance |
|
68 | - * |
|
69 | - * @return \PDO |
|
70 | - */ |
|
71 | - public function getDb() : \PDO |
|
72 | - { |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * returns the PDO instance |
|
68 | + * |
|
69 | + * @return \PDO |
|
70 | + */ |
|
71 | + public function getDb() : \PDO |
|
72 | + { |
|
73 | 73 | return $this->db; |
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns an array of everything in the database, videos and photos mixed |
|
78 | - * |
|
79 | - * @return array array of simple array structures containing the data from the database |
|
80 | - */ |
|
81 | - public function getAll() |
|
82 | - { |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns an array of everything in the database, videos and photos mixed |
|
78 | + * |
|
79 | + * @return array array of simple array structures containing the data from the database |
|
80 | + */ |
|
81 | + public function getAll() |
|
82 | + { |
|
83 | 83 | return $this->searchAllWithCondition(); |
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns an array of all photos in the database |
|
88 | - * |
|
89 | - * @return array array of simple array structures containing the data from the database |
|
90 | - */ |
|
91 | - public function getAllPhotos() |
|
92 | - { |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns an array of all photos in the database |
|
88 | + * |
|
89 | + * @return array array of simple array structures containing the data from the database |
|
90 | + */ |
|
91 | + public function getAllPhotos() |
|
92 | + { |
|
93 | 93 | return $this->getItem(self::TYPE_PHOTO); |
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Returns an array of all videos in the database |
|
98 | - * |
|
99 | - * @return array array of simple array structures containing the data from the database |
|
100 | - */ |
|
101 | - public function getAllVideos() |
|
102 | - { |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Returns an array of all videos in the database |
|
98 | + * |
|
99 | + * @return array array of simple array structures containing the data from the database |
|
100 | + */ |
|
101 | + public function getAllVideos() |
|
102 | + { |
|
103 | 103 | return $this->getItem(self::TYPE_VIDEO); |
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Returns a given photo by it's ID in PhotoTable |
|
108 | - * |
|
109 | - * @param string $id ID |
|
110 | - * |
|
111 | - * @return array |
|
112 | - */ |
|
113 | - public function getPhotoById($id) |
|
114 | - { |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Returns a given photo by it's ID in PhotoTable |
|
108 | + * |
|
109 | + * @param string $id ID |
|
110 | + * |
|
111 | + * @return array |
|
112 | + */ |
|
113 | + public function getPhotoById($id) |
|
114 | + { |
|
115 | 115 | return $this->getItem(self::TYPE_PHOTO, $id); |
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Returns a given video by it's ID in VideoTable |
|
120 | - * |
|
121 | - * @param string $id ID |
|
122 | - * |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - public function getVideoById($id) |
|
126 | - { |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Returns a given video by it's ID in VideoTable |
|
120 | + * |
|
121 | + * @param string $id ID |
|
122 | + * |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + public function getVideoById($id) |
|
126 | + { |
|
127 | 127 | return $this->getItem(self::TYPE_VIDEO, $id); |
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Returns all tags associated with a given Object ID. Make sure to get an Object ID first by |
|
132 | - * calling getObjectIdByNumericId(). |
|
133 | - * |
|
134 | - * @param string $objectId Object ID. |
|
135 | - * |
|
136 | - * @return bool Either an array of tags or false if the object doesn't exist |
|
137 | - */ |
|
138 | - public function getTagsByObjectId($objectId) |
|
139 | - { |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Returns all tags associated with a given Object ID. Make sure to get an Object ID first by |
|
132 | + * calling getObjectIdByNumericId(). |
|
133 | + * |
|
134 | + * @param string $objectId Object ID. |
|
135 | + * |
|
136 | + * @return bool Either an array of tags or false if the object doesn't exist |
|
137 | + */ |
|
138 | + public function getTagsByObjectId($objectId) |
|
139 | + { |
|
140 | 140 | if (is_null($this->tagMap)) { |
141 | - $this->tagMap = $this->getItemTagMap(); |
|
141 | + $this->tagMap = $this->getItemTagMap(); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | if (isset($this->tagMap[$objectId])) { |
145 | - $ret = $this->tagMap[$objectId]; |
|
145 | + $ret = $this->tagMap[$objectId]; |
|
146 | 146 | } else { |
147 | - return false; |
|
147 | + return false; |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | return $ret; |
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sets tags on a given item using an Oject ID. Make sure to get an Object ID first by |
|
155 | - * calling getObjectIdByNumericId(). This sets the tags to the ones you pass. |
|
156 | - * |
|
157 | - * @param string $objectId Object ID |
|
158 | - * @param array $tags An array of strings containing your tags |
|
159 | - * |
|
160 | - * @return bool true if all is good, false otherwise |
|
161 | - */ |
|
162 | - public function setItemTags($objectId, array $tags) |
|
163 | - { |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sets tags on a given item using an Oject ID. Make sure to get an Object ID first by |
|
155 | + * calling getObjectIdByNumericId(). This sets the tags to the ones you pass. |
|
156 | + * |
|
157 | + * @param string $objectId Object ID |
|
158 | + * @param array $tags An array of strings containing your tags |
|
159 | + * |
|
160 | + * @return bool true if all is good, false otherwise |
|
161 | + */ |
|
162 | + public function setItemTags($objectId, array $tags) |
|
163 | + { |
|
164 | 164 | $currentTags = $this->getTagsByObjectId($objectId); |
165 | 165 | |
166 | 166 | if ($currentTags === false) { |
167 | - return false; |
|
167 | + return false; |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | // get diff - those need to be added |
@@ -174,169 +174,169 @@ discard block |
||
174 | 174 | $tagDiffRemove = array_diff($currentTags, $tags); |
175 | 175 | |
176 | 176 | foreach ($tagDiffAdd as $tagName) { |
177 | - $this->manipulateItemOnTag($tagName, 'add', $objectId); |
|
177 | + $this->manipulateItemOnTag($tagName, 'add', $objectId); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | foreach ($tagDiffRemove as $tagName) { |
181 | - $this->manipulateItemOnTag($tagName, 'remove', $objectId); |
|
181 | + $this->manipulateItemOnTag($tagName, 'remove', $objectId); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | $this->tagMap = null; |
185 | 185 | |
186 | 186 | return true; |
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * This is a private function for doing common stuff on the tag. So not from item perspective. |
|
191 | - * |
|
192 | - * @param string $tagName the tag |
|
193 | - * @param string $whatToDo What you want to do (either 'add' or 'remove') |
|
194 | - * @param string $objectId Object ID |
|
195 | - * |
|
196 | - * @throws Exception |
|
197 | - * |
|
198 | - * @return boolean true if all seems ok, false otherwise |
|
199 | - */ |
|
200 | - private function manipulateItemOnTag($tagName, $whatToDo, $objectId) |
|
201 | - { |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * This is a private function for doing common stuff on the tag. So not from item perspective. |
|
191 | + * |
|
192 | + * @param string $tagName the tag |
|
193 | + * @param string $whatToDo What you want to do (either 'add' or 'remove') |
|
194 | + * @param string $objectId Object ID |
|
195 | + * |
|
196 | + * @throws Exception |
|
197 | + * |
|
198 | + * @return boolean true if all seems ok, false otherwise |
|
199 | + */ |
|
200 | + private function manipulateItemOnTag($tagName, $whatToDo, $objectId) |
|
201 | + { |
|
202 | 202 | $thisTagData = $this->getTag($tagName, true); |
203 | 203 | $ret = false; |
204 | 204 | |
205 | 205 | if (is_array($thisTagData)) { |
206 | - $thisTagItems = explode(',', $thisTagData['photo_id_list']); |
|
207 | - $setKey = array_search($objectId, $thisTagItems); |
|
206 | + $thisTagItems = explode(',', $thisTagData['photo_id_list']); |
|
207 | + $setKey = array_search($objectId, $thisTagItems); |
|
208 | 208 | |
209 | - // what to do? |
|
210 | - if ($whatToDo == 'add') { |
|
209 | + // what to do? |
|
210 | + if ($whatToDo == 'add') { |
|
211 | 211 | if ($setKey === false) { |
212 | - $thisTagItems[] = $objectId; |
|
212 | + $thisTagItems[] = $objectId; |
|
213 | 213 | } |
214 | - } elseif ($whatToDo == 'remove') { |
|
214 | + } elseif ($whatToDo == 'remove') { |
|
215 | 215 | if ($setKey !== false) { |
216 | - unset($thisTagItems[$setKey]); |
|
216 | + unset($thisTagItems[$setKey]); |
|
217 | + } |
|
217 | 218 | } |
218 | - } |
|
219 | 219 | |
220 | - $ret = $this->setTagItems($tagName, $thisTagItems); |
|
220 | + $ret = $this->setTagItems($tagName, $thisTagItems); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | return $ret; |
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Sets items on a tag |
|
228 | - * |
|
229 | - * @param string $tag the tag |
|
230 | - * @param array $items items |
|
231 | - * |
|
232 | - * @return bool true if all ok, false otherwise |
|
233 | - */ |
|
234 | - private function setTagItems($tag, array $items) |
|
235 | - { |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Sets items on a tag |
|
228 | + * |
|
229 | + * @param string $tag the tag |
|
230 | + * @param array $items items |
|
231 | + * |
|
232 | + * @return bool true if all ok, false otherwise |
|
233 | + */ |
|
234 | + private function setTagItems($tag, array $items) |
|
235 | + { |
|
236 | 236 | $thisTagId = $this->getTagId($tag); |
237 | 237 | $ret = false; |
238 | 238 | |
239 | 239 | if ($thisTagId !== false) { |
240 | - $q = "UPDATE TagTable SET `photo_id_list` = ? WHERE id = ?"; |
|
240 | + $q = "UPDATE TagTable SET `photo_id_list` = ? WHERE id = ?"; |
|
241 | 241 | |
242 | - $res = $this->db->prepare($q); |
|
242 | + $res = $this->db->prepare($q); |
|
243 | 243 | |
244 | - $saveString = implode(',', $items).','; |
|
244 | + $saveString = implode(',', $items).','; |
|
245 | 245 | |
246 | - // ensure we don't save ",," somewhere.. |
|
247 | - $saveString = str_replace(',,', ',', $saveString); |
|
246 | + // ensure we don't save ",," somewhere.. |
|
247 | + $saveString = str_replace(',,', ',', $saveString); |
|
248 | 248 | |
249 | - $res->execute([ |
|
249 | + $res->execute([ |
|
250 | 250 | $saveString, |
251 | 251 | $thisTagId |
252 | - ]); |
|
252 | + ]); |
|
253 | 253 | |
254 | - $ret = true; |
|
254 | + $ret = true; |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | return $ret; |
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Sets the rating on a tag using the Object ID. Make sure to get an Object ID first by |
|
262 | - * calling getObjectIdByNumericId(). |
|
263 | - * |
|
264 | - * @param string $objectId Object ID |
|
265 | - * @param int $rating Rating (1-5) |
|
266 | - * |
|
267 | - * @return bool true if all ok, false otherwise |
|
268 | - * @throws \Exception |
|
269 | - */ |
|
270 | - public function setItemRating($objectId, $rating) |
|
271 | - { |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Sets the rating on a tag using the Object ID. Make sure to get an Object ID first by |
|
262 | + * calling getObjectIdByNumericId(). |
|
263 | + * |
|
264 | + * @param string $objectId Object ID |
|
265 | + * @param int $rating Rating (1-5) |
|
266 | + * |
|
267 | + * @return bool true if all ok, false otherwise |
|
268 | + * @throws \Exception |
|
269 | + */ |
|
270 | + public function setItemRating($objectId, $rating) |
|
271 | + { |
|
272 | 272 | if (!is_numeric($rating)) { |
273 | - throw new \Exception(sprintf("Rating must be numeric, '%s' given", $rating)); |
|
273 | + throw new \Exception(sprintf("Rating must be numeric, '%s' given", $rating)); |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | $thisTable = null; |
277 | 277 | if ($this->getTypeByObjectId($objectId) == self::TYPE_PHOTO) { |
278 | - $thisTable = 'PhotoTable'; |
|
278 | + $thisTable = 'PhotoTable'; |
|
279 | 279 | } |
280 | 280 | if ($this->getTypeByObjectId($objectId) == self::TYPE_VIDEO) { |
281 | - $thisTable = 'VideoTable'; |
|
281 | + $thisTable = 'VideoTable'; |
|
282 | 282 | } |
283 | 283 | |
284 | 284 | if (is_null($thisTable)) { |
285 | - return false; |
|
285 | + return false; |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | $item = $this->getItemByObjectId($objectId); |
289 | 289 | |
290 | 290 | if (!isset($item['id'])) { |
291 | - return false; |
|
291 | + return false; |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | $q = "UPDATE `" . $thisTable . "` SET rating=? WHERE id=?"; |
295 | 295 | |
296 | 296 | $res = $this->db->prepare($q); |
297 | 297 | $ret = $res->execute(array( |
298 | - $rating, |
|
299 | - $item['id'] |
|
298 | + $rating, |
|
299 | + $item['id'] |
|
300 | 300 | )); |
301 | 301 | |
302 | 302 | return $ret; |
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Removes all tags from a given item using Object ID. Make sure to get an Object ID first by |
|
307 | - * calling getObjectIdByNumericId(). |
|
308 | - * |
|
309 | - * @param string $objectId Object ID |
|
310 | - * |
|
311 | - * @return void |
|
312 | - */ |
|
313 | - public function removeAllItemTags($objectId) |
|
314 | - { |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Removes all tags from a given item using Object ID. Make sure to get an Object ID first by |
|
307 | + * calling getObjectIdByNumericId(). |
|
308 | + * |
|
309 | + * @param string $objectId Object ID |
|
310 | + * |
|
311 | + * @return void |
|
312 | + */ |
|
313 | + public function removeAllItemTags($objectId) |
|
314 | + { |
|
315 | 315 | return $this->setItemTags($objectId, []); |
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * Gets an item |
|
320 | - * |
|
321 | - * @param string $itemType What type of item |
|
322 | - * @param null|string $objectId The object ID |
|
323 | - * |
|
324 | - * @return array the item |
|
325 | - */ |
|
326 | - private function getItem($itemType, $objectId = null) |
|
327 | - { |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * Gets an item |
|
320 | + * |
|
321 | + * @param string $itemType What type of item |
|
322 | + * @param null|string $objectId The object ID |
|
323 | + * |
|
324 | + * @return array the item |
|
325 | + */ |
|
326 | + private function getItem($itemType, $objectId = null) |
|
327 | + { |
|
328 | 328 | if ($itemType == self::TYPE_PHOTO) { |
329 | - $tableName = 'PhotoTable'; |
|
329 | + $tableName = 'PhotoTable'; |
|
330 | 330 | } elseif ($itemType == self::TYPE_VIDEO) { |
331 | - $tableName = 'VideoTable'; |
|
331 | + $tableName = 'VideoTable'; |
|
332 | 332 | } |
333 | 333 | |
334 | 334 | if (!is_null($objectId)) { |
335 | - $query = 'SELECT * FROM `' . $tableName . '` WHERE id=?'; |
|
336 | - $params = [$objectId]; |
|
335 | + $query = 'SELECT * FROM `' . $tableName . '` WHERE id=?'; |
|
336 | + $params = [$objectId]; |
|
337 | 337 | } else { |
338 | - $query = 'SELECT * FROM ' . $tableName; |
|
339 | - $params = []; |
|
338 | + $query = 'SELECT * FROM ' . $tableName; |
|
339 | + $params = []; |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | $res = $this->db->prepare($query); |
@@ -345,359 +345,359 @@ discard block |
||
345 | 345 | $ret = array(); |
346 | 346 | |
347 | 347 | while (($data = $res->fetch(\PDO::FETCH_ASSOC))) { |
348 | - if (is_array($data) && isset($data['id'])) { |
|
348 | + if (is_array($data) && isset($data['id'])) { |
|
349 | 349 | $data['object_id'] = $this->getObjectIdByNumericId($itemType, $data['id']); |
350 | 350 | $data['tags'] = $this->getTagsByObjectId($data['object_id']); |
351 | 351 | if ($data['tags'] === false) { |
352 | - $data['tags'] = []; |
|
352 | + $data['tags'] = []; |
|
353 | 353 | } |
354 | 354 | $data['type'] = $itemType; |
355 | 355 | $ret[] = $data; |
356 | - } |
|
356 | + } |
|
357 | 357 | } |
358 | 358 | |
359 | 359 | if (!is_null($objectId) && isset($ret[0])) { |
360 | - return $ret[0]; |
|
360 | + return $ret[0]; |
|
361 | 361 | } |
362 | 362 | |
363 | 363 | return $ret; |
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Gets an item by Object ID |
|
368 | - * |
|
369 | - * @param string $objectId Object ID |
|
370 | - * |
|
371 | - * @return array|bool Either the item data or false |
|
372 | - */ |
|
373 | - public function getItemByObjectId($objectId) |
|
374 | - { |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Gets an item by Object ID |
|
368 | + * |
|
369 | + * @param string $objectId Object ID |
|
370 | + * |
|
371 | + * @return array|bool Either the item data or false |
|
372 | + */ |
|
373 | + public function getItemByObjectId($objectId) |
|
374 | + { |
|
375 | 375 | $ret = false; |
376 | 376 | $type = $this->getTypeByObjectId($objectId); |
377 | 377 | |
378 | 378 | if ($type !== false) { |
379 | - $numId = $this->getNumericIdByObjectId($objectId); |
|
380 | - if ($type == self::TYPE_PHOTO) { |
|
379 | + $numId = $this->getNumericIdByObjectId($objectId); |
|
380 | + if ($type == self::TYPE_PHOTO) { |
|
381 | 381 | $ret = $this->getPhotoById($numId); |
382 | - } elseif ($type == self::TYPE_VIDEO) { |
|
382 | + } elseif ($type == self::TYPE_VIDEO) { |
|
383 | 383 | $ret = $this->getVideoById($numId); |
384 | - } |
|
384 | + } |
|
385 | 385 | |
386 | - if (is_array($ret)) { |
|
386 | + if (is_array($ret)) { |
|
387 | 387 | $ret['type'] = $type; |
388 | 388 | $ret['object_id'] = $objectId; |
389 | - } |
|
389 | + } |
|
390 | 390 | } |
391 | 391 | |
392 | 392 | return $ret; |
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Returns all tags in the database, optionally with the items attached to each tag. |
|
397 | - * |
|
398 | - * @param bool $withItems true to attach items |
|
399 | - * |
|
400 | - * @return array tags |
|
401 | - */ |
|
402 | - public function getAllTags($withItems = false) |
|
403 | - { |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Returns all tags in the database, optionally with the items attached to each tag. |
|
397 | + * |
|
398 | + * @param bool $withItems true to attach items |
|
399 | + * |
|
400 | + * @return array tags |
|
401 | + */ |
|
402 | + public function getAllTags($withItems = false) |
|
403 | + { |
|
404 | 404 | $q = 'SELECT * FROM TagTable'; |
405 | 405 | $res = $this->db->prepare($q); |
406 | 406 | $res->execute(); |
407 | 407 | $ret = []; |
408 | 408 | |
409 | 409 | while (($data = $res->fetch(\PDO::FETCH_ASSOC))) { |
410 | - if ($withItems === true) { |
|
410 | + if ($withItems === true) { |
|
411 | 411 | $data = $this->appendItemsToTag($data); |
412 | - } |
|
413 | - $ret[] = $data; |
|
412 | + } |
|
413 | + $ret[] = $data; |
|
414 | 414 | } |
415 | 415 | |
416 | 416 | return $ret; |
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Appends items to a tag |
|
421 | - * |
|
422 | - * @param string $tag tag |
|
423 | - * |
|
424 | - * @return array the altered tag |
|
425 | - */ |
|
426 | - private function appendItemsToTag($tag) |
|
427 | - { |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Appends items to a tag |
|
421 | + * |
|
422 | + * @param string $tag tag |
|
423 | + * |
|
424 | + * @return array the altered tag |
|
425 | + */ |
|
426 | + private function appendItemsToTag($tag) |
|
427 | + { |
|
428 | 428 | if (isset($tag['photo_id_list'])) { |
429 | - $thisItems = array_map('trim', explode(',', $tag['photo_id_list'])); |
|
430 | - foreach ($thisItems as $objectId) { |
|
429 | + $thisItems = array_map('trim', explode(',', $tag['photo_id_list'])); |
|
430 | + foreach ($thisItems as $objectId) { |
|
431 | 431 | if (!empty($objectId) > 0) { |
432 | - $tag['items'][] = $this->getItemByObjectId($objectId); |
|
432 | + $tag['items'][] = $this->getItemByObjectId($objectId); |
|
433 | + } |
|
433 | 434 | } |
434 | - } |
|
435 | 435 | } |
436 | 436 | return $tag; |
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Gets items that are linked to a given tag |
|
441 | - * |
|
442 | - * @param string $tagName name of the tag |
|
443 | - * |
|
444 | - * @return array items |
|
445 | - */ |
|
446 | - public function getItemsByTag($tagName) |
|
447 | - { |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Gets items that are linked to a given tag |
|
441 | + * |
|
442 | + * @param string $tagName name of the tag |
|
443 | + * |
|
444 | + * @return array items |
|
445 | + */ |
|
446 | + public function getItemsByTag($tagName) |
|
447 | + { |
|
448 | 448 | $tagData = $this->getTag($tagName); |
449 | 449 | $ret = array(); |
450 | 450 | |
451 | 451 | if (is_array($tagData) && isset($tagData['photo_id_list'])) { |
452 | - $foundItems = explode(",", $tagData['photo_id_list']); |
|
452 | + $foundItems = explode(",", $tagData['photo_id_list']); |
|
453 | 453 | |
454 | - foreach ($foundItems as $item) { |
|
454 | + foreach ($foundItems as $item) { |
|
455 | 455 | $item = trim($item); |
456 | 456 | if (!empty($item)) { |
457 | - $thisItem = $this->getItemByObjectId($item); |
|
458 | - if (is_array($thisItem)) { |
|
457 | + $thisItem = $this->getItemByObjectId($item); |
|
458 | + if (is_array($thisItem)) { |
|
459 | 459 | $ret[] = $thisItem; |
460 | - } |
|
460 | + } |
|
461 | + } |
|
461 | 462 | } |
462 | - } |
|
463 | 463 | } |
464 | 464 | |
465 | 465 | return $ret; |
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * Gets a given tag, possibly creating it when not existent and maybe attaching the items to it. |
|
470 | - * |
|
471 | - * @param string $tagName tag name |
|
472 | - * @param bool $autoCreate shall it be auto created if it doesn't exist? |
|
473 | - * @param bool $withItems shall items be attached to it ('items' element) |
|
474 | - * |
|
475 | - * @return array|mixed|null tag |
|
476 | - */ |
|
477 | - public function getTag($tagName, $autoCreate = false, $withItems = false) |
|
478 | - { |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * Gets a given tag, possibly creating it when not existent and maybe attaching the items to it. |
|
470 | + * |
|
471 | + * @param string $tagName tag name |
|
472 | + * @param bool $autoCreate shall it be auto created if it doesn't exist? |
|
473 | + * @param bool $withItems shall items be attached to it ('items' element) |
|
474 | + * |
|
475 | + * @return array|mixed|null tag |
|
476 | + */ |
|
477 | + public function getTag($tagName, $autoCreate = false, $withItems = false) |
|
478 | + { |
|
479 | 479 | $q = 'SELECT * FROM TagTable WHERE name=?'; |
480 | 480 | $res = $this->db->prepare($q); |
481 | 481 | $res->execute(array( |
482 | - $tagName |
|
482 | + $tagName |
|
483 | 483 | )); |
484 | 484 | |
485 | 485 | $ret = null; |
486 | 486 | $data = $res->fetch(\PDO::FETCH_ASSOC); |
487 | 487 | |
488 | 488 | if (is_array($data) && isset($data['id'])) { |
489 | - $ret = $data; |
|
490 | - if ($withItems === true) { |
|
489 | + $ret = $data; |
|
490 | + if ($withItems === true) { |
|
491 | 491 | $ret = $this->appendItemsToTag($ret); |
492 | - } |
|
492 | + } |
|
493 | 493 | } else { |
494 | - // autocreate? |
|
495 | - if ($autoCreate === true) { |
|
494 | + // autocreate? |
|
495 | + if ($autoCreate === true) { |
|
496 | 496 | $this->createTag($tagName); |
497 | 497 | return $this->getTag($tagName, $autoCreate, $withItems); |
498 | - } |
|
498 | + } |
|
499 | 499 | } |
500 | 500 | |
501 | 501 | return $ret; |
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Gets the ID from a tag name |
|
506 | - * |
|
507 | - * @param $tagName tag name |
|
508 | - * |
|
509 | - * @return bool|string either the id or false if it doesn't exist |
|
510 | - */ |
|
511 | - public function getTagId($tagName) |
|
512 | - { |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Gets the ID from a tag name |
|
506 | + * |
|
507 | + * @param $tagName tag name |
|
508 | + * |
|
509 | + * @return bool|string either the id or false if it doesn't exist |
|
510 | + */ |
|
511 | + public function getTagId($tagName) |
|
512 | + { |
|
513 | 513 | $ret = false; |
514 | 514 | $tagData = $this->getTag($tagName); |
515 | 515 | |
516 | 516 | if (isset($tagData['id'])) { |
517 | - $ret = $tagData['id']; |
|
517 | + $ret = $tagData['id']; |
|
518 | 518 | } |
519 | 519 | |
520 | 520 | return $ret; |
521 | - } |
|
522 | - |
|
523 | - /** |
|
524 | - * Create a new tag |
|
525 | - * |
|
526 | - * @param string $tagName tag name |
|
527 | - * |
|
528 | - * @return bool true if all good, false otherwise |
|
529 | - */ |
|
530 | - public function createTag($tagName) : bool |
|
531 | - { |
|
521 | + } |
|
522 | + |
|
523 | + /** |
|
524 | + * Create a new tag |
|
525 | + * |
|
526 | + * @param string $tagName tag name |
|
527 | + * |
|
528 | + * @return bool true if all good, false otherwise |
|
529 | + */ |
|
530 | + public function createTag($tagName) : bool |
|
531 | + { |
|
532 | 532 | $ret = false; |
533 | 533 | |
534 | 534 | if (is_null($this->getTag($tagName))) { |
535 | - $insQ = " |
|
535 | + $insQ = " |
|
536 | 536 | INSERT INTO TagTable |
537 | 537 | (`name`, `photo_id_list`, `time_created`) |
538 | 538 | VALUES (?, ?, ?) |
539 | 539 | "; |
540 | - $res = $this->db->prepare($insQ); |
|
541 | - $res->execute([$tagName, '', time()]); |
|
542 | - $ret = true; |
|
540 | + $res = $this->db->prepare($insQ); |
|
541 | + $res->execute([$tagName, '', time()]); |
|
542 | + $ret = true; |
|
543 | 543 | } |
544 | 544 | |
545 | 545 | return $ret; |
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Gets items whose filename (whole path) matches a given string |
|
550 | - * |
|
551 | - * @param string $path any part of the filename |
|
552 | - * |
|
553 | - * @return array matching items |
|
554 | - */ |
|
555 | - public function getItemsByPath($path) : array |
|
556 | - { |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Gets items whose filename (whole path) matches a given string |
|
550 | + * |
|
551 | + * @param string $path any part of the filename |
|
552 | + * |
|
553 | + * @return array matching items |
|
554 | + */ |
|
555 | + public function getItemsByPath($path) : array |
|
556 | + { |
|
557 | 557 | return $this->searchAllWithCondition('filename', $path); |
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Flexible search on the database |
|
562 | - * |
|
563 | - * @param string $field Which field |
|
564 | - * @param string $value Which value |
|
565 | - * |
|
566 | - * @return array matching items |
|
567 | - */ |
|
568 | - public function searchAllWithCondition($field = null, $value = null) : array |
|
569 | - { |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Flexible search on the database |
|
562 | + * |
|
563 | + * @param string $field Which field |
|
564 | + * @param string $value Which value |
|
565 | + * |
|
566 | + * @return array matching items |
|
567 | + */ |
|
568 | + public function searchAllWithCondition($field = null, $value = null) : array |
|
569 | + { |
|
570 | 570 | if ($field != null && $field != null) { |
571 | - $q = " |
|
571 | + $q = " |
|
572 | 572 | SELECT id, 'video' as type FROM VideoTable |
573 | 573 | WHERE `" . $field . "` LIKE ? |
574 | 574 | UNION ALL |
575 | 575 | SELECT id, 'photo' FROM PhotoTable |
576 | 576 | WHERE `" . $field . "` LIKE ?"; |
577 | 577 | |
578 | - $res = $this->db->prepare($q); |
|
579 | - $res->execute(['%' . $value . '%', '%' . $value . '%']); |
|
578 | + $res = $this->db->prepare($q); |
|
579 | + $res->execute(['%' . $value . '%', '%' . $value . '%']); |
|
580 | 580 | } else { |
581 | - $q = " |
|
581 | + $q = " |
|
582 | 582 | SELECT id, 'video' as type FROM VideoTable |
583 | 583 | UNION ALL |
584 | 584 | SELECT id, 'photo' FROM PhotoTable"; |
585 | 585 | |
586 | - $res = $this->db->prepare($q); |
|
587 | - $res->execute([]); |
|
586 | + $res = $this->db->prepare($q); |
|
587 | + $res->execute([]); |
|
588 | 588 | } |
589 | 589 | |
590 | 590 | $ret = []; |
591 | 591 | |
592 | 592 | while (($data = $res->fetch(\PDO::FETCH_ASSOC))) { |
593 | - if ($data['type'] == 'video') { |
|
593 | + if ($data['type'] == 'video') { |
|
594 | 594 | $item = $this->getVideoById($data['id']); |
595 | 595 | $ret[] = $item; |
596 | - } |
|
597 | - if ($data['type'] == 'photo') { |
|
596 | + } |
|
597 | + if ($data['type'] == 'photo') { |
|
598 | 598 | $item = $this->getPhotoById($data['id']); |
599 | 599 | $ret[] = $item; |
600 | - } |
|
600 | + } |
|
601 | 601 | } |
602 | 602 | |
603 | 603 | return $ret; |
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Returns an array that has every item as key (Object IDs) and the value |
|
608 | - * is an array of tags associated to that item. |
|
609 | - * |
|
610 | - * @return array |
|
611 | - */ |
|
612 | - public function getItemTagMap() : array |
|
613 | - { |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Returns an array that has every item as key (Object IDs) and the value |
|
608 | + * is an array of tags associated to that item. |
|
609 | + * |
|
610 | + * @return array |
|
611 | + */ |
|
612 | + public function getItemTagMap() : array |
|
613 | + { |
|
614 | 614 | $tagData = $this->getAllTags(); |
615 | 615 | $ret = []; |
616 | 616 | |
617 | 617 | foreach ($tagData as $tag) { |
618 | - $thisItems = explode(",", $tag['photo_id_list']); |
|
619 | - foreach ($thisItems as $objectId) { |
|
618 | + $thisItems = explode(",", $tag['photo_id_list']); |
|
619 | + foreach ($thisItems as $objectId) { |
|
620 | 620 | if (strlen($objectId) > 0) { |
621 | - $ret[$objectId][] = $tag['name']; |
|
621 | + $ret[$objectId][] = $tag['name']; |
|
622 | + } |
|
622 | 623 | } |
623 | - } |
|
624 | 624 | } |
625 | 625 | |
626 | 626 | return $ret; |
627 | - } |
|
628 | - |
|
629 | - /** |
|
630 | - * Gets the object type (video or photo, compare with the constants) of a given Object ID |
|
631 | - * |
|
632 | - * @param string $objectId Object ID |
|
633 | - * |
|
634 | - * @return bool|int|string type |
|
635 | - */ |
|
636 | - public function getTypeByObjectId($objectId) |
|
637 | - { |
|
627 | + } |
|
628 | + |
|
629 | + /** |
|
630 | + * Gets the object type (video or photo, compare with the constants) of a given Object ID |
|
631 | + * |
|
632 | + * @param string $objectId Object ID |
|
633 | + * |
|
634 | + * @return bool|int|string type |
|
635 | + */ |
|
636 | + public function getTypeByObjectId($objectId) |
|
637 | + { |
|
638 | 638 | $foundType = false; |
639 | 639 | foreach ($this->typeMap as $intName => $idPart) { |
640 | - if (substr($objectId, 0, strlen($idPart)) == $idPart) { |
|
640 | + if (substr($objectId, 0, strlen($idPart)) == $idPart) { |
|
641 | 641 | $foundType = $intName; |
642 | 642 | break; |
643 | - } |
|
643 | + } |
|
644 | 644 | } |
645 | 645 | |
646 | 646 | return $foundType; |
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * Returns the Object ID with a type and a numeric ID |
|
651 | - * |
|
652 | - * @param string $type Either photo or video, use the class constants |
|
653 | - * @param string $numericId Numeric ID |
|
654 | - * |
|
655 | - * @return string Object ID |
|
656 | - */ |
|
657 | - public function getObjectIdByNumericId($type, $numericId) |
|
658 | - { |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * Returns the Object ID with a type and a numeric ID |
|
651 | + * |
|
652 | + * @param string $type Either photo or video, use the class constants |
|
653 | + * @param string $numericId Numeric ID |
|
654 | + * |
|
655 | + * @return string Object ID |
|
656 | + */ |
|
657 | + public function getObjectIdByNumericId($type, $numericId) |
|
658 | + { |
|
659 | 659 | $ret = $this->typeMap[$type]; |
660 | 660 | $ret .= str_pad(dechex($numericId), 16, "0", STR_PAD_LEFT); |
661 | 661 | return $ret; |
662 | - } |
|
663 | - |
|
664 | - /** |
|
665 | - * Gets the type prefix for a given type |
|
666 | - * |
|
667 | - * @param string $type type |
|
668 | - * |
|
669 | - * @return bool|string prefix or false |
|
670 | - */ |
|
671 | - public function getObjectIdPartIdByType($type) |
|
672 | - { |
|
662 | + } |
|
663 | + |
|
664 | + /** |
|
665 | + * Gets the type prefix for a given type |
|
666 | + * |
|
667 | + * @param string $type type |
|
668 | + * |
|
669 | + * @return bool|string prefix or false |
|
670 | + */ |
|
671 | + public function getObjectIdPartIdByType($type) |
|
672 | + { |
|
673 | 673 | if (isset($this->typeMap[$type])) { |
674 | - return $this->typeMap[$type]; |
|
674 | + return $this->typeMap[$type]; |
|
675 | 675 | } |
676 | 676 | return false; |
677 | - } |
|
678 | - |
|
679 | - /** |
|
680 | - * Returns the numeric ID of a given Object ID |
|
681 | - * |
|
682 | - * @param string $objectId Object ID |
|
683 | - * |
|
684 | - * @return bool|number|string numeric id |
|
685 | - */ |
|
686 | - public function getNumericIdByObjectId($objectId) |
|
687 | - { |
|
677 | + } |
|
678 | + |
|
679 | + /** |
|
680 | + * Returns the numeric ID of a given Object ID |
|
681 | + * |
|
682 | + * @param string $objectId Object ID |
|
683 | + * |
|
684 | + * @return bool|number|string numeric id |
|
685 | + */ |
|
686 | + public function getNumericIdByObjectId($objectId) |
|
687 | + { |
|
688 | 688 | $ret = false; |
689 | 689 | $thisType = $this->getTypeByObjectId($objectId); |
690 | 690 | if ($thisType !== false) { |
691 | - $ret = ltrim( |
|
691 | + $ret = ltrim( |
|
692 | 692 | substr( |
693 | - $objectId, |
|
694 | - strlen($this->getObjectIdPartIdByType($thisType)) |
|
693 | + $objectId, |
|
694 | + strlen($this->getObjectIdPartIdByType($thisType)) |
|
695 | 695 | ), |
696 | 696 | ' 0' |
697 | - ); |
|
698 | - $ret = hexdec($ret); |
|
697 | + ); |
|
698 | + $ret = hexdec($ret); |
|
699 | 699 | } |
700 | 700 | |
701 | 701 | return $ret; |
702 | - } |
|
702 | + } |
|
703 | 703 | } |
@@ -57,10 +57,10 @@ discard block |
||
57 | 57 | public function __construct($dbPath) |
58 | 58 | { |
59 | 59 | if (!file_exists($dbPath) || !is_readable($dbPath)) { |
60 | - throw new \Exception("Database with path '" . $dbPath ."' doesn't exist or isn't readable!"); |
|
60 | + throw new \Exception("Database with path '".$dbPath."' doesn't exist or isn't readable!"); |
|
61 | 61 | } |
62 | 62 | |
63 | - $this->db = new \PDO('sqlite:' . $dbPath); |
|
63 | + $this->db = new \PDO('sqlite:'.$dbPath); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | return false; |
292 | 292 | } |
293 | 293 | |
294 | - $q = "UPDATE `" . $thisTable . "` SET rating=? WHERE id=?"; |
|
294 | + $q = "UPDATE `".$thisTable."` SET rating=? WHERE id=?"; |
|
295 | 295 | |
296 | 296 | $res = $this->db->prepare($q); |
297 | 297 | $ret = $res->execute(array( |
@@ -332,10 +332,10 @@ discard block |
||
332 | 332 | } |
333 | 333 | |
334 | 334 | if (!is_null($objectId)) { |
335 | - $query = 'SELECT * FROM `' . $tableName . '` WHERE id=?'; |
|
335 | + $query = 'SELECT * FROM `'.$tableName.'` WHERE id=?'; |
|
336 | 336 | $params = [$objectId]; |
337 | 337 | } else { |
338 | - $query = 'SELECT * FROM ' . $tableName; |
|
338 | + $query = 'SELECT * FROM '.$tableName; |
|
339 | 339 | $params = []; |
340 | 340 | } |
341 | 341 | |
@@ -570,13 +570,13 @@ discard block |
||
570 | 570 | if ($field != null && $field != null) { |
571 | 571 | $q = " |
572 | 572 | SELECT id, 'video' as type FROM VideoTable |
573 | - WHERE `" . $field . "` LIKE ? |
|
573 | + WHERE `" . $field."` LIKE ? |
|
574 | 574 | UNION ALL |
575 | 575 | SELECT id, 'photo' FROM PhotoTable |
576 | - WHERE `" . $field . "` LIKE ?"; |
|
576 | + WHERE `" . $field."` LIKE ?"; |
|
577 | 577 | |
578 | 578 | $res = $this->db->prepare($q); |
579 | - $res->execute(['%' . $value . '%', '%' . $value . '%']); |
|
579 | + $res->execute(['%'.$value.'%', '%'.$value.'%']); |
|
580 | 580 | } else { |
581 | 581 | $q = " |
582 | 582 | SELECT id, 'video' as type FROM VideoTable |