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