]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
notifications.php: move more code to NotificationsManager
[friendica.git] / mod / notifications.php
1 <?php
2 include_once("include/NotificationsManager.php");
3 include_once("include/bbcode.php");
4 include_once("include/contact_selectors.php");
5 include_once("include/Scrape.php");
6
7 function notifications_post(&$a) {
8
9         if(! local_user()) {
10                 goaway(z_root());
11         }
12
13         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
14
15         if($request_id === "all")
16                 return;
17
18         if($request_id) {
19
20                 $r = q("SELECT * FROM `intro` WHERE `id` = %d  AND `uid` = %d LIMIT 1",
21                         intval($request_id),
22                         intval(local_user())
23                 );
24
25                 if(count($r)) {
26                         $intro_id = $r[0]['id'];
27                         $contact_id = $r[0]['contact-id'];
28                 }
29                 else {
30                         notice( t('Invalid request identifier.') . EOL);
31                         return;
32                 }
33
34                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
35                 // that should not be deleted.
36
37                 $fid = $r[0]['fid'];
38
39                 if($_POST['submit'] == t('Discard')) {
40                         $r = q("DELETE FROM `intro` WHERE `id` = %d",
41                                 intval($intro_id)
42                         );
43                         if(! $fid) {
44
45                                 // The check for blocked and pending is in case the friendship was already approved
46                                 // and we just want to get rid of the now pointless notification
47
48                                 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
49                                         intval($contact_id),
50                                         intval(local_user())
51                                 );
52                         }
53                         goaway('notifications/intros');
54                 }
55                 if($_POST['submit'] == t('Ignore')) {
56                         $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
57                                 intval($intro_id));
58                         goaway('notifications/intros');
59                 }
60         }
61 }
62
63 function notifications_content(&$a) {
64
65         if(! local_user()) {
66                 notice( t('Permission denied.') . EOL);
67                 return;
68         }
69
70         $show   =       (x($_REQUEST,'show')            ? $_REQUEST['show']             : 0);
71         nav_set_selected('notifications');
72
73         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
74
75         $nm = new NotificationsManager();
76
77         $o = '';
78         // get the nav tabs for the notification pages
79         $tabs = $nm->getTabs();
80         $notif_content = array();
81
82         if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
83                 nav_set_selected('introductions');
84
85                 if(($a->argc > 2) && ($a->argv[2] == 'all'))
86                         $sql_extra = '';
87                 else
88                         $sql_extra = " AND `ignore` = 0 ";
89
90                 $notif_header = t('Notifications');
91                 $notif_tpl = get_markup_template('notifications.tpl');
92
93 //              $notif_show_lnk .= '<a href="' . ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ) . '" id="notifications-show-hide-link" >'
94 //                      . ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
95
96                 $notif_show_lnk = array(
97                         'href' => ((strlen($sql_extra)) ? 'notifications/intros/all' : 'notifications/intros' ),
98                         'text' => ((strlen($sql_extra)) ? t('Show Ignored Requests') : t('Hide Ignored Requests')),
99                         'id' => "notifications-show-hide-link",
100                 );
101
102                 $r = q("SELECT COUNT(*) AS `total` FROM `intro`
103                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
104                                 intval($_SESSION['uid'])
105                 );
106                 if(dbm::is_result($r)) {
107                         $a->set_pager_total($r[0]['total']);
108                         $a->set_pager_itemspage(20);
109                 }
110
111                 /// @todo Fetch contact details by "get_contact_details_by_url" instead of queries to contact, fcontact and gcontact
112
113                 $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`,
114                                 `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
115                                 `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
116                                 `gcontact`.`network` AS `gnetwork`
117                         FROM `intro`
118                                 LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
119                                 LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
120                                 LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
121                         WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0 ",
122                                 intval($_SESSION['uid']));
123
124                 if(dbm::is_result($r)) {
125
126                         $sugg = get_markup_template('suggestions.tpl');
127                         $tpl = get_markup_template("intros.tpl");
128
129                         foreach($r as $rr) {
130
131                                 if($rr['fid']) {
132
133                                         $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . (($a->path) ? '/' . $a->path : ''));
134
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')) ? proxy_url($rr['fphoto'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg"),
142                                                 '$fullname' => $rr['fname'],
143                                                 '$url' => zrl($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'), (intval(get_pconfig(local_user(),'system','post_newfriend')) ? '1' : 0), 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                                                 $lbl_knowyou = t('Claims to be known to you: ');
169                                                 $knowyou = (($rr['knowyou']) ? t('yes') : t('no'));
170                                                 $helptext = t('Shall your connection be bidirectional or not? "Friend" implies that you allow to read and you subscribe to their posts. "Fan/Admirer" means that you allow to read but you do not want to read theirs. Approve as: ');
171                                         } else {
172                                                 $knowyou = '';
173                                                 $helptext = t('Shall your connection be bidirectional or not? "Friend" implies that you allow to read and you subscribe to their posts. "Sharer" means that you allow to read but you do not want to read theirs. Approve as: ');
174                                         }
175
176                                         $dfrn_text = replace_macros($dfrn_tpl,array(
177                                                 '$intro_id' => $rr['intro_id'],
178                                                 '$friend_selected' => $friend_selected,
179                                                 '$fan_selected' => $fan_selected,
180                                                 '$approve_as' => $helptext,
181                                                 '$as_friend' => t('Friend'),
182                                                 '$as_fan' => (($rr['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Fan/Admirer'))
183                                         ));
184                                 }
185
186                                 $header = $rr["name"];
187
188                                 $ret = probe_url($rr["url"]);
189
190                                 if ($rr['gnetwork'] == "")
191                                         $rr['gnetwork'] = $ret["network"];
192
193                                 if ($ret["addr"] != "")
194                                         $header .= " <".$ret["addr"].">";
195
196                                 $header .= " (".network_to_name($rr['gnetwork'], $rr['url']).")";
197
198                                 // Don't show these data until you are connected. Diaspora is doing the same.
199                                 if($rr['gnetwork'] === NETWORK_DIASPORA) {
200                                         $rr['glocation'] = "";
201                                         $rr['gabout'] = "";
202                                         $rr['ggender'] = "";
203                                 }
204
205                                 $notif_content[] = replace_macros($tpl, array(
206                                         '$header' => htmlentities($header),
207                                         '$str_notifytype' => t('Notification type: '),
208                                         '$notify_type' => (($rr['network'] !== NETWORK_OSTATUS) ? t('Friend/Connect Request') : t('New Follower')),
209                                         '$dfrn_text' => $dfrn_text,
210                                         '$dfrn_id' => $rr['issued-id'],
211                                         '$uid' => $_SESSION['uid'],
212                                         '$intro_id' => $rr['intro_id'],
213                                         '$contact_id' => $rr['contact-id'],
214                                         '$photo' => ((x($rr,'photo')) ? proxy_url($rr['photo'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg"),
215                                         '$fullname' => $rr['name'],
216                                         '$location' => bbcode($rr['glocation'], false, false),
217                                         '$location_label' => t('Location:'),
218                                         '$about' => bbcode($rr['gabout'], false, false),
219                                         '$about_label' => t('About:'),
220                                         '$keywords' => $rr['gkeywords'],
221                                         '$keywords_label' => t('Tags:'),
222                                         '$gender' => $rr['ggender'],
223                                         '$gender_label' => t('Gender:'),
224                                         '$hidden' => array('hidden', t('Hide this contact from others'), ($rr['hidden'] == 1), ''),
225                                         '$activity' => array('activity', t('Post a new friend activity'), (intval(get_pconfig(local_user(),'system','post_newfriend')) ? '1' : 0), t('if applicable')),
226                                         '$url' => $rr['url'],
227                                         '$zrl' => zrl($rr['url']),
228                                         '$url_label' => t('Profile URL'),
229                                         '$addr' => $rr['addr'],
230                                         '$lbl_knowyou' => $lbl_knowyou,
231                                         '$lbl_network' => t('Network:'),
232                                         '$network' => network_to_name($rr['gnetwork'], $rr['url']),
233                                         '$knowyou' => $knowyou,
234                                         '$approve' => t('Approve'),
235                                         '$note' => $rr['note'],
236                                         '$ignore' => t('Ignore'),
237                                         '$discard' => t('Discard'),
238
239                                 ));
240                         }
241                 }
242                 else
243                         info( t('No introductions.') . EOL);
244
245         } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
246
247                 $notif_header = t('Network Notifications');
248                 $notif_tpl = get_markup_template('notifications.tpl');
249
250                 $notifs = $nm->networkNotifs($show);
251
252                 $notif_show_lnk = array(
253                         'href' => ($show ? 'notifications/network' : 'notifications/network?show=all' ),
254                         'text' => ($show ? t('Show unread') : t('Show all')),
255                 );
256
257                 if(!dbm::is_result($notifs)) {
258                         if($show)
259                                 $notif_show_lnk = array();
260
261                         $notif_nocontent = t('No more network notifications.');
262                 }
263
264         } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
265
266                 $notif_header = t('System Notifications');
267                 $notif_tpl = get_markup_template('notifications.tpl');
268
269                 $notifs = $nm->systemNotifs($show);
270
271                 $notif_show_lnk = array(
272                         'href' => ($show ? 'notifications/system' : 'notifications/system?show=all' ),
273                         'text' => ($show ? t('Show unread') : t('Show all')),
274                 );
275
276                 if(!dbm::is_result($notifs)) {
277                         if($show)
278                                 $notif_show_lnk = array();
279
280                         $notif_nocontent = t('No more system notifications.');
281                 }
282
283         } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
284
285                 $notif_header = t('Personal Notifications');
286                 $notif_tpl = get_markup_template('notifications.tpl');
287
288                 $notifs = $nm->personalNotifs($show);
289
290                 $notif_show_lnk = array(
291                         'href' => ($show ? 'notifications/personal' : 'notifications/personal?show=all' ),
292                         'text' => ($show ? t('Show unread') : t('Show all')),
293                 );
294
295                 if(!dbm::is_result($notifs)) {
296                         if($show)
297                                 $notif_show_lnk = array();
298
299                         $notif_nocontent = t('No more personal notifications.');
300                 }
301
302         } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
303
304                 $notif_header = t('Home Notifications');
305                 $notif_tpl = get_markup_template('notifications.tpl');
306
307                 $notifs = $nm->homeNotifs($show);
308
309                 $notif_show_lnk = array(
310                         'href' => ($show ? 'notifications/home' : 'notifications/home?show=all' ),
311                         'text' => ($show ? t('Show unread') : t('Show all')),
312                 );
313
314                 if(!dbm::is_result($notifs)) {
315                         if($show)
316                                 $notif_show_lnk = array();
317
318                         $notif_nocontent = t('No more home notifications.');
319                 }
320
321         }
322
323         if(count($notifs['notifications']) > 0 ) {
324                 // The template files we need in different cases for formatting the content
325                 $tpl_item_like = 'notifications_likes_item.tpl';
326                 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
327                 $tpl_item_friend = 'notifications_friends_item.tpl';
328                 $tpl_item_comment = 'notifications_comments_item.tpl';
329                 $tpl_item_post = 'notifications_posts_item.tpl';
330                 $tpl_item_notify = 'notify.tpl';
331
332                 foreach ($notifs['notifications'] as $it) {
333                         $tplname = 'tpl_item_'.$it['label'];
334                         $templ = get_markup_template($$tplname);
335
336                         $notif_content[] = replace_macros($templ,array(
337                                 '$item_label' => $it['label'],
338                                 '$item_link' => $it['link'],
339                                 '$item_image' => $it['image'],
340                                 '$item_text' => $it['text'],
341                                 '$item_when' => $it['when'],
342                                 '$item_seen' => $it['seen'],
343                         ));
344                 }
345
346         }
347
348         $o .= replace_macros($notif_tpl, array(
349                 '$notif_header' => $notif_header,
350                 '$tabs' => $tabs,
351                 '$notif_content' => $notif_content,
352                 '$notif_nocontent' => $notif_nocontent,
353                 '$notif_show_lnk' => $notif_show_lnk,
354         ));
355
356         $o .= paginate($a);
357         return $o;
358 }