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