]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Category.php
File and category aren't using "term" anymore
[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\Item;
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          * Generates the legacy item.file field string from an item ID.
42          * Includes only file and category terms.
43          *
44          * @param int $item_id
45          * @return string
46          * @throws \Exception
47          */
48         public static function getTextByURIId(int $uri_id, int $uid)
49         {
50                 $file_text = '';
51
52                 $tags = DBA::selectToArray('post-category', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid]);
53                 foreach ($tags as $tag) {
54                         if ($tag['type'] == self::CATEGORY) {
55                                 $file_text .= '<' . $tag['term'] . '>';
56                         } else {
57                                 $file_text .= '[' . $tag['term'] . ']';
58                         }
59                 }
60
61                 return $file_text;
62         }
63
64         /**
65          * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
66          * Deletes all previous file terms for the same item ID.
67          *
68          * @param integer $item_id item id
69          * @param         $files
70          * @return void
71          * @throws \Exception
72          */
73         public static function storeTextByURIId(int $uri_id, int $uid, string $files)
74         {
75                 $message = Item::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
76                 if (!DBA::isResult($message)) {
77                         return;
78                 }
79
80                 // Clean up all tags
81                 DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
82
83                 if ($message['deleted']) {
84                         return;
85                 }
86
87                 if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
88                         foreach ($result[1] as $file) {
89                                 $tagid = Tag::getID($file);
90                                 if (empty($tagid)) {
91                                         continue;
92                                 }
93
94                                 DBA::insert('post-category', [
95                                         'uri-id' => $uri_id,
96                                         'uid' => $uid,
97                                         'type' => self::FILE,
98                                         'tid' => $tagid
99                                 ]);
100                         }
101                 }
102
103                 if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
104                         foreach ($result[1] as $file) {
105                                 $tagid = Tag::getID($file);
106                                 if (empty($tagid)) {
107                                         continue;
108                                 }
109
110                                 DBA::insert('post-category', [
111                                         'uri-id' => $uri_id,
112                                         'uid' => $uid,
113                                         'type' => self::CATEGORY,
114                                         'tid' => $tagid
115                                 ]);
116                         }
117                 }
118         }
119 }