]> git.mxchange.org Git - friendica.git/commitdiff
Create Post/Tag/Remove module class and route
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 2 Nov 2022 03:42:20 +0000 (23:42 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 3 Nov 2022 04:27:08 +0000 (00:27 -0400)
- Fix tag name parameter in single tag removal link in mod/photos
- Remove "/post" from backend routes as it was preventing sessions from working in this module. No existing module had a route starting with "/post".

mod/photos.php
src/App/Mode.php
src/Module/Post/Tag/Remove.php [new file with mode: 0644]
static/routes.config.php
view/templates/post/tag/remove.tpl [new file with mode: 0644]

index 2d7516be932a68cb88e530c9585831ca4c05cd37..ccf0525cb00040c36ae74def0f9fd8f6d080db2c 100644 (file)
@@ -1250,15 +1250,17 @@ function photos_content(App $a)
                if (!empty($link_item['id'])) {
                        // parse tags and add links
                        $tag_arr = [];
-                       foreach (Tag::getByURIId($link_item['uri-id']) as $tag) {
-                               $tag_arr[] = [
-                                       'name' => $tag['name'],
-                                       'removeurl' => '/tagrm/' . $link_item['id'] . '/' . bin2hex($tag['name'])
-                               ];
+                       foreach (explode(',', Tag::getCSVByURIId($link_item['uri-id'])) as $tag_name) {
+                               if ($tag_name) {
+                                       $tag_arr[] = [
+                                               'name'      => BBCode::toPlaintext($tag_name),
+                                               'removeurl' => 'post/' . $link_item['id'] . '/tag/remove/' . bin2hex($tag_name) . '?return=' . urlencode(DI::args()->getCommand()),
+                                       ];
+                               }
                        }
                        $tags = ['title' => DI::l10n()->t('Tags: '), 'tags' => $tag_arr];
                        if ($cmd === 'edit') {
-                               $tags['removeanyurl'] = 'tagrm/' . $link_item['id'];
+                               $tags['removeanyurl'] = 'post/' . $link_item['id'] . '/tag/remove?return=' . urlencode(DI::args()->getCommand());
                                $tags['removetitle'] = DI::l10n()->t('[Select tags to remove]');
                        }
                }
index 5d6bd759f27197f2bd1410cc2b3f7e8fb2ecd36d..59a47d8284360bf8838c826ebf52dc45f9105c14 100644 (file)
@@ -68,7 +68,6 @@ class Mode
                'objects',
                'outbox',
                'poco',
-               'post',
                'pubsub',
                'pubsubhubbub',
                'receive',
diff --git a/src/Module/Post/Tag/Remove.php b/src/Module/Post/Tag/Remove.php
new file mode 100644 (file)
index 0000000..94a1ecf
--- /dev/null
@@ -0,0 +1,140 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Post\Tag;
+
+use Friendica\App;
+use Friendica\Content\Text\BBCode;
+use Friendica\Core\L10n;
+use Friendica\Core\Renderer;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Model\Post;
+use Friendica\Model\Tag;
+use Friendica\Module\Response;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+
+class Remove extends \Friendica\BaseModule
+{
+       /** @var IHandleUserSessions */
+       private $session;
+
+       public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->session = $session;
+       }
+
+       protected function post(array $request = [])
+       {
+               if (!$this->session->getLocalUserId()) {
+                       $this->baseUrl->redirect($request['return'] ?? '');
+               }
+
+
+               if (isset($request['cancel'])) {
+                       $this->baseUrl->redirect($request['return'] ?? '');
+               }
+
+               $tags = [];
+               foreach ($request['tag'] ?? [] as $tag => $checked) {
+                       if ($checked) {
+                               $tags[] = hex2bin(trim($tag));
+                       }
+               }
+
+               $this->removeTagsFromItem($this->parameters['item_id'], $tags);
+               $this->baseUrl->redirect($request['return'] ?? '');
+       }
+
+       protected function content(array $request = []): string
+       {
+               $returnUrl = $request['return'] ?? '';
+
+               if (!$this->session->getLocalUserId()) {
+                       $this->baseUrl->redirect($returnUrl);
+               }
+
+               if (isset($this->parameters['tag_name'])) {
+                       $this->removeTagsFromItem($this->parameters['item_id'], [trim(hex2bin($this->parameters['tag_name']))]);
+                       $this->baseUrl->redirect($returnUrl);
+               }
+
+               $item_id = intval($this->parameters['item_id']);
+               if (!$item_id) {
+                       $this->baseUrl->redirect($returnUrl);
+               }
+
+               $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
+               if (!$item) {
+                       $this->baseUrl->redirect($returnUrl);
+               }
+
+               $tag_text = Tag::getCSVByURIId($item['uri-id']);
+
+               $tags = explode(',', $tag_text);
+               if (empty($tags)) {
+                       $this->baseUrl->redirect($returnUrl);
+               }
+
+               $tag_checkboxes = array_map(function ($tag_text) {
+                       return ['tag[' . bin2hex($tag_text) . ']', BBCode::toPlaintext($tag_text)];
+               }, $tags);
+
+               $tpl = Renderer::getMarkupTemplate('post/tag/remove.tpl');
+               return Renderer::replaceMacros($tpl, [
+                       '$l10n' => [
+                               'header' => $this->t('Remove Item Tag'),
+                               'desc'   => $this->t('Select a tag to remove: '),
+                               'remove' => $this->t('Remove'),
+                               'cancel' => $this->t('Cancel'),
+                       ],
+
+                       '$item_id'        => $item_id,
+                       '$return'         => $returnUrl,
+                       '$tag_checkboxes' => $tag_checkboxes,
+               ]);
+       }
+
+       /**
+        * @param int   $item_id
+        * @param array $tags
+        * @throws \Exception
+        */
+       private function removeTagsFromItem(int $item_id, array $tags)
+       {
+               if (empty($item_id) || empty($tags)) {
+                       return;
+               }
+
+               $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
+               if (empty($item)) {
+                       return;
+               }
+
+               foreach ($tags as $tag) {
+                       if (preg_match('~([#@!])\[url=([^\[\]]*)]([^\[\]]*)\[/url]~im', $tag, $results)) {
+                               Tag::removeByHash($item['uri-id'], $results[1], $results[3], $results[2]);
+                       }
+               }
+       }
+}
index 53bbf3eb7f95c5492888b977b4a58a898cb3dd32..9066c4876237842d915acd78710931e4fab2c56d 100644 (file)
@@ -531,6 +531,11 @@ return [
        ],
 
        '/ping'              => [Module\Notifications\Ping::class, [R::GET]],
+
+       '/post' => [
+               '/{item_id}/tag/remove[/{tag_name}]'                       => [Module\Post\Tag\Remove::class, [R::GET, R::POST]],
+       ],
+
        '/pretheme'          => [Module\ThemeDetails::class, [R::GET]],
        '/probe'             => [Module\Debug\Probe::class,  [R::GET]],
 
diff --git a/view/templates/post/tag/remove.tpl b/view/templates/post/tag/remove.tpl
new file mode 100644 (file)
index 0000000..86c54af
--- /dev/null
@@ -0,0 +1,19 @@
+<div class="generic-page-wrapper">
+       <h3>{{$l10n.header}}</h3>
+
+       <p id="tag-remove-desc">{{$l10n.desc}}</p>
+
+       <form id="tagrm" action="post/{{$item_id}}/tag/remove?return={{$return}}" method="post">
+               <ul>
+{{foreach $tag_checkboxes as $tag_checkbox}}
+                       <li>
+                {{include file="field_checkbox.tpl" field=$tag_checkbox}}
+                       </li>
+{{/foreach}}
+               </ul>
+               <p>
+                       <button type="submit" id="tagrm-submit" class="btn btn-primary" name="submit">{{$l10n.remove}}</button>
+                       <button type="submit" id="tagrm-submit" class="btn" name="cancel">{{$l10n.cancel}}</button>
+               </p>
+       </form>
+</div>