3 * @copyright Copyright (C) 2010-2023, 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\Session\Capability\IHandleUserSessions;
32 use Friendica\Core\System;
33 use Friendica\Navigation\Notifications\ValueObject\FormattedNotify;
34 use Friendica\Network\HTTPException\ForbiddenException;
35 use Friendica\Util\Profiler;
36 use Psr\Log\LoggerInterface;
39 * Base Module for each tab of the notification display
41 * General possibility to print it as JSON as well
43 abstract class BaseNotifications extends BaseModule
45 /** @var array Array of URL parameters */
47 FormattedNotify::NETWORK => 'network',
48 FormattedNotify::SYSTEM => 'system',
49 FormattedNotify::HOME => 'home',
50 FormattedNotify::PERSONAL => 'personal',
51 FormattedNotify::INTRO => 'intros',
54 /** @var array Array of the allowed notifications and their printable name */
56 FormattedNotify::NETWORK => 'Network',
57 FormattedNotify::SYSTEM => 'System',
58 FormattedNotify::HOME => 'Home',
59 FormattedNotify::PERSONAL => 'Personal',
60 FormattedNotify::INTRO => 'Introductions',
63 /** @var array The array of access keys for notification pages */
65 FormattedNotify::NETWORK => 'w',
66 FormattedNotify::SYSTEM => 'y',
67 FormattedNotify::HOME => 'h',
68 FormattedNotify::PERSONAL => 'r',
69 FormattedNotify::INTRO => 'i',
72 /** @var int The default count of items per page */
73 const ITEMS_PER_PAGE = 20;
74 /** @var int The default limit of notifications per page */
75 const DEFAULT_PAGE_LIMIT = 80;
77 /** @var boolean True, if ALL entries should get shown */
79 /** @var int The determined start item of the current page */
80 protected $firstItemNum;
86 * Collects all notifications from the backend
88 * @return array The determined notification array
89 * ['header', 'notifications']
91 abstract public function getNotifications();
93 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, array $server, array $parameters = [])
95 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
97 if (!$session->getLocalUserId()) {
98 throw new ForbiddenException($this->t('Permission denied.'));
101 $page = ($_REQUEST['page'] ?? 0) ?: 1;
103 $this->firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
104 $this->showAll = ($_REQUEST['show'] ?? '') === 'all';
107 protected function rawContent(array $request = [])
109 // If the last argument of the query is NOT json, return
110 if ($this->args->get($this->args->getArgc() - 1) !== 'json') {
115 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
117 // Add additional informations (needed for json output)
119 'notifications' => $this->getNotifications(),
120 'items_page' => $pager->getItemsPerPage(),
121 'page' => $pager->getPage(),
124 System::jsonExit($notifications);
128 * Shows the printable result of notifications for a specific tab
130 * @param string $header The notification header
131 * @param array $notifications The array with the notifications
132 * @param string $noContent The string in case there are no notifications
133 * @param array $showLink The possible links at the top
135 * @return string The rendered output
137 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
139 protected function printContent(string $header, array $notifications, string $noContent, array $showLink)
141 // Get the nav tabs for the notification pages
142 $tabs = $this->getTabs();
145 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
147 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
148 return Renderer::replaceMacros($notif_tpl, [
149 '$header' => $header ?? $this->t('Notifications'),
151 '$notifications' => $notifications,
152 '$noContent' => $noContent,
153 '$showLink' => $showLink,
154 '$paginate' => $pager->renderMinimal(count($notifications))
159 * List of pages for the Notifications TabBar
161 * @return array with with notifications TabBar data
164 private function getTabs()
166 $selected = $this->args->get(1, '');
170 foreach (self::URL_TYPES as $type => $url) {
172 'label' => $this->t(self::PRINT_TYPES[$type]),
173 'url' => 'notifications/' . $url,
174 'sel' => (($selected == $url) ? 'active' : ''),
175 'id' => $type . '-tab',
176 'accesskey' => self::ACCESS_KEYS[$type],