]> git.mxchange.org Git - friendica.git/blob - src/Module/Notifications/Introductions.php
Merge pull request #9304 from annando/suggestions
[friendica.git] / src / Module / Notifications / Introductions.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Notifications;
23
24 use Friendica\Content\ContactSelector;
25 use Friendica\Content\Nav;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Module\BaseNotifications;
31 use Friendica\Object\Notification\Introduction;
32
33 /**
34  * Prints notifications about introduction
35  */
36 class Introductions extends BaseNotifications
37 {
38         /**
39          * @inheritDoc
40          */
41         public static function getNotifications()
42         {
43                 $id  = (int)DI::args()->get(2, 0);
44                 $all = DI::args()->get(2) == 'all';
45
46                 $notifications = [
47                         'ident'         => 'introductions',
48                         'notifications' => DI::notificationIntro()->getList($all, self::$firstItemNum, self::ITEMS_PER_PAGE, $id),
49                 ];
50
51                 return [
52                         'header'        => DI::l10n()->t('Notifications'),
53                         'notifications' => $notifications,
54                 ];
55         }
56
57         public static function content(array $parameters = [])
58         {
59                 Nav::setSelected('introductions');
60
61                 $all = DI::args()->get(2) == 'all';
62
63                 $notificationContent   = [];
64                 $notificationNoContent = '';
65
66                 $notificationResult = self::getNotifications();
67                 $notifications      = $notificationResult['notifications'] ?? [];
68                 $notificationHeader = $notificationResult['header'] ?? '';
69
70                 $notificationSuggestions = Renderer::getMarkupTemplate('notifications/suggestions.tpl');
71                 $notificationTemplate    = Renderer::getMarkupTemplate('notifications/intros.tpl');
72
73                 // The link to switch between ignored and normal connection requests
74                 $notificationShowLink = [
75                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros'),
76                         'text' => (!$all ? DI::l10n()->t('Show Ignored Requests') : DI::l10n()->t('Hide Ignored Requests')),
77                 ];
78
79                 // Loop through all introduction notifications.This creates an array with the output html for each
80                 // introduction
81                 /** @var Introduction $notification */
82                 foreach ($notifications['notifications'] as $notification) {
83
84                         $helptext  = DI::l10n()->t('Shall your connection be bidirectional or not?');
85                         $helptext2 = DI::l10n()->t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notification->getName(), $notification->getName());
86                         $helptext3 = DI::l10n()->t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notification->getName());
87
88                         $friend = ['duplex', DI::l10n()->t('Friend'), '1', $helptext2, true];
89                         $follower = ['duplex', DI::l10n()->t('Subscriber'), '0', $helptext3, false];
90
91                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
92                         // We have to distinguish between these two because they use different data.
93                         switch ($notification->getLabel()) {
94                                 case 'friend_suggestion':
95                                         $notificationContent[] = Renderer::replaceMacros($notificationSuggestions, [
96                                                 '$type'                  => $notification->getLabel(),
97                                                 '$str_notification_type' => DI::l10n()->t('Notification type:'),
98                                                 '$str_type'              => $notification->getType(),
99                                                 '$intro_id'              => $notification->getIntroId(),
100                                                 '$lbl_madeby'            => DI::l10n()->t('Suggested by:'),
101                                                 '$madeby'                => $notification->getMadeBy(),
102                                                 '$madeby_url'            => $notification->getMadeByUrl(),
103                                                 '$madeby_zrl'            => $notification->getMadeByZrl(),
104                                                 '$madeby_addr'           => $notification->getMadeByAddr(),
105                                                 '$contact_id'            => $notification->getContactId(),
106                                                 '$photo'                 => $notification->getPhoto(),
107                                                 '$fullname'              => $notification->getName(),
108                                                 '$lbl_connection_type'   => $helptext,
109                                                 '$friend'                => $friend,
110                                                 '$follower'              => $follower,
111                                                 '$url'                   => $notification->getUrl(),
112                                                 '$zrl'                   => $notification->getZrl(),
113                                                 '$lbl_url'               => DI::l10n()->t('Profile URL'),
114                                                 '$addr'                  => $notification->getAddr(),
115                                                 '$hidden'                => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''],
116                                                 '$knowyou'               => $notification->getKnowYou(),
117                                                 '$approve'               => DI::l10n()->t('Approve'),
118                                                 '$note'                  => $notification->getNote(),
119                                                 '$request'               => $notification->getRequest(),
120                                                 '$ignore'                => DI::l10n()->t('Ignore'),
121                                                 '$discard'               => DI::l10n()->t('Discard'),
122                                         ]);
123                                         break;
124
125                                 // Normal connection requests
126                                 default:
127                                         if ($notification->getNetwork() === Protocol::DFRN) {
128                                                 $lbl_knowyou = DI::l10n()->t('Claims to be known to you: ');
129                                                 $knowyou     = ($notification->getKnowYou() ? DI::l10n()->t('Yes') : DI::l10n()->t('No'));
130                                         } else {
131                                                 $lbl_knowyou = '';
132                                                 $knowyou = '';
133                                         }
134
135                                         $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notification->getContactId()]);
136
137                                         if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
138                                                 $action = 'follow_confirm';
139                                         } else {
140                                                 $action = 'dfrn_confirm';
141                                         }
142
143                                         $header = $notification->getName();
144
145                                         if ($notification->getAddr() != '') {
146                                                 $header .= ' <' . $notification->getAddr() . '>';
147                                         }
148
149                                         $header .= ' (' . ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()) . ')';
150
151                                         if ($notification->getNetwork() != Protocol::DIASPORA) {
152                                                 $discard = DI::l10n()->t('Discard');
153                                         } else {
154                                                 $discard = '';
155                                         }
156
157                                         $notificationContent[] = Renderer::replaceMacros($notificationTemplate, [
158                                                 '$type'                  => $notification->getLabel(),
159                                                 '$header'                => $header,
160                                                 '$str_notification_type' => DI::l10n()->t('Notification type:'),
161                                                 '$str_type'              => $notification->getType(),
162                                                 '$dfrn_id'               => $notification->getDfrnId(),
163                                                 '$uid'                   => $notification->getUid(),
164                                                 '$intro_id'              => $notification->getIntroId(),
165                                                 '$contact_id'            => $notification->getContactId(),
166                                                 '$photo'                 => $notification->getPhoto(),
167                                                 '$fullname'              => $notification->getName(),
168                                                 '$location'              => $notification->getLocation(),
169                                                 '$lbl_location'          => DI::l10n()->t('Location:'),
170                                                 '$about'                 => $notification->getAbout(),
171                                                 '$lbl_about'             => DI::l10n()->t('About:'),
172                                                 '$keywords'              => $notification->getKeywords(),
173                                                 '$lbl_keywords'          => DI::l10n()->t('Tags:'),
174                                                 '$hidden'                => ['hidden', DI::l10n()->t('Hide this contact from others'), $notification->isHidden(), ''],
175                                                 '$lbl_connection_type'   => $helptext,
176                                                 '$friend'                => $friend,
177                                                 '$follower'              => $follower,
178                                                 '$url'                   => $notification->getUrl(),
179                                                 '$zrl'                   => $notification->getZrl(),
180                                                 '$lbl_url'               => DI::l10n()->t('Profile URL'),
181                                                 '$addr'                  => $notification->getAddr(),
182                                                 '$lbl_knowyou'           => $lbl_knowyou,
183                                                 '$lbl_network'           => DI::l10n()->t('Network:'),
184                                                 '$network'               => ContactSelector::networkToName($notification->getNetwork(), $notification->getUrl()),
185                                                 '$knowyou'               => $knowyou,
186                                                 '$approve'               => DI::l10n()->t('Approve'),
187                                                 '$note'                  => $notification->getNote(),
188                                                 '$ignore'                => DI::l10n()->t('Ignore'),
189                                                 '$discard'               => $discard,
190                                                 '$action'                => $action,
191                                         ]);
192                                         break;
193                         }
194                 }
195
196                 if (count($notifications['notifications']) == 0) {
197                         notice(DI::l10n()->t('No introductions.'));
198                         $notificationNoContent = DI::l10n()->t('No more %s notifications.', $notifications['ident']);
199                 }
200
201                 return self::printContent($notificationHeader, $notificationContent, $notificationNoContent, $notificationShowLink);
202         }
203 }