]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Category.php
Use the owner, not the author
[friendica.git] / src / Model / Post / Category.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model\Post;
23
24 use Friendica\Database\DBA;
25 use Friendica\Model\Post;
26 use Friendica\Model\Tag;
27
28 /**
29  * Class Category
30  *
31  * This Model class handles category table interactions.
32  * This tables stores user-applied categories related to posts.
33  */
34 class Category
35 {
36         const UNKNOWN           = 0;
37         const CATEGORY          = 3;
38         const FILE              = 5;
39         const SUBCRIPTION       = 10;
40
41         /**
42          * Delete all categories and files from a given uri-id and user
43          *
44          * @param int $uri_id
45          * @param int $uid
46          * @return boolean success
47          * @throws \Exception
48          */
49         public static function deleteByURIId(int $uri_id, int $uid)
50         {
51                 return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
52         }
53
54         /**
55          * Delete all categories and files from a given uri-id and user
56          *
57          * @param int $uri_id
58          * @param int $uid
59          * @return boolean success
60          * @throws \Exception
61          */
62         public static function deleteFileByURIId(int $uri_id, int $uid, int $type, string $file)
63         {
64                 $tagid = Tag::getID($file);
65                 if (empty($tagid)) {
66                         return false;
67                 }
68
69                 return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type, 'tid' => $tagid]);
70         }
71         /**
72          * Generates the legacy item.file field string from an item ID.
73          * Includes only file and category terms.
74          *
75          * @param int $uri_id
76          * @param int $uid
77          * @return string
78          * @throws \Exception
79          */
80         public static function getTextByURIId(int $uri_id, int $uid)
81         {
82                 $file_text = '';
83
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'] . '>';
88                         } else {
89                                 $file_text .= '[' . $tag['name'] . ']';
90                         }
91                 }
92
93                 return $file_text;
94         }
95
96         /**
97          * Generates an array of files or categories of a given uri-id
98          *
99          * @param int $uid
100          * @param int $type
101          * @return array
102          * @throws \Exception
103          */
104         public static function getArray(int $uid, int $type)
105         {
106                 $tags = DBA::selectToArray('category-view', ['name'], ['uid' => $uid, 'type' => $type],
107                         ['group_by' => ['name'], 'order' => ['name']]);
108                 if (empty($tags)) {
109                         return [];
110                 }
111
112                 return array_column($tags, 'name');
113         }
114
115         public static function existsForURIId(int $uri_id, int $uid)
116         {
117                 return DBA::exists('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
118         }
119
120         /**
121          * Generates an array of files or categories of a given uri-id
122          *
123          * @param int $uri_id
124          * @param int $uid
125          * @param int $type
126          * @return array
127          * @throws \Exception
128          */
129         public static function getArrayByURIId(int $uri_id, int $uid, int $type = self::CATEGORY)
130         {
131                 $tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type]);
132                 if (empty($tags)) {
133                         return [];
134                 }
135
136                 return array_column($tags, 'name');
137         }
138
139         /**
140          * Generates a comma separated list of files or categories
141          *
142          * @param int $uri_id
143          * @param int $uid
144          * @param int $type
145          * @return string
146          * @throws \Exception
147          */
148         public static function getCSVByURIId(int $uri_id, int $uid, int $type)
149         {
150                 return implode(',', self::getArrayByURIId($uri_id, $uid, $type));
151         }
152
153         /**
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.
156          *
157          * @param integer $item_id item id
158          * @param         $files
159          * @return void
160          * @throws \Exception
161          */
162         public static function storeTextByURIId(int $uri_id, int $uid, string $files)
163         {
164                 $message = Post::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
165                 if (DBA::isResult($message)) {
166                         // Clean up all tags
167                         DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
168
169                         if ($message['deleted']) {
170                                 return;
171                         }
172                 }
173
174                 if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
175                         foreach ($result[1] as $file) {
176                                 $tagid = Tag::getID($file);
177                                 if (empty($tagid)) {
178                                         continue;
179                                 }
180
181                                 self::storeByURIId($uri_id, $uid, self::FILE, $tagid);
182                         }
183                 }
184
185                 if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
186                         foreach ($result[1] as $file) {
187                                 self::storeFileByURIId($uri_id, $uid, self::CATEGORY, $file);
188                         }
189                 }
190         }
191
192         public static function storeFileByURIId(int $uri_id, int $uid, int $type, string $file, string $url = ''): bool
193         {
194                 $tagid = Tag::getID($file, $url);
195                 if (empty($tagid)) {
196                         return false;
197                 }
198
199                 return self::storeByURIId($uri_id, $uid, $type, $tagid);
200         }
201
202         private static function storeByURIId(int $uri_id, int $uid, int $type, int $tagid): bool
203         {
204                 return DBA::replace('post-category', [
205                         'uri-id' => $uri_id,
206                         'uid' => $uid,
207                         'type' => $type,
208                         'tid' => $tagid
209                 ]);
210         }
211 }