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