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;
31 use Friendica\Navigation\Notifications\ValueObject\FormattedNotification;
32 use Friendica\Network\HTTPException\ForbiddenException;
35 * Base Module for each tab of the notification display
37 * General possibility to print it as JSON as well
39 abstract class BaseNotifications extends BaseModule
41 /** @var array Array of URL parameters */
43 FormattedNotification::NETWORK => 'network',
44 FormattedNotification::SYSTEM => 'system',
45 FormattedNotification::HOME => 'home',
46 FormattedNotification::PERSONAL => 'personal',
47 FormattedNotification::INTRO => 'intros',
50 /** @var array Array of the allowed notifications and their printable name */
52 FormattedNotification::NETWORK => 'Network',
53 FormattedNotification::SYSTEM => 'System',
54 FormattedNotification::HOME => 'Home',
55 FormattedNotification::PERSONAL => 'Personal',
56 FormattedNotification::INTRO => 'Introductions',
59 /** @var array The array of access keys for notification pages */
61 FormattedNotification::NETWORK => 'w',
62 FormattedNotification::SYSTEM => 'y',
63 FormattedNotification::HOME => 'h',
64 FormattedNotification::PERSONAL => 'r',
65 FormattedNotification::INTRO => 'i',
68 /** @var int The default count of items per page */
69 const ITEMS_PER_PAGE = 20;
70 /** @var int The default limit of notifications per page */
71 const DEFAULT_PAGE_LIMIT = 80;
73 /** @var boolean True, if ALL entries should get shown */
75 /** @var int The determined start item of the current page */
76 protected $firstItemNum;
82 * Collects all notifications from the backend
84 * @return array The determined notification array
85 * ['header', 'notifications']
87 abstract public function getNotifications();
89 public function __construct(Arguments $args, L10n $l10n, array $parameters = [])
91 parent::__construct($l10n, $parameters);
94 throw new ForbiddenException($this->l10n->t('Permission denied.'));
97 $page = ($_REQUEST['page'] ?? 0) ?: 1;
99 $this->firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
100 $this->showAll = ($_REQUEST['show'] ?? '') === 'all';
105 public function rawContent()
107 // If the last argument of the query is NOT json, return
108 if ($this->args->get($this->args->getArgc() - 1) !== 'json') {
113 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
115 // Add additional informations (needed for json output)
117 'notifications' => $this->getNotifications(),
118 'items_page' => $pager->getItemsPerPage(),
119 'page' => $pager->getPage(),
122 System::jsonExit($notifications);
126 * Shows the printable result of notifications for a specific tab
128 * @param string $header The notification header
129 * @param array $notifications The array with the notifications
130 * @param string $noContent The string in case there are no notifications
131 * @param array $showLink The possible links at the top
133 * @return string The rendered output
135 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
137 protected function printContent(string $header, array $notifications, string $noContent, array $showLink)
139 // Get the nav tabs for the notification pages
140 $tabs = $this->getTabs();
143 $pager = new Pager($this->l10n, $this->args->getQueryString(), self::ITEMS_PER_PAGE);
145 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
146 return Renderer::replaceMacros($notif_tpl, [
147 '$header' => $header ?? $this->l10n->t('Notifications'),
149 '$notifications' => $notifications,
150 '$noContent' => $noContent,
151 '$showLink' => $showLink,
152 '$paginate' => $pager->renderMinimal(count($notifications))
157 * List of pages for the Notifications TabBar
159 * @return array with with notifications TabBar data
162 private function getTabs()
164 $selected = $this->args->get(1, '');
168 foreach (self::URL_TYPES as $type => $url) {
170 'label' => $this->l10n->t(self::PRINT_TYPES[$type]),
171 'url' => 'notifications/' . $url,
172 'sel' => (($selected == $url) ? 'active' : ''),
173 'id' => $type . '-tab',
174 'accesskey' => self::ACCESS_KEYS[$type],