]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Channels.php
Special search keywords added
[friendica.git] / src / Module / Settings / Channels.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\Settings;
23
24 use Friendica\App;
25 use Friendica\Content\Conversation\Factory\Timeline;
26 use Friendica\Content\Conversation\Repository\Channel;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Module\BaseSettings;
31 use Friendica\Module\Response;
32 use Friendica\Network\HTTPException;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
35
36 class Channels extends BaseSettings
37 {
38         /** @var Channel */
39         private $channel;
40         /** @var Timeline */
41         private $timeline;
42
43         public function __construct(Timeline $timeline, Channel $channel, App\Page $page, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
44         {
45                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
46
47                 $this->timeline = $timeline;
48                 $this->channel  = $channel;
49         }
50
51         protected function post(array $request = [])
52         {
53                 $uid = $this->session->getLocalUserId();
54                 if (!$uid) {
55                         throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
56                 }
57
58                 if (empty($request['edit_channel']) && empty($request['add_channel'])) {
59                         return;
60                 }
61
62                 self::checkFormSecurityTokenRedirectOnError('/settings/channels', 'settings_channels');
63
64                 if (!empty($request['add_channel'])) {
65                         $channel = $this->timeline->createFromTableRow([
66                                 'label'            => $request['new_label'],
67                                 'description'      => $request['new_description'],
68                                 'access-key'       => substr(mb_strtolower($request['new_access_key']), 0, 1),
69                                 'uid'              => $uid,
70                                 'include-tags'     => $this->cleanTags($request['new_include_tags']),
71                                 'exclude-tags'     => $this->cleanTags($request['new_exclude_tags']),
72                                 'full-text-search' => $this->cleanTags($request['new_text_search']),
73                                 'media-type'       => ($request['new_image'] ? 1 : 0) | ($request['new_video'] ? 2 : 0) | ($request['new_audio'] ? 4 : 0),
74                         ]);
75                         $saved = $this->channel->save($channel);
76                         $this->logger->debug('New channel added', ['saved' => $saved]);
77                         return;
78                 }
79
80                 foreach (array_keys($request['label']) as $id) {
81                         if ($request['delete'][$id]) {
82                                 $success = $this->channel->deleteById($id, $uid);
83                                 $this->logger->debug('Channel deleted', ['id' => $id, 'success' => $success]);
84                                 continue;
85                         }
86
87                         $channel = $this->timeline->createFromTableRow([
88                                 'id'               => $id,
89                                 'label'            => $request['label'][$id],
90                                 'description'      => $request['description'][$id],
91                                 'access-key'       => substr(mb_strtolower($request['access_key'][$id]), 0, 1),
92                                 'uid'              => $uid,
93                                 'include-tags'     => $this->cleanTags($request['include_tags'][$id]),
94                                 'exclude-tags'     => $this->cleanTags($request['exclude_tags'][$id]),
95                                 'full-text-search' => $this->cleanTags($request['text_search'][$id]),
96                                 'media-type'       => ($request['image'][$id] ? 1 : 0) | ($request['video'][$id] ? 2 : 0) | ($request['audio'][$id] ? 4 : 0),
97                         ]);
98                         $saved = $this->channel->save($channel);
99                         $this->logger->debug('Save channel', ['id' => $id, 'saved' => $saved]);
100                 }
101
102                 $this->baseUrl->redirect('/settings/channels');
103         }
104
105         protected function content(array $request = []): string
106         {
107                 parent::content();
108
109                 $uid = $this->session->getLocalUserId();
110                 if (!$uid) {
111                         throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
112                 }
113
114                 $blocklistform = [];
115                 foreach ($this->channel->selectByUid($uid) as $channel) {
116                         $blocklistform[] = [
117                                 'label'        => ["label[$channel->code]", $this->t('Label'), $channel->label, '', $this->t('Required')],
118                                 'description'  => ["description[$channel->code]", $this->t("Description"), $channel->description],
119                                 'access_key'   => ["access_key[$channel->code]", $this->t("Access Key"), $channel->accessKey],
120                                 'include_tags' => ["include_tags[$channel->code]", $this->t("Include Tags"), $channel->includeTags],
121                                 'exclude_tags' => ["exclude_tags[$channel->code]", $this->t("Exclude Tags"), $channel->excludeTags],
122                                 'text_search'  => ["text_search[$channel->code]", $this->t("Full Text Search"), $channel->fullTextSearch],
123                                 'image'        => ["image[$channel->code]", $this->t("Images"), $channel->mediaType & 1],
124                                 'video'        => ["video[$channel->code]", $this->t("Videos"), $channel->mediaType & 2],
125                                 'audio'        => ["audio[$channel->code]", $this->t("Audio"), $channel->mediaType & 4],
126                                 'delete'       => ["delete[$channel->code]", $this->t("Delete channel") . ' (' . $channel->label . ')', false, $this->t("Check to delete this entry from the channel list")]
127                         ];
128                 }
129
130                 $t = Renderer::getMarkupTemplate('settings/channels.tpl');
131                 return Renderer::replaceMacros($t, [
132                         'label'        => ["new_label", $this->t('Label'), '', $this->t('Short name for the channel. It is displayed on the channels widget.'), $this->t('Required')],
133                         'description'  => ["new_description", $this->t("Description"), '', $this->t('This should describe the content of the channel in a few word.')],
134                         'access_key'   => ["new_access_key", $this->t("Access Key"), '', $this->t('When you want to access this channel via an access key, you can define it here. Pay attentioon to not use an already used one.')],
135                         'include_tags' => ["new_include_tags", $this->t("Include Tags"), '', $this->t('Comma separated list of tags. A post will be used when it contains any of the listed tags.')],
136                         'exclude_tags' => ["new_exclude_tags", $this->t("Exclude Tags"), '', $this->t('Comma separated list of tags. If a post contain any of these tags, then it will not be part of nthis channel.')],
137                         'text_search'  => ["new_text_search", $this->t("Full Text Search"), '', $this->t('Search terms for the body.')], // @todo Add dcumentation for the keywords
138                         'image'        => ['new_image', $this->t("Images"), false, $this->t("Check to display images in the channel.")],
139                         'video'        => ["new_video", $this->t("Videos"), false, $this->t("Check to display videos in the channel.")],
140                         'audio'        => ["new_audio", $this->t("Audio"), false, $this->t("Check to display audio in the channel.")],
141                         '$l10n'        => [
142                                 'title'          => $this->t('Channels'),
143                                 'intro'          => $this->t('This page can be used to define your own channels.'),
144                                 'addtitle'       => $this->t('Add new entry to the channel list'),
145                                 'addsubmit'      => $this->t('Add'),
146                                 'savechanges'    => $this->t('Save'),
147                                 'currenttitle'   => $this->t('Current Entries in the channel list'),
148                                 'thurl'          => $this->t('Blocked server domain pattern'),
149                                 'threason'       => $this->t('Reason for the block'),
150                                 'delentry'       => $this->t('Delete entry from the channel list'),
151                                 'confirm_delete' => $this->t('Delete entry from the channel list?'),
152                         ],
153                         '$entries' => $blocklistform,
154                         '$baseurl' => $this->baseUrl,
155
156                         '$form_security_token' => self::getFormSecurityToken('settings_channels'),
157                 ]);
158         }
159
160         private function cleanTags(string $tag_list): string
161         {
162                 $tags = [];
163
164                 $tagitems = explode(',', mb_strtolower($tag_list));
165                 foreach ($tagitems as $tag) {
166                         $tag = trim($tag, '# ');
167                         if (!empty($tag)) {
168                                 $tags[] = $tag;
169                         }
170                 }
171                 return implode(',', $tags);
172         }
173 }