<?php
function dacdb_speakers($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    $speakers = '<section class="speakers-list">';

    $account_id = $options['dacdb_district'];
    $club_id    = $options['dacdb_club'];

    $N = 0;
    if (isset($atts['n'])) {            // No of Speakers
        $N = $atts['n'];
    }

    if ($fDebug) {
        $speakers .= "=================================================================<BR>";
        $speakers .= "function dacdb_speakers called<br>";
        $speakers .= "AccountID=" . $options['dacdb_district'] . "<br>";
        $speakers .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $speakers .= "CatIDs=" . $options['speaker_cats'] . "<br>";
        $speakers .= "Mo=" . $options['speakers_months'] . "<br>";
        $speakers .= "N=" . $N . "<br>";
        $speakers .= $data_path . 'wp_speakers.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $club_id . '&CatIDs=' . $options['speaker_cats'] . '&Mo=' . $options['speakers_months'] . "<br>";
        $speakers .= "=================================================================<BR>";
    }
    $xml = simplexml_load_file($data_path . 'wp_speakers.cfm?AccountID=' . $options['dacdb_district'] . '&Mo=' . $options['speakers_months'] . '&CatIDs=' . $options['speaker_cats'] . '&ClubID=' . $options['dacdb_club'] . '&R=' . rand());
    if (strlen($xml) == 0) {
        return '<p class="no-speakers">There are no speakers at this time.</p>';
    }

    $counter = 0;
    foreach ($xml->speakerinfo as $speakerinfo) {
        $eventDateTime = strtotime($speakerinfo->eventdate);
        $eventUrl = $data_path . 'R_Event.cfm?EventID=' . $speakerinfo->eventid;

        $speakers .= '<article class="speaker-event dacdb_plus_event">';

        // Speaker Info
        if (!empty($speakerinfo->speaker)) {
            $speakers .= '<div class="speaker-name">' . esc_html($speakerinfo->speaker) . '</div>';
        }

        // Topic/Description
        if (!empty($speakerinfo->topic) || !empty($speakerinfo->description)) {
            $speakers .= '<div class="speaker-details">';
            if (!empty($speakerinfo->topic)) {
                $speakers .= '<p class="speaker-topic">' . esc_html($speakerinfo->topic) . '</p>';
            }
            if (!empty($speakerinfo->description)) {
                $speakers .= '<div class="speaker-description">' . wp_kses_post($speakerinfo->description) . '</div>';
            }
            $speakers .= '</div>';
        }

        // Event Info
        $speakers .= '<div class="event-details">';
        $speakers .= '<time datetime="' . date('Y-m-d\TH:i:s', $eventDateTime) . '">';
        $speakers .= '<a href="' . esc_url($eventUrl) . '" class="modal-link">';
        $speakers .= '<p class="event-name">' . esc_html($speakerinfo->eventname) . '</p>';
        $speakers .= date('F jS Y \a\t g:i a', $eventDateTime);
        $speakers .= '</a>';
        $speakers .= '</time>';
        $speakers .= '</div>';

        $speakers .= '</article>';

        $counter++;
        if ($N > 0 && $counter >= $N) {
            break;
        }
    }

    $speakers .= '</section>';
    return $speakers;
}
