MEC Single Layout Time Format + Recurring Dates

Hi,

I am posting this in the Beta forum, as this is about an MEC Single Layouts - I hope this is the best place for this.

I have two questions:

  1. As you can see in the screenshot below, using dynamic content {{dc:post:meta key="mec_start_time_hour"}}:{{dc:post:meta key="mec_start_time_minutes"}}{{dc:post:meta key="mec_start_time_ampm"}}, the minute part of the time shows as a single digit, if it is not a double digit minute.

I have tried adding {{dc:post:meta key="mec_start_time_minutes" type="time" format="i"}}, but this seems to have the minute the actual minute of the current time. The time in the screenshot should read 6:00PM. Is there any way to achieve this?

  1. My MEC has a majority of standard events. However, there are some with a repeating occurrance. When these are brought through via dynamic content, I can only seem to get the original start date, not the actual date of the event clicked on in the MEC calendar.

Is there any way of dealing with standard and repeating dates?

I am also making the assumption that MEC data is accessed by Post Meta in the dynamic content, rather than any other way?

I hope you can help with the two questions.

Thanks,
Christopher

For that piece of data, I believe it’s needed to use one of their APIs. This would be part of larger scale integration with MEC including conditions and dynamic content. If you want to help me test I was using this to get my next occurance dates. We can start working on this for a point release or 6.7.0 if it needs a larger test base.

{{dc:mec:next_occurrence_start type="date" format="Y/m/d h"}}

{{dc:mec:next_occurrence_end type="date" format="Y/m/d h"}}

/**
 * MEC Event Dynamic Content
 */
add_filter( 'cs_dynamic_content_mec', function($result, $field, $args = []) {

  $events = (new MEC_skin_single())->get_event_mec(get_the_ID());

  // No event from this global ID
  if (empty($events)) {
    return $result;
  }

  $event = (array) current($events);

  switch ($field) {
    case 'next_occurrence_start':
      $next_date = $event['dates'][0];
      $result = date('c', $next_date['start']['timestamp']);
      break;
    case 'next_occurrence_end':
      $next_date = $event['dates'][0];
      $result = date('c', $next_date['end']['timestamp']);
      break;
  }

  return $result;
}, 10, 4 );

Thanks for that. It partially works in that the next occurrance does display, as do standard events.

However, on the front-end, if the calendar view is used and any date (past, present or future) is clicked on, only the next occurance from today’s date is displayed. In other words, any recurring event will only show the next date using the above.

1 Like

What do you mean by calendar view? Or where is that on your site?

Details in SN

Just a thought on this one, could the new CS cookie dynamic content come into play and store the correct date (data-mec-cell) for the event clicked? I’m afraid this is a bit above my pay-grade in technical terms!

Hi Charlie,

I now have a fully working date field, which successfully deals with both single events and recurring events.

In summary, what we are doing is creating a cookie via Javascript, which grabs the content of data-mec-cell from the date clicked on the calendar.

This is then used to create a cookie with the name mec_event_date. Once the date and event have been selected, the new cookie dynamic content feature grabs the cookie

It then displays the correct date on the single layout page for the event. There is a mechanism built in to deal with the change of month, so the cookie is deleted upon month change and added back when the selected event is chosen.

The time uses your function and DC from post 2 of this thread: {{dc:mec:next_occurrence_start type="date" format="g:ia"}}

image

You can see it in action on the single layout you have already seen from earlier secure notes. Here are the steps to get this working, as I am sure you will be able to develop something much neater for a future release:

SINGLE LAYOUT
Following JS added to Global JS:

document.addEventListener("DOMContentLoaded", function () {

    function handleDateClick(event) {
        let dateElement = event.currentTarget;
        let selectedDate = dateElement.getAttribute("data-mec-cell");

        // Set the selected date in a cookie
        document.cookie = `mec_event_date=${selectedDate}; path=/;`;
    }

    function attachEventListeners() {
        let eventDays = document.querySelectorAll("dt.mec-calendar-day.mec-has-event");

        eventDays.forEach(day => {
            let eventDate = day.getAttribute("data-mec-cell");

            // Remove any existing listener before adding a new one to avoid duplicates
            day.removeEventListener("click", handleDateClick);
            day.addEventListener("click", handleDateClick);
        });
    }

    function handleMonthChange() {
        setTimeout(() => {
            setTimeout(() => {
                attachEventListeners();

                // Delete old cookie when month changes
                document.cookie = "mec_event_date=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
            }, 1000); // Adjust timing if needed
        }, 500); // Initial delay after clicking the navigation button
    }

    function observeMonthChange() {
        let monthNavButtons = document.querySelectorAll(".mec-next-month, .mec-previous-month");

        monthNavButtons.forEach(button => {
            button.removeEventListener("click", handleMonthChange);
            button.addEventListener("click", handleMonthChange);
        });
    }

    // General click listener to ensure date selection works after month change
    document.addEventListener("click", function (event) {
        let dateElement = event.target.closest("dt.mec-calendar-day.mec-has-event");
        if (dateElement) {
            // Manually trigger handleDateClick to ensure the cookie is set
            handleDateClick({ currentTarget: dateElement });
        }
    });

    // Initial setup
    attachEventListeners();
    observeMonthChange();
});

Date Field To Display The Event Date
{{dc:cookie:get name=β€œmec_event_date” type=β€œdate” format="jS F Y}}

Just in case it helps, here is the same Javascript, but with a load of debugging steps included:

document.addEventListener("DOMContentLoaded", function () {
    console.log("πŸš€ Script loaded, initializing MEC event listeners...");

    function handleDateClick(event) {
        let dateElement = event.currentTarget;
        let selectedDate = dateElement.getAttribute("data-mec-cell");
        console.log("πŸ“… Date clicked with value:", selectedDate);

        // Set the selected date in a cookie
        document.cookie = `mec_event_date=${selectedDate}; path=/;`;
        console.log(`πŸ“… Setting mec_event_date to: ${selectedDate}`);
    }

    function attachEventListeners() {
        let eventDays = document.querySelectorAll("dt.mec-calendar-day.mec-has-event");
        console.log(`πŸ“… Found ${eventDays.length} event dates in this month.`);

        eventDays.forEach(day => {
            let eventDate = day.getAttribute("data-mec-cell");
            console.log("πŸ“… Event date found:", eventDate);

            // Remove any existing listener before adding a new one to avoid duplicates
            day.removeEventListener("click", handleDateClick);
            day.addEventListener("click", handleDateClick);

            console.log(`βœ… Listener attached to: ${eventDate}`);
        });

        console.log("βœ… Date click listeners successfully attached.");
    }

    function handleMonthChange() {
        console.log("πŸ›  Month navigation clicked.");
        
        setTimeout(() => {
            console.log("πŸ”„ Month changed, waiting for MEC to update...");

            setTimeout(() => {
                console.log("πŸ“… MEC updated, reattaching event listeners...");
                attachEventListeners();

                // Delete old cookie when month changes
                document.cookie = "mec_event_date=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
                console.log("🚫 Deleted mec_event_date cookie");
            }, 1000); // Adjust timing if needed

        }, 500); // Initial delay after clicking the navigation button
    }

    function observeMonthChange() {
        let monthNavButtons = document.querySelectorAll(".mec-next-month, .mec-previous-month");

        monthNavButtons.forEach(button => {
            button.removeEventListener("click", handleMonthChange);
            button.addEventListener("click", handleMonthChange);
        });

        console.log("πŸ”„ Month navigation listeners attached.");
    }

    // General click listener to ensure date selection works after month change
    document.addEventListener("click", function (event) {
        let dateElement = event.target.closest("dt.mec-calendar-day.mec-has-event");
        if (dateElement) {
            let selectedDate = dateElement.getAttribute("data-mec-cell");
            console.log("πŸ“… Date clicked from general click listener:", selectedDate);

            // Manually trigger handleDateClick to ensure the cookie is set
            handleDateClick({ currentTarget: dateElement });
        }
    });

    // Initial setup
    attachEventListeners();
    observeMonthChange();
});

Thanks,
Christopher

1 Like

Very cool thanks for the notes. How does MEC handle this without a CS layout? Does it actually show the date based on what calendar day you pressed or is this something new? If MEC does do that on their own layouts, there is probably something we can tap into.

No problem!

I have only used this on the monthly calendar layout. My own uses have the accordion-type upcoming shortcodes as the only other methods of accessing singular events. The non-CS events seem to work as normal, but I am using only event single layouts.

Please have a good play with the staging website you have access to - it’s all yours!