]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseNotifications.php
Merge pull request #8175 from MrPetovan/task/revert-profile-default-tab
[friendica.git] / src / Module / BaseNotifications.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Exception;
6 use Friendica\BaseModule;
7 use Friendica\Content\Pager;
8 use Friendica\Core\Renderer;
9 use Friendica\Core\System;
10 use Friendica\DI;
11 use Friendica\Network\HTTPException\ForbiddenException;
12 use Friendica\Object\Notification\Notification;
13
14 /**
15  * Base Module for each tab of the notification display
16  *
17  * General possibility to print it as JSON as well
18  */
19 abstract class BaseNotifications extends BaseModule
20 {
21         /** @var array Array of URL parameters */
22         const URL_TYPES = [
23                 Notification::NETWORK  => 'network',
24                 Notification::SYSTEM   => 'system',
25                 Notification::HOME     => 'home',
26                 Notification::PERSONAL => 'personal',
27                 Notification::INTRO    => 'intros',
28         ];
29
30         /** @var array Array of the allowed notifications and their printable name */
31         const PRINT_TYPES = [
32                 Notification::NETWORK  => 'Network',
33                 Notification::SYSTEM   => 'System',
34                 Notification::HOME     => 'Home',
35                 Notification::PERSONAL => 'Personal',
36                 Notification::INTRO    => 'Introductions',
37         ];
38
39         /** @var array The array of access keys for notification pages */
40         const ACCESS_KEYS = [
41                 Notification::NETWORK  => 'w',
42                 Notification::SYSTEM   => 'y',
43                 Notification::HOME     => 'h',
44                 Notification::PERSONAL => 'r',
45                 Notification::INTRO    => 'i',
46         ];
47
48         /** @var int The default count of items per page */
49         const ITEMS_PER_PAGE = 20;
50         /** @var int The default limit of notifications per page */
51         const DEFAULT_PAGE_LIMIT = 80;
52
53         /** @var boolean True, if ALL entries should get shown */
54         protected static $showAll;
55         /** @var int The determined start item of the current page */
56         protected static $firstItemNum;
57
58         /**
59          * Collects all notifications from the backend
60          *
61          * @return array The determined notification array
62          *               ['header', 'notifications']
63          */
64         abstract public static function getNotifications();
65
66         public static function init(array $parameters = [])
67         {
68                 if (!local_user()) {
69                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
70                 }
71
72                 $page = ($_REQUEST['page'] ?? 0) ?: 1;
73
74                 self::$firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
75                 self::$showAll      = ($_REQUEST['show'] ?? '') === 'all';
76         }
77
78         public static function post(array $parameters = [])
79         {
80                 $request_id = DI::args()->get(1);
81
82                 if ($request_id === 'all') {
83                         return;
84                 }
85
86                 if ($request_id) {
87                         $intro = DI::intro()->selectFirst(['id' => $request_id, 'uid' => local_user()]);
88
89                         switch ($_POST['submit']) {
90                                 case DI::l10n()->t('Discard'):
91                                         $intro->discard();
92                                         break;
93                                 case DI::l10n()->t('Ignore'):
94                                         $intro->ignore();
95                                         break;
96                         }
97
98                         DI::baseUrl()->redirect('notifications/intros');
99                 }
100         }
101
102         public static function rawContent(array $parameters = [])
103         {
104                 // If the last argument of the query is NOT json, return
105                 if (DI::args()->get(DI::args()->getArgc() - 1) !== 'json') {
106                         return;
107                 }
108
109                 // Set the pager
110                 $pager = new Pager(DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
111
112                 // Add additional informations (needed for json output)
113                 $notifications = [
114                         'notifications' => static::getNotifications(),
115                         'items_page'    => $pager->getItemsPerPage(),
116                         'page'          => $pager->getPage(),
117                 ];
118
119                 System::jsonExit($notifications);
120         }
121
122         /**
123          * Shows the printable result of notifications for a specific tab
124          *
125          * @param string $header        The notification header
126          * @param array  $notifications The array with the notifications
127          * @param string $noContent     The string in case there are no notifications
128          * @param array  $showLink      The possible links at the top
129          *
130          * @return string The rendered output
131          *
132          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
133          */
134         protected static function printContent(string $header, array $notifications, string $noContent, array $showLink)
135         {
136                 // Get the nav tabs for the notification pages
137                 $tabs = self::getTabs();
138
139                 // Set the pager
140                 $pager = new Pager(DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
141
142                 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
143                 return Renderer::replaceMacros($notif_tpl, [
144                         '$header'        => $header ?? DI::l10n()->t('Notifications'),
145                         '$tabs'          => $tabs,
146                         '$notifications' => $notifications,
147                         '$noContent'     => $noContent,
148                         '$showLink'      => $showLink,
149                         '$paginate'      => $pager->renderMinimal(count($notifications))
150                 ]);
151         }
152
153         /**
154          * List of pages for the Notifications TabBar
155          *
156          * @return array with with notifications TabBar data
157          * @throws Exception
158          */
159         private static function getTabs()
160         {
161                 $selected = DI::args()->get(1, '');
162
163                 $tabs = [];
164
165                 foreach (self::URL_TYPES as $type => $url) {
166                         $tabs[] = [
167                                 'label'     => DI::l10n()->t(self::PRINT_TYPES[$type]),
168                                 'url'       => 'notifications/' . $url,
169                                 'sel'       => (($selected == $url) ? 'active' : ''),
170                                 'id'        => $type . '-tab',
171                                 'accesskey' => self::ACCESS_KEYS[$type],
172                         ];
173                 }
174
175                 return $tabs;
176         }
177 }