[frio] Fix event time display

This commit is contained in:
Hypolite Petovan 2021-10-03 13:29:28 -04:00
parent 9935e8e711
commit a9bb53b021
4 changed files with 123 additions and 46 deletions

View file

@ -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 {

104
src/Module/Events/Json.php Normal file
View file

@ -0,0 +1,104 @@
<?php
namespace Friendica\Module\Events;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Event;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Temporal;
class Json extends \Friendica\BaseModule
{
public static function rawContent(array $parameters = [])
{
if (!local_user()) {
throw new HTTPException\UnauthorizedException();
}
$y = intval(DateTimeFormat::localNow('Y'));
$m = intval(DateTimeFormat::localNow('m'));
// Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
if ($y < 1901) {
$y = 1900;
}
$dim = Temporal::getDaysInMonth($y, $m);
$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 (!empty($_GET['start'])) {
$start = $_GET['start'];
}
if (!empty($_GET['end'])) {
$finish = $_GET['end'];
}
// put the event parametes in an array so we can better transmit them
$event_params = [
'event_id' => 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);
}
}

View file

@ -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]],

View file

@ -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 () {
"<img src='{0}' style='height:12px; width:12px'>{1}<p>{2}</p><p>{3}</p>".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 () {
"<img src='{0}' style='height:24px;width:24px'>{1}<p>{2}</p><p>{3}</p>".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