]> git.mxchange.org Git - friendica.git/blob - src/Module/Events/Json.php
Merge remote-tracking branch 'upstream/develop' into forum3
[friendica.git] / src / Module / Events / 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\Events;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Event;
27 use Friendica\Model\Item;
28 use Friendica\Model\Post;
29 use Friendica\Network\HTTPException;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Temporal;
32
33 class Json extends \Friendica\BaseModule
34 {
35         protected function rawContent(array $request = [])
36         {
37                 if (!local_user()) {
38                         throw new HTTPException\UnauthorizedException();
39                 }
40
41                 $y = intval(DateTimeFormat::localNow('Y'));
42                 $m = intval(DateTimeFormat::localNow('m'));
43
44                 // Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
45                 if ($y < 1901) {
46                         $y = 1900;
47                 }
48
49                 $dim    = Temporal::getDaysInMonth($y, $m);
50                 $start  = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
51                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
52
53                 if (!empty($_GET['start'])) {
54                         $start = $_GET['start'];
55                 }
56
57                 if (!empty($_GET['end'])) {
58                         $finish = $_GET['end'];
59                 }
60
61                 // put the event parametes in an array so we can better transmit them
62                 $event_params = [
63                         'event_id' => intval($_GET['id'] ?? 0),
64                         'start'    => $start,
65                         'finish'   => $finish,
66                         'ignore'   => 0,
67                 ];
68
69                 // get events by id or by date
70                 if ($event_params['event_id']) {
71                         $r = Event::getListById(local_user(), $event_params['event_id']);
72                 } else {
73                         $r = Event::getListByDate(local_user(), $event_params);
74                 }
75
76                 $links = [];
77
78                 if (DBA::isResult($r)) {
79                         $r = Event::sortByDate($r);
80                         foreach ($r as $rr) {
81                                 $j = DateTimeFormat::utc($rr['start'], 'j');
82                                 if (empty($links[$j])) {
83                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
84                                 }
85                         }
86                 }
87
88                 $events = [];
89
90                 // transform the event in a usable array
91                 if (DBA::isResult($r)) {
92                         $events = Event::sortByDate($r);
93
94                         $events = self::map($events);
95                 }
96
97                 header('Content-Type: application/json');
98                 echo json_encode($events);
99                 exit();
100         }
101
102         private static function map(array $events): array
103         {
104                 return array_map(function ($event) {
105                         $item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private', 'uri-id'], ['id' => $event['itemid']]);
106                         if (!DBA::isResult($item)) {
107                                 // Using default values when no item had been found
108                                 $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC, 'uri-id' => ($event['uri-id'] ?? 0)];
109                         }
110
111                         return [
112                                 'id'       => $event['id'],
113                                 'title'    => $event['summary'],
114                                 'start'    => DateTimeFormat::local($event['start']),
115                                 'end'      => DateTimeFormat::local($event['finish']),
116                                 'nofinish' => $event['nofinish'],
117                                 'desc'     => $event['desc'],
118                                 'location' => $event['location'],
119                                 'item'     => $item,
120                         ];
121                 }, $events);
122         }
123 }