<?php 

//ANCHOR - DISTRICT EVENTS
/**
 * Fetches and formats district event data.
 *
 * This function fetches XML data from a remote service, parses that data, and
 * then formats it into a specific HTML structure for display. It handles both
 * district events and club events depending on the value of `$club_id`.
 *
 * @param array $atts An associative array of attributes.
 * @global bool $fDebug A flag indicating whether debugging is enabled.
 * @global string $data_path The base path or URL for data files.
 * @global string $file_path The base path or URL for other files.
 * @global array $options An associative array of options.
 *
 * @return string A string of formatted HTML representing the events.
 */
function dacdb_d_events($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    $d_events = '';

    if ($club_id == 0) {
        $categoryids = $options['district_events_types_page'];
        $mo = $options['district_events_months'];
    } else {
        $categoryids = $options['club_events_types_page'];
        $mo = $options['club_events_months'];
    }

    if ($fDebug) {
        $d_events .= "=================================================================<BR>";
        $d_events .= "function dacdb_d_events called<br>";
        $d_events .= "AccountID=" . $options['dacdb_district'] . "<br>";
        $d_events .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $d_events .= "Mo=" . $options['club_events_months'] . "<br>";
        $d_events .= "categoryids=" . $categoryids . "<br>";
        $d_events .= $data_path . 'wp_event.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $club_id . '&CatIDs=' . $categoryids . '&Mo=' . $mo . "<br>";
        $d_events .= "=================================================================<BR>";
    }

    $xml = simplexml_load_file($data_path . 'wp_event.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&CatIDs=' . $categoryids . '&Mo=' . $mo . '&R=' . rand());
    if (strlen($xml) == 0) {
        $d_events .= 'There are no events at this time.';
        return  $d_events;
    }

    $counter = 1;
    foreach ($xml->event as $event) {
        $d_events .= '<div class="dacdb_plus_d_event">';

        if ($options['events_global_date'] == 1) {
            if (date("Y-m-d", strtotime($event->startdate)) == date("Y-m-d", strtotime($event->enddate))) {
                $d_events .= date("F jS Y", strtotime($event->startdate)) . ' at ' . date("g:i a", strtotime($event->startdate));
            } else {
                $d_events .= date("F jS", strtotime($event->startdate)) . ' - ' . date("F jS Y", strtotime($event->enddate)) . ' at ' . date("g:i a", strtotime($event->startdate));
            }
            $d_events .= '<br>';
        }
        $d_events .= '<strong>' . $event->eventname . '</strong>';
        if ($event->eventreg == 'Y') {
            // $d_events .= '<br><small>Requires Registration</small>';
        }
        if ($options['events_global_date'] == 0) {
            $d_events .= '<br>';
            if (date("Y-m-d", strtotime($event->startdate)) == date("Y-m-d", strtotime($event->enddate))) {
                $d_events .= date("F jS Y", strtotime($event->startdate));
            } else {
                $d_events .= date("F jS", strtotime($event->startdate)) . ' - ' . date("F jS Y", strtotime($event->enddate));
            }
        }
        $d_events .= '<br>';
        // $d_events .= '<br>';
        // $d_events .= ''.$event->eventdesc.'';
        $d_events .= '<a class="iframe cboxElement" href="' . $data_path . 'R_Event.cfm?EventID=' . $event->eventid . '">More info</a>';
        $d_events .= '<br></div>';
        $d_events .= '<hr class="dacdb_plus_d_event_hr">';
        $counter = $counter + 1;
    }
    $event_total = count($xml->event);
    if ($event_total == 0) {
        $d_events .= 'There are no events at this time.';
    }
    return $d_events;
}