]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
quattro: add "view in context" link in search results
[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                         goaway($a->get_baseurl() . '/notifications/intros');
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                         goaway($a->get_baseurl() . '/notifications/intros');
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                                                 '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
145                                                 '$activity' => array('activity', t('Post a new friend activity'), 1, t('if applicable')),
146
147                                                 '$knowyou' => $knowyou,
148                                                 '$approve' => t('Approve'),
149                                                 '$note' => $rr['note'],
150                                                 '$request' => $rr['frequest'] . '?addr=' . $return_addr,
151                                                 '$ignore' => t('Ignore'),
152                                                 '$discard' => t('Discard')
153
154                                         ));
155
156                                         continue;
157
158                                 }
159                                 $friend_selected = (($rr['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
160                                 $fan_selected = (($rr['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
161                                 $dfrn_tpl = get_markup_template('netfriend.tpl');
162
163                                 $knowyou   = '';
164                                 $dfrn_text = '';
165
166                                 if($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
167                                         if($rr['network'] === NETWORK_DFRN)
168                                                 $knowyou = t('Claims to be known to you: ') . (($rr['knowyou']) ? t('yes') : t('no'));
169                                         else
170                                                 $knowyou = '';
171                                         $dfrn_text = replace_macros($dfrn_tpl,array(
172                                                 '$intro_id' => $rr['intro_id'],
173                                                 '$friend_selected' => $friend_selected,
174                                                 '$fan_selected' => $fan_selected,
175                                                 '$approve_as' => t('Approve as: '),
176                                                 '$as_friend' => t('Friend'),
177                                                 '$as_fan' => (($rr['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Fan/Admirer'))
178                                         ));
179                                 }                       
180
181                                 $notif_content .= replace_macros($tpl,array(
182                                         '$str_notifytype' => t('Notification type: '),
183                                         '$notify_type' => (($rr['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
184                                         '$dfrn_text' => $dfrn_text,     
185                                         '$dfrn_id' => $rr['issued-id'],
186                                         '$uid' => $_SESSION['uid'],
187                                         '$intro_id' => $rr['intro_id'],
188                                         '$contact_id' => $rr['contact-id'],
189                                         '$photo' => ((x($rr,'photo')) ? $rr['photo'] : "images/default-profile.jpg"),
190                                         '$fullname' => $rr['name'],
191                                         '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
192                                         '$activity' => array('activity', t('Post a new friend activity'), 1, t('if applicable')),
193                                         '$url' => $rr['url'],
194                                         '$knowyou' => $knowyou,
195                                         '$approve' => t('Approve'),
196                                         '$note' => $rr['note'],
197                                         '$ignore' => t('Ignore'),
198                                         '$discard' => t('Discard')
199
200                                 ));
201                         }
202                 }
203                 else
204                         info( t('No introductions.') . EOL);
205                 
206                 $o .= replace_macros($notif_tpl,array(
207                         '$notif_header' => t('Notifications'),
208                         '$tabs' => $tabs,
209                         '$notif_content' => $notif_content,
210                 ));
211                 
212                 $o .= paginate($a);
213                 return $o;
214                                 
215         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
216                 
217                 $notif_tpl = get_markup_template('notifications.tpl');
218                 
219                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
220                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
221                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
222                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
223                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
224                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
225                         intval(local_user())
226                 );
227                 
228                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
229                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
230                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
231                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
232                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
233                 
234                 $notif_content = '';
235                 
236                 if (count($r) > 0) {
237                         
238                         foreach ($r as $it) {
239                                 switch($it['verb']){
240                                         case ACTIVITY_LIKE:
241                                                 $notif_content .= replace_macros($tpl_item_likes,array(
242                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
243                                                         '$item_image' => $it['author-avatar'],
244                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
245                                                         '$item_when' => relative_date($it['created'])
246                                                 ));
247                                                 break;
248                                                 
249                                         case ACTIVITY_DISLIKE:
250                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
251                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
252                                                         '$item_image' => $it['author-avatar'],
253                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
254                                                         '$item_when' => relative_date($it['created'])
255                                                 ));
256                                                 break;
257                                                 
258                                         case ACTIVITY_FRIEND:
259                                         
260                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
261                                                 $obj = parse_xml_string($xmlhead.$it['object']);
262                                                 $it['fname'] = $obj->title;
263                                                 
264                                                 $notif_content .= replace_macros($tpl_item_friends,array(
265                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
266                                                         '$item_image' => $it['author-avatar'],
267                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
268                                                         '$item_when' => relative_date($it['created'])
269                                                 ));
270                                                 break;
271                                                 
272                                         default:
273                                                 $item_text = (($it['id'] == $it['parent'])
274                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
275                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
276                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
277
278                                                 $notif_content .= replace_macros($tpl,array(
279                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
280                                                         '$item_image' => $it['author-avatar'],
281                                                         '$item_text' => $item_text,
282                                                         '$item_when' => relative_date($it['created'])
283                                                 ));
284                                 }
285                         }
286                         
287                 } else {
288                         
289                         $notif_content = t('No more network notifications.');
290                 }
291                 
292                 $o .= replace_macros($notif_tpl,array(
293                         '$notif_header' => t('Notifications'),
294                         '$tabs' => $tabs,
295                         '$notif_content' => $notif_content,
296                 ));
297                 
298         } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
299                 
300                 $notif_tpl = get_markup_template('notifications.tpl');
301                 
302                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
303                 $myurl = substr($myurl,strpos($myurl,'://')+3);
304                 $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
305                 $diasp_url = str_replace('/profile/','/u/',$myurl);
306                 $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' or `item`.`tag` regexp '%s' ) ",
307                         dbesc($myurl . '$'),
308                         dbesc($myurl . '\\]'),
309                         dbesc($diasp_url . '\\]')
310                 );
311
312
313                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
314                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
315                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
316                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
317                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 
318                                 $sql_extra
319                                 AND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
320                         intval(local_user())
321                 );
322                 
323                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
324                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
325                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
326                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
327                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
328                 
329                 $notif_content = '';
330                 
331                 if (count($r) > 0) {
332                         
333                         foreach ($r as $it) {
334                                 switch($it['verb']){
335                                         case ACTIVITY_LIKE:
336                                                 $notif_content .= replace_macros($tpl_item_likes,array(
337                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
338                                                         '$item_image' => $it['author-avatar'],
339                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
340                                                         '$item_when' => relative_date($it['created'])
341                                                 ));
342                                                 break;
343                                                 
344                                         case ACTIVITY_DISLIKE:
345                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
346                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
347                                                         '$item_image' => $it['author-avatar'],
348                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
349                                                         '$item_when' => relative_date($it['created'])
350                                                 ));
351                                                 break;
352                                                 
353                                         case ACTIVITY_FRIEND:
354                                         
355                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
356                                                 $obj = parse_xml_string($xmlhead.$it['object']);
357                                                 $it['fname'] = $obj->title;
358                                                 
359                                                 $notif_content .= replace_macros($tpl_item_friends,array(
360                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
361                                                         '$item_image' => $it['author-avatar'],
362                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
363                                                         '$item_when' => relative_date($it['created'])
364                                                 ));
365                                                 break;
366                                                 
367                                         default:
368                                                 $item_text = (($it['id'] == $it['parent'])
369                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
370                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
371                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
372
373                                                 $notif_content .= replace_macros($tpl,array(
374                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
375                                                         '$item_image' => $it['author-avatar'],
376                                                         '$item_text' => $item_text,
377                                                         '$item_when' => relative_date($it['created'])
378                                                 ));
379                                 }
380                         }
381                         
382                 } else {
383                         
384                         $notif_content = t('No more personal notifications.');
385                 }
386                 
387                 $o .= replace_macros($notif_tpl,array(
388                         '$notif_header' => t('Notifications'),
389                         '$tabs' => $tabs,
390                         '$notif_content' => $notif_content,
391                 ));
392                 
393
394
395
396
397
398         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
399                 
400                 $notif_tpl = get_markup_template('notifications.tpl');
401                 
402                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
403                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
404                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
405                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
406                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
407                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC",
408                         intval(local_user())
409                 );
410                 
411                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
412                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
413                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
414                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
415                 
416                 $notif_content = '';
417                 
418                 if (count($r) > 0) {
419                         
420                         foreach ($r as $it) {
421                                 switch($it['verb']){
422                                         case ACTIVITY_LIKE:
423                                                 $notif_content .= replace_macros($tpl_item_likes,array(
424                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
425                                                         '$item_image' => $it['author-avatar'],
426                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
427                                                         '$item_when' => relative_date($it['created'])
428                                                 ));
429
430                                                 break;
431                                         case ACTIVITY_DISLIKE:
432                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
433                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
434                                                         '$item_image' => $it['author-avatar'],
435                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
436                                                         '$item_when' => relative_date($it['created'])
437                                                 ));
438
439                                                 break;
440                                         case ACTIVITY_FRIEND:
441                                         
442                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
443                                                 $obj = parse_xml_string($xmlhead.$it['object']);
444                                                 $it['fname'] = $obj->title;
445                                                 
446                                                 $notif_content .= replace_macros($tpl_item_friends,array(
447                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
448                                                         '$item_image' => $it['author-avatar'],
449                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
450                                                         '$item_when' => relative_date($it['created'])
451                                                 ));
452
453                                                 break;
454                                         default:
455                                                 $notif_content .= replace_macros($tpl_item_comments,array(
456                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
457                                                         '$item_image' => $it['author-avatar'],
458                                                         '$item_text' => sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']),
459                                                         '$item_when' => relative_date($it['created'])
460                                                 ));
461                                 }
462                         }
463                                 
464                 } else {
465                         $notif_content = t('No more home notifications.');
466                 }
467                 
468                 $o .= replace_macros($notif_tpl,array(
469                         '$notif_header' => t('Notifications'),
470                         '$tabs' => $tabs,
471                         '$notif_content' => $notif_content,
472                 ));
473         }
474
475         $o .= paginate($a);
476         return $o;
477 }