7f9974d30a1e5737b1d61ddc4dd089a29641c70e
[friendica.git/.git] / mod / notify.php
1 <?php
2 /**
3  * @file mod/notify.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\L10n;
9 use Friendica\Core\NotificationsManager;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Item;
14 use Friendica\Module\Login;
15 use Friendica\Util\Temporal;
16
17 function notify_init(App $a)
18 {
19         if (! local_user()) {
20                 return;
21         }
22
23         $nm = new NotificationsManager();
24
25         if ($a->argc > 2 && $a->argv[1] === 'view' && intval($a->argv[2])) {
26                 $note = $nm->getByID($a->argv[2]);
27                 if ($note) {
28                         $nm->setSeen($note);
29                         System::externalRedirect($note['link']);
30                 }
31
32                 $a->internalRedirect();
33         }
34
35         if ($a->argc > 2 && $a->argv[1] === 'mark' && $a->argv[2] === 'all') {
36                 $r = $nm->setAllSeen();
37                 $j = json_encode(['result' => ($r) ? 'success' : 'fail']);
38                 echo $j;
39                 exit();
40         }
41 }
42
43 function notify_content(App $a)
44 {
45         if (! local_user()) {
46                 return Login::form();
47         }
48
49         $notif_content = '';
50
51         $nm = new NotificationsManager();
52
53         $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
54
55         $not_tpl = Renderer::getMarkupTemplate('notify.tpl');
56
57         $r = $nm->getAll(['seen'=>0]);
58         if (DBA::isResult($r) > 0) {
59                 foreach ($r as $it) {
60                         $notif_content .= Renderer::replaceMacros($not_tpl, [
61                                 '$item_link' => System::baseUrl(true).'/notify/view/'. $it['id'],
62                                 '$item_image' => $it['photo'],
63                                 '$item_text' => strip_tags(BBCode::convert($it['msg'])),
64                                 '$item_when' => Temporal::getRelativeDate($it['date'])
65                         ]);
66                 }
67         } else {
68                 $notif_content .= L10n::t('No more system notifications.');
69         }
70
71         $o = Renderer::replaceMacros($notif_tpl, [
72                 '$notif_header' => L10n::t('System Notifications'),
73                 '$tabs' => false, // $tabs,
74                 '$notif_content' => $notif_content,
75         ]);
76
77         return $o;
78 }