]> git.mxchange.org Git - friendica.git/blob - src/Module/Filer/RemoveTag.php
Remove the activity
[friendica.git] / src / Module / Filer / RemoveTag.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Filer;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Session\Capability\IHandleUserSessions;
28 use Friendica\Core\System;
29 use Friendica\Database\DBA;
30 use Friendica\Model\Post;
31 use Friendica\Module\Response;
32 use Friendica\Navigation\SystemMessages;
33 use Friendica\Network\HTTPException;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
36
37 /**
38  * Remove a tag from a file
39  */
40 class RemoveTag extends BaseModule
41 {
42         /** @var SystemMessages */
43         private $systemMessages;
44         /** @var IHandleUserSessions */
45         private $userSession;
46
47         public function __construct(SystemMessages $systemMessages, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, array $server, array $parameters = [])
48         {
49                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->systemMessages = $systemMessages;
52                 $this->userSession    = $userSession;
53         }
54
55         protected function post(array $request = [])
56         {
57                 System::httpError($this->removeTag($request));
58         }
59
60         protected function content(array $request = []): string
61         {
62                 if (!$this->userSession->getLocalUserId()) {
63                         throw new HTTPException\ForbiddenException();
64                 }
65
66                 $this->removeTag($request, $type, $term);
67
68                 if ($type == Post\Category::FILE) {
69                         $this->baseUrl->redirect('filed?file=' . rawurlencode($term));
70                 }
71
72                 return '';
73         }
74
75         /**
76          * @param array       $request The $_REQUEST array
77          * @param string|null $type    Output parameter with the computed type
78          * @param string|null $term    Output parameter with the computed term
79          *
80          * @return int The relevant HTTP code
81          *
82          * @throws \Exception
83          */
84         private function removeTag(array $request, string &$type = null, string &$term = null): int
85         {
86                 $item_id = $this->parameters['id'] ?? 0;
87
88                 $term = trim($request['term'] ?? '');
89                 $cat = trim($request['cat'] ?? '');
90
91                 if (!empty($cat)) {
92                         $type = Post\Category::CATEGORY;
93                         $term = $cat;
94                 } else {
95                         $type = Post\Category::FILE;
96                 }
97
98                 $this->logger->info('Filer - Remove Tag', [
99                         'term' => $term,
100                         'item' => $item_id,
101                         'type' => $type
102                 ]);
103
104                 if (!$item_id || !strlen($term)) {
105                         $this->systemMessages->addNotice($this->l10n->t('Item was not deleted'));
106                         return 401;
107                 }
108
109                 $item = Post::selectFirst(['uri-id'], ['id' => $item_id]);
110                 if (!DBA::isResult($item)) {
111                         return 404;
112                 }
113
114                 if (!Post\Category::deleteFileByURIId($item['uri-id'], $this->userSession->getLocalUserId(), $type, $term)) {
115                         $this->systemMessages->addNotice($this->l10n->t('Item was not removed'));
116                         return 500;
117                 }
118
119                 return 200;
120         }
121 }