]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
name change stragglers, fix newmember links, liveupdate glitch
[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                 $diasp_url = str_replace('/profile/','/u/',$myurl);
301                 $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' or `item`.`tag` regexp '%s' ) ",
302                         dbesc($myurl . '$'),
303                         dbesc($myurl . '\\]'),
304                         dbesc($diasp_url . '\\]')
305                 );
306
307
308                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
309                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
310                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
311                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
312                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 
313                                 $sql_extra
314                                 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
315                         intval(local_user())
316                 );
317                 
318                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
319                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
320                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
321                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
322                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
323                 
324                 $notif_content = '';
325                 
326                 if (count($r) > 0) {
327                         
328                         foreach ($r as $it) {
329                                 switch($it['verb']){
330                                         case ACTIVITY_LIKE:
331                                                 $notif_content .= replace_macros($tpl_item_likes,array(
332                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
333                                                         '$item_image' => $it['author-avatar'],
334                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
335                                                         '$item_when' => relative_date($it['created'])
336                                                 ));
337                                                 break;
338                                                 
339                                         case ACTIVITY_DISLIKE:
340                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
341                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
342                                                         '$item_image' => $it['author-avatar'],
343                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
344                                                         '$item_when' => relative_date($it['created'])
345                                                 ));
346                                                 break;
347                                                 
348                                         case ACTIVITY_FRIEND:
349                                         
350                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
351                                                 $obj = parse_xml_string($xmlhead.$it['object']);
352                                                 $it['fname'] = $obj->title;
353                                                 
354                                                 $notif_content .= replace_macros($tpl_item_friends,array(
355                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
356                                                         '$item_image' => $it['author-avatar'],
357                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
358                                                         '$item_when' => relative_date($it['created'])
359                                                 ));
360                                                 break;
361                                                 
362                                         default:
363                                                 $item_text = (($it['id'] == $it['parent'])
364                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
365                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
366                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
367
368                                                 $notif_content .= replace_macros($tpl,array(
369                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
370                                                         '$item_image' => $it['author-avatar'],
371                                                         '$item_text' => $item_text,
372                                                         '$item_when' => relative_date($it['created'])
373                                                 ));
374                                 }
375                         }
376                         
377                 } else {
378                         
379                         $notif_content = t('No more personal notifications.');
380                 }
381                 
382                 $o .= replace_macros($notif_tpl,array(
383                         '$notif_header' => t('Notifications'),
384                         '$tabs' => $tabs,
385                         '$notif_content' => $notif_content,
386                 ));
387                 
388
389
390
391
392
393         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
394                 
395                 $notif_tpl = get_markup_template('notifications.tpl');
396                 
397                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
398                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
399                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
400                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
401                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
402                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC",
403                         intval(local_user())
404                 );
405                 
406                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
407                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
408                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
409                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
410                 
411                 $notif_content = '';
412                 
413                 if (count($r) > 0) {
414                         
415                         foreach ($r as $it) {
416                                 switch($it['verb']){
417                                         case ACTIVITY_LIKE:
418                                                 $notif_content .= replace_macros($tpl_item_likes,array(
419                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
420                                                         '$item_image' => $it['author-avatar'],
421                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
422                                                         '$item_when' => relative_date($it['created'])
423                                                 ));
424
425                                                 break;
426                                         case ACTIVITY_DISLIKE:
427                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
428                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
429                                                         '$item_image' => $it['author-avatar'],
430                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
431                                                         '$item_when' => relative_date($it['created'])
432                                                 ));
433
434                                                 break;
435                                         case ACTIVITY_FRIEND:
436                                         
437                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
438                                                 $obj = parse_xml_string($xmlhead.$it['object']);
439                                                 $it['fname'] = $obj->title;
440                                                 
441                                                 $notif_content .= replace_macros($tpl_item_friends,array(
442                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
443                                                         '$item_image' => $it['author-avatar'],
444                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
445                                                         '$item_when' => relative_date($it['created'])
446                                                 ));
447
448                                                 break;
449                                         default:
450                                                 $notif_content .= replace_macros($tpl_item_comments,array(
451                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
452                                                         '$item_image' => $it['author-avatar'],
453                                                         '$item_text' => sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']),
454                                                         '$item_when' => relative_date($it['created'])
455                                                 ));
456                                 }
457                         }
458                                 
459                 } else {
460                         $notif_content = t('No more home notifications.');
461                 }
462                 
463                 $o .= replace_macros($notif_tpl,array(
464                         '$notif_header' => t('Notifications'),
465                         '$tabs' => $tabs,
466                         '$notif_content' => $notif_content,
467                 ));
468         }
469
470         $o .= paginate($a);
471         return $o;
472 }