<?php
/**
 * DACdb Plus Widget Class
 *
 * @package     DACdb Plus
 * @author      Member Minder Pro, LLC
 * @copyright   2023 Member Minder Pro, LLC
 * @license     GPL-2.0+
 * @link        https://github.com/memberminderpro/dacdbplus
 *
 * @description Provides widget functionality for displaying DACdb content including:
 *             - Club meeting information
 *             - Club/District events calendar
 *             - Member birthdays
 *             - Club leadership directory
 *             - Club newsletters
 *             - Meeting speakers
 *             - Club sponsors
 */

// ANCHOR: Widget Class Definition
class dacdb_plus_widget extends WP_Widget {

    // ANCHOR: Widget Constructor
    function __construct() {
        parent::__construct(
            'dacdb_plus_widget',
            __('DACdb Plus', 'dacdb_plus_widget'),
            array('description' => __('Allows you to display your DACdb Plus Wigets in your sidebars', 'dacdb_plus_widget'))
        );
    }

    // ANCHOR: Frontend Widget Display
    public function widget($args, $instance) {
        // NOTE: Main widget rendering method - handles all display logic
        global $fDebug, $data_path, $file_path, $options;

        // SECTION: Initial Setup
        $options = get_mmp_options();
        error_log(print_r($options, true)); 

        $title = apply_filters('widget_title', $instance['title']);
        $show = $instance['show'];
        $count = $instance['count'];
        $club_id = isset($instance['club_id']) && $instance['club_id'] !== '' ? $instance['club_id'] : $options['dacdb_club'];

        // SECTION: Data Path Configuration
        setDataPath();   

        if ($data_path == '') {
            $pagedata .= "=================================================================<BR>";
            $pagedata .=  "Missing System selction under Plugin Settings!<BR>";
            $pagedata .=  "=================================================================<BR>";
            $pagedata .=  "<BR>";
        }

        // SECTION: Subscription Validation
        $xml = simplexml_load_file($data_path . 'wp_subscription2.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&R=' . rand());
        if ($xml->status != '1') {
            $pagedata .= "=================================================================<BR>";
            $pagedata .=  "&nbsp; &nbsp;  Missing subscription record. System=" . $options['dacdb_system'] . ", AccountID=" . $options['dacdb_district'] . ", ClubID=" . $options['dacdb_club'] . "<BR>";
            $pagedata .=  "&nbsp; &nbsp;  Please contact: support@memberminderpro.com<BR>";
            $pagedata .=  "subscription=" . $xml->status . "<br>";
            if ($fDebug) {
                $pagedata .=  "&nbsp; &nbsp;  url=" . $data_path . 'wp_subscription2.cfm?AccountId=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . "<BR>";
            }
            $pagedata .=  "=================================================================<BR>";
            $pagedata .=  "<BR>";
        }

        // SECTION: Debug Information
        if ($fDebug) {
            $pagedata .=  "=================================================================<BR>";
            $pagedata .=  "Version=" . MMPDB_PLUGIN_VERSION . "<br>";
            $pagedata .=  "Show=" . $show . "<br>";
            $pagedata .=  "CatID=" . $catid . "<br>";
            $pagedata .=  "System=" . $options['dacdb_system'] . "<BR>";
            $pagedata .=  "data_path=" . $data_path . "<BR>";
            $pagedata .=  "file_path=" . $file_path . "<BR>";
            $pagedata .=  "subscription=" . $xml->status . "<BR>";
            $pagedata .=  "=================================================================<BR>";
        }

        // SECTION: Widget Output Initialization
        echo $args['before_widget'];
        if (!empty($title))
            echo $args['before_title'] . $title . $args['after_title'];

        // SECTION: Display Type Flags
        $show_stories = 0;
        $show_events = 0;
        $show_speakers = 0;
        $show_leaders = 0;
        $show_members = 0;
        $show_birthdays = 0;
        $show_club_meetings = 0;
        $show_newsletters = 0;
        $show_sponsors = 0;

        // SECTION: Display Type Selection
        // NOTE: Determines which content type to display based on widget settings
        if ($show == 'stories') {
            $show_stories = 1;
        } elseif ($show == 2) {
            $show_events = 1;
        } elseif ($show == 3) {
            $show_d_events = 1;
        } elseif ($show == 7) {
            $show_speakers = 1;
        } elseif ($show == 5) {
            $show_leaders = 1;
        } elseif ($show == 4) {
            $show_birthdays = 1;
        } elseif ($show == 1) {
            $show_meeting_info = 1;
        } elseif ($show == 6) {
            $show_newsletters = 1;
        } elseif ($show == 8) {
            $show_sponsors = 1;
        }

        // ANCHOR: Meeting Information Display
        if (isset($show_meeting_info) && $show_meeting_info == 1) {
            if (!$club_id) {
                $club_id = $options['dacdb_club'];
            }

            // NOTE: Debug output for meeting information
            if ($fDebug) {
                $meetings = "";
                $meetings .= "=======================<BR>";
                $meetings .= "function widget show_meeting_info<br>";
                $meetings .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $meetings .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $meetings .= "URL=" . $data_path . 'wp_club_meetings.cfm?AccountID=' . $options['dacdb_district'] . '&SortBy=' . $sort . '<br>';
                $meetings .= "=======================<BR>";
                echo $meetings;
            }

            // SECTION: Meeting Data Retrieval and Display
            $xml = simplexml_load_file($data_path . 'wp_club_meetings.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $club_id . '&R=' . rand());
            $meetings = array();
            $counter = 1;
            foreach ($xml->meeting as $wcm) {
                echo '<div class="side-meeting-info">';
                echo '<span class="side-meeting-info-dt">' . $wcm->meetingday . 's at ' . $wcm->meetingtime . '</span><br/>';
                echo '<strong>' . $wcm->meetingplace . '</strong><br>';
                echo $wcm->meetingaddress . '<br>';
                echo $wcm->meetingcity . ', ' . $wcm->meetingstatecode . ' ' . $wcm->meetingpostalcode . '<br/>';
                if ($wcm->latlng != "") {
                    echo '<span style="float:right;"><a href="' . $wcm->latlng . '" target="new">Map It</a></span>';
                }
                echo '<br></div>';
                $counter = $counter + 1;
            }
            $meet_total = count($xml->meeting);
            if ($meet_total == 0) {
                echo 'There are no club meetings to display at this time.';
            }
        }

        // ANCHOR: Club Events Display
        if (isset($show_events) && $show_events == 1) {
            // SECTION: Club Events Setup
            if (!$club_id) {
                $club_id = $options['dacdb_club'];
            } else {
                $byShowSpeaker = 1;
            }

            // NOTE: Configure event categories and months based on club/district settings
            if ($club_id == 0) {
                $categoryids = $options['district_events_types_page'];
                $mo = $options['district_events_months_side'];
            } else {
                $categoryids = $options['club_events_types_page'];
                $mo = $options['club_events_months_side'];
            }

            // SECTION: Debug Information for Events
            if ($fDebug) {
                $events = "";
                $events .= "=======================<BR>";
                $events .= "function widget show_events<br>";
                $events .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $events .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $events .= "Mo=" . $options['club_events_months'] . "<br>";
                $events .= "categoryids=" . $categoryids . "<br>";
                $events .= $data_path . 'wp_event.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&CatIDs=' . $categoryids . '&Mo=' . $mo . '<br />';
                $events .= "=======================<BR>";
                echo $events;
            }

// SECTION: Event Data Retrieval and Display
            $xml = simplexml_load_file($data_path . 'wp_event.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&CatIDS=' . $categoryids . '&Mo=' . $mo . '&R=' . rand());
            //                if ( $options['club_events_types_side'] == '') {
            //                    $xml = simplexml_load_file($data_path.'wp_event.cfm?AccountID='.$options['dacdb_district'].'&ClubID='.$club_id.'&Cat=3,5,4&Mo='.$options['club_events_months_side'].'&R='.rand());
            //                } else {
            //                    $xml = simplexml_load_file($data_path.'wp_event.cfm?AccountID='.$options['dacdb_district'].'&ClubID='.$club_id.'&Cat='.$options['club_events_types_side'].'&Mo='.$options['club_events_months_side'].'&R='.rand());
            //                }
            $events = array();
            $counter = 1;
            $showEvent = 1;

            // SECTION: Event Loop Processing
            $event_total = count($xml->event);
            foreach ($xml->event as $event) {
                // NOTE: Handle speaker visibility logic
                if ($options['club_events_show_speakers_side'] == 0 && $event->speaker != '') {
                    $showEvent = 0;
                } else if ($options['club_events_show_speakers_side'] == 0 && $event->speaker == '') {
                    $showEvent = 1;
                }

                if ($byShowSpeaker = 1 && $event->speaker == '') {
                    $showEvent = 1;
                }

                // SECTION: Event Display Logic
                if ($showEvent == 1) {
                    if ($count >= $counter) {
                        echo '<div class="side-club-event" style="line-height:1.5;">';
                        if ($options['events_global_date'] == 1) {
                            if (date("Y-m-d", strtotime($event->startdate)) == date("Y-m-d", strtotime($event->enddate))) {
                                echo date("F jS Y", strtotime($event->startdate)) . ' ' . date("g:i a", strtotime($event->startdate));
                            } else {
                                echo date("M jS", strtotime($event->startdate)) . ' - ' . date("M jS Y", strtotime($event->enddate)) . ' ' . date("g:i a", strtotime($event->startdate));
                            }
                            echo '<br>';
                        }

                        echo '<strong>' . $event->eventname . '</strong>';

                        // NOTE: Display speaker information if enabled
                        if ($options['club_events_show_speakers_side_info'] == 1) {
                            if ($event->speaker != "") {
                                echo '<br><strong>Speaker: ' . $event->speaker . '</strong>';
                            }
                            if ($event->topic != "") {
                                echo '<br>Topic: ' . $event->topic;
                            }
                        }

                        if ($options['events_global_date'] == 0) {
                            echo '<br>';
                            if (date("Y-m-d", strtotime($event->startdate)) == date("Y-m-d", strtotime($event->enddate))) {
                                echo date("F jS Y", strtotime($event->startdate)) . ' ' . date("g:i a", strtotime($event->startdate));
                            } else {
                                echo date("M jS", strtotime($event->startdate)) . ' - ' . date("M jS Y", strtotime($event->enddate)) . ' ' . date("g:i a", strtotime($event->startdate));
                            }
                        }

                        echo '<br>';
                        echo '<a class="iframe2 cboxElement" href="' . $data_path . 'R_Event.cfm?EventID=' . $event->eventid . '" style="float:right;">More info</a>';
                        echo '<br>';
                        if ($count > $counter && $counter < $event_total) {
                            echo '<hr>';
                        }
                        $counter = $counter + 1;
                        echo '</div>';
                    }
                }
            }
            if ($event_total == 0) {
                echo 'There are no events at this time.';
            }
        }

        // ANCHOR: District Events Display
        if (isset($show_d_events) && $show_d_events == 1) {
            // SECTION: District Events Configuration
            if ($club_id == 0) {
                $categoryids = $options['district_events_types_page'];
                $mo = $options['district_events_months_side'];
            } else {
                $categoryids = $options['club_events_types_page'];
                $mo = $options['club_events_months_side'];
            }

            // SECTION: Debug Information
            if ($fDebug) {
                $events = "";
                $events .= "=======================<BR>";
                $events .= "function widget show_d_events<br>";
                $events .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $events .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $events .= "Mo=" . $options['district_events_months'] . "<br>";
                $events .= "categoryids=" . $categoryids . "<br>";
                $events .= $data_path . 'wp_event.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&CatIDs=' . $categoryids . '&Mo=' . $mo . '<br />';
                $events .= "=======================<BR>";
                echo $events;
            }

            // SECTION: District Events Data Retrieval and Display
            $xml = simplexml_load_file($data_path . 'wp_event.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&CatIDs=' . $categoryids . '&Mo=' . $mo . '&R=' . rand());
            //                if ( $options['district_events_types_side'] == '') {
            //                    $xml = simplexml_load_file($data_path.'wp_event.cfm?AccountID='.$options['dacdb_district'].'&ClubID='.$options['dacdb_club'].'&CatIDS,2,123&Mo='.$options['district_events_months_side'].'&R='.rand());
            //                } else {
            //                    $xml = simplexml_load_file($data_path.'wp_event.cfm?AccountID='.$options['dacdb_district'].'&ClubID='.$options['dacdb_club'].'&CatIDs='.$options['district_events_types_side'].'&Mo='.$options['district_events_months_side'].'&R='.rand());
            //                }
            $events = array();
            $counter = 1;
            $event_total = count($xml->event);
            foreach ($xml->event as $event) {
                if ($count >= $counter) {
                    echo '<div class="side-district-event" style="line-height:1.5;">';

                    if ($options['events_global_date'] == 1) {
                        if (date("Y-m-d", strtotime($event->startdate)) == date("Y-m-d", strtotime($event->enddate))) {
                            echo date("F jS Y", strtotime($event->startdate)) . ' ' . date("g:i a", strtotime($event->startdate));
                        } else {
                            echo date("M jS", strtotime($event->startdate)) . ' - ' . date("M jS Y", strtotime($event->enddate)) . ' ' . date("g:i a", strtotime($event->startdate));
                        }
                        echo '<br>';
                    }

                    echo '<strong>' . $event->eventname . '</strong>';

                    if ($options['events_global_date'] == 0) {
                        echo '<br>';
                        if (date("Y-m-d", strtotime($event->startdate)) == date("Y-m-d", strtotime($event->enddate))) {
                            echo date("F jS Y", strtotime($event->startdate));
                        } else {
                            echo date("M jS", strtotime($event->startdate)) . ' - ' . date("M jS Y", strtotime($event->enddate));
                        }
                    }
                    echo '<br>';
                    echo '<a class="iframe2 cboxElement" href="' . $data_path . 'R_Event.cfm?EventID=' . $event->eventid . '" style="float:right;">More info</a>';
                    echo '<br>';
                    if ($count > $counter && $counter < $event_total) {
                        echo '<hr>';
                    }
                    echo '</div>';
                }
                $counter = $counter + 1;
            }
            if ($event_total == 0) {
                echo 'There are no events at this time.';
            }
        }

        // ANCHOR: Birthdays Display
        if ($show_birthdays == 1) {

            // SECTION: Birthday Debug Information
            if ($fDebug) {
                $events = "";
                $events .= "=======================<BR>";
                $events .= "function widget show_birthdays<br>";
                $events .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $events .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $events .= $data_path . 'wp_birthdays.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '<br />';
                $events .= "=======================<BR>";
                echo $events;
            }

            // SECTION: Birthday Data Retrieval and Display
            $xml = simplexml_load_file($data_path . 'wp_birthdays.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&R=' . rand());
            $birthdays = array();
            $birthday_total = count($xml->birthday);
            $counter = 1;
            echo '<div class="side-birthdays">';
            foreach ($xml->birthday as $birthday) {
                echo '<div style=""><strong>' . $birthday->membername . '</strong><br>' . $birthday->birthday . '<br><br></div>';
                if ($counter < $birthday_total) {
                    echo '<hr>';
                }
                $counter = $counter + 1;
            }
            if ($birthday_total == 0) {
                echo 'There are no upcoming birthdays at this time.';
            }
            echo '</div>';
        }

        // ANCHOR: Leaders Display
        if ($show_leaders == 1) {
            // SECTION: Leaders Debug Information
            if ($fDebug) {
                $events = "";
                $events .= "=======================<BR>";
                $events .= "function widget show_leaders<br>";
                $events .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $events .= $data_path . 'wp_leaders.cfm?AccountID=' . $options['dacdb_district'] . '<br />';
                $events .= "=======================<BR>";
                echo $events;
            }

            if (!$club_id) {
                $club_id = $options['dacdb_club'];
            }

            // SECTION: Leaders Data Display
            echo '<div class="side-leaders">';
            if ($options['dacdb_club'] == 0) {
                $xml = simplexml_load_file($data_path . 'wp_leaders.cfm?AccountID=' . $options['dacdb_district'] . '&R=' . rand());
                $leaders = array();
                $counter = 1;
                $leader_total = count($xml->leader);
                foreach ($xml->leader as $leader) {
                    echo '<div class="row col-1-1" style="overflow:hidden;margin-bottom:20px;">';
                    echo '<div style="width:60px;float:left;"><img src="' . $leader->imgpath . '' . $leader->image . '" width="50"></div>';
                    echo '<div style=""><strong><a class="iframe cboxElement" href="' . $data_path . 'R_Emailer.cfm?UserID=' . $leader->userid . '">' . $leader->username . '</a></strong><br>' . $leader->clubposition . '</div>';
                    echo '</div>';
                }
                if ($leader_total == 0) {
                    echo 'There are no leaders to display at this time.';
                }
            }             else {
                $xml = simplexml_load_file($data_path . 'wp_leaders.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $club_id . '&R=' . rand());
                $leaders = array();
                $counter = 1;
                $leader_total = count($xml->leader);
                foreach ($xml->leader as $leader) {
                    echo '<div class="row col-1-1" style="overflow:hidden;margin-bottom:20px;">';
                    echo '<div style="width:60px;float:left;"><img src="' . $leader->imgpath . '' . $leader->image . '" width="50"></div>';
                    echo '<div style=""><strong><a class="iframe cboxElement" href="' . $data_path . 'R_Emailer.cfm?UserID=' . $leader->userid . '">' . $leader->username . '</a></strong><br>' . $leader->clubposition . '</div>';
                    echo '</div>';
                }
                if ($leader_total == 0) {
                    echo 'There are no leaders to display at this time.';
                }
            }
            echo '</div>';
        }

        // ANCHOR: Newsletters Display
        if ($show_newsletters == 1) {
            // SECTION: Newsletter Configuration
            $count = $options['newsletters_weeks_widget'];

            // SECTION: Newsletter Debug Information
            if ($fDebug) {
                $newsletters = "";
                $newsletters .= "=======================<BR>";
                $newsletters .= "function widget show_newsletters<br>";
                $newsletters .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $newsletters .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $newsletters .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $newsletters .= $data_path . 'wp_newsletters.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&Weeks=' . $options['newsletters_weeks_widget'] . '<br />';
                $newsletters .= "=======================<BR>";
                echo $newsletters;
            }

            // SECTION: Newsletter Data Retrieval and Display
            $xml = simplexml_load_file($data_path . 'wp_newsletters.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&Weeks=' . $options['newsletters_weeks_widget'] . '&R=' . rand());

            $newsletters = array();
            $counter = 1;
            $newsletter_total = count($xml->newsletter);
            echo '<div class="side-newsletters">';
            foreach ($xml->newsletter as $newsletter) {
                if ($counter <= $count) {
                    echo '<span class="side-newsletters-link"><a href="' . $newsletter->path . '" target="new">' . $newsletter->name . '</a> </span>';
                    echo '<span class="pull-right" style="float:right;">' . $newsletter->datelastmodified . '</span>';
                    echo '<hr>';
                }
                $counter = $counter + 1;
            }
            if ($newsletter_total == 0) {
                echo 'There are no newsletters to display at this time.';
            }
            echo '</div>';
        }

        // ANCHOR: Speakers Display
        if ($show_speakers == 1) {

            if ($fDebug) {
                $speakers = "";
                $speakers .= "=======================<BR>";
                $speakers .= "function widget show_speakers<br>";
                $speakers .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $speakers .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $speakers .= "CatIDs=" . $options['speaker_cats'] . "<br>";
                $speakers .= $data_path . 'wp_speakers.cfm?AccountID=' . $options['dacdb_district'] . '&Mo=' . $options['speakers_months'] . '&CatIDs=' . $options['speaker_cats'] . '&ClubID=' . $options['dacdb_club'] . '<br/>';
                $speakers .= "=======================<BR>";
                echo $events;
            }

            $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());
            $speakers = array();
            $speaker_counter = 1;
            foreach ($xml->speakerinfo as $speakerinfo) {
                if ($count >= $speaker_counter) {
                    echo '<div class="side-speakers" style="line-height:1.5;">';

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


                    if ($options['events_global_date'] == 0) {
                        echo '' . date("M jS Y", strtotime($speakerinfo->eventdate)) . ' ' . date("g:i a", strtotime($speakerinfo->eventdate)) . '<br />';
                    }
                    echo '<a class="iframe cboxElement" href="' . $data_path . 'R_Event.cfm?EventID=' . $speakerinfo->eventid . '" style="float:right;">More info</a><br />';
                    if ($count > $speaker_counter) {
                        echo '<hr>';
                    }
                    $speaker_counter = $speaker_counter + 1;
                    echo '</div>';
                }
            }
            $speakerinfo_total = count($xml->speakerinfo);
            if ($speakerinfo_total == 0) {
                echo '<div class="side-speakers">';
                echo 'There are no speakers at this time.';
                echo '</div">';
            }
        }

        // ANCHOR: Sponsors Display
        if ($show_sponsors == 1) {
            if ($fDebug) {
                $sponsors = "";
                $sponsors .= "=======================<BR>";
                $sponsors .= "function widget show_sponsors<br>";
                $sponsors .= "AccountID=" . $options['dacdb_district'] . "<br>";
                $sponsors .= "ClubID=" . $options['dacdb_club'] . "<br>";
                $sponsors .= $data_path . 'wp_sponsor.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '<br />';
                $sponsors .= "=======================<BR>";
                echo $sponsors;
            }

            $xml = simplexml_load_file($data_path . 'wp_sponsor.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&R=' . rand());
            $sponsors = array();
            $counter = 1;
            foreach ($xml->sponsor as $sponsor) {
                echo '<div><center>';
                echo '<a href="' . $sponsor->website . '" target="_blank"><img src="' . $sponsor->imagepath . '' . $sponsor->image . '"></a><br><br>';
                echo '</center></div>';
                $counter = $counter + 1;
            }
            //echo '<small>Click on browser refresh tab to see more sponsors</small>';
            $sponsor_total = count($xml->sponsor);
            if ($sponsor_total == 0) {
                echo '<div class="side-sponsors">';
                echo 'There are no sponsors to display at this time.';
                echo '</div>';
            }
        }
        echo $args['after_widget'];
    }

    // ANCHOR: Widget Backend Form
    public function form($instance) {
        $options = get_mmp_options();
        if (isset($instance['title'])) {
            $title = $instance['title'];
            $show = $instance['show'];
            $club_id = $instance['club_id'];
            $count = $instance['count'];
        } else {
            $title = __('', 'dacdb_plus_widget');
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>">
            <label for="<?php echo $this->get_field_id('club_id'); ?>"><?php _e('^Club ID:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('club_id'); ?>" name="<?php echo $this->get_field_name('club_id'); ?>" value="<?php echo esc_attr($club_id); ?>">
            <label for="<?php echo $this->get_field_id('show'); ?>"><?php _e('Show:'); ?></label>
            <select class="widefat" id="<?php echo $this->get_field_id('show'); ?>" name="<?php echo $this->get_field_name('show'); ?>">
                <option value="<?php echo $show; ?>">
                    <?php if ($show == '1') {
                        echo 'Meeting Info';
                    } elseif ($show == '2') {
                        echo 'Events';
                    } elseif ($show == '3') {
                        echo 'District Events';
                    } elseif ($show == '4') {
                        echo 'Birthdays';
                    } elseif ($show == '5') {
                        echo 'Leadership';
                    } elseif ($show == '6') {
                        echo 'Newsletters';
                    } elseif ($show == '7') {
                        echo 'Speakers';
                    } elseif ($show == '8') {
                        echo 'Sponsors';
                    } else {
                        echo 'Select One';
                    } ?></option>
                <?php if ($options['dacdb_club'] == 0) { ?>
                    <option value="3">*District Events</option>
                    <option value="6">*Newsletters</option>
                    <option value="7">*Speakers</option>
                    <option value="8">Sponsors</option>
                <?php } else { ?>
                    <option value="1">^ Meeting Info</option>
                    <option value="5">^ Leadership</option>
                    <option value="4">Birthdays</option>
                    <option value="8">^ Sponsors</option>
                    <option value="2">*^ Events</option>
                    <option value="3">*District Events</option>
                    <option value="6">*Newsletters</option>
                    <option value="7">*Speakers</option>
                <?php } ?>
            </select>
            <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('*Number of items to display:'); ?></label>
            <select class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>">
                <option></option>
                <?php for($i=1; $i<=20; $i++) { ?>
                    <option value="<?php echo $i; ?>" <?php if ($count == $i) { echo 'selected'; } ?>><?php echo $i; ?></option>
                <?php } ?>
            </select>
        </p>
        <?php
    }

    // ANCHOR: Widget Update Method
    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        $instance['show'] = (!empty($new_instance['show'])) ? strip_tags($new_instance['show']) : '';
        $instance['count'] = (!empty($new_instance['count'])) ? strip_tags($new_instance['count']) : '';
        $instance['club_id'] = (!empty($new_instance['club_id'])) ? strip_tags($new_instance['club_id']) : '';
        return $instance;
    }
}

// TODO: Add caching for API responses
// TODO: Implement error logging for failed API requests 
// FIXME: Add proper error handling for XML parsing
// REVIEW: Consider adding filter hooks for widget output customization