| @@ -1,0 +1,110 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Classes; | |
| 4 | + | |
| 5 | +use AcyMailing\Core\AcymClass; | |
| 6 | + | |
| 7 | +class TagClass extends AcymClass | |
| 8 | +{ | |
| 9 | + const TYPE_MAIL = 'mail'; | |
| 10 | + const TYPE_LIST = 'list'; | |
| 11 | + | |
| 12 | + public function __construct() | |
| 13 | + { | |
| 14 | + parent::__construct(); | |
| 15 | + | |
| 16 | + $this->table = 'tag'; | |
| 17 | + $this->pkey = 'id'; | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Attaches the tags passed in parameter to the specified element | |
| 22 | + */ | |
| 23 | + public function setTags(string $type, int $elementId, array $newTags): void | |
| 24 | + { | |
| 25 | + // Remove the old tags from the element | |
| 26 | + acym_query('DELETE FROM #__acym_tag WHERE `type` = '.acym_escapeDB($type).' AND `id_element` = '.intval($elementId)); | |
| 27 | + | |
| 28 | + $tagsToInsertQuery = []; | |
| 29 | + | |
| 30 | + foreach ($newTags as $oneTag) { | |
| 31 | + if (empty($oneTag)) { | |
| 32 | + continue; | |
| 33 | + } | |
| 34 | + | |
| 35 | + $newTag = new \stdClass(); | |
| 36 | + $newTag->type = $type; | |
| 37 | + | |
| 38 | + if (strpos($oneTag, 'acy_new_tag_') !== false) { | |
| 39 | + $tagName = substr($oneTag, 12); | |
| 40 | + if (empty($tagName)) { | |
| 41 | + continue; | |
| 42 | + } | |
| 43 | + | |
| 44 | + $newTag->name = $tagName; | |
| 45 | + } else { | |
| 46 | + $newTag->name = $oneTag; | |
| 47 | + } | |
| 48 | + $tagsToInsertQuery[] = '('.acym_escapeDB($newTag->name).','.acym_escapeDB($newTag->type).', '.intval($elementId).')'; | |
| 49 | + } | |
| 50 | + | |
| 51 | + if (!empty($tagsToInsertQuery)) { | |
| 52 | + acym_query('INSERT INTO #__acym_tag (`name`, `type`, `id_element`) VALUES '.implode(',', $tagsToInsertQuery)); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + public function getAllTagsByType(string $type): array | |
| 57 | + { | |
| 58 | + return acym_loadObjectList( | |
| 59 | + 'SELECT `name` AS `value`, `name` | |
| 60 | + FROM #__acym_tag | |
| 61 | + WHERE `type` = '.acym_escapeDB($type).' | |
| 62 | + GROUP BY `name`' | |
| 63 | + ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public function getAllTagsByElementId(string $type, int $id): array | |
| 67 | + { | |
| 68 | + if (empty($id)) { | |
| 69 | + return []; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $tags = acym_loadResultArray( | |
| 73 | + 'SELECT * | |
| 74 | + FROM #__acym_tag | |
| 75 | + WHERE `type` = '.acym_escapeDB($type).' | |
| 76 | + AND `id_element` = '.intval($id) | |
| 77 | + ); | |
| 78 | + | |
| 79 | + return empty($tags) ? [] : $tags; | |
| 80 | + } | |
| 81 | + | |
| 82 | + public function getAllTagsByTypeAndElementIds(string $type, array $ids): array | |
| 83 | + { | |
| 84 | + acym_arrayToInteger($ids); | |
| 85 | + if (empty($ids)) { | |
| 86 | + return []; | |
| 87 | + } | |
| 88 | + | |
| 89 | + return acym_loadObjectList( | |
| 90 | + 'SELECT * | |
| 91 | + FROM #__acym_tag | |
| 92 | + WHERE `type` = '.acym_escapeDB($type).' | |
| 93 | + AND `id_element` IN ('.implode(',', $ids).')' | |
| 94 | + ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + public function getAllTagsForSelect(): array | |
| 98 | + { | |
| 99 | + return acym_loadObjectList( | |
| 100 | + 'SELECT DISTINCT `name` | |
| 101 | + FROM #__acym_tag | |
| 102 | + ORDER BY `name` ASC' | |
| 103 | + ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + public function deleteByName(string $name): void | |
| 107 | + { | |
| 108 | + acym_query('DELETE FROM #__acym_tag WHERE `name` = '.acym_escapeDB($name)); | |
| 109 | + } | |
| 110 | +} | |