]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Category.php
We now store the receivers as well
[friendica.git] / src / Model / Post / Category.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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
40         /**
41          * Delete all categories and files from a given uri-id and user
42          *
43          * @param int $uri_id
44          * @param int $uid
45          * @return boolean success
46          * @throws \Exception
47          */
48         public static function deleteByURIId(int $uri_id, int $uid)
49         {
50                 return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
51         }
52
53         /**
54          * Delete all categories and files from a given uri-id and user
55          *
56          * @param int $uri_id
57          * @param int $uid
58          * @return boolean success
59          * @throws \Exception
60          */
61         public static function deleteFileByURIId(int $uri_id, int $uid, int $type, string $file)
62         {
63                 $tagid = Tag::getID($file);
64                 if (empty($tagid)) {
65                         return false;
66                 }
67
68                 return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type, 'tid' => $tagid]);
69         }
70         /**
71          * Generates the legacy item.file field string from an item ID.
72          * Includes only file and category terms.
73          *
74          * @param int $uri_id
75          * @param int $uid
76          * @return string
77          * @throws \Exception
78          */
79         public static function getTextByURIId(int $uri_id, int $uid)
80         {
81                 $file_text = '';
82
83                 $tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid]);
84                 foreach ($tags as $tag) {
85                         if ($tag['type'] == self::CATEGORY) {
86                                 $file_text .= '<' . $tag['name'] . '>';
87                         } else {
88                                 $file_text .= '[' . $tag['name'] . ']';
89                         }
90                 }
91
92                 return $file_text;
93         }
94
95         /**
96          * Generates an array of files or categories of a given uri-id
97          *
98          * @param int $uid
99          * @param int $type
100          * @return array
101          * @throws \Exception
102          */
103         public static function getArray(int $uid, int $type)
104         {
105                 $tags = DBA::selectToArray('category-view', ['name'], ['uid' => $uid, 'type' => $type],
106                         ['group_by' => ['name'], 'order' => ['name']]);
107                 if (empty($tags)) {
108                         return [];
109                 }
110
111                 return array_column($tags, 'name');
112         }
113
114         public static function existsForURIId(int $uri_id, int $uid)
115         {
116                 return DBA::exists('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
117         }
118
119         /**
120          * Generates an array of files or categories of a given uri-id
121          *
122          * @param int $uri_id
123          * @param int $uid
124          * @param int $type
125          * @return array
126          * @throws \Exception
127          */
128         public static function getArrayByURIId(int $uri_id, int $uid, int $type = self::CATEGORY)
129         {
130                 $tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type]);
131                 if (empty($tags)) {
132                         return [];
133                 }
134
135                 return array_column($tags, 'name');
136         }
137
138         /**
139          * Generates a comma separated list of files or categories
140          *
141          * @param int $uri_id
142          * @param int $uid
143          * @param int $type
144          * @return string
145          * @throws \Exception
146          */
147         public static function getCSVByURIId(int $uri_id, int $uid, int $type)
148         {
149                 return implode(',', self::getArrayByURIId($uri_id, $uid, $type));
150         }
151
152         /**
153          * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
154          * Deletes all previous file terms for the same item ID.
155          *
156          * @param integer $item_id item id
157          * @param         $files
158          * @return void
159          * @throws \Exception
160          */
161         public static function storeTextByURIId(int $uri_id, int $uid, string $files)
162         {
163                 $message = Post::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
164                 if (DBA::isResult($message)) {
165                         // Clean up all tags
166                         DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
167
168                         if ($message['deleted']) {
169                                 return;
170                         }
171                 }
172
173                 if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
174                         foreach ($result[1] as $file) {
175                                 $tagid = Tag::getID($file);
176                                 if (empty($tagid)) {
177                                         continue;
178                                 }
179
180                                 DBA::replace('post-category', [
181                                         'uri-id' => $uri_id,
182                                         'uid' => $uid,
183                                         'type' => self::FILE,
184                                         'tid' => $tagid
185                                 ]);
186                         }
187                 }
188
189                 if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
190                         foreach ($result[1] as $file) {
191                                 self::storeFileByURIId($uri_id, $uid, self::CATEGORY, $file);
192                         }
193                 }
194         }
195
196         public static function storeFileByURIId(int $uri_id, int $uid, int $type, string $file)
197         {
198                 $tagid = Tag::getID($file);
199                 if (empty($tagid)) {
200                         return false;
201                 }
202
203                 return DBA::replace('post-category', [
204                         'uri-id' => $uri_id,
205                         'uid' => $uid,
206                         'type' => $type,
207                         'tid' => $tagid
208                 ]);
209         }
210 }