]> git.mxchange.org Git - friendica.git/blob - src/Module/Post/Tag/Remove.php
Use the post language for the language detection / config for quality
[friendica.git] / src / Module / Post / Tag / Remove.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Module\Post\Tag;
23
24 use Friendica\App;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Model\Post;
30 use Friendica\Model\Tag;
31 use Friendica\Module\Response;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
34
35 class Remove extends \Friendica\BaseModule
36 {
37         /** @var IHandleUserSessions */
38         private $session;
39
40         public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
41         {
42                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
43
44                 $this->session = $session;
45         }
46
47         protected function post(array $request = [])
48         {
49                 if (!$this->session->getLocalUserId()) {
50                         $this->baseUrl->redirect($request['return'] ?? '');
51                 }
52
53
54                 if (isset($request['cancel'])) {
55                         $this->baseUrl->redirect($request['return'] ?? '');
56                 }
57
58                 $tags = [];
59                 foreach ($request['tag'] ?? [] as $tag => $checked) {
60                         if ($checked) {
61                                 $tags[] = hex2bin(trim($tag));
62                         }
63                 }
64
65                 $this->removeTagsFromItem($this->parameters['item_id'], $tags);
66                 $this->baseUrl->redirect($request['return'] ?? '');
67         }
68
69         protected function content(array $request = []): string
70         {
71                 $returnUrl = $request['return'] ?? '';
72
73                 if (!$this->session->getLocalUserId()) {
74                         $this->baseUrl->redirect($returnUrl);
75                 }
76
77                 if (isset($this->parameters['tag_name'])) {
78                         $this->removeTagsFromItem($this->parameters['item_id'], [trim(hex2bin($this->parameters['tag_name']))]);
79                         $this->baseUrl->redirect($returnUrl);
80                 }
81
82                 $item_id = intval($this->parameters['item_id']);
83                 if (!$item_id) {
84                         $this->baseUrl->redirect($returnUrl);
85                 }
86
87                 $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
88                 if (!$item) {
89                         $this->baseUrl->redirect($returnUrl);
90                 }
91
92                 $tag_text = Tag::getCSVByURIId($item['uri-id']);
93
94                 $tags = explode(',', $tag_text);
95                 if (empty($tags)) {
96                         $this->baseUrl->redirect($returnUrl);
97                 }
98
99                 $tag_checkboxes = array_map(function ($tag_text) {
100                         return ['tag[' . bin2hex($tag_text) . ']', BBCode::toPlaintext($tag_text)];
101                 }, $tags);
102
103                 $tpl = Renderer::getMarkupTemplate('post/tag/remove.tpl');
104                 return Renderer::replaceMacros($tpl, [
105                         '$l10n' => [
106                                 'header' => $this->t('Remove Item Tag'),
107                                 'desc'   => $this->t('Select a tag to remove: '),
108                                 'remove' => $this->t('Remove'),
109                                 'cancel' => $this->t('Cancel'),
110                         ],
111
112                         '$item_id'        => $item_id,
113                         '$return'         => $returnUrl,
114                         '$tag_checkboxes' => $tag_checkboxes,
115                 ]);
116         }
117
118         /**
119          * @param int   $item_id
120          * @param array $tags
121          * @throws \Exception
122          */
123         private function removeTagsFromItem(int $item_id, array $tags)
124         {
125                 if (empty($item_id) || empty($tags)) {
126                         return;
127                 }
128
129                 $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
130                 if (empty($item)) {
131                         return;
132                 }
133
134                 foreach ($tags as $tag) {
135                         if (preg_match('~([#@!])\[url=([^\[\]]*)]([^\[\]]*)\[/url]~im', $tag, $results)) {
136                                 Tag::removeByHash($item['uri-id'], $results[1], $results[3], $results[2]);
137                         }
138                 }
139         }
140 }