]> git.mxchange.org Git - friendica.git/blob - src/Model/Term.php
File and category aren't using "term" anymore
[friendica.git] / src / Model / Term.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;
23
24 use Friendica\Database\DBA;
25
26 /**
27  * Class Term
28  *
29  * This Model class handles term table interactions.
30  * This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
31  * user-applied categories.
32  */
33 class Term
34 {
35     const UNKNOWN           = 0;
36     const CATEGORY          = 3;
37     const FILE              = 5;
38
39     const OBJECT_TYPE_POST  = 1;
40
41         /**
42          * Generates the legacy item.file field string from an item ID.
43          * Includes only file and category terms.
44          *
45          * @param int $item_id
46          * @return string
47          * @throws \Exception
48          */
49         public static function fileTextFromItemId($item_id)
50         {
51                 $file_text = '';
52
53                 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]];
54                 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
55                 foreach ($tags as $tag) {
56                         if ($tag['type'] == self::CATEGORY) {
57                                 $file_text .= '<' . $tag['term'] . '>';
58                         } else {
59                                 $file_text .= '[' . $tag['term'] . ']';
60                         }
61                 }
62
63                 return $file_text;
64         }
65
66         /**
67          * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
68          * Deletes all previous file terms for the same item ID.
69          *
70          * @param integer $item_id item id
71          * @param         $files
72          * @return void
73          * @throws \Exception
74          */
75         public static function insertFromFileFieldByItemId($item_id, $files)
76         {
77                 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
78                 if (!DBA::isResult($message)) {
79                         return;
80                 }
81
82                 // Clean up all tags
83                 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
84
85                 if ($message["deleted"]) {
86                         return;
87                 }
88
89                 $message['file'] = $files;
90
91                 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
92                         foreach ($files[1] as $file) {
93                                 DBA::insert('term', [
94                                         'uid' => $message["uid"],
95                                         'oid' => $item_id,
96                                         'otype' => self::OBJECT_TYPE_POST,
97                                         'type' => self::FILE,
98                                         'term' => $file
99                                 ]);
100                         }
101                 }
102
103                 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
104                         foreach ($files[1] as $file) {
105                                 DBA::insert('term', [
106                                         'uid' => $message["uid"],
107                                         'oid' => $item_id,
108                                         'otype' => self::OBJECT_TYPE_POST,
109                                         'type' => self::CATEGORY,
110                                         'term' => $file
111                                 ]);
112                         }
113                 }
114         }
115 }