]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Display.php
Timelines are now an object instead of an array
[friendica.git] / src / Module / Settings / Display.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\Text\BBCode;
26 use Friendica\Content\Conversation\Factory\Timeline as TimelineFactory;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\Hook;
29 use Friendica\Core\L10n;
30 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session\Capability\IHandleUserSessions;
33 use Friendica\Core\Theme;
34 use Friendica\Model\User;
35 use Friendica\Module\BaseSettings;
36 use Friendica\Module\Response;
37 use Friendica\Navigation\SystemMessages;
38 use Friendica\Network\HTTPException;
39 use Friendica\Util\Profiler;
40 use Psr\Log\LoggerInterface;
41
42 /**
43  * Module to update user settings
44  */
45 class Display extends BaseSettings
46 {
47         /** @var IManageConfigValues */
48         private $config;
49         /** @var IManagePersonalConfigValues */
50         private $pConfig;
51         /** @var App */
52         private $app;
53         /** @var SystemMessages */
54         private $systemMessages;
55         /** @var TimelineFactory */
56         protected $timeline;
57
58         public function __construct(TimelineFactory $timeline, SystemMessages $systemMessages, App $app, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
59         {
60                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
61
62                 $this->config         = $config;
63                 $this->pConfig        = $pConfig;
64                 $this->app            = $app;
65                 $this->systemMessages = $systemMessages;
66                 $this->timeline       = $timeline;
67         }
68
69         protected function post(array $request = [])
70         {
71                 $uid = $this->session->getLocalUserId();
72                 if (!$uid) {
73                         throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
74                 }
75
76                 self::checkFormSecurityTokenRedirectOnError('/settings/display', 'settings_display');
77
78                 $user = User::getById($uid);
79
80                 $theme                  = !empty($request['theme'])                  ? trim($request['theme'])                    : $user['theme'];
81                 $mobile_theme           = !empty($request['mobile_theme'])           ? trim($request['mobile_theme'])             : '';
82                 $enable_smile           = !empty($request['enable_smile'])           ? intval($request['enable_smile'])           : 0;
83                 $enable                 = !empty($request['enable'])                 ? $request['enable']                         : [];
84                 $bookmark               = !empty($request['bookmark'])               ? $request['bookmark']                       : [];
85                 $channel_languages      = !empty($request['channel_languages'])      ? $request['channel_languages']              : [];
86                 $first_day_of_week      = !empty($request['first_day_of_week'])      ? intval($request['first_day_of_week'])      : 0;
87                 $calendar_default_view  = !empty($request['calendar_default_view'])  ? trim($request['calendar_default_view'])    : 'month';
88                 $infinite_scroll        = !empty($request['infinite_scroll'])        ? intval($request['infinite_scroll'])        : 0;
89                 $enable_smart_threading = !empty($request['enable_smart_threading']) ? intval($request['enable_smart_threading']) : 0;
90                 $enable_dislike         = !empty($request['enable_dislike'])         ? intval($request['enable_dislike'])         : 0;
91                 $display_resharer       = !empty($request['display_resharer'])       ? intval($request['display_resharer'])       : 0;
92                 $stay_local             = !empty($request['stay_local'])             ? intval($request['stay_local'])             : 0;
93                 $preview_mode           = !empty($request['preview_mode'])           ? intval($request['preview_mode'])           : 0;
94                 $browser_update         = !empty($request['browser_update'])         ? intval($request['browser_update'])         : 0;
95                 if ($browser_update != -1) {
96                         $browser_update = $browser_update * 1000;
97                         if ($browser_update < 10000) {
98                                 $browser_update = 10000;
99                         }
100                 }
101
102                 $enabled_timelines = [];
103                 foreach ($enable as $code => $enabled) {
104                         if ($enabled) {
105                                 $enabled_timelines[] = $code;
106                         }
107                 }
108
109                 $network_timelines = [];
110                 foreach ($bookmark as $code => $bookmarked) {
111                         if ($bookmarked) {
112                                 $network_timelines[] = $code;
113                         }
114                 }
115
116                 $itemspage_network = !empty($request['itemspage_network']) ?
117                         intval($request['itemspage_network']) :
118                         $this->config->get('system', 'itemspage_network');
119                 if ($itemspage_network > 100) {
120                         $itemspage_network = 100;
121                 }
122                 $itemspage_mobile_network = !empty($request['itemspage_mobile_network']) ?
123                         intval($request['itemspage_mobile_network']) :
124                         $this->config->get('system', 'itemspage_network_mobile');
125                 if ($itemspage_mobile_network > 100) {
126                         $itemspage_mobile_network = 100;
127                 }
128
129                 if ($mobile_theme !== '') {
130                         $this->pConfig->set($uid, 'system', 'mobile_theme', $mobile_theme);
131                 }
132
133                 $this->pConfig->set($uid, 'system', 'itemspage_network'       , $itemspage_network);
134                 $this->pConfig->set($uid, 'system', 'itemspage_mobile_network', $itemspage_mobile_network);
135                 $this->pConfig->set($uid, 'system', 'update_interval'         , $browser_update);
136                 $this->pConfig->set($uid, 'system', 'no_smilies'              , !$enable_smile);
137                 $this->pConfig->set($uid, 'system', 'infinite_scroll'         , $infinite_scroll);
138                 $this->pConfig->set($uid, 'system', 'no_smart_threading'      , !$enable_smart_threading);
139                 $this->pConfig->set($uid, 'system', 'hide_dislike'            , !$enable_dislike);
140                 $this->pConfig->set($uid, 'system', 'display_resharer'        , $display_resharer);
141                 $this->pConfig->set($uid, 'system', 'stay_local'              , $stay_local);
142                 $this->pConfig->set($uid, 'system', 'preview_mode'            , $preview_mode);
143
144                 $this->pConfig->set($uid, 'system', 'network_timelines'       , $network_timelines);
145                 $this->pConfig->set($uid, 'system', 'enabled_timelines'       , $enabled_timelines);
146                 $this->pConfig->set($uid, 'channel', 'languages'              , $channel_languages);
147
148                 $this->pConfig->set($uid, 'calendar', 'first_day_of_week'     , $first_day_of_week);
149                 $this->pConfig->set($uid, 'calendar', 'default_view'          , $calendar_default_view);
150
151                 if (in_array($theme, Theme::getAllowedList())) {
152                         if ($theme == $user['theme']) {
153                                 // call theme_post only if theme has not been changed
154                                 if ($themeconfigfile = Theme::getConfigFile($theme)) {
155                                         require_once $themeconfigfile;
156                                         theme_post($this->app);
157                                 }
158                         } else {
159                                 User::update(['theme' => $theme], $uid);
160                         }
161                 } else {
162                         $this->systemMessages->addNotice($this->t('The theme you chose isn\'t available.'));
163                 }
164
165                 Hook::callAll('display_settings_post', $request);
166
167                 $this->baseUrl->redirect('settings/display');
168         }
169
170         protected function content(array $request = []): string
171         {
172                 parent::content();
173
174                 $uid = $this->session->getLocalUserId();
175                 if (!$uid) {
176                         throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
177                 }
178
179                 $default_theme = $this->config->get('system', 'theme');
180                 if (!$default_theme) {
181                         $default_theme = 'default';
182                 }
183
184                 $default_mobile_theme = $this->config->get('system', 'mobile-theme');
185                 if (!$default_mobile_theme) {
186                         $default_mobile_theme = 'none';
187                 }
188
189                 $user = User::getById($uid);
190
191                 $allowed_themes = Theme::getAllowedList();
192
193                 $themes = [];
194                 $mobile_themes = ['---' => $this->t('No special theme for mobile devices')];
195                 foreach ($allowed_themes as $theme) {
196                         $is_experimental = file_exists('view/theme/' . $theme . '/experimental');
197                         $is_unsupported  = file_exists('view/theme/' . $theme . '/unsupported');
198                         $is_mobile       = file_exists('view/theme/' . $theme . '/mobile');
199                         if (!$is_experimental || $this->config->get('experimental', 'exp_themes')) {
200                                 $theme_name = ucfirst($theme);
201                                 if ($is_unsupported) {
202                                         $theme_name = $this->t('%s - (Unsupported)', $theme_name);
203                                 } elseif ($is_experimental) {
204                                         $theme_name = $this->t('%s - (Experimental)', $theme_name);
205                                 }
206
207                                 if ($is_mobile) {
208                                         $mobile_themes[$theme] = $theme_name;
209                                 } else {
210                                         $themes[$theme] = $theme_name;
211                                 }
212                         }
213                 }
214
215                 $theme_selected        = $user['theme'] ?: $default_theme;
216                 $mobile_theme_selected = $this->session->get('mobile-theme', $default_mobile_theme);
217
218                 $itemspage_network = intval($this->pConfig->get($uid, 'system', 'itemspage_network'));
219                 $itemspage_network = (($itemspage_network > 0 && $itemspage_network < 101) ? $itemspage_network : $this->config->get('system', 'itemspage_network'));
220                 $itemspage_mobile_network = intval($this->pConfig->get($uid, 'system', 'itemspage_mobile_network'));
221                 $itemspage_mobile_network = (($itemspage_mobile_network > 0 && $itemspage_mobile_network < 101) ? $itemspage_mobile_network : $this->config->get('system', 'itemspage_network_mobile'));
222
223                 $browser_update = intval($this->pConfig->get($uid, 'system', 'update_interval'));
224                 if ($browser_update != -1) {
225                         $browser_update = (($browser_update == 0) ? 40 : $browser_update / 1000); // default if not set: 40 seconds
226                 }
227
228                 $enable_smile           = !$this->pConfig->get($uid, 'system', 'no_smilies', 0);
229                 $infinite_scroll        =  $this->pConfig->get($uid, 'system', 'infinite_scroll', 0);
230                 $enable_smart_threading = !$this->pConfig->get($uid, 'system', 'no_smart_threading', 0);
231                 $enable_dislike         = !$this->pConfig->get($uid, 'system', 'hide_dislike', 0);
232                 $display_resharer       =  $this->pConfig->get($uid, 'system', 'display_resharer', 0);
233                 $stay_local             =  $this->pConfig->get($uid, 'system', 'stay_local', 0);
234
235                 $preview_mode  =  $this->pConfig->get($uid, 'system', 'preview_mode', BBCode::PREVIEW_LARGE);
236                 $preview_modes = [
237                         BBCode::PREVIEW_NONE     => $this->t('No preview'),
238                         BBCode::PREVIEW_NO_IMAGE => $this->t('No image'),
239                         BBCode::PREVIEW_SMALL    => $this->t('Small Image'),
240                         BBCode::PREVIEW_LARGE    => $this->t('Large Image'),
241                 ];
242
243                 $bookmarked_timelines = $this->pConfig->get($uid, 'system', 'network_timelines', array_keys($this->getAvailableTimelines($uid, true)));
244                 $enabled_timelines    = $this->pConfig->get($uid, 'system', 'enabled_timelines', array_keys($this->getAvailableTimelines($uid, false)));
245
246                 $channel_languages = $this->pConfig->get($uid, 'channel', 'languages', [User::getLanguageCode($uid)]);
247                 $languages         = $this->l10n->getAvailableLanguages(true);
248
249                 $timelines = [];
250                 foreach ($this->getAvailableTimelines($uid) as $code => $timeline) {
251                         $timelines[] = [
252                                 'label'        => $timeline->label,
253                                 'description'  => $timeline->description,
254                                 'enable'       => ["enable[$code]", '', in_array($code, $enabled_timelines)],
255                                 'bookmark'     => ["bookmark[$code]", '', in_array($code, $bookmarked_timelines)],
256                         ];
257                 }
258
259                 $first_day_of_week = $this->pConfig->get($uid, 'calendar', 'first_day_of_week', 0);
260                 $weekdays          = [
261                         0 => $this->t('Sunday'),
262                         1 => $this->t('Monday'),
263                         2 => $this->t('Tuesday'),
264                         3 => $this->t('Wednesday'),
265                         4 => $this->t('Thursday'),
266                         5 => $this->t('Friday'),
267                         6 => $this->t('Saturday')
268                 ];
269
270                 $calendar_default_view = $this->pConfig->get($uid, 'calendar', 'default_view', 'month');
271                 $calendarViews         = [
272                         'month'      => $this->t('month'),
273                         'agendaWeek' => $this->t('week'),
274                         'agendaDay'  => $this->t('day'),
275                         'listMonth'  => $this->t('list')
276                 ];
277
278                 $theme_config = '';
279                 if ($themeconfigfile = Theme::getConfigFile($theme_selected)) {
280                         require_once $themeconfigfile;
281                         $theme_config = theme_content($this->app);
282                 }
283
284                 $tpl = Renderer::getMarkupTemplate('settings/display.tpl');
285                 return Renderer::replaceMacros($tpl, [
286                         '$ptitle'         => $this->t('Display Settings'),
287                         '$submit'         => $this->t('Save Settings'),
288                         '$d_tset'         => $this->t('General Theme Settings'),
289                         '$d_ctset'        => $this->t('Custom Theme Settings'),
290                         '$d_cset'         => $this->t('Content Settings'),
291                         '$stitle'         => $this->t('Theme settings'),
292                         '$timeline_title' => $this->t('Timelines'),
293                         '$channel_title'  => $this->t('Channels'),
294                         '$calendar_title' => $this->t('Calendar'),
295
296                         '$form_security_token' => self::getFormSecurityToken('settings_display'),
297                         '$uid'                 => $uid,
298
299                         '$theme'            => ['theme', $this->t('Display Theme:'), $theme_selected, '', $themes, true],
300                         '$mobile_theme' => ['mobile_theme', $this->t('Mobile Theme:'), $mobile_theme_selected, '', $mobile_themes, false],
301                         '$theme_config' => $theme_config,
302
303                         '$itemspage_network'        => ['itemspage_network'       , $this->t('Number of items to display per page:'), $itemspage_network, $this->t('Maximum of 100 items')],
304                         '$itemspage_mobile_network' => ['itemspage_mobile_network', $this->t('Number of items to display per page when viewed from mobile device:'), $itemspage_mobile_network, $this->t('Maximum of 100 items')],
305                         '$ajaxint'                  => ['browser_update'          , $this->t('Update browser every xx seconds'), $browser_update, $this->t('Minimum of 10 seconds. Enter -1 to disable it.')],
306                         '$enable_smile'             => ['enable_smile'            , $this->t('Display emoticons'), $enable_smile, $this->t('When enabled, emoticons are replaced with matching symbols.')],
307                         '$infinite_scroll'          => ['infinite_scroll'         , $this->t('Infinite scroll'), $infinite_scroll, $this->t('Automatic fetch new items when reaching the page end.')],
308                         '$enable_smart_threading'   => ['enable_smart_threading'  , $this->t('Enable Smart Threading'), $enable_smart_threading, $this->t('Enable the automatic suppression of extraneous thread indentation.')],
309                         '$enable_dislike'           => ['enable_dislike'          , $this->t('Display the Dislike feature'), $enable_dislike, $this->t('Display the Dislike button and dislike reactions on posts and comments.')],
310                         '$display_resharer'         => ['display_resharer'        , $this->t('Display the resharer'), $display_resharer, $this->t('Display the first resharer as icon and text on a reshared item.')],
311                         '$stay_local'               => ['stay_local'              , $this->t('Stay local'), $stay_local, $this->t("Don't go to a remote system when following a contact link.")],
312                         '$preview_mode'             => ['preview_mode'            , $this->t('Link preview mode'), $preview_mode, $this->t('Appearance of the link preview that is added to each post with a link.'), $preview_modes, false],
313
314                         '$timeline_label'       => $this->t('Label'),
315                         '$timeline_descriptiom' => $this->t('Description'),
316                         '$timeline_enable'      => $this->t('Enable'),
317                         '$timeline_bookmark'    => $this->t('Bookmark'),
318                         '$timelines'            => $timelines,
319                         '$timeline_explanation' => $this->t('Enable timelines that you want to see in the channels widget. Bookmark timelines that you want to see in the top menu.'),
320
321                         '$channel_languages' => ['channel_languages[]', $this->t('Channel languages:'), $channel_languages, $this->t('Select all languages that you want to see in your channels.'), $languages, 'multiple'],
322
323                         '$first_day_of_week'     => ['first_day_of_week'    , $this->t('Beginning of week:')    , $first_day_of_week    , '', $weekdays     , false],
324                         '$calendar_default_view' => ['calendar_default_view', $this->t('Default calendar view:'), $calendar_default_view, '', $calendarViews, false],
325                 ]);
326         }
327
328         private function getAvailableTimelines(int $uid, bool $only_network = false): array
329         {
330                 $timelines = [];
331
332                 foreach ($this->timeline->getNetworkFeeds('') as $channel) {
333                         $timelines[$channel->code] = $channel;
334                 }
335
336                 if ($only_network) {
337                         return $timelines;
338                 }
339
340                 foreach ($this->timeline->getChannelsForUser($uid) as $channel) {
341                         $timelines[$channel->code] = $channel;
342                 }
343
344                 foreach ($this->timeline->getCommunities(true) as $community) {
345                         $timelines[$community->code] = $community;
346                 }
347
348                 return $timelines;
349         }
350 }