Пример #1
0
function _upcoming_list($atts, $content = null)
{
    global $connections, $wpdb;
    $template = new cnTemplate();
    $atts = shortcode_atts(array('list_type' => 'birthday', 'days' => '30', 'private_override' => FALSE, 'date_format' => 'F jS', 'show_lastname' => FALSE, 'list_title' => NULL, 'template' => $connections->options->getActiveTemplate('birthday')), $atts);
    if (is_user_logged_in() || $atts['private_override'] != FALSE) {
        $visibilityfilter = " AND (visibility='private' OR visibility='public') AND (" . $atts['list_type'] . " != '')";
    } else {
        $visibilityfilter = " AND (visibility='public') AND (`" . $atts['list_type'] . "` != '')";
    }
    if ($atts['list_title'] == NULL) {
        switch ($atts['list_type']) {
            case 'birthday':
                if ($atts['days'] >= 1) {
                    $list_title = 'Upcoming Birthdays the next ' . $atts['days'] . ' days';
                } else {
                    $list_title = 'Today\'s Birthdays';
                }
                break;
            case 'anniversary':
                if ($atts['days'] >= 1) {
                    $list_title = 'Upcoming Anniversaries the next ' . $atts['days'] . ' days';
                } else {
                    $list_title = 'Today\'s Anniversaries';
                }
                break;
        }
    } else {
        $list_title = $atts['list_title'];
    }
    /*
     * $atts['template'] can be either a string or an object. It is a string when set
     * with the shortcode attribute. If it is a string, the template will be loaded
     * via the cnTemplate class.
     * 
     * If the attribute is not set, it will be the object returned from the
     * cnOptions::getActiveTemplate() method which stores the default template
     * per list style.
     */
    if (isset($atts['template']) && !is_object($atts['template'])) {
        $template->load($atts['template']);
        $template->includeFunctions();
    } else {
        $template->init($connections->options->getActiveTemplate($atts['list_type']));
        $template->includeFunctions();
    }
    /* Old and busted query!2
    	$sql = "SELECT id, ".$atts['list_type'].", last_name, first_name FROM ".$wpdb->prefix."connections where (YEAR(DATE_ADD(CURRENT_DATE, INTERVAL ".$atts['days']." DAY))"
            . " - YEAR(FROM_UNIXTIME(".$atts['list_type'].")) )"
            . " - ( MID(DATE_ADD(CURRENT_DATE, INTERVAL ".$atts['days']." DAY),5,6)"
            . " < MID(FROM_UNIXTIME(".$atts['list_type']."),5,6) )"
            . " > ( YEAR(CURRENT_DATE)"
            . " - YEAR(FROM_UNIXTIME(".$atts['list_type'].")) )"
            . " - ( MID(CURRENT_DATE,5,6)"
            . " < MID(FROM_UNIXTIME(".$atts['list_type']."),5,6) )"
    		. $visibilityfilter
    		. " ORDER BY FROM_UNIXTIME(".$atts['list_type'].") ASC";
    	*/
    // Get the current date from WP which should have the current time zone offset.
    $wpCurrentDate = date('Y-m-d', $connections->options->wpCurrentTime);
    /*
     * 
     */
    $newSQL = "SELECT * FROM " . CN_ENTRY_TABLE . " WHERE" . "  (YEAR(DATE_ADD('{$wpCurrentDate}', INTERVAL " . $atts['days'] . " DAY))" . " - YEAR(DATE_ADD(FROM_UNIXTIME(`" . $atts['list_type'] . "`), INTERVAL " . $connections->options->sqlTimeOffset . " SECOND)) )" . " - ( MID(DATE_ADD('{$wpCurrentDate}', INTERVAL " . $atts['days'] . " DAY),5,6)" . " < MID(DATE_ADD(FROM_UNIXTIME(`" . $atts['list_type'] . "`), INTERVAL " . $connections->options->sqlTimeOffset . " SECOND),5,6) )" . " > ( YEAR('{$wpCurrentDate}')" . " - YEAR(DATE_ADD(FROM_UNIXTIME(`" . $atts['list_type'] . "`), INTERVAL " . $connections->options->sqlTimeOffset . " SECOND)) )" . " - ( MID('{$wpCurrentDate}',5,6)" . " <= MID(DATE_ADD(FROM_UNIXTIME(`" . $atts['list_type'] . "`), INTERVAL " . $connections->options->sqlTimeOffset . " SECOND),5,6) )" . $visibilityfilter;
    $results = $wpdb->get_results($newSQL);
    if ($results != NULL) {
        /*The SQL returns an array sorted by the birthday and/or anniversary date. However the year end wrap needs to be accounted for.
        		Otherwise earlier months of the year show before the later months in the year. Example Jan before Dec. The desired output is to show
        		Dec then Jan dates.  This function checks to see if the month is a month earlier than the current month. If it is the year is changed to the following year rather than the current.
        		After a new list is built, it is resorted based on the date.*/
        foreach ($results as $key => $row) {
            if (mktime(23, 59, 59, date('m', $row->{$atts}['list_type']), date('d', $row->{$atts}['list_type']), date('Y', $connections->options->wpCurrentTime)) < $connections->options->wpCurrentTime) {
                $dateSort[] = $row->{$atts}['list_type'] = mktime(0, 0, 0, date('m', $row->{$atts}['list_type']), date('d', $row->{$atts}['list_type']), date('Y', $connections->options->wpCurrentTime) + 1);
            } else {
                $dateSort[] = $row->{$atts}['list_type'] = mktime(0, 0, 0, date('m', $row->{$atts}['list_type']), date('d', $row->{$atts}['list_type']), date('Y', $connections->options->wpCurrentTime));
            }
        }
        array_multisort($dateSort, SORT_ASC, $results);
        $out = '';
        // Prints the template's CSS file.
        if (method_exists($template, 'printCSS')) {
            $out .= $template->printCSS();
        }
        // Prints the javascript tag in the footer if $template->js path is set
        if (method_exists($template, 'printJS')) {
            $template->printJS();
        }
        $out .= '<div class="connections-list cn-upcoming ' . $atts['list_type'] . '">' . "\n";
        $out .= '<div class="cn-upcoming-title">' . $list_title . '</div>';
        foreach ($results as $row) {
            $entry = new cnvCard($row);
            $vCard =& $entry;
            $entry->name = '';
            $alternate == '' ? $alternate = '-alternate' : ($alternate = '');
            !$atts['show_lastname'] ? $entry->name = $entry->getFirstName() : ($entry->name = $entry->getFullFirstLastName());
            if (isset($template->file)) {
                $out .= '<div class="cn-upcoming-row' . $alternate . ' vcard ' . $template->slug . '">' . "\n";
                ob_start();
                include $template->file;
                $out .= ob_get_contents();
                ob_end_clean();
                $out .= '</div>' . "\n";
            } else {
                // If no template is found, return an error message.
                return '<p style="color:red; font-weight:bold; text-align:center;">ERROR: Template "' . $atts['template_name'] . '" not found.</p>';
            }
        }
        $out .= "</div>\n";
        return $out;
    }
}