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