c2912fa73fa7a890351003deff36d18b463ab29e
[friendica.git/.git] / src / Model / Post / Category.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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         /**
115          * Generates an array of files or categories of a given uri-id
116          *
117          * @param int $uri_id
118          * @param int $uid
119          * @param int $type
120          * @return array
121          * @throws \Exception
122          */
123         public static function getArrayByURIId(int $uri_id, int $uid, int $type = self::CATEGORY)
124         {
125                 $tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type]);
126                 if (empty($tags)) {
127                         return [];
128                 }
129
130                 return array_column($tags, 'name');
131         }
132
133         /**
134          * Generates a comma separated list of files or categories
135          *
136          * @param int $uri_id
137          * @param int $uid
138          * @param int $type
139          * @return string
140          * @throws \Exception
141          */
142         public static function getCSVByURIId(int $uri_id, int $uid, int $type)
143         {
144                 return implode(',', self::getArrayByURIId($uri_id, $uid, $type));
145         }
146
147         /**
148          * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
149          * Deletes all previous file terms for the same item ID.
150          *
151          * @param integer $item_id item id
152          * @param         $files
153          * @return void
154          * @throws \Exception
155          */
156         public static function storeTextByURIId(int $uri_id, int $uid, string $files)
157         {
158                 $message = Post::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
159                 if (DBA::isResult($message)) {
160                         // Clean up all tags
161                         DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
162
163                         if ($message['deleted']) {
164                                 return;
165                         }
166                 }
167
168                 if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
169                         foreach ($result[1] as $file) {
170                                 $tagid = Tag::getID($file);
171                                 if (empty($tagid)) {
172                                         continue;
173                                 }
174
175                                 DBA::replace('post-category', [
176                                         'uri-id' => $uri_id,
177                                         'uid' => $uid,
178                                         'type' => self::FILE,
179                                         'tid' => $tagid
180                                 ]);
181                         }
182                 }
183
184                 if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
185                         foreach ($result[1] as $file) {
186                                 self::storeFileByURIId($uri_id, $uid, self::FILE, $file);
187                         }
188                 }
189         }
190
191         public static function storeFileByURIId(int $uri_id, int $uid, int $type, string $file)
192         {
193                 $tagid = Tag::getID($file);
194                 if (empty($tagid)) {
195                         return false;
196                 }
197
198                 return DBA::replace('post-category', [
199                         'uri-id' => $uri_id,
200                         'uid' => $uid,
201                         'type' => $type,
202                         'tid' => $tagid
203                 ]);
204         }
205 }