3 namespace Friendica\Module\Events;
5 use Friendica\Database\DBA;
7 use Friendica\Model\Event;
8 use Friendica\Model\Item;
9 use Friendica\Model\Post;
10 use Friendica\Network\HTTPException;
11 use Friendica\Util\DateTimeFormat;
12 use Friendica\Util\Temporal;
14 class Json extends \Friendica\BaseModule
16 public function rawContent()
19 throw new HTTPException\UnauthorizedException();
22 $y = intval(DateTimeFormat::localNow('Y'));
23 $m = intval(DateTimeFormat::localNow('m'));
25 // Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
30 $dim = Temporal::getDaysInMonth($y, $m);
31 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
32 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
34 if (!empty($_GET['start'])) {
35 $start = $_GET['start'];
38 if (!empty($_GET['end'])) {
39 $finish = $_GET['end'];
42 // put the event parametes in an array so we can better transmit them
44 'event_id' => intval($_GET['id'] ?? 0),
50 // get events by id or by date
51 if ($event_params['event_id']) {
52 $r = Event::getListById(local_user(), $event_params['event_id']);
54 $r = Event::getListByDate(local_user(), $event_params);
59 if (DBA::isResult($r)) {
60 $r = Event::sortByDate($r);
62 $j = DateTimeFormat::utc($rr['start'], 'j');
63 if (empty($links[$j])) {
64 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
71 // transform the event in a usable array
72 if (DBA::isResult($r)) {
73 $events = Event::sortByDate($r);
75 $events = self::map($events);
78 header('Content-Type: application/json');
79 echo json_encode($events);
83 private static function map(array $events): array
85 return array_map(function ($event) {
86 $item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private', 'uri-id'], ['id' => $event['itemid']]);
87 if (!DBA::isResult($item)) {
88 // Using default values when no item had been found
89 $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC, 'uri-id' => ($event['uri-id'] ?? 0)];
94 'title' => $event['summary'],
95 'start' => DateTimeFormat::local($event['start']),
96 'end' => DateTimeFormat::local($event['finish']),
97 'nofinish' => $event['nofinish'],
98 'desc' => $event['desc'],
99 'location' => $event['location'],