]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseNotifications.php
a0c49a06460fdd5fac4e94fcebca6baa44dc8bba
[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\Model\Notification;
12 use Friendica\Network\HTTPException\ForbiddenException;
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
51         /** @var boolean True, if ALL entries should get shown */
52         protected static $showAll;
53         /** @var int The determined start item of the current page */
54         protected static $firstItemNum;
55
56         /**
57          * Collects all notifications from the backend
58          *
59          * @return array The determined notification array
60          *               ['header', 'notifications']
61          */
62         abstract public static function getNotifications();
63
64         public static function init(array $parameters = [])
65         {
66                 if (!local_user()) {
67                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
68                 }
69
70                 $page = ($_REQUEST['page'] ?? 0) ?: 1;
71
72                 self::$firstItemNum = ($page * self::ITEMS_PER_PAGE) - self::ITEMS_PER_PAGE;
73                 self::$showAll      = ($_REQUEST['show'] ?? '') === 'all';
74         }
75
76         public static function post(array $parameters = [])
77         {
78                 $request_id = DI::args()->get(1);
79
80                 if ($request_id === 'all') {
81                         return;
82                 }
83
84                 if ($request_id) {
85                         $intro = DI::intro()->selectFirst(['id' => $request_id, 'uid' => local_user()]);
86
87                         switch ($_POST['submit']) {
88                                 case DI::l10n()->t('Discard'):
89                                         $intro->discard();
90                                         break;
91                                 case DI::l10n()->t('Ignore'):
92                                         $intro->ignore();
93                                         break;
94                         }
95
96                         DI::baseUrl()->redirect('notifications/intros');
97                 }
98         }
99
100         public static function rawContent(array $parameters = [])
101         {
102                 // If the last argument of the query is NOT json, return
103                 if (DI::args()->get(DI::args()->getArgc() - 1) !== 'json') {
104                         return;
105                 }
106
107                 System::jsonExit(static::getNotifications()['notifs'] ?? []);
108         }
109
110         /**
111          * Shows the printable result of notifications for a specific tab
112          *
113          * @param string $header    The notification header
114          * @param array  $content   The array with the notifications
115          * @param string $noContent The string in case there are no notifications
116          * @param array  $showLink  The possible links at the top
117          *
118          * @return string The rendered output
119          *
120          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
121          */
122         protected static function printContent(string $header, array $content, string $noContent, array $showLink)
123         {
124                 // Get the nav tabs for the notification pages
125                 $tabs = self::getTabs();
126
127                 // Set the pager
128                 $pager = new Pager(DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
129
130                 $notif_tpl = Renderer::getMarkupTemplate('notifications/notifications.tpl');
131                 return Renderer::replaceMacros($notif_tpl, [
132                         '$notif_header'    => $header ?? DI::l10n()->t('Notifications'),
133                         '$tabs'            => $tabs,
134                         '$notif_content'   => $content,
135                         '$notif_nocontent' => $noContent,
136                         '$notif_show_lnk'  => $showLink,
137                         '$notif_paginate'  => $pager->renderMinimal(count($content))
138                 ]);
139         }
140
141         /**
142          * List of pages for the Notifications TabBar
143          *
144          * @return array with with notifications TabBar data
145          * @throws Exception
146          */
147         private static function getTabs()
148         {
149                 $selected = DI::args()->get(1, '');
150
151                 $tabs = [];
152
153                 foreach (self::URL_TYPES as $type => $url) {
154                         $tabs[] = [
155                                 'label'     => DI::l10n()->t(self::PRINT_TYPES[$type]),
156                                 'url'       => 'notifications/' . $url,
157                                 'sel'       => (($selected == $url) ? 'active' : ''),
158                                 'id'        => $type . '-tab',
159                                 'accesskey' => self::ACCESS_KEYS[$type],
160                         ];
161                 }
162
163                 return $tabs;
164         }
165 }