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