3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
26 use Friendica\App\Arguments;
27 use Friendica\BaseModule;
28 use Friendica\Content\Pager;
29 use Friendica\Core\L10n;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\System;
32 use Friendica\Navigation\Notifications\ValueObject\FormattedNotify;
33 use Friendica\Network\HTTPException\ForbiddenException;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
38 * Base Module for each tab of the notification display
40 * General possibility to print it as JSON as well
42 abstract class BaseNotifications extends BaseModule
44 /** @var array Array of URL parameters */
46 FormattedNotify::NETWORK => 'network',
47 FormattedNotify::SYSTEM => 'system',
48 FormattedNotify::HOME => 'home',
49 FormattedNotify::PERSONAL => 'personal',
50 FormattedNotify::INTRO => 'intros',
53 /** @var array Array of the allowed notifications and their printable name */
55 FormattedNotify::NETWORK => 'Network',
56 FormattedNotify::SYSTEM => 'System',
57 FormattedNotify::HOME => 'Home',
58 FormattedNotify::PERSONAL => 'Personal',
59 FormattedNotify::INTRO => 'Introductions',
62 /** @var array The array of access keys for notification pages */
64 FormattedNotify::NETWORK => 'w',
65 FormattedNotify::SYSTEM => 'y',
66 FormattedNotify::HOME => 'h',
67 FormattedNotify::PERSONAL => 'r',
68 FormattedNotify::INTRO => 'i',
71 /** @var int The default count of items per page */
72 const ITEMS_PER_PAGE = 20;
73 /** @var int The default limit of notifications per page */
74 const DEFAULT_PAGE_LIMIT = 80;
76 /** @var boolean True, if ALL entries should get shown */
78 /** @var int The determined start item of the current page */
79 protected $firstItemNum;
85 * Collects all notifications from the backend
87 * @return array The determined notification array
88 * ['header', 'notifications']
90 abstract public function getNotifications();
92 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
94 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
97 throw new ForbiddenException($this->t('Permission denied.'));
100 $page = ($_REQUEST['page'] ?? 0) ?: 1;
102 $this->firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
103 $this->showAll = ($_REQUEST['show'] ?? '') === 'all';
106 protected function rawContent(array $request = [])
108 // If the last argument of the query is NOT json, return
109 if ($this->args->get($this->args->getArgc() - 1) !== 'json') {
114 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
116 // Add additional informations (needed for json output)
118 'notifications' => $this->getNotifications(),
119 'items_page' => $pager->getItemsPerPage(),
120 'page' => $pager->getPage(),
123 System::jsonExit($notifications);
127 * Shows the printable result of notifications for a specific tab
129 * @param string $header The notification header
130 * @param array $notifications The array with the notifications
131 * @param string $noContent The string in case there are no notifications
132 * @param array $showLink The possible links at the top
134 * @return string The rendered output
136 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
138 protected function printContent(string $header, array $notifications, string $noContent, array $showLink)
140 // Get the nav tabs for the notification pages
141 $tabs = $this->getTabs();
144 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
146 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
147 return Renderer::replaceMacros($notif_tpl, [
148 '$header' => $header ?? $this->t('Notifications'),
150 '$notifications' => $notifications,
151 '$noContent' => $noContent,
152 '$showLink' => $showLink,
153 '$paginate' => $pager->renderMinimal(count($notifications))
158 * List of pages for the Notifications TabBar
160 * @return array with with notifications TabBar data
163 private function getTabs()
165 $selected = $this->args->get(1, '');
169 foreach (self::URL_TYPES as $type => $url) {
171 'label' => $this->t(self::PRINT_TYPES[$type]),
172 'url' => 'notifications/' . $url,
173 'sel' => (($selected == $url) ? 'active' : ''),
174 'id' => $type . '-tab',
175 'accesskey' => self::ACCESS_KEYS[$type],