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

    // Merge original shortcode attributes if available (for n and other custom attributes)
    if (isset($GLOBALS['dacdb_plus_current_shortcode_id']) && 
        isset($GLOBALS['dacdb_plus_original_atts_' . $GLOBALS['dacdb_plus_current_shortcode_id']]) && 
        is_array($GLOBALS['dacdb_plus_original_atts_' . $GLOBALS['dacdb_plus_current_shortcode_id']])) {
        $atts = array_merge($atts, $GLOBALS['dacdb_plus_original_atts_' . $GLOBALS['dacdb_plus_current_shortcode_id']]);
    }

    // Parse shortcode attributes with defaults
    $atts = shortcode_atts(array(
        'n' => 0, // Default to 0 (show all speakers)
    ), $atts, 'dacdb_speakers');

    $speakers = '';

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

    $N = intval($atts['n']); // Ensure we have a valid integer

    if ($fDebug && dacdb_is_admin()) {
        $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) {
        $speakers .= 'There are no speakers at this time.';
        return $speakers;
    }

    $counter = 0;
    foreach ($xml->speakerinfo as $speakerinfo) {
        $speakers .= '<div class="dacdb_plus_event">';

        if ($options['events_global_date'] == 1) {
            $speakers .= date("F jS Y", strtotime($speakerinfo->eventdate)) . ' at ' . date("g:i a", strtotime($speakerinfo->eventdate));
            $speakers .= '<br>';
        }
        $speakers .= '<strong>' . $speakerinfo->eventname . '</strong>';
        $speakers .= '<br><strong>Speaker: ' . $speakerinfo->speaker . '</strong>';
        $speakers .= '<br>' . $speakerinfo->topic;

        $speakers .= '<br>';
        if ($options['events_global_date'] == 0) {
            $speakers .= date("F jS Y", strtotime($speakerinfo->eventdate)) . ' at ' . date("g:i a", strtotime($speakerinfo->eventdate));
            $speakers .= '<br>';
        }
        if ($options['speaker_page_full'] == 1) {
            $speakers .= '' . $speakerinfo->description;
        } else {
            // Speaker "More info" links
            
            $speakers .= '<a class="modal-link" href="' . $data_path . 'R_Event.cfm?EventID=' . $speakerinfo->eventid . '" style="float:right;">More info</a><br />';
           
        }
        $speakers .= '</div>';
        $speakers .= '<hr>';
        $counter = $counter + 1;
        if ($N > 0 and $counter >= $N) {
            return $speakers;
        }
    }
    return $speakers;
}