3 * @copyright Copyright (C) 2010-2021, 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;
25 use Friendica\App\Arguments;
26 use Friendica\BaseModule;
27 use Friendica\Content\Pager;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\System;
32 use Friendica\Navigation\Notifications\ValueObject\FormattedNotification;
33 use Friendica\Network\HTTPException\ForbiddenException;
36 * Base Module for each tab of the notification display
38 * General possibility to print it as JSON as well
40 abstract class BaseNotifications extends BaseModule
42 /** @var array Array of URL parameters */
44 FormattedNotification::NETWORK => 'network',
45 FormattedNotification::SYSTEM => 'system',
46 FormattedNotification::HOME => 'home',
47 FormattedNotification::PERSONAL => 'personal',
48 FormattedNotification::INTRO => 'intros',
51 /** @var array Array of the allowed notifications and their printable name */
53 FormattedNotification::NETWORK => 'Network',
54 FormattedNotification::SYSTEM => 'System',
55 FormattedNotification::HOME => 'Home',
56 FormattedNotification::PERSONAL => 'Personal',
57 FormattedNotification::INTRO => 'Introductions',
60 /** @var array The array of access keys for notification pages */
62 FormattedNotification::NETWORK => 'w',
63 FormattedNotification::SYSTEM => 'y',
64 FormattedNotification::HOME => 'h',
65 FormattedNotification::PERSONAL => 'r',
66 FormattedNotification::INTRO => 'i',
69 /** @var int The default count of items per page */
70 const ITEMS_PER_PAGE = 20;
71 /** @var int The default limit of notifications per page */
72 const DEFAULT_PAGE_LIMIT = 80;
74 /** @var boolean True, if ALL entries should get shown */
76 /** @var int The determined start item of the current page */
77 protected $firstItemNum;
83 * Collects all notifications from the backend
85 * @return array The determined notification array
86 * ['header', 'notifications']
88 abstract public function getNotifications();
90 public function __construct(Arguments $args, L10n $l10n, array $parameters = [])
92 parent::__construct($l10n, $parameters);
95 throw new ForbiddenException($this->l10n->t('Permission denied.'));
98 $page = ($_REQUEST['page'] ?? 0) ?: 1;
100 $this->firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
101 $this->showAll = ($_REQUEST['show'] ?? '') === 'all';
106 public function rawContent()
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 = self::getTabs();
144 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
146 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
147 return Renderer::replaceMacros($notif_tpl, [
148 '$header' => $header ?? DI::l10n()->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 static function getTabs()
165 $selected = DI::args()->get(1, '');
169 foreach (self::URL_TYPES as $type => $url) {
171 'label' => DI::l10n()->t(self::PRINT_TYPES[$type]),
172 'url' => 'notifications/' . $url,
173 'sel' => (($selected == $url) ? 'active' : ''),
174 'id' => $type . '-tab',
175 'accesskey' => self::ACCESS_KEYS[$type],