]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
personal notifications
[friendica.git] / mod / notifications.php
1 <?php
2
3 function notifications_post(&$a) {
4
5         if(! local_user()) {
6                 goaway(z_root());
7         }
8         
9         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
10         
11         if($request_id === "all")
12                 return;
13
14         if($request_id) {
15
16                 $r = q("SELECT * FROM `intro` WHERE `id` = %d  AND `uid` = %d LIMIT 1",
17                         intval($request_id),
18                         intval(local_user())
19                 );
20         
21                 if(count($r)) {
22                         $intro_id = $r[0]['id'];
23                         $contact_id = $r[0]['contact-id'];
24                 }
25                 else {
26                         notice( t('Invalid request identifier.') . EOL);
27                         return;
28                 }
29
30                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
31                 // that should not be deleted.
32
33                 $fid = $r[0]['fid'];
34
35                 if($_POST['submit'] == t('Discard')) {
36                         $r = q("DELETE FROM `intro` WHERE `id` = %d LIMIT 1", 
37                                 intval($intro_id)
38                         );      
39                         if(! $fid) {
40                                 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1", 
41                                         intval($contact_id),
42                                         intval(local_user())
43                                 );
44                         }
45                         return;
46                 }
47                 if($_POST['submit'] == t('Ignore')) {
48                         $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d LIMIT 1",
49                                 intval($intro_id));
50                         return;
51                 }
52         }
53 }
54
55
56
57
58
59 function notifications_content(&$a) {
60
61         if(! local_user()) {
62                 notice( t('Permission denied.') . EOL);
63                 return;
64         }
65
66         nav_set_selected('notifications');              
67
68         $o = '';
69         $tabs = array(
70                 array(
71                         'label' => t('Network'),
72                         'url'=>$a->get_baseurl() . '/notifications/network',
73                         'sel'=> (($a->argv[1] == 'network') ? 'active' : ''),
74                 ),
75                 array(
76                         'label' => t('Personal'),
77                         'url'=>$a->get_baseurl() . '/notifications/personal',
78                         'sel'=> (($a->argv[1] == 'personal') ? 'active' : ''),
79                 ),
80                 array(
81                         'label' => t('Home'),
82                         'url' => $a->get_baseurl() . '/notifications/home',
83                         'sel'=> (($a->argv[1] == 'home') ? 'active' : ''),
84                 ),
85                 array(
86                         'label' => t('Introductions'),
87                         'url' => $a->get_baseurl() . '/notifications/intros',
88                         'sel'=> (($a->argv[1] == 'intros') ? 'active' : ''),
89                 ),
90                 array(
91                         'label' => t('Messages'),
92                         'url' => $a->get_baseurl() . '/message',
93                         'sel'=> '',
94                 ),
95         );
96         
97         $o = "";
98
99         
100         if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
101                 nav_set_selected('introductions');
102                 if(($a->argc > 2) && ($a->argv[2] == 'all'))
103                         $sql_extra = '';
104                 else
105                         $sql_extra = " AND `ignore` = 0 ";
106                 
107                 $notif_tpl = get_markup_template('notifications.tpl');
108                 
109                 $notif_content .= '<a href="' . ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ) . '" id="notifications-show-hide-link" >'
110                         . ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
111
112                 $r = q("SELECT COUNT(*) AS `total` FROM `intro` 
113                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
114                                 intval($_SESSION['uid'])
115                 );
116                 if($r && count($r)) {
117                         $a->set_pager_total($r[0]['total']);
118                         $a->set_pager_itemspage(20);
119                 }
120
121                 $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`
122                         FROM `intro` LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` 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(($r !== false) && (count($r))) {
127
128                         $sugg = get_markup_template('suggestions.tpl');
129                         $tpl = get_markup_template("intros.tpl");
130
131                         foreach($r as $rr) {
132                                 if($rr['fid']) {
133
134                                         $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . (($a->path) ? '/' . $a->path : ''));
135                                         $notif_content .= replace_macros($sugg,array(
136                                                 '$str_notifytype' => t('Notification type: '),
137                                                 '$notify_type' => t('Friend Suggestion'),
138                                                 '$intro_id' => $rr['intro_id'],
139                                                 '$madeby' => sprintf( t('suggested by %s'),$rr['name']),
140                                                 '$contact_id' => $rr['contact-id'],
141                                                 '$photo' => ((x($rr,'fphoto')) ? $rr['fphoto'] : "images/default-profile.jpg"),
142                                                 '$fullname' => $rr['fname'],
143                                                 '$url' => $rr['furl'],
144                                                 '$knowyou' => $knowyou,
145                                                 '$approve' => t('Approve'),
146                                                 '$note' => $rr['note'],
147                                                 '$request' => $rr['frequest'] . '?addr=' . $return_addr,
148                                                 '$ignore' => t('Ignore'),
149                                                 '$discard' => t('Discard')
150
151                                         ));
152
153                                         continue;
154
155                                 }
156                                 $friend_selected = (($rr['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
157                                 $fan_selected = (($rr['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
158                                 $dfrn_tpl = get_markup_template('netfriend.tpl');
159
160                                 $knowyou   = '';
161                                 $dfrn_text = '';
162
163                                 if($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
164                                         if($rr['network'] === NETWORK_DFRN)
165                                                 $knowyou = t('Claims to be known to you: ') . (($rr['knowyou']) ? t('yes') : t('no'));
166                                         else
167                                                 $knowyou = '';
168                                         $dfrn_text = replace_macros($dfrn_tpl,array(
169                                                 '$intro_id' => $rr['intro_id'],
170                                                 '$friend_selected' => $friend_selected,
171                                                 '$fan_selected' => $fan_selected,
172                                                 '$approve_as' => t('Approve as: '),
173                                                 '$as_friend' => t('Friend'),
174                                                 '$as_fan' => (($rr['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Fan/Admirer'))
175                                         ));
176                                 }                       
177
178                                 $notif_content .= replace_macros($tpl,array(
179                                         '$str_notifytype' => t('Notification type: '),
180                                         '$notify_type' => (($rr['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
181                                         '$dfrn_text' => $dfrn_text,     
182                                         '$dfrn_id' => $rr['issued-id'],
183                                         '$uid' => $_SESSION['uid'],
184                                         '$intro_id' => $rr['intro_id'],
185                                         '$contact_id' => $rr['contact-id'],
186                                         '$photo' => ((x($rr,'photo')) ? $rr['photo'] : "images/default-profile.jpg"),
187                                         '$fullname' => $rr['name'],
188                                         '$url' => $rr['url'],
189                                         '$knowyou' => $knowyou,
190                                         '$approve' => t('Approve'),
191                                         '$note' => $rr['note'],
192                                         '$ignore' => t('Ignore'),
193                                         '$discard' => t('Discard')
194
195                                 ));
196                         }
197                 }
198                 else
199                         info( t('No introductions.') . EOL);
200                 
201                 $o .= replace_macros($notif_tpl,array(
202                         '$notif_header' => t('Notifications'),
203                         '$tabs' => $tabs,
204                         '$notif_content' => $notif_content,
205                 ));
206                 
207                 $o .= paginate($a);
208                 return $o;
209                                 
210         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
211                 
212                 $notif_tpl = get_markup_template('notifications.tpl');
213                 
214                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
215                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
216                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
217                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
218                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
219                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
220                         intval(local_user())
221                 );
222                 
223                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
224                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
225                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
226                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
227                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
228                 
229                 $notif_content = '';
230                 
231                 if (count($r) > 0) {
232                         
233                         foreach ($r as $it) {
234                                 switch($it['verb']){
235                                         case ACTIVITY_LIKE:
236                                                 $notif_content .= replace_macros($tpl_item_likes,array(
237                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
238                                                         '$item_image' => $it['author-avatar'],
239                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
240                                                         '$item_when' => relative_date($it['created'])
241                                                 ));
242                                                 break;
243                                                 
244                                         case ACTIVITY_DISLIKE:
245                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
246                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
247                                                         '$item_image' => $it['author-avatar'],
248                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
249                                                         '$item_when' => relative_date($it['created'])
250                                                 ));
251                                                 break;
252                                                 
253                                         case ACTIVITY_FRIEND:
254                                         
255                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
256                                                 $obj = parse_xml_string($xmlhead.$it['object']);
257                                                 $it['fname'] = $obj->title;
258                                                 
259                                                 $notif_content .= replace_macros($tpl_item_friends,array(
260                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
261                                                         '$item_image' => $it['author-avatar'],
262                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
263                                                         '$item_when' => relative_date($it['created'])
264                                                 ));
265                                                 break;
266                                                 
267                                         default:
268                                                 $item_text = (($it['id'] == $it['parent'])
269                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
270                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
271                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
272
273                                                 $notif_content .= replace_macros($tpl,array(
274                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
275                                                         '$item_image' => $it['author-avatar'],
276                                                         '$item_text' => $item_text,
277                                                         '$item_when' => relative_date($it['created'])
278                                                 ));
279                                 }
280                         }
281                         
282                 } else {
283                         
284                         $notif_content = t('No more network notifications.');
285                 }
286                 
287                 $o .= replace_macros($notif_tpl,array(
288                         '$notif_header' => t('Notifications'),
289                         '$tabs' => $tabs,
290                         '$notif_content' => $notif_content,
291                 ));
292                 
293         } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
294                 
295                 $notif_tpl = get_markup_template('notifications.tpl');
296                 
297                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
298                 $myurl = substr($myurl,strpos($myurl,'://')+3);
299                 $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
300                 $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' ) ",
301                         dbesc($myurl . '$'),
302                         dbesc($myurl . '\\]')
303                 );
304
305
306                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
307                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
308                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
309                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
310                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 
311                                 $sql_extra
312                                 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
313                         intval(local_user())
314                 );
315                 
316                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
317                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
318                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
319                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
320                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
321                 
322                 $notif_content = '';
323                 
324                 if (count($r) > 0) {
325                         
326                         foreach ($r as $it) {
327                                 switch($it['verb']){
328                                         case ACTIVITY_LIKE:
329                                                 $notif_content .= replace_macros($tpl_item_likes,array(
330                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
331                                                         '$item_image' => $it['author-avatar'],
332                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
333                                                         '$item_when' => relative_date($it['created'])
334                                                 ));
335                                                 break;
336                                                 
337                                         case ACTIVITY_DISLIKE:
338                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
339                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
340                                                         '$item_image' => $it['author-avatar'],
341                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
342                                                         '$item_when' => relative_date($it['created'])
343                                                 ));
344                                                 break;
345                                                 
346                                         case ACTIVITY_FRIEND:
347                                         
348                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
349                                                 $obj = parse_xml_string($xmlhead.$it['object']);
350                                                 $it['fname'] = $obj->title;
351                                                 
352                                                 $notif_content .= replace_macros($tpl_item_friends,array(
353                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
354                                                         '$item_image' => $it['author-avatar'],
355                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
356                                                         '$item_when' => relative_date($it['created'])
357                                                 ));
358                                                 break;
359                                                 
360                                         default:
361                                                 $item_text = (($it['id'] == $it['parent'])
362                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
363                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
364                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
365
366                                                 $notif_content .= replace_macros($tpl,array(
367                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
368                                                         '$item_image' => $it['author-avatar'],
369                                                         '$item_text' => $item_text,
370                                                         '$item_when' => relative_date($it['created'])
371                                                 ));
372                                 }
373                         }
374                         
375                 } else {
376                         
377                         $notif_content = t('No more personal notifications.');
378                 }
379                 
380                 $o .= replace_macros($notif_tpl,array(
381                         '$notif_header' => t('Notifications'),
382                         '$tabs' => $tabs,
383                         '$notif_content' => $notif_content,
384                 ));
385                 
386
387
388
389
390
391         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
392                 
393                 $notif_tpl = get_markup_template('notifications.tpl');
394                 
395                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
396                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
397                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
398                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
399                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
400                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC",
401                         intval(local_user())
402                 );
403                 
404                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
405                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
406                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
407                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
408                 
409                 $notif_content = '';
410                 
411                 if (count($r) > 0) {
412                         
413                         foreach ($r as $it) {
414                                 switch($it['verb']){
415                                         case ACTIVITY_LIKE:
416                                                 $notif_content .= replace_macros($tpl_item_likes,array(
417                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
418                                                         '$item_image' => $it['author-avatar'],
419                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
420                                                         '$item_when' => relative_date($it['created'])
421                                                 ));
422
423                                                 break;
424                                         case ACTIVITY_DISLIKE:
425                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
426                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
427                                                         '$item_image' => $it['author-avatar'],
428                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
429                                                         '$item_when' => relative_date($it['created'])
430                                                 ));
431
432                                                 break;
433                                         case ACTIVITY_FRIEND:
434                                         
435                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
436                                                 $obj = parse_xml_string($xmlhead.$it['object']);
437                                                 $it['fname'] = $obj->title;
438                                                 
439                                                 $notif_content .= replace_macros($tpl_item_friends,array(
440                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
441                                                         '$item_image' => $it['author-avatar'],
442                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
443                                                         '$item_when' => relative_date($it['created'])
444                                                 ));
445
446                                                 break;
447                                         default:
448                                                 $notif_content .= replace_macros($tpl_item_comments,array(
449                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
450                                                         '$item_image' => $it['author-avatar'],
451                                                         '$item_text' => sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']),
452                                                         '$item_when' => relative_date($it['created'])
453                                                 ));
454                                 }
455                         }
456                                 
457                 } else {
458                         $notif_content = t('No more home notifications.');
459                 }
460                 
461                 $o .= replace_macros($notif_tpl,array(
462                         '$notif_header' => t('Notifications'),
463                         '$tabs' => $tabs,
464                         '$notif_content' => $notif_content,
465                 ));
466         }
467
468         $o .= paginate($a);
469         return $o;
470 }