Skip to content

Commit

Permalink
display event picture (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwaxweiler authored Aug 4, 2024
1 parent a543a25 commit 0edad98
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions source/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### [Unreleased]
#### Added
- Display event picture if available
#### Changed
#### Deprecated
#### Removed
Expand Down
15 changes: 14 additions & 1 deletion source/front/events-displayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Formatter from './formatter.js'
import { createAnchorElement } from './html-creator.js'
import { createAnchorElement, createImageElement } from './html-creator.js'

export function clearEventsList(container) {
const list = container.querySelector('ul')
Expand All @@ -18,6 +18,19 @@ export function displayEvents({ events, document, container, maxEventsCount }) {
const list = container.querySelector('ul')
for (let i = 0; i < eventsCount; i++) {
const li = document.createElement('li')
li.style.lineHeight = '150%'
li.style.marginTop = '20px'

if (events[i].picture) {
const img = createImageElement({
document,
alt: events[i].picture.alt ? events[i].picture.alt : '',
src: events[i].picture.base64 ? events[i].picture.base64 : '',
})
img.style.display = 'block'
img.style.maxWidth = '100%'
li.appendChild(img)
}

const a = createAnchorElement({
document,
Expand Down
7 changes: 7 additions & 0 deletions source/front/html-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ export function createAnchorElement({ document, text, url }) {
a.innerHTML = text
return a
}

export function createImageElement({ document, alt, src }) {
const img = document.createElement('img')
img.setAttribute('alt', alt)
img.setAttribute('src', src)
return img
}
57 changes: 57 additions & 0 deletions source/includes/GraphQlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static function get_upcoming_events(string $url, int $limit): array {
physicalAddress {
description,
locality
},
picture {
alt,
contentType,
url
}
},
total
Expand All @@ -57,6 +62,17 @@ public static function get_upcoming_events(string $url, int $limit): array {
self::checkData($data);

$events = $data['data']['events']['elements'];
foreach ($events as &$event) {
if ($event['picture']) {
$picture_response = self::download_image($event['picture']['url']);
if ($picture_response !== false) {
$picture_encoded = 'data:' . $event['picture']['contentType'] . ';base64,' . base64_encode($picture_response);
$event['picture']['base64'] = $picture_encoded;
}
}
unset($event);
}

EventsCache::set(['url' => $url, 'query' => $query, 'limit' => $limit], $events);
return $events;
}
Expand All @@ -75,6 +91,11 @@ public static function get_upcoming_events_by_group_name(string $url, int $limit
physicalAddress {
description,
locality
},
picture {
alt,
contentType,
url
}
},
total
Expand All @@ -95,6 +116,18 @@ public static function get_upcoming_events_by_group_name(string $url, int $limit
self::checkData($data);

$events = $data['data']['group']['organizedEvents']['elements'];

foreach ($events as &$event) {
if ($event['picture']) {
$picture_response = self::download_image($event['picture']['url']);
if ($picture_response !== false) {
$picture_encoded = 'data:' . $event['picture']['contentType'] . ';base64,' . base64_encode($picture_response);
$event['picture']['base64'] = $picture_encoded;
}
}
unset($event);
}

EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit], $events);
return $events;
}
Expand All @@ -110,4 +143,28 @@ private static function checkData($data) {
}
}
}

private static function download_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)

// 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));
}

// Close curl handle
curl_close($ch);

return $image_data;
}
}
7 changes: 5 additions & 2 deletions source/view/events-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
?>
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list">
<ul style="list-style-type: none; padding-left: 0;">
<?php foreach($events as $event) { ?>
<li style="margin-top: 10px;">
<?php foreach ($events as $event) { ?>
<li style="line-height: 150%; margin-top: 20px;">
<?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; max-width: 100%;">
<?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)); ?>
Expand Down

0 comments on commit 0edad98

Please sign in to comment.