]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
Class file relocations
[friendica.git] / mod / notifications.php
1 <?php
2
3 /**
4  * @file mod/notifications.php
5  * @brief The notifications module
6  */
7
8 use Friendica\App;
9 use Friendica\Core\NotificationsManager;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12
13 require_once("include/contact_selectors.php");
14 require_once("include/network.php");
15
16 function notifications_post(App $a) {
17
18         if (! local_user()) {
19                 goaway(System::baseUrl());
20         }
21
22         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
23
24         if($request_id === "all")
25                 return;
26
27         if($request_id) {
28
29                 $r = q("SELECT * FROM `intro` WHERE `id` = %d  AND `uid` = %d LIMIT 1",
30                         intval($request_id),
31                         intval(local_user())
32                 );
33
34                 if (DBM::is_result($r)) {
35                         $intro_id = $r[0]['id'];
36                         $contact_id = $r[0]['contact-id'];
37                 }
38                 else {
39                         notice( t('Invalid request identifier.') . EOL);
40                         return;
41                 }
42
43                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
44                 // that should not be deleted.
45
46                 $fid = $r[0]['fid'];
47
48                 if($_POST['submit'] == t('Discard')) {
49                         $r = q("DELETE FROM `intro` WHERE `id` = %d",
50                                 intval($intro_id)
51                         );
52                         if(! $fid) {
53
54                                 // The check for blocked and pending is in case the friendship was already approved
55                                 // and we just want to get rid of the now pointless notification
56
57                                 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
58                                         intval($contact_id),
59                                         intval(local_user())
60                                 );
61                         }
62                         goaway('notifications/intros');
63                 }
64                 if($_POST['submit'] == t('Ignore')) {
65                         $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
66                                 intval($intro_id));
67                         goaway('notifications/intros');
68                 }
69         }
70 }
71
72 function notifications_content(App $a) {
73
74         if (! local_user()) {
75                 notice( t('Permission denied.') . EOL);
76                 return;
77         }
78
79         $page   =       (x($_REQUEST,'page')            ? $_REQUEST['page']             : 1);
80         $show   =       (x($_REQUEST,'show')            ? $_REQUEST['show']             : 0);
81
82         nav_set_selected('notifications');
83
84         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
85
86         $nm = new NotificationsManager();
87
88         $o = '';
89         // Get the nav tabs for the notification pages
90         $tabs = $nm->getTabs();
91         $notif_content = array();
92
93         // Notification results per page
94         $perpage = 20;
95         $startrec = ($page * $perpage) - $perpage;
96
97         // Get introductions
98         if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
99                 nav_set_selected('introductions');
100                 $notif_header = t('Notifications');
101
102                 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
103
104                 $notifs = $nm->introNotifs($all, $startrec, $perpage);
105
106         // Get the network notifications
107         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
108
109                 $notif_header = t('Network Notifications');
110                 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
111
112         // Get the system notifications
113         } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
114
115                 $notif_header = t('System Notifications');
116                 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
117
118         // Get the personal notifications
119         } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
120
121                 $notif_header = t('Personal Notifications');
122                 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
123
124         // Get the home notifications
125         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
126
127                 $notif_header = t('Home Notifications');
128                 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
129
130         }
131
132
133         // Set the pager
134         $a->set_pager_total($notifs['total']);
135         $a->set_pager_itemspage($perpage);
136
137         // Add additional informations (needed for json output)
138         $notifs['items_page'] = $a->pager['itemspage'];
139         $notifs['page'] = $a->pager['page'];
140
141         // Json output
142         if(intval($json) === 1)
143                 json_return_and_die($notifs);
144
145         $notif_tpl = get_markup_template('notifications.tpl');
146
147         // Process the data for template creation
148         if($notifs['ident'] === 'introductions') {
149
150                 $sugg = get_markup_template('suggestions.tpl');
151                 $tpl = get_markup_template("intros.tpl");
152
153                 // The link to switch between ignored and normal connection requests
154                 $notif_show_lnk = array(
155                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
156                         'text' => (!$all ? t('Show Ignored Requests') : t('Hide Ignored Requests'))
157                 );
158
159                 // Loop through all introduction notifications.This creates an array with the output html for each
160                 // introduction
161                 foreach ($notifs['notifications'] as $it) {
162
163                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
164                         // We have to distinguish between these two because they use different data.
165                         switch ($it['label']) {
166                                 case 'friend_suggestion':
167                                         $notif_content[] = replace_macros($sugg, array(
168                                                 '$str_notifytype' => t('Notification type: '),
169                                                 '$notify_type' => $it['notify_type'],
170                                                 '$intro_id' => $it['intro_id'],
171                                                 '$madeby' => sprintf( t('suggested by %s'),$it['madeby']),
172                                                 '$contact_id' => $it['contact-id'],
173                                                 '$photo' => $it['photo'],
174                                                 '$fullname' => $it['name'],
175                                                 '$url' => $it['url'],
176                                                 '$hidden' => array('hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''),
177                                                 '$activity' => array('activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')),
178
179                                                 '$knowyou' => $it['knowyou'],
180                                                 '$approve' => t('Approve'),
181                                                 '$note' => $it['note'],
182                                                 '$request' => $it['request'],
183                                                 '$ignore' => t('Ignore'),
184                                                 '$discard' => t('Discard'),
185                                         ));
186                                         break;
187
188                                 // Normal connection requests
189                                 default:
190                                         $friend_selected = (($it['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
191                                         $fan_selected = (($it['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
192                                         $dfrn_tpl = get_markup_template('netfriend.tpl');
193
194                                         $knowyou   = '';
195                                         $dfrn_text = '';
196
197                                         if($it['network'] === NETWORK_DFRN || $it['network'] === NETWORK_DIASPORA) {
198                                                 if($it['network'] === NETWORK_DFRN) {
199                                                         $lbl_knowyou = t('Claims to be known to you: ');
200                                                         $knowyou = (($it['knowyou']) ? t('yes') : t('no'));
201                                                         $helptext = t('Shall your connection be bidirectional or not?');
202                                                         $helptext2 = sprintf(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.'), $it['name'], $it['name']);
203                                                         $helptext3 = sprintf(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.'), $it['name']);
204                                                 } else {
205                                                         $knowyou = '';
206                                                         $helptext = t('Shall your connection be bidirectional or not?');
207                                                         $helptext2 = sprintf(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.'), $it['name'], $it['name']);
208                                                         $helptext3 = sprintf(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.'), $it['name']);
209                                                 }
210                                         }
211
212                                         $dfrn_text = replace_macros($dfrn_tpl,array(
213                                                 '$intro_id' => $it['intro_id'],
214                                                 '$friend_selected' => $friend_selected,
215                                                 '$fan_selected' => $fan_selected,
216                                                 '$approve_as1' => $helptext,
217                                                 '$approve_as2' => $helptext2,
218                                                 '$approve_as3' => $helptext3,
219                                                 '$as_friend' => t('Friend'),
220                                                 '$as_fan' => (($it['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Subscriber'))
221                                         ));
222
223                                         $header = $it["name"];
224
225                                         if ($it["addr"] != "")
226                                                 $header .= " <".$it["addr"].">";
227
228                                         $header .= " (".network_to_name($it['network'], $it['url']).")";
229
230                                         if ($it['network'] != NETWORK_DIASPORA) {
231                                                 $discard = t('Discard');
232                                         } else {
233                                                 $discard = '';
234                                         }
235
236                                         $notif_content[] = replace_macros($tpl, array(
237                                                 '$header' => htmlentities($header),
238                                                 '$str_notifytype' => t('Notification type: '),
239                                                 '$notify_type' => $it['notify_type'],
240                                                 '$dfrn_text' => $dfrn_text,
241                                                 '$dfrn_id' => $it['dfrn_id'],
242                                                 '$uid' => $it['uid'],
243                                                 '$intro_id' => $it['intro_id'],
244                                                 '$contact_id' => $it['contact_id'],
245                                                 '$photo' => $it['photo'],
246                                                 '$fullname' => $it['name'],
247                                                 '$location' => $it['location'],
248                                                 '$lbl_location' => t('Location:'),
249                                                 '$about' => $it['about'],
250                                                 '$lbl_about' => t('About:'),
251                                                 '$keywords' => $it['keywords'],
252                                                 '$lbl_keywords' => t('Tags:'),
253                                                 '$gender' => $it['gender'],
254                                                 '$lbl_gender' => t('Gender:'),
255                                                 '$hidden' => array('hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''),
256                                                 '$activity' => array('activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')),
257                                                 '$url' => $it['url'],
258                                                 '$zrl' => $it['zrl'],
259                                                 '$lbl_url' => t('Profile URL'),
260                                                 '$addr' => $it['addr'],
261                                                 '$lbl_knowyou' => $lbl_knowyou,
262                                                 '$lbl_network' => t('Network:'),
263                                                 '$network' => network_to_name($it['network'], $it['url']),
264                                                 '$knowyou' => $knowyou,
265                                                 '$approve' => t('Approve'),
266                                                 '$note' => $it['note'],
267                                                 '$ignore' => t('Ignore'),
268                                                 '$discard' => $discard,
269
270                                         ));
271                                         break;
272                         }
273                 }
274
275                 if($notifs['total'] == 0)
276                         info( t('No introductions.') . EOL);
277
278         // Normal notifications (no introductions)
279         } else {
280
281                 // The template files we need in different cases for formatting the content
282                 $tpl_item_like = 'notifications_likes_item.tpl';
283                 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
284                 $tpl_item_attend = 'notifications_attend_item.tpl';
285                 $tpl_item_attendno = 'notifications_attend_item.tpl';
286                 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
287                 $tpl_item_friend = 'notifications_friends_item.tpl';
288                 $tpl_item_comment = 'notifications_comments_item.tpl';
289                 $tpl_item_post = 'notifications_posts_item.tpl';
290                 $tpl_item_notify = 'notify.tpl';
291
292                 // Loop trough ever notification This creates an array with the output html for each
293                 // notification and apply the correct template according to the notificationtype (label).
294                 foreach ($notifs['notifications'] as $it) {
295
296                         // We use the notification label to get the correct template file
297                         $tpl_var_name = 'tpl_item_'.$it['label'];
298                         $tpl_notif = get_markup_template($$tpl_var_name);
299
300                         $notif_content[] = replace_macros($tpl_notif,array(
301                                 '$item_label' => $it['label'],
302                                 '$item_link' => $it['link'],
303                                 '$item_image' => $it['image'],
304                                 '$item_url' => $it['url'],
305                                 '$item_text' => $it['text'],
306                                 '$item_when' => $it['when'],
307                                 '$item_ago' => $it['ago'],
308                                 '$item_seen' => $it['seen'],
309                         ));
310                 }
311
312                 // It doesn't make sense to show the Show unread / Show all link visible if the user is on the
313                 // "Show all" page and there are no notifications. So we will hide it.
314                 if($show == 0 || intval($show) && $notifs['total'] > 0) {
315                         $notif_show_lnk = array(
316                                 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
317                                 'text' => ($show ? t('Show unread') : t('Show all')),
318                         );
319                 }
320
321                 // Output if there aren't any notifications available
322                 if($notifs['total'] == 0)
323                         $notif_nocontent = sprintf( t('No more %s notifications.'), $notifs['ident']);
324         }
325
326         $o .= replace_macros($notif_tpl, array(
327                 '$notif_header' => $notif_header,
328                 '$tabs' => $tabs,
329                 '$notif_content' => $notif_content,
330                 '$notif_nocontent' => $notif_nocontent,
331                 '$notif_show_lnk' => $notif_show_lnk,
332                 '$notif_paginate' => paginate($a)
333         ));
334
335         return $o;
336 }