]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
a22703ba95c4cdb7a9bf77e591006ec094317ffe
[friendica.git] / mod / notifications.php
1 <?php
2 include_once("include/NotificationsManager.php");
3 include_once("include/bbcode.php");
4 include_once("include/contact_selectors.php");
5 include_once("include/Scrape.php");
6
7 function notifications_post(&$a) {
8
9         if(! local_user()) {
10                 goaway(z_root());
11         }
12
13         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
14
15         if($request_id === "all")
16                 return;
17
18         if($request_id) {
19
20                 $r = q("SELECT * FROM `intro` WHERE `id` = %d  AND `uid` = %d LIMIT 1",
21                         intval($request_id),
22                         intval(local_user())
23                 );
24
25                 if(count($r)) {
26                         $intro_id = $r[0]['id'];
27                         $contact_id = $r[0]['contact-id'];
28                 }
29                 else {
30                         notice( t('Invalid request identifier.') . EOL);
31                         return;
32                 }
33
34                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
35                 // that should not be deleted.
36
37                 $fid = $r[0]['fid'];
38
39                 if($_POST['submit'] == t('Discard')) {
40                         $r = q("DELETE FROM `intro` WHERE `id` = %d",
41                                 intval($intro_id)
42                         );
43                         if(! $fid) {
44
45                                 // The check for blocked and pending is in case the friendship was already approved
46                                 // and we just want to get rid of the now pointless notification
47
48                                 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
49                                         intval($contact_id),
50                                         intval(local_user())
51                                 );
52                         }
53                         goaway('notifications/intros');
54                 }
55                 if($_POST['submit'] == t('Ignore')) {
56                         $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
57                                 intval($intro_id));
58                         goaway('notifications/intros');
59                 }
60         }
61 }
62
63 function notifications_content(&$a) {
64
65         if(! local_user()) {
66                 notice( t('Permission denied.') . EOL);
67                 return;
68         }
69
70         $page   =       (x($_REQUEST,'page')            ? $_REQUEST['page']             : 1);
71         $show   =       (x($_REQUEST,'show')            ? $_REQUEST['show']             : 0);
72
73         nav_set_selected('notifications');
74
75         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
76
77         $nm = new NotificationsManager();
78
79         $o = '';
80         // get the nav tabs for the notification pages
81         $tabs = $nm->getTabs();
82         $notif_content = array();
83         $perpage = 20;
84         $startrec = ($page * $perpage) - $perpage;
85
86         if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
87                 nav_set_selected('introductions');
88
89                 if(($a->argc > 2) && ($a->argv[2] == 'all'))
90                         $sql_extra = '';
91                 else
92                         $sql_extra = " AND `ignore` = 0 ";
93
94                 $notif_header = t('Notifications');
95                 $notif_tpl = get_markup_template('notifications.tpl');
96
97
98                 $notif_show_lnk = array(
99                         'href' => ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ),
100                         'text' => ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')),
101                         'id' => "notifications-show-hide-link",
102                 );
103
104                 $r = q("SELECT COUNT(*) AS `total` FROM `intro`
105                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
106                                 intval($_SESSION['uid'])
107                 );
108                 if(dbm::is_result($r)) {
109                         $a->set_pager_total($r[0]['total']);
110                         $a->set_pager_itemspage($perpage);
111                 }
112
113                 /// @todo Fetch contact details by "get_contact_details_by_url" instead of queries to contact, fcontact and gcontact
114
115                 $r = q("SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, `fcontact`.`name` AS `fname`,`fcontact`.`url` AS `furl`,`fcontact`.`photo` AS `fphoto`,`fcontact`.`request` AS `frequest`,
116                                 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
117                                 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
118                                 `gcontact`.`network` AS `gnetwork`
119                         FROM `intro`
120                                 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
121                                 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
122                                 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
123                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
124                                 intval($_SESSION['uid']));
125
126                 if(dbm::is_result($r)) {
127
128                         $sugg = get_markup_template('suggestions.tpl');
129                         $tpl = get_markup_template("intros.tpl");
130
131                         foreach($r as $rr) {
132
133                                 if($rr['fid']) {
134
135                                         $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . (($a->path) ? '/' . $a->path : ''));
136
137                                         $notif_content[] = replace_macros($sugg, array(
138                                                 '$str_notifytype' => t('Notification type: '),
139                                                 '$notify_type' => t('Friend Suggestion'),
140                                                 '$intro_id' => $rr['intro_id'],
141                                                 '$madeby' => sprintf( t('suggested by %s'),$rr['name']),
142                                                 '$contact_id' => $rr['contact-id'],
143                                                 '$photo' => ((x($rr,'fphoto')) ? proxy_url($rr['fphoto'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg"),
144                                                 '$fullname' => $rr['fname'],
145                                                 '$url' => zrl($rr['furl']),
146                                                 '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
147                                                 '$activity' => array('activity', t('Post a new friend activity'), (intval(get_pconfig(local_user(),'system','post_newfriend')) ? '1' : 0), t('if applicable')),
148
149                                                 '$knowyou' => $knowyou,
150                                                 '$approve' => t('Approve'),
151                                                 '$note' => $rr['note'],
152                                                 '$request' => $rr['frequest'] . '?addr=' . $return_addr,
153                                                 '$ignore' => t('Ignore'),
154                                                 '$discard' => t('Discard'),
155
156                                         ));
157
158                                         continue;
159
160                                 }
161                                 $friend_selected = (($rr['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
162                                 $fan_selected = (($rr['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
163                                 $dfrn_tpl = get_markup_template('netfriend.tpl');
164
165                                 $knowyou   = '';
166                                 $dfrn_text = '';
167
168                                 if($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
169                                         if($rr['network'] === NETWORK_DFRN) {
170                                                 $lbl_knowyou = t('Claims to be known to you: ');
171                                                 $knowyou = (($rr['knowyou']) ? t('yes') : t('no'));
172                                                 $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: ');
173                                         } else {
174                                                 $knowyou = '';
175                                                 $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: ');
176                                         }
177
178                                         $dfrn_text = replace_macros($dfrn_tpl,array(
179                                                 '$intro_id' => $rr['intro_id'],
180                                                 '$friend_selected' => $friend_selected,
181                                                 '$fan_selected' => $fan_selected,
182                                                 '$approve_as' => $helptext,
183                                                 '$as_friend' => t('Friend'),
184                                                 '$as_fan' => (($rr['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Fan/Admirer'))
185                                         ));
186                                 }
187
188                                 $header = $rr["name"];
189
190                                 $ret = probe_url($rr["url"]);
191
192                                 if ($rr['gnetwork'] == "")
193                                         $rr['gnetwork'] = $ret["network"];
194
195                                 if ($ret["addr"] != "")
196                                         $header .= " <".$ret["addr"].">";
197
198                                 $header .= " (".network_to_name($rr['gnetwork'], $rr['url']).")";
199
200                                 // Don't show these data until you are connected. Diaspora is doing the same.
201                                 if($rr['gnetwork'] === NETWORK_DIASPORA) {
202                                         $rr['glocation'] = "";
203                                         $rr['gabout'] = "";
204                                         $rr['ggender'] = "";
205                                 }
206
207                                 $notif_content[] = replace_macros($tpl, array(
208                                         '$header' => htmlentities($header),
209                                         '$str_notifytype' => t('Notification type: '),
210                                         '$notify_type' => (($rr['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
211                                         '$dfrn_text' => $dfrn_text,
212                                         '$dfrn_id' => $rr['issued-id'],
213                                         '$uid' => $_SESSION['uid'],
214                                         '$intro_id' => $rr['intro_id'],
215                                         '$contact_id' => $rr['contact-id'],
216                                         '$photo' => ((x($rr,'photo')) ? proxy_url($rr['photo'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg"),
217                                         '$fullname' => $rr['name'],
218                                         '$location' => bbcode($rr['glocation'], false, false),
219                                         '$location_label' => t('Location:'),
220                                         '$about' => bbcode($rr['gabout'], false, false),
221                                         '$about_label' => t('About:'),
222                                         '$keywords' => $rr['gkeywords'],
223                                         '$keywords_label' => t('Tags:'),
224                                         '$gender' => $rr['ggender'],
225                                         '$gender_label' => t('Gender:'),
226                                         '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
227                                         '$activity' => array('activity', t('Post a new friend activity'), (intval(get_pconfig(local_user(),'system','post_newfriend')) ? '1' : 0), t('if applicable')),
228                                         '$url' => $rr['url'],
229                                         '$zrl' => zrl($rr['url']),
230                                         '$url_label' => t('Profile URL'),
231                                         '$addr' => $rr['addr'],
232                                         '$lbl_knowyou' => $lbl_knowyou,
233                                         '$lbl_network' => t('Network:'),
234                                         '$network' => network_to_name($rr['gnetwork'], $rr['url']),
235                                         '$knowyou' => $knowyou,
236                                         '$approve' => t('Approve'),
237                                         '$note' => $rr['note'],
238                                         '$ignore' => t('Ignore'),
239                                         '$discard' => t('Discard'),
240
241                                 ));
242                         }
243                 }
244                 else
245                         info( t('No introductions.') . EOL);
246
247         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
248
249                 $notif_header = t('Network Notifications');
250                 $notif_tpl = get_markup_template('notifications.tpl');
251
252                 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
253
254                 $notif_show_lnk = array(
255                         'href' => ($show ? 'notifications/network' : 'notifications/network?show=all' ),
256                         'text' => ($show ? t('Show unread') : t('Show all')),
257                 );
258
259                 if(!dbm::is_result($notifs)) {
260                         if($show)
261                                 $notif_show_lnk = array();
262
263                         $notif_nocontent = t('No more network notifications.');
264                 }
265
266         } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
267
268                 $notif_header = t('System Notifications');
269                 $notif_tpl = get_markup_template('notifications.tpl');
270
271                 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
272
273                 $notif_show_lnk = array(
274                         'href' => ($show ? 'notifications/system' : 'notifications/system?show=all' ),
275                         'text' => ($show ? t('Show unread') : t('Show all')),
276                 );
277
278                 if(!dbm::is_result($notifs)) {
279                         if($show)
280                                 $notif_show_lnk = array();
281
282                         $notif_nocontent = t('No more system notifications.');
283                 }
284
285         } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
286
287                 $notif_header = t('Personal Notifications');
288                 $notif_tpl = get_markup_template('notifications.tpl');
289
290                 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
291
292                 $notif_show_lnk = array(
293                         'href' => ($show ? 'notifications/personal' : 'notifications/personal?show=all' ),
294                         'text' => ($show ? t('Show unread') : t('Show all')),
295                 );
296
297                 if(!dbm::is_result($notifs)) {
298                         if($show)
299                                 $notif_show_lnk = array();
300
301                         $notif_nocontent = t('No more personal notifications.');
302                 }
303
304         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
305
306                 $notif_header = t('Home Notifications');
307                 $notif_tpl = get_markup_template('notifications.tpl');
308
309                 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
310
311                 $notif_show_lnk = array(
312                         'href' => ($show ? 'notifications/home' : 'notifications/home?show=all' ),
313                         'text' => ($show ? t('Show unread') : t('Show all')),
314                 );
315
316                 if(!dbm::is_result($notifs)) {
317                         if($show)
318                                 $notif_show_lnk = array();
319
320                         $notif_nocontent = t('No more home notifications.');
321                 }
322
323         }
324
325         if(count($notifs['notifications']) > 0 ) {
326                 // set the pager
327                 $a->set_pager_total($notifs['total']);
328                 $a->set_pager_itemspage($perpage);
329
330                 // add additional informations (needed for json output)
331                 $notifs['items_page'] = $a->pager['itemspage'];
332                 $notifs['page'] = $a->pager['page'];
333
334                 // The template files we need in different cases for formatting the content
335                 $tpl_item_like = 'notifications_likes_item.tpl';
336                 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
337                 $tpl_item_attend = 'notifications_attend_item.tpl';
338                 $tpl_item_attendno = 'notifications_attend_item.tpl';
339                 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
340                 $tpl_item_friend = 'notifications_friends_item.tpl';
341                 $tpl_item_comment = 'notifications_comments_item.tpl';
342                 $tpl_item_post = 'notifications_posts_item.tpl';
343                 $tpl_item_notify = 'notify.tpl';
344
345                 foreach ($notifs['notifications'] as $it) {
346                         $tplname = 'tpl_item_'.$it['label'];
347                         $templ = get_markup_template($$tplname);
348
349                         $notif_content[] = replace_macros($templ,array(
350                                 '$item_label' => $it['label'],
351                                 '$item_link' => $it['link'],
352                                 '$item_image' => $it['image'],
353                                 '$item_text' => $it['text'],
354                                 '$item_when' => $it['when'],
355                                 '$item_seen' => $it['seen'],
356                         ));
357                 }
358
359         }
360
361         $o .= replace_macros($notif_tpl, array(
362                 '$notif_header' => $notif_header,
363                 '$tabs' => $tabs,
364                 '$notif_content' => $notif_content,
365                 '$notif_nocontent' => $notif_nocontent,
366                 '$notif_show_lnk' => $notif_show_lnk,
367                 '$notif_paginate' => paginate($a)
368         ));
369
370         return $o;
371 }