diff --git a/mod/events.php b/mod/events.php index 6ce322a4df..1b8a509b70 100644 --- a/mod/events.php +++ b/mod/events.php @@ -49,12 +49,6 @@ function events_init(App $a) return; } - // If it's a json request abort here because we don't - // need the widget data - if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] === 'json') { - return; - } - if (empty(DI::page()['aside'])) { DI::page()['aside'] = ''; } @@ -321,19 +315,6 @@ function events_content(App $a) $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0); $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59); - if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] === 'json') { - if (!empty($_GET['start'])) { - $start = $_GET['start']; - } - if (!empty($_GET['end'])) { - $finish = $_GET['end']; - } - } - - $start = DateTimeFormat::utc($start); - $finish = DateTimeFormat::utc($finish); - - // put the event parametes in an array so we can better transmit them $event_params = [ 'event_id' => intval($_GET['id'] ?? 0), @@ -369,12 +350,6 @@ function events_content(App $a) $events = Event::prepareListForTemplate($r); } - if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] === 'json') { - header('Content-Type: application/json'); - echo json_encode($events); - exit(); - } - if (!empty($_GET['id'])) { $tpl = Renderer::getMarkupTemplate("event.tpl"); } else { diff --git a/src/Module/Events/Json.php b/src/Module/Events/Json.php new file mode 100644 index 0000000000..50e468a9c7 --- /dev/null +++ b/src/Module/Events/Json.php @@ -0,0 +1,104 @@ + intval($_GET['id'] ?? 0), + 'start' => $start, + 'finish' => $finish, + 'ignore' => 0, + ]; + + // get events by id or by date + if ($event_params['event_id']) { + $r = Event::getListById(local_user(), $event_params['event_id']); + } else { + $r = Event::getListByDate(local_user(), $event_params); + } + + $links = []; + + if (DBA::isResult($r)) { + $r = Event::sortByDate($r); + foreach ($r as $rr) { + $j = DateTimeFormat::utc($rr['start'], 'j'); + if (empty($links[$j])) { + $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j; + } + } + } + + $events = []; + + // transform the event in a usable array + if (DBA::isResult($r)) { + $events = Event::sortByDate($r); + + $events = self::map($events); + } + + header('Content-Type: application/json'); + echo json_encode($events); + exit(); + } + + private static function map(array $events): array + { + return array_map(function ($event) { + $item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private', 'uri-id'], ['id' => $event['itemid']]); + if (!DBA::isResult($item)) { + // Using default values when no item had been found + $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC, 'uri-id' => ($event['uri-id'] ?? 0)]; + } + + return [ + 'id' => $event['id'], + 'title' => $event['summary'], + 'start' => DateTimeFormat::local($event['start']), + 'end' => DateTimeFormat::local($event['finish']), + 'nofinish' => $event['nofinish'], + 'desc' => $event['desc'], + 'location' => $event['location'], + 'item' => $item, + ]; + }, $events); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 1bcae4a7d3..2f20d51770 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -259,6 +259,8 @@ return [ '/dirfind' => [Module\Search\Directory::class, [R::GET]], '/directory' => [Module\Directory::class, [R::GET]], + '/events/json' => [Module\Events\Json::class, [R::GET]], + '/feed' => [ '/{nickname}' => [Module\Feed::class, [R::GET]], '/{nickname}/posts' => [Module\Feed::class, [R::GET]], diff --git a/view/theme/frio/js/mod_events.js b/view/theme/frio/js/mod_events.js index 464afc3e60..c1f22761c4 100644 --- a/view/theme/frio/js/mod_events.js +++ b/view/theme/frio/js/mod_events.js @@ -42,7 +42,6 @@ $(document).ready(function () { defaultView: "month", aspectRatio: 1, eventRender: function (event, element, view) { - //console.log(view.name); switch (view.name) { case "month": element @@ -52,8 +51,8 @@ $(document).ready(function () { event.item["author-avatar"], event.item["author-name"], event.title, - event.item.desc, - event.item.location, + event.desc, + event.location, ), ); break; @@ -65,8 +64,8 @@ $(document).ready(function () { "{1}

{2}

{3}

".format( event.item["author-avatar"], event.item["author-name"], - event.item.desc, - htmlToText(event.item.location), + event.desc, + htmlToText(event.location), ), ); break; @@ -78,8 +77,8 @@ $(document).ready(function () { "{1}

{2}

{3}

".format( event.item["author-avatar"], event.item["author-name"], - event.item.desc, - htmlToText(event.item.location), + event.desc, + htmlToText(event.location), ), ); break; @@ -252,27 +251,24 @@ function eventHoverHtmlContent(event) { moment.locale(locale); // format dates to different styles - var startDate = moment(event.item.start).format("dd HH:mm"); - var endDate = moment(event.item.finsih).format("dd HH:mm"); - var monthShort = moment(event.item.start).format("MMM"); - var dayNumberStart = moment(event.item.start).format("DD"); - var dayNumberEnd = moment(event.item.finish).format("DD"); - var startTime = moment(event.item.start).format("HH:mm"); - var endTime = moment(event.item.finish).format("HH:mm"); - var monthNumber; + var startDate = event.start.format('dd HH:mm'); + var monthShort = event.start.format('MMM'); + var dayNumberStart = event.start.format('DD'); var formattedDate = startDate; // We only need the to format the end date if the event does have // a finish date. - if (event.item.nofinish == 0) { + if (event.nofinish === 0 && event.end !== null) { + var dayNumberEnd = event.end.format('DD'); + var endTime = event.end.format('HH:mm'); + formattedDate = startDate + " - " + endTime; // use a different Format (15. Feb - 18. Feb) if the events end date // is not the start date - if (dayNumberStart != dayNumberEnd) { - formattedDate = - moment(event.item.start).format("Do MMM") + " - " + moment(event.item.finish).format("Do MMM"); + if (dayNumberStart !== dayNumberEnd) { + formattedDate = event.start.format('Do MMM') + ' - ' + event.end.format('Do MMM'); } } @@ -280,8 +276,8 @@ function eventHoverHtmlContent(event) { data = eventHoverBodyTemplate(); // Get only template data if there exists location data - if (event.item.location) { - var eventLocationText = htmlToText(event.item.location); + if (event.location) { + var eventLocationText = htmlToText(event.location); // Get the the html template for formatting the location var eventLocationTemplate = eventHoverLocationTemplate(); // Format the event location data according to the the event location