]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
3182a790c6f93bb2104deda92c40cb711751d89a
[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('Home'),
77                         'url' => $a->get_baseurl() . '/notifications/home',
78                         'sel'=> (($a->argv[1] == 'home') ? 'active' : ''),
79                 ),
80                 array(
81                         'label' => t('Introductions'),
82                         'url' => $a->get_baseurl() . '/notifications/intros',
83                         'sel'=> (($a->argv[1] == 'intros') ? 'active' : ''),
84                 ),
85                 array(
86                         'label' => t('Messages'),
87                         'url' => $a->get_baseurl() . '/message',
88                         'sel'=> '',
89                 ),
90         );
91         
92         $o = "";
93
94         
95         if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
96                 nav_set_selected('introductions');
97                 if(($a->argc > 2) && ($a->argv[2] == 'all'))
98                         $sql_extra = '';
99                 else
100                         $sql_extra = " AND `ignore` = 0 ";
101                 
102                 $notif_tpl = get_markup_template('notifications.tpl');
103                 
104                 $notif_content .= '<a href="' . ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ) . '" id="notifications-show-hide-link" >'
105                         . ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
106
107                 $r = q("SELECT COUNT(*) AS `total` FROM `intro` 
108                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
109                                 intval($_SESSION['uid'])
110                 );
111                 if($r && count($r)) {
112                         $a->set_pager_total($r[0]['total']);
113                         $a->set_pager_itemspage(20);
114                 }
115
116                 $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`
117                         FROM `intro` LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
118                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
119                                 intval($_SESSION['uid']));
120
121                 if(($r !== false) && (count($r))) {
122
123                         $sugg = get_markup_template('suggestions.tpl');
124                         $tpl = get_markup_template("intros.tpl");
125
126                         foreach($r as $rr) {
127                                 if($rr['fid']) {
128
129                                         $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . (($a->path) ? '/' . $a->path : ''));
130                                         $notif_content .= replace_macros($sugg,array(
131                                                 '$str_notifytype' => t('Notification type: '),
132                                                 '$notify_type' => t('Friend Suggestion'),
133                                                 '$intro_id' => $rr['intro_id'],
134                                                 '$madeby' => sprintf( t('suggested by %s'),$rr['name']),
135                                                 '$contact_id' => $rr['contact-id'],
136                                                 '$photo' => ((x($rr,'fphoto')) ? $rr['fphoto'] : "images/default-profile.jpg"),
137                                                 '$fullname' => $rr['fname'],
138                                                 '$url' => $rr['furl'],
139                                                 '$knowyou' => $knowyou,
140                                                 '$approve' => t('Approve'),
141                                                 '$note' => $rr['note'],
142                                                 '$request' => $rr['frequest'] . '?addr=' . $return_addr,
143                                                 '$ignore' => t('Ignore'),
144                                                 '$discard' => t('Discard')
145
146                                         ));
147
148                                         continue;
149
150                                 }
151                                 $friend_selected = (($rr['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
152                                 $fan_selected = (($rr['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
153                                 $dfrn_tpl = get_markup_template('netfriend.tpl');
154
155                                 $knowyou   = '';
156                                 $dfrn_text = '';
157
158                                 if($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
159                                         if($rr['network'] === NETWORK_DFRN)
160                                                 $knowyou = t('Claims to be known to you: ') . (($rr['knowyou']) ? t('yes') : t('no'));
161                                         else
162                                                 $knowyou = '';
163                                         $dfrn_text = replace_macros($dfrn_tpl,array(
164                                                 '$intro_id' => $rr['intro_id'],
165                                                 '$friend_selected' => $friend_selected,
166                                                 '$fan_selected' => $fan_selected,
167                                                 '$approve_as' => t('Approve as: '),
168                                                 '$as_friend' => t('Friend'),
169                                                 '$as_fan' => (($rr['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Fan/Admirer'))
170                                         ));
171                                 }                       
172
173                                 $notif_content .= replace_macros($tpl,array(
174                                         '$str_notifytype' => t('Notification type: '),
175                                         '$notify_type' => (($rr['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
176                                         '$dfrn_text' => $dfrn_text,     
177                                         '$dfrn_id' => $rr['issued-id'],
178                                         '$uid' => $_SESSION['uid'],
179                                         '$intro_id' => $rr['intro_id'],
180                                         '$contact_id' => $rr['contact-id'],
181                                         '$photo' => ((x($rr,'photo')) ? $rr['photo'] : "images/default-profile.jpg"),
182                                         '$fullname' => $rr['name'],
183                                         '$url' => $rr['url'],
184                                         '$knowyou' => $knowyou,
185                                         '$approve' => t('Approve'),
186                                         '$note' => $rr['note'],
187                                         '$ignore' => t('Ignore'),
188                                         '$discard' => t('Discard')
189
190                                 ));
191                         }
192                 }
193                 else
194                         info( t('No notifications.') . EOL);
195                 
196                 $o .= replace_macros($notif_tpl,array(
197                         '$notif_header' => t('Notifications'),
198                         '$tabs' => $tabs,
199                         '$notif_content' => $notif_content,
200                         '$activetab' => 'intros'
201                 ));
202                 
203                 $o .= paginate($a);
204                 return $o;
205                                 
206         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
207                 
208                 $notif_tpl = get_markup_template('notifications.tpl');
209                 
210                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
211                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
212                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
213                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
214                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
215                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC" ,
216                         intval(local_user())
217                 );
218                 
219                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
220                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
221                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
222                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
223                 $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
224                 
225                 $notif_content = '';
226                 
227                 if (count($r) > 0) {
228                         
229                         foreach ($r as $it) {
230                                 switch($it['verb']){
231                                         case ACTIVITY_LIKE:
232                                                 $notif_content .= replace_macros($tpl_item_likes,array(
233                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
234                                                         '$item_image' => $it['author-avatar'],
235                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
236                                                         '$item_when' => relative_date($it['created'])
237                                                 ));
238                                                 break;
239                                                 
240                                         case ACTIVITY_DISLIKE:
241                                                 $notif_content .= replace_macros($tpl_item_dislikes,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 disliked %s's post"), $it['author-name'], $it['pname']),
245                                                         '$item_when' => relative_date($it['created'])
246                                                 ));
247                                                 break;
248                                                 
249                                         case ACTIVITY_FRIEND:
250                                         
251                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
252                                                 $obj = parse_xml_string($xmlhead.$it['object']);
253                                                 $it['fname'] = $obj->title;
254                                                 
255                                                 $notif_content .= replace_macros($tpl_item_friends,array(
256                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
257                                                         '$item_image' => $it['author-avatar'],
258                                                         '$item_text' => sprintf( t("%s is now friends with %s"), $it['author-name'], $it['fname']),
259                                                         '$item_when' => relative_date($it['created'])
260                                                 ));
261                                                 break;
262                                                 
263                                         default:
264                                                 $item_text = (($it['id'] == $it['parent'])
265                                                         ? sprintf( t("%s created a new post"), $it['author-name'])
266                                                         : sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']));
267                                                 $tpl = (($it['id'] == $it['parent']) ? $tpl_item_posts : $tpl_item_comments);
268
269                                                 $notif_content .= replace_macros($tpl,array(
270                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
271                                                         '$item_image' => $it['author-avatar'],
272                                                         '$item_text' => $item_text,
273                                                         '$item_when' => relative_date($it['created'])
274                                                 ));
275                                 }
276                         }
277                         
278                 } else {
279                         
280                         $notif_content = t('Nothing new!');
281                 }
282                 
283                 $o .= replace_macros($notif_tpl,array(
284                         '$notif_header' => t('Notifications'),
285                         '$tabs' => $tabs,
286                         '$notif_content' => $notif_content,
287                         '$activetab' => 'network'
288                 ));
289                 
290         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
291                 
292                 $notif_tpl = get_markup_template('notifications.tpl');
293                 
294                 $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, 
295                                 `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`, 
296                                 `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` 
297                                 FROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`
298                                 WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
299                                  `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC",
300                         intval(local_user())
301                 );
302                 
303                 $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
304                 $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
305                 $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
306                 $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
307                 
308                 $notif_content = '';
309                 
310                 if (count($r) > 0) {
311                         
312                         foreach ($r as $it) {
313                                 switch($it['verb']){
314                                         case ACTIVITY_LIKE:
315                                                 $notif_content .= replace_macros($tpl_item_likes,array(
316                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
317                                                         '$item_image' => $it['author-avatar'],
318                                                         '$item_text' => sprintf( t("%s liked %s's post"), $it['author-name'], $it['pname']),
319                                                         '$item_when' => relative_date($it['created'])
320                                                 ));
321
322                                                 break;
323                                         case ACTIVITY_DISLIKE:
324                                                 $notif_content .= replace_macros($tpl_item_dislikes,array(
325                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
326                                                         '$item_image' => $it['author-avatar'],
327                                                         '$item_text' => sprintf( t("%s disliked %s's post"), $it['author-name'], $it['pname']),
328                                                         '$item_when' => relative_date($it['created'])
329                                                 ));
330
331                                                 break;
332                                         case ACTIVITY_FRIEND:
333                                         
334                                                 $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
335                                                 $obj = parse_xml_string($xmlhead.$it['object']);
336                                                 $it['fname'] = $obj->title;
337                                                 
338                                                 $notif_content .= replace_macros($tpl_item_friends,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 is now friends with %s"), $it['author-name'], $it['fname']),
342                                                         '$item_when' => relative_date($it['created'])
343                                                 ));
344
345                                                 break;
346                                         default:
347                                                 $notif_content .= replace_macros($tpl_item_comments,array(
348                                                         '$item_link' => $a->get_baseurl().'/display/'.$a->user['nickname']."/".$it['parent'],
349                                                         '$item_image' => $it['author-avatar'],
350                                                         '$item_text' => sprintf( t("%s commented on %s's post"), $it['author-name'], $it['pname']),
351                                                         '$item_when' => relative_date($it['created'])
352                                                 ));
353                                 }
354                         }
355                                 
356                 } else {
357                         $notif_content = t('Nothing new!');
358                 }
359                 
360                 $o .= replace_macros($notif_tpl,array(
361                         '$notif_header' => t('Notifications'),
362                         '$tabs' => $tabs,
363                         '$notif_content' => $notif_content,
364                         '$activetab' => 'home'
365                 ));
366         }
367
368         $o .= paginate($a);
369         return $o;
370 }