]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Term.php
Merge pull request #4962 from astifter/fix_cropped_find_button
[friendica.git] / src / Model / Term.php
index c730bf74499b8914c962c414aa11fcb5d3675aee..03f19197a361705910867c8400e6c6d24f119713 100644 (file)
@@ -9,6 +9,7 @@ use Friendica\Database\DBM;
 use dba;
 
 require_once 'boot.php';
+require_once 'include/conversation.php';
 require_once 'include/dba.php';
 
 class Term
@@ -122,19 +123,6 @@ class Term
                }
        }
 
-       public static function insertFromTagFieldByItemUri($itemuri, $uid)
-       {
-               $messages = dba::select('item', ['id'], ['uri' => $itemuri, 'uid' => $uid]);
-
-               if (DBM::is_result($messages)) {
-                       while ($message = dba::fetch($messages)) {
-                               self::insertFromTagFieldByItemId($message['id']);
-                       }
-                       dba::close($messages);
-               }
-       }
-
-
        /**
         * @param integer $itemid item id
         * @return void
@@ -183,18 +171,54 @@ class Term
        }
 
        /**
-        * @param string  $itemuri item uri
-        * @param integer $uid     uid
-        * @return void
+        * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
+        * provided item's body with them.
+        *
+        * @param array $item
+        * @return array
         */
-       public static function insertFromFileFieldByItemUri($itemuri, $uid)
+       public static function populateTagsFromItem(&$item)
        {
-               $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
+               $return = [
+                       'tags' => [],
+                       'hashtags' => [],
+                       'mentions' => [],
+               ];
+
+               $searchpath = System::baseUrl() . "/search?tag=";
+
+               $taglist = dba::select(
+                       'term',
+                       ['type', 'term', 'url'],
+                       ["`otype` = ? AND `oid` = ? AND `type` IN (?, ?)", TERM_OBJ_POST, $item['id'], TERM_HASHTAG, TERM_MENTION],
+                       ['order' => ['tid']]
+               );
+
+               while ($tag = dba::fetch($taglist)) {
+                       if ($tag["url"] == "") {
+                               $tag["url"] = $searchpath . strtolower($tag["term"]);
+                       }
+
+                       $orig_tag = $tag["url"];
 
-               if (count($messages)) {
-                       foreach ($messages as $message) {
-                               self::insertFromFileFieldByItemId($message["id"]);
+                       $tag["url"] = best_link_url($item, $sp, $tag["url"]);
+
+                       if ($tag["type"] == TERM_HASHTAG) {
+                               if ($orig_tag != $tag["url"]) {
+                                       $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
+                               }
+
+                               $return['hashtags'][] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
+                               $prefix = "#";
+                       } elseif ($tag["type"] == TERM_MENTION) {
+                               $return['mentions'][] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
+                               $prefix = "@";
                        }
+
+                       $return['tags'][] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
                }
+               dba::close($taglist);
+
+               return $return;
        }
 }