]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Notifications/Notifications.php
Allow the search for contacts on blocked servers via web
[friendica.git] / src / Module / Notifications / Notifications.php
index 50995645c38537f05dd99451122574ceee5e8e04..7852f81c2430e613ebb9cba837cf1e5ddd04ff59 100644 (file)
@@ -1,12 +1,37 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module\Notifications;
 
+use Friendica\App;
+use Friendica\App\Arguments;
 use Friendica\Content\Nav;
-use Friendica\Content\Pager;
+use Friendica\Core\L10n;
 use Friendica\Core\Renderer;
-use Friendica\DI;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\Module\BaseNotifications;
+use Friendica\Module\Response;
+use Friendica\Navigation\Notifications\ValueObject\FormattedNotify;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 /**
  * Prints all notification types except introduction:
@@ -17,102 +42,104 @@ use Friendica\Module\BaseNotifications;
  */
 class Notifications extends BaseNotifications
 {
+       /** @var \Friendica\Navigation\Notifications\Factory\FormattedNotify */
+       protected $formattedNotifyFactory;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, \Friendica\Navigation\Notifications\Factory\FormattedNotify $formattedNotifyFactory, IHandleUserSessions $userSession, array $server, array $parameters = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $userSession, $server, $parameters);
+
+               $this->formattedNotifyFactory = $formattedNotifyFactory;
+       }
+
        /**
         * {@inheritDoc}
         */
-       public static function getNotifies()
+       public function getNotifications()
        {
-               $nm = DI::notify();
-
-               $notif_header = '';
-
-               // Get the network notifications
-               if ((DI::args()->get(1) == 'network')) {
-                       $notif_header = DI::l10n()->t('Network Notifications');
-                       $notifs       = $nm->getNetworkList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE);
-
-                       // Get the system notifications
-               } elseif ((DI::args()->get(1) == 'system')) {
-                       $notif_header = DI::l10n()->t('System Notifications');
-                       $notifs       = $nm->getSystemList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE);
-
-                       // Get the personal notifications
-               } elseif ((DI::args()->get(1) == 'personal')) {
-                       $notif_header = DI::l10n()->t('Personal Notifications');
-                       $notifs       = $nm->getPersonalList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE);
-
-                       // Get the home notifications
-               } elseif ((DI::args()->get(1) == 'home')) {
-                       $notif_header = DI::l10n()->t('Home Notifications');
-                       $notifs       = $nm->getHomeList(self::$showAll, self::$firstItemNum, self::ITEMS_PER_PAGE);
-                       // fallback - redirect to main page
+               $notificationHeader = '';
+               $notifications = [];
+
+               $factory = $this->formattedNotifyFactory;
+
+               if (($this->args->get(1) == 'network')) {
+                       $notificationHeader = $this->t('Network Notifications');
+                       $notifications      = [
+                               'ident'        => FormattedNotify::NETWORK,
+                               'notifications' => $factory->getNetworkList($this->showAll, $this->firstItemNum, self::ITEMS_PER_PAGE),
+                       ];
+               } elseif (($this->args->get(1) == 'system')) {
+                       $notificationHeader = $this->t('System Notifications');
+                       $notifications      = [
+                               'ident'        => FormattedNotify::SYSTEM,
+                               'notifications' => $factory->getSystemList($this->showAll, $this->firstItemNum, self::ITEMS_PER_PAGE),
+                       ];
+               } elseif (($this->args->get(1) == 'personal')) {
+                       $notificationHeader = $this->t('Personal Notifications');
+                       $notifications      = [
+                               'ident'        => FormattedNotify::PERSONAL,
+                               'notifications' => $factory->getPersonalList($this->showAll, $this->firstItemNum, self::ITEMS_PER_PAGE),
+                       ];
+               } elseif (($this->args->get(1) == 'home')) {
+                       $notificationHeader = $this->t('Home Notifications');
+                       $notifications      = [
+                               'ident'        => FormattedNotify::HOME,
+                               'notifications' => $factory->getHomeList($this->showAll, $this->firstItemNum, self::ITEMS_PER_PAGE),
+                       ];
                } else {
-                       DI::baseUrl()->redirect('notifications');
+                       $this->baseUrl->redirect('notifications');
                }
 
-               // Set the pager
-               $pager = new Pager(DI::args()->getQueryString(), self::ITEMS_PER_PAGE);
-
-               // Add additional informations (needed for json output)
-               $notifs['items_page'] = $pager->getItemsPerPage();
-               $notifs['page']       = $pager->getPage();
-
                return [
-                       'header' => $notif_header,
-                       'notifs' => $notifs,
+                       'header'        => $notificationHeader,
+                       'notifications' => $notifications,
                ];
        }
 
-       public static function content(array $parameters = [])
+       protected function content(array $request = []): string
        {
                Nav::setSelected('notifications');
 
-               $notif_content   = [];
-               $notif_nocontent = '';
-
-               $notif_result = self::getNotifies();
-               $notifs       = $notif_result['notifs'] ?? [];
-               $notif_header = $notif_result['header'] ?? '';
-
-
-               if (!empty($notifs['notifications'])) {
+               $notificationContent   = [];
+               $notificationNoContent = '';
+
+               $notificationResult = $this->getNotifications();
+               $notifications      = $notificationResult['notifications'] ?? [];
+               $notificationHeader = $notificationResult['header'] ?? '';
+
+               if (!empty($notifications['notifications'])) {
+                       $notificationTemplates = [
+                               'like'         => 'notifications/likes_item.tpl',
+                               'dislike'      => 'notifications/dislikes_item.tpl',
+                               'attend'       => 'notifications/attend_item.tpl',
+                               'attendno'     => 'notifications/attend_item.tpl',
+                               'attendmaybe'  => 'notifications/attend_item.tpl',
+                               'friend'       => 'notifications/friends_item.tpl',
+                               'comment'      => 'notifications/comments_item.tpl',
+                               'post'         => 'notifications/posts_item.tpl',
+                               'notification' => 'notifications/notification.tpl',
+                       ];
                        // Loop trough ever notification This creates an array with the output html for each
                        // notification and apply the correct template according to the notificationtype (label).
-                       foreach ($notifs['notifications'] as $notif) {
-                               $notification_templates = [
-                                       'like'        => 'notifications_likes_item.tpl',
-                                       'dislike'     => 'notifications_dislikes_item.tpl',
-                                       'attend'      => 'notifications_attend_item.tpl',
-                                       'attendno'    => 'notifications_attend_item.tpl',
-                                       'attendmaybe' => 'notifications_attend_item.tpl',
-                                       'friend'      => 'notifications_friends_item.tpl',
-                                       'comment'     => 'notifications_comments_item.tpl',
-                                       'post'        => 'notifications_posts_item.tpl',
-                                       'notify'      => 'notify.tpl',
-                               ];
-
-                               $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
-
-                               $notif_content[] = Renderer::replaceMacros($tpl_notif, [
-                                       '$item_label' => $notif['label'],
-                                       '$item_link'  => $notif['link'],
-                                       '$item_image' => $notif['image'],
-                                       '$item_url'   => $notif['url'],
-                                       '$item_text'  => $notif['text'],
-                                       '$item_when'  => $notif['when'],
-                                       '$item_ago'   => $notif['ago'],
-                                       '$item_seen'  => $notif['seen'],
+                       /** @var FormattedNotify $Notification */
+                       foreach ($notifications['notifications'] as $Notification) {
+                               $notificationArray = $Notification->toArray();
+
+                               $notificationTemplate = Renderer::getMarkupTemplate($notificationTemplates[$notificationArray['label']]);
+
+                               $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
+                                       '$notification' => $notificationArray
                                ]);
                        }
                } else {
-                       $notif_nocontent = DI::l10n()->t('No more %s notifications.', $notifs['ident']);
+                       $notificationNoContent = $this->t('No more %s notifications.', $notificationResult['ident']);
                }
 
-               $notif_show_lnk = [
-                       'href' => (self::$showAll ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all'),
-                       'text' => (self::$showAll ? DI::l10n()->t('Show unread') : DI::l10n()->t('Show all')),
+               $notificationShowLink = [
+                       'href' => ($this->showAll ? 'notifications/' . $notifications['ident'] : 'notifications/' . $notifications['ident'] . '?show=all'),
+                       'text' => ($this->showAll ? $this->t('Show unread') : $this->t('Show all')),
                ];
 
-               return self::printContent($notif_header, $notif_content, $notif_nocontent, $notif_show_lnk);
+               return $this->printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
        }
 }