Jump to content

Widget:UPDEvents: Difference between revisions

From Iskomunidad
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
<includeonly>
<includeonly>
<div id="upd-events-widget">
<div id="upd-events-widget" class="upd-events-widget">
   <div class="upd-events-loading">
   <div class="upd-events-loading">Loading upcoming events...</div>
    Loading upcoming events...
  </div>
   <ul class="upd-events-list" style="display:none;"></ul>
   <ul class="upd-events-list" style="display:none;"></ul>
</div>
</div>
<style>
.upd-events-widget {
  color: #000;
  font-size: 15px;
}
.upd-events-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.upd-events-list li {
  margin: 0;
  padding: 10px 0;
  border-bottom: 1px solid #e5e5e5;
}
.upd-events-list li:last-child {
  border-bottom: none;
}
.upd-events-list a {
  color: #0E6021 !important;
  text-decoration: none;
  font-weight: 600;
}
.upd-events-list a:hover {
  text-decoration: underline;
}
.upd-events-loading,
.upd-events-empty {
  color: #555;
  font-size: 14px;
}
</style>


<script>
<script>
(function () {
(function () {
   var container = document.getElementById('upd-events-widget');
   var container = document.getElementById('upd-events-widget');
   if (!container || container.dataset.loaded === '1') return;
 
   if (!container || container.dataset.loaded === '1') {
    return;
  }
 
   container.dataset.loaded = '1';
   container.dataset.loaded = '1';


   var loading = container.querySelector('.upd-events-loading');
   var loading = container.querySelector('.upd-events-loading');
   var list = container.querySelector('.upd-events-list');
   var list = container.querySelector('.upd-events-list');
  var pageUrl = 'https://upd.edu.ph/events-list/';


   function showError() {
   loading.textContent =
    loading.textContent = 'Unable to load events automatically. ';
     'Widget JavaScript is running. Requesting UP Diliman...';
     var link = document.createElement('a');
    link.href = pageUrl;
    link.textContent = 'View all UP Diliman events';
    link.target = '_blank';
    link.rel = 'noopener noreferrer';
    loading.appendChild(link);
  }


   function parseEventDate(text) {
   var endpoint = new URL(
     var match = text.match(
     'https://upd.edu.ph/wp-admin/admin-ajax.php'
      /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+(\d{1,2})\s*\|\s*(\d{1,2}):(\d{2})\s*(AM|PM)/i
  );
    );


     if (!match) return null;
  var params = {
     action: 'search_events',
    id: '1',
    scope: 'future',
    orderby: 'event_start_date,event_start_time,event_name',
    view: 'list-grouped',
    pno: '1'
  };


    var months = {
  Object.keys(params).forEach(function (key) {
      jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
    endpoint.searchParams.set(key, params[key]);
      jul:6, aug:7, sep:8, oct:9, nov:10, dec:11
  });
    };


    var now = new Date();
  var finished = false;
    var month = months[match[1].slice(0, 3).toLowerCase()];
    var day = Number(match[2]);
    var hour = Number(match[3]) % 12;
    var minute = Number(match[4]);


     if (match[5].toUpperCase() === 'PM') hour += 12;
  var timeout = setTimeout(function () {
     if (finished) return;


     var year = now.getFullYear();
     finished = true;
     var eventDate = new Date(year, month, day, hour, minute);
     loading.textContent =
      'The UP Diliman request timed out or is blocked.';
  }, 10000);


    // Handle year boundaries.
  fetch(endpoint.toString(), {
    if (month === 0 && now.getMonth() === 11) {
     method: 'GET',
      eventDate.setFullYear(year + 1);
     mode: 'cors',
     } else if (month === 11 && now.getMonth() === 0) {
     credentials: 'omit'
      eventDate.setFullYear(year - 1);
   })
     }
 
     return eventDate;
   }
 
 
loading.textContent = 'Testing connection to UP Diliman...';
 
fetch(pageUrl)
   .then(function (response) {
   .then(function (response) {
     loading.textContent =
     if (finished) return null;
      'Connection response: HTTP ' + response.status +
      '. Reading event list...';


     if (!response.ok) {
     if (!response.ok) {
Line 76: Line 96:
   })
   })
   .then(function (html) {
   .then(function (html) {
     loading.textContent =
     if (finished || html === null) return;
      'Page received (' + html.length +
 
      ' characters). Checking event markup...';
    finished = true;
    clearTimeout(timeout);


     var doc = new DOMParser().parseFromString(html, 'text/html');
     var doc = new DOMParser().parseFromString(html, 'text/html');


     var anchors = doc.querySelectorAll(
     var anchors = doc.querySelectorAll(
       '#em-events-list-grouped-1 a[href*="/events/"]'
       '#em-events-list-grouped-1 a[href*="/events/"], ' +
      '.em-events-list a[href*="/events/"]'
     );
     );


     loading.textContent =
     var seen = new Set();
       'Page received. Event links found: ' + anchors.length;
    var events = [];
 
    anchors.forEach(function (anchor) {
      var heading = anchor.querySelector('h4');
      if (!heading) return;
 
      var title = heading.textContent.trim();
      var url;
 
      try {
        url = new URL(
          anchor.getAttribute('href'),
          'https://upd.edu.ph'
        ).href;
      } catch (e) {
        return;
      }
 
      if (!title || seen.has(url)) return;
 
      if (!/^https:\/\/upd\.edu\.ph\/events\//i.test(url)) {
        return;
      }
 
      seen.add(url);
      events.push({
        title: title,
        url: url
      });
    });
 
    list.replaceChildren();
 
    if (events.length === 0) {
      loading.textContent =
        'The request succeeded, but no event titles were found.';
 
       return;
    }
 
    events.slice(0, 5).forEach(function (event) {
      var li = document.createElement('li');
      var a = document.createElement('a');
 
      a.href = event.url;
      a.textContent = event.title;
      a.target = '_blank';
      a.rel = 'noopener noreferrer';
 
      li.appendChild(a);
      list.appendChild(li);
    });
 
    loading.style.display = 'none';
    list.style.display = 'block';
   })
   })
   .catch(function (error) {
   .catch(function (error) {
    if (finished) return;
    finished = true;
    clearTimeout(timeout);
    console.error('UPEvents widget error:', error);
     loading.textContent =
     loading.textContent =
       'Connection failed: ' + error.message;
       'Request failed: ' +
    console.error('UPEvents diagnostic:', error);
      error.name + ' — ' + error.message;
   });
   });
   
.catch(function (error) {
  console.error('UPEvents widget error:', error);
  loading.textContent =
    'Fetch error: ' + error.message +
    '. Check the browser Console for details.';
});
);
})();
})();
</script>
</script>
</includeonly>
</includeonly>

Latest revision as of 03:24, 29 October 2026