]> git.mxchange.org Git - friendica.git/blob - src/Module/Calendar/Json.php
Issue 11831: Activate "notify on new post" via API
[friendica.git] / src / Module / Calendar / Json.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Calendar;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Event;
28 use Friendica\Model\Item;
29 use Friendica\Model\Post;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Temporal;
33
34 class Json extends \Friendica\BaseModule
35 {
36         protected function rawContent(array $request = [])
37         {
38                 if (!DI::userSession()->getLocalUserId()) {
39                         throw new HTTPException\UnauthorizedException();
40                 }
41
42                 $y = intval(DateTimeFormat::localNow('Y'));
43                 $m = intval(DateTimeFormat::localNow('m'));
44
45                 // Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
46                 if ($y < 1901) {
47                         $y = 1900;
48                 }
49
50                 $dim    = Temporal::getDaysInMonth($y, $m);
51                 $start  = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
52                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
53
54                 if (!empty($request['start'])) {
55                         $start = $request['start'];
56                 }
57
58                 if (!empty($request['end'])) {
59                         $finish = $request['end'];
60                 }
61
62                 // put the event parametes in an array so we can better transmit them
63                 $event_params = [
64                         'event_id' => intval($request['id'] ?? 0),
65                         'start'    => $start,
66                         'finish'   => $finish,
67                         'ignore'   => 0,
68                 ];
69
70                 // get events by id or by date
71                 if ($event_params['event_id']) {
72                         $r = Event::getListById(DI::userSession()->getLocalUserId(), $event_params['event_id']);
73                 } else {
74                         $r = Event::getListByDate(DI::userSession()->getLocalUserId(), $event_params);
75                 }
76
77                 $links = [];
78
79                 if (DBA::isResult($r)) {
80                         $r = Event::sortByDate($r);
81                         foreach ($r as $rr) {
82                                 $j = DateTimeFormat::utc($rr['start'], 'j');
83                                 if (empty($links[$j])) {
84                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
85                                 }
86                         }
87                 }
88
89                 $events = [];
90
91                 // transform the event in a usable array
92                 if (DBA::isResult($r)) {
93                         $events = Event::sortByDate($r);
94
95                         $events = self::map($events);
96                 }
97
98                 System::jsonExit($events);
99         }
100
101         private static function map(array $events): array
102         {
103                 return array_map(function ($event) {
104                         $item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private', 'uri-id'], ['id' => $event['itemid']]);
105                         if (!DBA::isResult($item)) {
106                                 // Using default values when no item had been found
107                                 $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC, 'uri-id' => ($event['uri-id'] ?? 0)];
108                         }
109
110                         return [
111                                 'id'       => $event['id'],
112                                 'title'    => $event['summary'],
113                                 'start'    => DateTimeFormat::local($event['start']),
114                                 'end'      => DateTimeFormat::local($event['finish']),
115                                 'nofinish' => $event['nofinish'],
116                                 'desc'     => $event['desc'],
117                                 'location' => $event['location'],
118                                 'item'     => $item,
119                         ];
120                 }, $events);
121         }
122 }