]> git.mxchange.org Git - friendica.git/commitdiff
Tags can now be added and removed from photos
authorMichael <heluecht@pirati.ca>
Sat, 18 Apr 2020 20:46:41 +0000 (20:46 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 18 Apr 2020 20:46:41 +0000 (20:46 +0000)
mod/photos.php
mod/tagrm.php
src/Model/Tag.php

index 3f558429d4ea4d45714044494b1a4b1b02e31f13..0107a179d059821de6689a63c4a7c0fa46ae50ab 100644 (file)
@@ -36,6 +36,7 @@ use Friendica\Model\Contact;
 use Friendica\Model\Item;
 use Friendica\Model\Photo;
 use Friendica\Model\Profile;
+use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Module\BaseProfile;
 use Friendica\Network\Probe;
@@ -421,7 +422,7 @@ function photos_post(App $a)
                }
 
                if ($item_id) {
-                       $item = Item::selectFirst(['tag', 'inform'], ['id' => $item_id, 'uid' => $page_owner_uid]);
+                       $item = Item::selectFirst(['tag', 'inform', 'uri-id'], ['id' => $item_id, 'uid' => $page_owner_uid]);
 
                        if (DBA::isResult($item)) {
                                $old_tag    = $item['tag'];
@@ -521,10 +522,17 @@ function photos_post(App $a)
 
                                                        $profile = str_replace(',', '%2c', $profile);
                                                        $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
+
+                                                       if (!empty($item['uri-id'])) {
+                                                               Tag::store($item['uri-id'], Tag::MENTION, $newname, $profile);
+                                                       }       
                                                }
                                        } elseif (strpos($tag, '#') === 0) {
                                                $tagname = substr($tag, 1);
                                                $str_tags .= '#[url=' . DI::baseUrl() . "/search?tag=" . $tagname . ']' . $tagname . '[/url],';
+                                               if (!empty($item['uri-id'])) {
+                                                       Tag::store($item['uri-id'], Tag::HASHTAG, $tagname);
+                                               }
                                        }
                                }
                        }
index 2fa75133ca44681366ec12df9b0fea3bfe3fbf97..51000c9854d60ff68ff7403580f886cbf1456106 100644 (file)
@@ -24,6 +24,7 @@ use Friendica\Content\Text\BBCode;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Item;
+use Friendica\Model\Tag;
 use Friendica\Model\Term;
 use Friendica\Util\Strings;
 
@@ -62,7 +63,7 @@ function update_tags($item_id, $tags){
                return;
        }
 
-       $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
+       $item = Item::selectFirst(['tag', 'uri-id'], ['id' => $item_id, 'uid' => local_user()]);
        if (!DBA::isResult($item)) {
                return;
        }
@@ -70,6 +71,12 @@ function update_tags($item_id, $tags){
        $old_tags = explode(',', $item['tag']);
 
        foreach ($tags as $new_tag) {
+               if (preg_match_all('/([#@!])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism', $new_tag, $results, PREG_SET_ORDER)) {
+                       foreach ($results as $tag) {
+                               Tag::removeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
+                       }
+               }
+       
                foreach ($old_tags as $index => $old_tag) {
                        if (strcmp($old_tag, $new_tag) == 0) {
                                unset($old_tags[$index]);
index c3d1c501626c771bb76e1d172937bd70aa494551..b9e04ddd7a31865120bcec792d993df9e9f374e9 100644 (file)
@@ -92,7 +92,7 @@ class Tag
                }
 
                if (empty($cid)) {
-                       $fields = ['name' => substr($name, 0, 96)];
+                       $fields = ['name' => substr($name, 0, 96), 'url' => ''];
 
                        if (!empty($url) && ($url != $name)) {
                                $fields['url'] = strtolower($url);
@@ -163,4 +163,48 @@ class Tag
                        self::storeByHash($uriid, $tag[1], $tag[3], $tag[2]);
                }
        }
+
+       /**
+        * Remove tag/mention
+        *
+        * @param integer $uriid
+        * @param integer $type
+        * @param string $name
+        * @param string $url
+        */
+       public static function remove(int $uriid, int $type, string $name, string $url = '')
+       {
+               $tag = DBA::fetchFirst("SELECT `id` FROM `tag` INNER JOIN `post-tag` ON `post-tag`.`tid` = `tag`.`id`
+                       WHERE `uri-id` = ? AND `type` = ? AND `name` = ? AND `url` = ?", $uriid, $type, $name, $url);
+               if (!DBA::isResult($tag)) {
+                       return;
+               }
+               Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['id'], 'name' => $name, 'url' => $url]);
+               DBA::delete('post-tag', ['uri-id' => $uriid, 'tid' => $tag['id']]);
+       }
+
+       /**
+        * Remove tag/mention
+        *
+        * @param integer $uriid
+        * @param string $hash
+        * @param string $name
+        * @param string $url
+        */
+       public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
+       {
+               if ($hash == self::TAG_CHARACTER[self::MENTION]) {
+                       $type = self::MENTION;
+               } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
+                       $type = self::EXCLUSIVE_MENTION;
+               } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
+                       $type = self::IMPLICIT_MENTION;
+               } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
+                       $type = self::HASHTAG;
+               } else {
+                       return;
+               }
+
+               self::remove($uriid, $type, $name, $url);
+       }
 }