3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Events;
24 use Friendica\Database\DBA;
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;
33 class Json extends \Friendica\BaseModule
35 protected function rawContent(array $request = [])
38 throw new HTTPException\UnauthorizedException();
41 $y = intval(DateTimeFormat::localNow('Y'));
42 $m = intval(DateTimeFormat::localNow('m'));
44 // Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
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);
53 if (!empty($_GET['start'])) {
54 $start = $_GET['start'];
57 if (!empty($_GET['end'])) {
58 $finish = $_GET['end'];
61 // put the event parametes in an array so we can better transmit them
63 'event_id' => intval($_GET['id'] ?? 0),
69 // get events by id or by date
70 if ($event_params['event_id']) {
71 $r = Event::getListById(local_user(), $event_params['event_id']);
73 $r = Event::getListByDate(local_user(), $event_params);
78 if (DBA::isResult($r)) {
79 $r = Event::sortByDate($r);
81 $j = DateTimeFormat::utc($rr['start'], 'j');
82 if (empty($links[$j])) {
83 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
90 // transform the event in a usable array
91 if (DBA::isResult($r)) {
92 $events = Event::sortByDate($r);
94 $events = self::map($events);
97 header('Content-Type: application/json');
98 echo json_encode($events);
102 private static function map(array $events): array
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)];
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'],