<?php
// SECTION: "Where Clubs Meet" (WCM)
function dacdb_wcm($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    $meetings = '';
    $sort = 'club';
    if ($options['wcm_show_layout'] == 'default') {
        $sort = 'club';
    } elseif ($options['wcm_show_layout'] == 'area') {
        $sort = 'area';
    } elseif ($options['wcm_show_layout'] == 'date') {
        $sort = 'date';
    } elseif ($options['wcm_show_layout'] == 'detail') {
        $sort = 'detail';
    } elseif ($options['wcm_show_layout'] == 'club') {
        $sort = 'club';
    } elseif ($options['wcm_show_layout'] == 'officers') {
        $sort = 'officers';
    } elseif ($options['wcm_show_layout'] == 'address') {
        $sort = 'address';
    }

    if ($fDebug && dacdb_is_admin()) {
        $meetings .= "==================================================================<BR>";
        $meetings .= "function dacdb_wcm called<br>";
        $meetings .= "AccountID=" . $options['dacdb_district'] . "<br>";
        $meetings .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $meetings .= "sort=" . $sort . "<br>";
        $meetings .= "URL=" . $data_path . 'wp_club_meetings.cfm?AccountID=' . $options['dacdb_district'] . '&SortBy=' . $sort . '<br>';
        $meetings .= "==================================================================<BR>";
    }

    $xml = simplexml_load_file($data_path . 'wp_club_meetings.cfm?AccountID=' . $options['dacdb_district'] . '&SortBy=' . $sort . '&R=' . rand());
    if (strlen($xml) == 0) {
        $meetings .= 'There are no club meetings to display at this time.';
        return $meetings;
    }

    if ($options['wcm_show_title'] == 'No') {
        $meetings .= '<style>h1.entry-title { display:none !important; }</style>';
        $meetings .= '<style>.entry-header { display:none !important; }</style>';
    }

    $meetings .= '<table width="100%" class="table" style="border:0;">';
    if ($sort == 'area') {
        $meetings .= '<tr style="background-color:#cccccc;"><td><strong>Day</strong></td>';
        $meetings .= '<td><strong>Time</strong></td>';
        $meetings .= '<td><strong>Club</strong></td>';
        $meetings .= '<td><strong>Meeting Place</strong></td></tr>';
    }
    if ($sort == 'date') {
        $meetings .= '<tr style="background-color:#cccccc;"><td><strong>Time</strong></td>';
        $meetings .= '<td><strong>Club</strong></td>';
        $meetings .= '<td><strong>Meeting Place</strong></td>';
        $meetings .= '<td><strong>City</strong></td></tr>';
    }
    if ($sort == 'address') {
        $meetings .= '<tr>';
        $meetings .= '<th>Address</th>';
        $meetings .= '<th>Meeting Information</th>';
    }

    $this_area = '';
    $this_day = '';
    $counter = 1;

    foreach ($xml->meeting as $wcm) {

        if ($sort == 'area') {
            if (intval($wcm->region) == $this_area) {
            } else {
                $meetings .= '<tr><td style="background-color:#e3e3e3;" colspan="4">AREA ' . $wcm->region . '</td></tr>';
                $this_area = $wcm->region;
            }
            $meetings .= '<tr>';
            $meetings .= '<td valign="top">' . $wcm->meetingday . '</td>';
            $meetings .= '<td valign="top" nowrap>' . $wcm->meetingtime . '</td>';
            if ($wcm->cluburl != '') {
                $meetings .= '<td valign="top"><a href="' . $wcm->cluburl . '" target="new">' . $wcm->clubname . '</a></td>';
            } else {
                $meetings .= '<td valign="top">' . $wcm->clubname . '</td>';
            }
            $meetings .= '<td valign="top">' . $wcm->meetingplace . '</td>';
            $meetings .= '</tr>';
        } elseif ($sort == 'date') {
            if (trim($wcm->meetingday) === $this_day) {
            } else {
                $meetings .= '<tr><td style="background-color:#e3e3e3;" colspan="4">' . $wcm->meetingday . '</td></tr>';
                $this_day = trim($wcm->meetingday);
            }
            $meetings .= '<tr>';
            $meetings .= '<td valign="top" nowrap>' . $wcm->meetingtime . '</td>';
            if ($wcm->cluburl != '') {
                $meetings .= '<td valign="top"><a href="' . $wcm->cluburl . '" target="new">' . $wcm->clubname . '</a></td>';
            } else {
                $meetings .= '<td valign="top">' . $wcm->clubname . '</td>';
            }
            $meetings .= '<td valign="top">' . $wcm->meetingplace . '</td>';
            $meetings .= '<td valign="top">' . $wcm->meetingcity . '</td>';
            $meetings .= '</tr>';
        } elseif ($sort == 'detail') {

            $meetings .= '<tr>';
            $meetings .= '<td valign="top" colspan="4" style="border:0;">';
            $meetings .= '
                <a href="' . $wcm->cluburl . '" target="new"><strong>' . $wcm->clubname . '</strong></a><br>
                <i>' . $wcm->clubaddress . '<br>
                ' . $wcm->clubcity . ', ' . $wcm->clubstatecode . '</i><br>
                <strong>Where We Meet </strong><br>
                ' . $wcm->meetingplace . '<br>';
            if ($wcm->meetingaddress != '') {
                $meetings .= '' . $wcm->meetingaddress . '<br>';
            }
            $meetings .= '' . $wcm->meetingcity . ', ' . $wcm->meetingstatecode . ' ' . $wcm->meetingpostalcode . '<br>
                <strong>When</strong><br>
                ' . $wcm->meetingday . ' at ' . $wcm->meetingtime . '';
            $meetings .= '<span style="float: right;"><a href="http://maps.google.com/maps?q=' . $wcm->latitude . ',' . $wcm->longitude . '" target="new">Map It</a></span>';
            $meetings .= '<hr style="margin:10px 0 5px 0;"></td></tr>';
        } elseif ($sort == 'officers') {
            $setDivWidth = 100;
            $setImgWidth = 95;

            $meetings .= '<tr>';
            $meetings .= '<td valign="top" align="left" width="40%">';
            $officer_total = count($wcm->officers);

            if ($officer_total > 0) {
                $meetings .= '<table border="1" cellpadding="0" cellspacing="0">';
                foreach ($wcm->officers->officer as $wcmofficer) {
                    $meetings .= '<tr>';
                    $meetings .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                    if ($wcmofficer->image == '') {
                        $meetings .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . MMPDB_PLUGIN_URL . 'img/avatar.svg" width="' . $setImgWidth . '"></div>';
                    } else {
                        $meetings .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $wcmofficer->image . '" width="' . $setImgWidth . '"></div>';
                    }
                    $meetings .= '</td>';
                    $meetings .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $meetings .= '<strong>' . $wcmofficer->username . '</strong><BR>';
                    $meetings .= $wcmofficer->clubposition . '<BR>';
                    $meetings .= '</td>';
                    $meetings .= '</tr>';
                }
                $meetings .= '</table>';
            }
            $meetings .= '</td>';

            $meetings .= '<td valign="top" width="60%">';
            $meetings .= '
                                    <a href="' . $wcm->cluburl . '" target="new"><strong>' . $wcm->clubname . '</strong></a><br>
                                    <i>' . $wcm->clubaddress . '<br>
                                    ' . $wcm->clubcity . ', ' . $wcm->clubstatecode . '</i><br>
                                    <strong>Where We Meet </strong><br>
                                    ' . $wcm->meetingplace . '<br>';
            if ($wcm->meetingaddress != '') {
                $meetings .= '' . $wcm->meetingaddress . '<br>';
            }
            $meetings .= '' . $wcm->meetingcity . ', ' . $wcm->meetingstatecode . ' ' . $wcm->meetingpostalcode . '<br>
                                    <strong>When</strong><br>
                                    ' . $wcm->meetingday . ' at ' . $wcm->meetingtime . '';
            $meetings .= '<span style="float: right;"><a href="https://maps.google.com/maps?q=' . $wcm->latitude . ',' . $wcm->longitude . '" target="new">Map It</a></span>';

            $meetings .= '</td>';
            $meetings .= '</tr>';
            $meetings .= '<tr><td colspan="2"><hr style="margin:10px 0 5px 0;"></td></tr>';
        } elseif ($sort == 'address') {

            $meetings .= '<tr>';
            if ($wcm->cluburl != '') {
                $meetings .= '<td><strong><a href="' . $wcm->cluburl . '" target="new">' . $wcm->clubname . '</a></strong>';
            } else {
                $meetings .= '<td><strong>' . $wcm->clubname . '</strong>';
            }
            if ($wcm->clubaddress != '') {
                $meetings .= '<address>' . $wcm->clubaddress;
                $meetings .= '<br>' . $wcm->clubcity . ', ' . $wcm->clubstatecode . ' ' . $wcm->clubpostalzip . '</address>';
            }
            $meetings .= '</td>';
            $meetings .= '<td>';
            if ($wcm->meetingplace != '') {
                $meetings .= '<strong>' . $wcm->meetingplace . '</strong>';
                $meetings .= '<address>';
                if ($wcm->meetingaddress != '') {
                    $meetings .= $wcm->meetingaddress;
                }
                $meetings .= '<br>' . $wcm->meetingcity . ', ' . $wcm->meetingstatecode . ' ' . $wcm->meetingpostalzip . '</address>';
            }
            if ($wcm->meeting_info != '') {
                $meetings .= '<p>' . $wcm->meeting_info . '</p>';
            }
            $meetings .= '</address>';
            $meetings .= '<p>Meets ';
            if ($wcm->meetingday != '') {
                $meetings .= $wcm->meetingday . ' ';
            }
            if ($wcm->meetingtime != '') {
                $meetings .= 'at ' . $wcm->meetingtime;
            }
            $meetings .= '</p>';            
            $meetings .= '</td>';
            $meetings .= '</tr>';
        } else {

            $meetings .= '<tr><td valign="top">';
            if ($wcm->cluburl != '') {
                $meetings .= '<a href="' . $wcm->cluburl . '" target="new">' . $wcm->clubname . '</a> - Area ';
            } else {
                $meetings .= '' . $wcm->clubname . ' - Area ';
            }
            $meetings .= '' . $wcm->region . ' ';
            $meetings .= '' . $wcm->meetingday . ' ';
            $meetings .= '' . $wcm->meetingplace . ' ';
            $meetings .= '</td></tr>';
        } 
        $counter = $counter + 1;
    }
    $meetings .= '</table>';
    return $meetings;
}
// !SECTION: "Where Clubs Meet" (WCM)