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