This commit is contained in:
Daniel Waxweiler 2024-05-14 10:22:40 +02:00
parent 7bc35a3923
commit 6547f3dba7
3 changed files with 70 additions and 8 deletions

View File

@ -18,6 +18,9 @@ export function displayEvents({ events, document, container, maxEventsCount }) {
const list = container.querySelector('ul')
for (let i = 0; i < eventsCount; i++) {
const li = document.createElement('li')
// TODO add style
// TODO add image
const a = createAnchorElement({
document,

View File

@ -40,6 +40,11 @@ final class GraphQlClient {
physicalAddress {
description,
locality
},
picture {
alt,
contentType,
url
}
},
total
@ -75,6 +80,11 @@ final class GraphQlClient {
physicalAddress {
description,
locality
},
picture {
alt,
contentType,
url
}
},
total
@ -85,18 +95,34 @@ final class GraphQlClient {
$afterDatetime = date(\DateTime::ISO8601);
$cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
if ($cachedEvents !== false) {
return $cachedEvents;
}
// TODO
// $cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
// if ($cachedEvents !== false) {
// return $cachedEvents;
// }
$endpoint = $url . '/api';
$data = self::query($endpoint, $query, ['afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
self::checkData($data);
$events = $data['data']['group']['organizedEvents']['elements'];
EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit], $events);
return $events;
foreach ($data['data']['group']['organizedEvents']['elements'] as &$event) {
if ($event['picture']) {
$picture_response = self::get_encoded_image($event['picture']['url']);
if ($picture_response !== false) {
$picture_encoded = 'data:' . $event['picture']['contentType'] . ';base64,' . base64_encode($picture_response);
$event['picture']['base64'] = $picture_encoded;
// TODO
// EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit, 'eventId' => $event['id']], $picture_encoded);
}
}
unset($event);
}
EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit], $data['data']['group']['organizedEvents']['elements']);
return $data['data']['group']['organizedEvents']['elements'];
}
private static function checkData($data) {
@ -110,4 +136,34 @@ final class GraphQlClient {
}
}
}
private static function get_encoded_image($url) {
// Initialize curl handle
$ch = curl_init($url);
// Set curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Set timeout to 60 seconds (adjust as needed)
curl_setopt($ch, CURLOPT_VERBOSE, 1); // TODO
// Execute the request
$image_data = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
print_r(curl_error($ch));
throw new \Error('Error: ' . curl_error($ch));
// return false;
}
// Close curl handle
curl_close($ch);
// Encode image data (base64 in this example)
// $encoded_image = base64_encode($image_data);
return $image_data;
}
}

View File

@ -8,8 +8,11 @@ if (!defined('ABSPATH')) {
?>
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list">
<ul style="list-style-type: none; padding-left: 0;">
<?php foreach($events as $event) { ?>
<li>
<?php foreach ($events as $event) { ?>
<li style="margin-top: 10px;">
<?php if (isset($event['picture'])) { ?>
<img alt="<?php echo esc_attr($event['picture']['alt']); ?>" src="<?php echo esc_attr($event['picture']['base64']); ?>" style="display: block;">
<?php } ?>
<a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html_e($event['title']); ?></a>
<br>
<?php echo esc_html_e(Formatter::format_date($locale, $timeZone, $event['beginsOn'], $event['endsOn'], $isShortOffsetNameShown)); ?>