Widget:UPDEvents: Difference between revisions
Appearance
Created page with " <?php →* * UP Diliman Events feed for Iskomunidad. * Retrieves up to five upcoming events from upd.edu.ph.: header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: public, max-age=900'); date_default_timezone_set('Asia/Manila'); $sourceUrl = 'https://upd.edu.ph/events-list/'; function respond($events, $status = 200) { http_response_code($status); echo json_encode( $events, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_..." |
No edit summary |
||
| Line 1: | Line 1: | ||
< | <includeonly> | ||
<div id="upd-events-widget" class="upd-events-widget"> | |||
<div class="upd-events-loading"> | |||
Loading UP Diliman events... | |||
</div> | |||
<ul class="upd-events-list" style="display:none;"></ul> | |||
<div class="upd-events-error" style="display:none;"> | |||
Events are temporarily unavailable. | |||
<a href="https://upd.edu.ph/events-list/" target="_blank" rel="noopener"> | |||
View the official events list | |||
</a>. | |||
</div> | |||
<div class="upd-events-footer"> | |||
<a href="https://upd.edu.ph/events/" target="_blank" rel="noopener"> | |||
View all UP Diliman events → | |||
</a> | |||
</div> | |||
</div> | |||
<style> | |||
.upd-events-widget { | |||
width: 100%; | |||
box-sizing: border-box; | |||
color: #000; | |||
} | } | ||
.upd-events-widget .upd-events-list { | |||
list-style: none; | |||
padding: 0; | |||
margin: 0; | |||
} | } | ||
.upd-events-widget .upd-events-list li { | |||
padding: 10px 0; | |||
border-bottom: 1px solid #e5e5e5; | |||
line-height: 1.5; | |||
} | |||
.upd-events-widget .upd-events-list li:last-child { | |||
border-bottom: none; | |||
} | |||
.upd-events-widget .upd-events-list a { | |||
color: #850038 !important; | |||
text-decoration: none; | |||
font-weight: 500; | |||
} | |||
.upd-events-widget .upd-events-list a:hover { | |||
text-decoration: underline; | |||
} | } | ||
.upd-events-widget .upd-events-loading, | |||
.upd-events-widget .upd-events-error { | |||
padding: 8px 0; | |||
color: #333; | |||
} | |||
.upd-events-widget .upd-events-footer { | |||
margin-top: 12px; | |||
font-size: 0.9em; | |||
} | |||
.upd-events-widget .upd-events-footer a { | |||
color: #850038 !important; | |||
text-decoration: none; | |||
} | |||
.upd-events-widget .upd-events-footer a:hover { | |||
text-decoration: underline; | |||
} | } | ||
</style> | |||
<script> | |||
(function () { | |||
"use strict"; | |||
var widgets = document.querySelectorAll( | |||
'.upd-events-widget:not([data-loaded])' | |||
); | |||
widgets.forEach(function (widget) { | |||
widget.setAttribute('data-loaded', 'true'); | |||
var loading = widget.querySelector('.upd-events-loading'); | |||
var list = widget.querySelector('.upd-events-list'); | |||
var error = widget.querySelector('.upd-events-error'); | |||
// Endpoint on the same Iskomunidad server. | |||
var endpoint = '/upd-events-proxy.php'; | |||
fetch(endpoint, { | |||
method: 'GET', | |||
credentials: 'same-origin', | |||
cache: 'no-cache' | |||
}) | |||
.then(function (response) { | |||
if (!response.ok) { | |||
throw new Error('Could not load events.'); | |||
} | |||
return response.json(); | |||
}) | |||
.then(function (events) { | |||
if (!Array.isArray(events)) { | |||
throw new Error('Unexpected event data.'); | |||
} | |||
loading.style.display = 'none'; | |||
if (events.length === 0) { | |||
error.style.display = 'block'; | |||
error.textContent = | |||
'No upcoming events were found. '; | |||
var link = document.createElement('a'); | |||
link.href = 'https://upd.edu.ph/events-list/'; | |||
link.target = '_blank'; | |||
link.rel = 'noopener'; | |||
link.textContent = 'View the official events list'; | |||
error.appendChild(link); | |||
error.appendChild(document.createTextNode('.')); | |||
return; | |||
} | |||
events.slice(0, 5).forEach(function (event) { | |||
if (!event.title || !event.url) { | |||
return; | |||
} | |||
// Only allow official UP Diliman event links. | |||
var url; | |||
try { | |||
url = new URL(event.url); | |||
} catch (e) { | |||
return; | |||
} | |||
if ( | |||
url.protocol !== 'https:' || | |||
url.hostname !== 'upd.edu.ph' || | |||
!url.pathname.startsWith('/events/') | |||
) { | |||
return; | |||
} | |||
var item = document.createElement('li'); | |||
var anchor = document.createElement('a'); | |||
anchor.href = url.href; | |||
anchor.target = '_blank'; | |||
anchor.rel = 'noopener noreferrer'; | |||
anchor.textContent = event.title; | |||
item.appendChild(anchor); | |||
list.appendChild(item); | |||
}); | |||
}); | |||
if (list.children.length === 0) { | |||
error.style.display = 'block'; | |||
} else { | |||
list.style.display = 'block'; | |||
} | |||
}) | |||
.catch(function (err) { | |||
console.error('UP Diliman Events widget:', err); | |||
loading.style.display = 'none'; | |||
error.style.display = 'block'; | |||
}); | |||
}); | |||
})(); | |||
</script> | |||
</includeonly> | |||
Revision as of 01:27, 29 October 2026