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\Calendar;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
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;
34 class Json extends \Friendica\BaseModule
36 protected function rawContent(array $request = [])
38 if (!DI::userSession()->getLocalUserId()) {
39 throw new HTTPException\UnauthorizedException();
42 $y = intval(DateTimeFormat::localNow('Y'));
43 $m = intval(DateTimeFormat::localNow('m'));
45 // Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
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);
54 if (!empty($request['start'])) {
55 $start = $request['start'];
58 if (!empty($request['end'])) {
59 $finish = $request['end'];
62 // put the event parametes in an array so we can better transmit them
64 'event_id' => intval($request['id'] ?? 0),
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']);
74 $r = Event::getListByDate(DI::userSession()->getLocalUserId(), $event_params);
79 if (DBA::isResult($r)) {
80 $r = Event::sortByDate($r);
82 $j = DateTimeFormat::utc($rr['start'], 'j');
83 if (empty($links[$j])) {
84 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
91 // transform the event in a usable array
92 if (DBA::isResult($r)) {
93 $events = Event::sortByDate($r);
95 $events = self::map($events);
98 System::jsonExit($events);
101 private static function map(array $events): array
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)];
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'],