3 * @copyright Copyright (C) 2020, Friendica
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;
25 use Friendica\BaseModule;
26 use Friendica\Content\Pager;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\System;
30 use Friendica\Network\HTTPException\ForbiddenException;
31 use Friendica\Object\Notification\Notification;
34 * Base Module for each tab of the notification display
36 * General possibility to print it as JSON as well
38 abstract class BaseNotifications extends BaseModule
40 /** @var array Array of URL parameters */
42 Notification::NETWORK => 'network',
43 Notification::SYSTEM => 'system',
44 Notification::HOME => 'home',
45 Notification::PERSONAL => 'personal',
46 Notification::INTRO => 'intros',
49 /** @var array Array of the allowed notifications and their printable name */
51 Notification::NETWORK => 'Network',
52 Notification::SYSTEM => 'System',
53 Notification::HOME => 'Home',
54 Notification::PERSONAL => 'Personal',
55 Notification::INTRO => 'Introductions',
58 /** @var array The array of access keys for notification pages */
60 Notification::NETWORK => 'w',
61 Notification::SYSTEM => 'y',
62 Notification::HOME => 'h',
63 Notification::PERSONAL => 'r',
64 Notification::INTRO => 'i',
67 /** @var int The default count of items per page */
68 const ITEMS_PER_PAGE = 20;
69 /** @var int The default limit of notifications per page */
70 const DEFAULT_PAGE_LIMIT = 80;
72 /** @var boolean True, if ALL entries should get shown */
73 protected static $showAll;
74 /** @var int The determined start item of the current page */
75 protected static $firstItemNum;
78 * Collects all notifications from the backend
80 * @return array The determined notification array
81 * ['header', 'notifications']
83 abstract public static function getNotifications();
85 public static function init(array $parameters = [])
88 throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
91 $page = ($_REQUEST['page'] ?? 0) ?: 1;
93 self::$firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
94 self::$showAll = ($_REQUEST['show'] ?? '') === 'all';
97 public static function rawContent(array $parameters = [])
99 // If the last argument of the query is NOT json, return
100 if (DI::args()->get(DI::args()->getArgc() - 1) !== 'json') {
105 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
107 // Add additional informations (needed for json output)
109 'notifications' => static::getNotifications(),
110 'items_page' => $pager->getItemsPerPage(),
111 'page' => $pager->getPage(),
114 System::jsonExit($notifications);
118 * Shows the printable result of notifications for a specific tab
120 * @param string $header The notification header
121 * @param array $notifications The array with the notifications
122 * @param string $noContent The string in case there are no notifications
123 * @param array $showLink The possible links at the top
125 * @return string The rendered output
127 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
129 protected static function printContent(string $header, array $notifications, string $noContent, array $showLink)
131 // Get the nav tabs for the notification pages
132 $tabs = self::getTabs();
135 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
137 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
138 return Renderer::replaceMacros($notif_tpl, [
139 '$header' => $header ?? DI::l10n()->t('Notifications'),
141 '$notifications' => $notifications,
142 '$noContent' => $noContent,
143 '$showLink' => $showLink,
144 '$paginate' => $pager->renderMinimal(count($notifications))
149 * List of pages for the Notifications TabBar
151 * @return array with with notifications TabBar data
154 private static function getTabs()
156 $selected = DI::args()->get(1, '');
160 foreach (self::URL_TYPES as $type => $url) {
162 'label' => DI::l10n()->t(self::PRINT_TYPES[$type]),
163 'url' => 'notifications/' . $url,
164 'sel' => (($selected == $url) ? 'active' : ''),
165 'id' => $type . '-tab',
166 'accesskey' => self::ACCESS_KEYS[$type],