3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Database\DBA;
25 use Friendica\Model\Post;
26 use Friendica\Model\Tag;
31 * This Model class handles category table interactions.
32 * This tables stores user-applied categories related to posts.
39 const SUBCRIPTION = 10;
42 * Delete all categories and files from a given uri-id and user
46 * @return boolean success
49 public static function deleteByURIId(int $uri_id, int $uid)
51 return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
55 * Delete all categories and files from a given uri-id and user
59 * @return boolean success
62 public static function deleteFileByURIId(int $uri_id, int $uid, int $type, string $file)
64 $tagid = Tag::getID($file);
69 return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type, 'tid' => $tagid]);
72 * Generates the legacy item.file field string from an item ID.
73 * Includes only file and category terms.
80 public static function getTextByURIId(int $uri_id, int $uid)
84 $tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => [Category::FILE, Category::CATEGORY]]);
85 foreach ($tags as $tag) {
86 if ($tag['type'] == self::CATEGORY) {
87 $file_text .= '<' . $tag['name'] . '>';
89 $file_text .= '[' . $tag['name'] . ']';
97 * Generates an array of files or categories of a given uri-id
104 public static function getArray(int $uid, int $type)
106 $tags = DBA::selectToArray('category-view', ['name'], ['uid' => $uid, 'type' => $type],
107 ['group_by' => ['name'], 'order' => ['name']]);
112 return array_column($tags, 'name');
115 public static function existsForURIId(int $uri_id, int $uid)
117 return DBA::exists('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
121 * Generates an array of files or categories of a given uri-id
129 public static function getArrayByURIId(int $uri_id, int $uid, int $type = self::CATEGORY)
131 $tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type]);
136 return array_column($tags, 'name');
140 * Generates a comma separated list of files or categories
148 public static function getCSVByURIId(int $uri_id, int $uid, int $type)
150 return implode(',', self::getArrayByURIId($uri_id, $uid, $type));
154 * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
155 * Deletes all previous file terms for the same item ID.
157 * @param integer $item_id item id
162 public static function storeTextByURIId(int $uri_id, int $uid, string $files)
164 $message = Post::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
165 if (DBA::isResult($message)) {
167 DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
169 if ($message['deleted']) {
174 if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
175 foreach ($result[1] as $file) {
176 $tagid = Tag::getID($file);
181 self::storeByURIId($uri_id, $uid, self::FILE, $tagid);
185 if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
186 foreach ($result[1] as $file) {
187 self::storeFileByURIId($uri_id, $uid, self::CATEGORY, $file);
192 public static function storeFileByURIId(int $uri_id, int $uid, int $type, string $file, string $url = ''): bool
194 $tagid = Tag::getID($file, $url);
199 return self::storeByURIId($uri_id, $uid, $type, $tagid);
202 private static function storeByURIId(int $uri_id, int $uid, int $type, int $tagid): bool
204 return DBA::replace('post-category', [