Esempio n. 1
0
function mc_search_results($query)
{
    $limits = true;
    $before = apply_filters('mc_past_search_results', 0);
    $after = apply_filters('mc_future_search_results', 10);
    // return only future events, nearest 10
    if (is_string($query)) {
        $select_category = $limit_string = $select_author = $select_host = '';
        $search = " MATCH(event_title,event_desc,event_short,event_label,event_city,event_postcode,event_registration) AGAINST ('{$query}' IN BOOLEAN MODE) AND ";
    } else {
        extract($query);
        $select_category = $category != 'default' ? mc_select_category($category) : '';
        $limit_string = mc_limit_string();
        $select_author = $author != 'default' ? mc_select_author($author) : '';
        $select_host = $host != 'default' ? mc_select_host($host) : '';
        $prefix = $select_category . $limit_string . $select_author . $select_host != '' ? ' AND' : '';
        $search = "{$prefix} MATCH(event_title,event_desc,event_short,event_label,event_city,event_postcode,event_registration) AGAINST ('{$query}' IN BOOLEAN MODE) AND ";
    }
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $date = date('Y', current_time('timestamp')) . '-' . date('m', current_time('timestamp')) . '-' . date('d', current_time('timestamp'));
    // if a value is non-zero, I'll grab a handful of extra events so I can throw out holidays and others like that.
    if ($before > 0) {
        $before = $before + 5;
        $events1 = $mcdb->get_results("SELECT *, UNIX_TIMESTAMP(occur_begin) AS ts_occur_begin, UNIX_TIMESTAMP(occur_end) AS ts_occur_end \n\t\tFROM " . MY_CALENDAR_EVENTS_TABLE . " \n\t\tJOIN " . MY_CALENDAR_TABLE . " \n\t\tON (event_id=occur_event_id) \n\t\tJOIN " . MY_CALENDAR_CATEGORIES_TABLE . " \n\t\tON (event_category=category_id) WHERE {$select_category} {$select_author} {$select_host} {$limit_string} {$search} event_approved = 1 AND event_flagged <> 1 \n\t\tAND DATE(occur_begin) < '{$date}' ORDER BY occur_begin DESC LIMIT 0,{$before}");
    } else {
        $events1 = array();
    }
    $events3 = $mcdb->get_results("SELECT *, UNIX_TIMESTAMP(occur_begin) AS ts_occur_begin, UNIX_TIMESTAMP(occur_end) AS ts_occur_end \n\t\tFROM " . MY_CALENDAR_EVENTS_TABLE . " \n\t\tJOIN " . MY_CALENDAR_TABLE . " \n\t\tON (event_id=occur_event_id) \n\t\tJOIN " . MY_CALENDAR_CATEGORIES_TABLE . " \n\t\tON (event_category=category_id) WHERE {$select_category} {$select_author} {$select_host} {$limit_string} {$search} event_approved = 1 AND event_flagged <> 1 \n\t\tAND DATE(occur_begin) = '{$date}'");
    if ($after > 0) {
        $after = $after + 5;
        $events2 = $mcdb->get_results("SELECT *, UNIX_TIMESTAMP(occur_begin) AS ts_occur_begin, UNIX_TIMESTAMP(occur_end) AS ts_occur_end \n\t\tFROM " . MY_CALENDAR_EVENTS_TABLE . " \n\t\tJOIN " . MY_CALENDAR_TABLE . " \n\t\tON (event_id=occur_event_id) \n\t\tJOIN " . MY_CALENDAR_CATEGORIES_TABLE . " \n\t\tON (event_category=category_id) WHERE {$select_category} {$select_author} {$select_host} {$limit_string} {$search} event_approved = 1 AND event_flagged <> 1 \n\t\tAND DATE(occur_begin) > '{$date}' ORDER BY occur_begin ASC LIMIT 0,{$after}");
    } else {
        $events2 = array();
    }
    $arr_events = array();
    if (!empty($events1) || !empty($events2) || !empty($events3)) {
        $arr_events = array_merge($events1, $events3, $events2);
    }
    if (!get_option('mc_skip_holidays_category') || get_option('mc_skip_holidays_category') == '') {
        $holidays = array();
    } else {
        $holidays = mc_get_all_holidays($before, $after, 'yes');
        $holiday_array = mc_set_date_array($holidays);
    }
    if (is_array($arr_events) && !empty($arr_events)) {
        $no_events = false;
        $event_array = mc_set_date_array($arr_events);
        if (is_array($holidays) && count($holidays) > 0) {
            $event_array = mc_holiday_limit($event_array, $holiday_array);
            // if there are holidays, rejigger.
        }
    }
    if (!empty($event_array)) {
        $template = '<strong>{date}</strong> {title} {details}';
        $template = apply_filters('mc_search_template', $template);
        // no filters parameter prevents infinite looping on the_content filters.
        $output = mc_produce_upcoming_events($event_array, $template, 'list', 'ASC', 0, $before, $after, 'yes', 'nofilters');
    } else {
        $output = "<li class='no-results'>" . __('Sorry, your search produced no results.', 'my-calendar') . "</li>";
    }
    return "<ol class='mc-search-results'>{$output}</ol>";
}
function my_calendar_upcoming_events($before = 'default', $after = 'default', $type = 'default', $category = 'default', $template = 'default', $substitute = '', $order = 'asc', $skip = 0, $show_today = 'yes', $author = 'default', $host = 'default', $ltype = '', $lvalue = '', $from = '', $to = '', $site = false)
{
    global $default_template;
    if ($site) {
        $site = $site == 'global' ? BLOG_ID_CURRENT_SITE : $site;
        switch_to_blog($site);
    }
    $args = array('before' => $before, 'after' => $after, 'type' => $type, 'category' => $category, 'template' => $template, 'fallback' => $substitute, 'order' => $order, 'skip' => $skip, 'show_today' => $show_today, 'author' => $author, 'host' => $host, 'ltype' => $ltype, 'lvalue' => $lvalue, 'from' => $from, 'to' => $to, 'site' => $site);
    $hash = md5(implode(',', $args));
    $output = '';
    $widget_defaults = (array) get_option('mc_widget_defaults');
    $display_upcoming_type = $type == 'default' ? $widget_defaults['upcoming']['type'] : $type;
    $display_upcoming_type = $display_upcoming_type == '' ? 'events' : $display_upcoming_type;
    // Get number of units we should go into the future
    $after = $after == 'default' ? $widget_defaults['upcoming']['after'] : $after;
    $after = $after == '' ? 10 : $after;
    // Get number of units we should go into the past
    $before = $before == 'default' ? $widget_defaults['upcoming']['before'] : $before;
    $before = $before == '' ? 0 : $before;
    $category = $category == 'default' ? '' : $category;
    // allow reference by file to external template.
    if ($template != '' && mc_file_exists(sanitize_file_name($template))) {
        $template = @file_get_contents(mc_get_file(sanitize_file_name($template)));
    }
    $template = $template == 'default' ? $widget_defaults['upcoming']['template'] : $template;
    $template = $template == '' ? $default_template : $template;
    if (mc_key_exists($template)) {
        $template = mc_get_custom_template($template);
    }
    $template = apply_filters('mc_upcoming_events_template', $template);
    $no_event_text = $substitute == '' ? $widget_defaults['upcoming']['text'] : $substitute;
    $header = "<ul id='upcoming-events-{$hash}' class='upcoming-events'>";
    $footer = "</ul>";
    $display_events = $display_upcoming_type == 'events' || $display_upcoming_type == 'event' ? true : false;
    if (!$display_events) {
        $temp_array = array();
        if ($display_upcoming_type == "days") {
            $from = date('Y-m-d', strtotime("-{$before} days"));
            $to = date('Y-m-d', strtotime("+{$after} days"));
        }
        if ($display_upcoming_type == "month") {
            $from = date('Y-m-1');
            $to = date('Y-m-t');
        }
        if ($display_upcoming_type == 'custom' && $from != '' && $to != '') {
            $from = date('Y-m-d', strtotime($from));
            $to = $to == 'today' ? date('Y-m-d', current_time('timestamp')) : date('Y-m-d', strtotime($to));
        }
        /* Yes, this is crude. But sometimes simplicity works best. There are only 12 possibilities, after all. */
        if ($display_upcoming_type == 'month+1') {
            $from = date('Y-m-1', strtotime('+1 month'));
            $to = date('Y-m-t', strtotime('+1 month'));
        }
        if ($display_upcoming_type == 'month+2') {
            $from = date('Y-m-1', strtotime('+2 month'));
            $to = date('Y-m-t', strtotime('+2 month'));
        }
        if ($display_upcoming_type == 'month+3') {
            $from = date('Y-m-1', strtotime('+3 month'));
            $to = date('Y-m-t', strtotime('+3 month'));
        }
        if ($display_upcoming_type == 'month+4') {
            $from = date('Y-m-1', strtotime('+4 month'));
            $to = date('Y-m-t', strtotime('+4 month'));
        }
        if ($display_upcoming_type == 'month+5') {
            $from = date('Y-m-1', strtotime('+5 month'));
            $to = date('Y-m-t', strtotime('+5 month'));
        }
        if ($display_upcoming_type == 'month+6') {
            $from = date('Y-m-1', strtotime('+6 month'));
            $to = date('Y-m-t', strtotime('+6 month'));
        }
        if ($display_upcoming_type == 'month+7') {
            $from = date('Y-m-1', strtotime('+7 month'));
            $to = date('Y-m-t', strtotime('+7 month'));
        }
        if ($display_upcoming_type == 'month+8') {
            $from = date('Y-m-1', strtotime('+8 month'));
            $to = date('Y-m-t', strtotime('+8 month'));
        }
        if ($display_upcoming_type == 'month+9') {
            $from = date('Y-m-1', strtotime('+9 month'));
            $to = date('Y-m-t', strtotime('+9 month'));
        }
        if ($display_upcoming_type == 'month+10') {
            $from = date('Y-m-1', strtotime('+10 month'));
            $to = date('Y-m-t', strtotime('+10 month'));
        }
        if ($display_upcoming_type == 'month+11') {
            $from = date('Y-m-1', strtotime('+11 month'));
            $to = date('Y-m-t', strtotime('+11 month'));
        }
        if ($display_upcoming_type == 'month+12') {
            $from = date('Y-m-1', strtotime('+12 month'));
            $to = date('Y-m-t', strtotime('+12 month'));
        }
        if ($display_upcoming_type == "year") {
            $from = date('Y-1-1');
            $to = date('Y-12-31');
        }
        $from = apply_filters('mc_upcoming_date_from', $from, $args);
        $to = apply_filters('mc_upcoming_date_to', $to, $args);
        $event_array = my_calendar_events($from, $to, $category, $ltype, $lvalue, 'upcoming', $author, $host, '', $site);
        if (count($event_array) != 0) {
            foreach ($event_array as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k => $v) {
                        $event = mc_create_tags($v);
                        if ($v->category_private == 1 && !is_user_logged_in()) {
                            // this event is private.
                        } else {
                            $temp_array[] = $event;
                        }
                    }
                }
            }
        }
        $i = 0;
        $last_item = $last_id = $last_date = '';
        $skips = array();
        foreach (reverse_array($temp_array, true, $order) as $details) {
            $item = jd_draw_template($details, $template);
            if ($i < $skip && $skip != 0) {
                $i++;
            } else {
                $today = date('Y-m-d H:i', current_time('timestamp'));
                $date = date('Y-m-d H:i', strtotime($details['ts_occur_begin']));
                $class = my_calendar_date_comp($date, $today) === true ? "past-event" : "future-event";
                $category = 'mc_' . sanitize_title($details['category_name']);
                $prepend = apply_filters('mc_event_upcoming_before', "<li class='{$class} {$category}'>", $class, $category);
                $append = apply_filters('mc_event_upcoming_after', '</li>', $class, $category);
                // if same group, and same date, use it.
                if ($details['group'] !== $last_id || $details['date'] == $last_date || $details['group'] == '0') {
                    if (!in_array($details['dateid'], $skips)) {
                        $output .= $item == $last_item ? '' : $prepend . $item . $append;
                    }
                }
            }
            $skips[] = $details['dateid'];
            // prevent the same event from showing more than once.
            $last_id = $details['group'];
            // prevent group events from displaying in a row. Not if there are intervening events.
            $last_item = $item;
            $last_date = $details['date'];
        }
    } else {
        $caching = apply_filters('mc_cache_enabled', false);
        if ($caching) {
            $cache = get_transient('mc_cache_upcoming');
            $output .= "<!-- Cached -->";
            if ($cache) {
                if (isset($cache[$category])) {
                    $events = $cache[$category];
                } else {
                    $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue, $site);
                    $cache[$category] = $events;
                    set_transient('mc_cache_upcoming', $cache, 60 * 30);
                }
            } else {
                $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue, $site);
                $cache[$category] = $events;
                set_transient('mc_cache_upcoming', $cache, 60 * 30);
            }
        } else {
            $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue, $site);
            // grab all events within reasonable proximity
        }
        $holiday_array = array();
        if (!get_option('mc_skip_holidays_category') || get_option('mc_skip_holidays_category') == '') {
            $holidays = array();
        } else {
            $holidays = mc_get_all_holidays($before, $after, $show_today);
            $holiday_array = mc_set_date_array($holidays);
        }
        if (is_array($events) && !empty($events)) {
            $event_array = mc_set_date_array($events);
            if (is_array($holidays) && count($holidays) > 0) {
                $event_array = mc_holiday_limit($event_array, $holiday_array);
                // if there are holidays, rejigger.
            }
        }
        if (!empty($event_array)) {
            $output .= mc_produce_upcoming_events($event_array, $template, 'list', $order, $skip, $before, $after, $show_today);
        } else {
            $output = '';
        }
    }
    if ($output != '') {
        $output = apply_filters('mc_upcoming_events_header', $header) . $output . apply_filters('mc_upcoming_events_footer', $footer);
        $return = get_option('mc_process_shortcodes') == 'true' ? do_shortcode($output) : $output;
    } else {
        $return = stripcslashes($no_event_text);
    }
    if ($site) {
        restore_current_blog();
    }
    return $return;
}
function my_calendar_upcoming_events($before = 'default', $after = 'default', $type = 'default', $category = 'default', $template = 'default', $substitute = '', $order = 'asc', $skip = 0, $show_today = 'yes', $author = 'default', $host = 'default', $ltype = '', $lvalue = '')
{
    global $default_template;
    $output = '';
    $widget_defaults = (array) get_option('mc_widget_defaults');
    $display_upcoming_type = $type == 'default' ? $widget_defaults['upcoming']['type'] : $type;
    $display_upcoming_type = $display_upcoming_type == '' ? 'event' : $display_upcoming_type;
    // Get number of units we should go into the future
    $after = $after == 'default' ? $widget_defaults['upcoming']['after'] : $after;
    $after = $after == '' ? 10 : $after;
    // Get number of units we should go into the past
    $before = $before == 'default' ? $widget_defaults['upcoming']['before'] : $before;
    $before = $before == '' ? 0 : $before;
    $category = $category == 'default' ? '' : $category;
    // allow reference by file to external template.
    if ($template != '' && mc_file_exists(sanitize_file_name($template))) {
        $template = @file_get_contents(mc_get_file(sanitize_file_name($template)));
    }
    $template = $template == 'default' ? $widget_defaults['upcoming']['template'] : $template;
    $template = $template == '' ? $default_template : $template;
    $no_event_text = $substitute == '' ? $widget_defaults['upcoming']['text'] : $substitute;
    $header = "<ul id='upcoming-events'>";
    $footer = "</ul>";
    if ($display_upcoming_type == "days" || $display_upcoming_type == "month" || $display_upcoming_type == 'month+1' || $display_upcoming_type == "year") {
        $temp_array = array();
        if ($display_upcoming_type == "days") {
            $from = date('Y-m-d', strtotime("-{$before} days"));
            $to = date('Y-m-d', strtotime("+{$after} days"));
        }
        if ($display_upcoming_type == "month") {
            $from = date('Y-m-1');
            $to = date('Y-m-t');
        }
        /* Yes, this is crude. But sometimes simplicity works best. There are only 12 possibilities, after all. */
        if ($display_upcoming_type == 'month+1') {
            $from = date('Y-m-1', strtotime('+1 month'));
            $to = date('Y-m-t', strtotime('+1 month'));
        }
        if ($display_upcoming_type == 'month+2') {
            $from = date('Y-m-1', strtotime('+2 month'));
            $to = date('Y-m-t', strtotime('+2 month'));
        }
        if ($display_upcoming_type == 'month+3') {
            $from = date('Y-m-1', strtotime('+3 month'));
            $to = date('Y-m-t', strtotime('+3 month'));
        }
        if ($display_upcoming_type == 'month+4') {
            $from = date('Y-m-1', strtotime('+4 month'));
            $to = date('Y-m-t', strtotime('+4 month'));
        }
        if ($display_upcoming_type == 'month+5') {
            $from = date('Y-m-1', strtotime('+5 month'));
            $to = date('Y-m-t', strtotime('+5 month'));
        }
        if ($display_upcoming_type == 'month+6') {
            $from = date('Y-m-1', strtotime('+6 month'));
            $to = date('Y-m-t', strtotime('+6 month'));
        }
        if ($display_upcoming_type == 'month+7') {
            $from = date('Y-m-1', strtotime('+7 month'));
            $to = date('Y-m-t', strtotime('+7 month'));
        }
        if ($display_upcoming_type == 'month+8') {
            $from = date('Y-m-1', strtotime('+8 month'));
            $to = date('Y-m-t', strtotime('+8 month'));
        }
        if ($display_upcoming_type == 'month+9') {
            $from = date('Y-m-1', strtotime('+9 month'));
            $to = date('Y-m-t', strtotime('+9 month'));
        }
        if ($display_upcoming_type == 'month+10') {
            $from = date('Y-m-1', strtotime('+10 month'));
            $to = date('Y-m-t', strtotime('+10 month'));
        }
        if ($display_upcoming_type == 'month+11') {
            $from = date('Y-m-1', strtotime('+11 month'));
            $to = date('Y-m-t', strtotime('+11 month'));
        }
        if ($display_upcoming_type == 'month+12') {
            $from = date('Y-m-1', strtotime('+12 month'));
            $to = date('Y-m-t', strtotime('+12 month'));
        }
        if ($display_upcoming_type == "year") {
            $from = date('Y-1-1');
            $to = date('Y-12-31');
        }
        $from = apply_filters('mc_upcoming_date_from', $from);
        $to = apply_filters('mc_upcoming_date_to', $to);
        $event_array = my_calendar_events($from, $to, $category, $ltype, $lvalue, 'upcoming', $author, $host);
        if (count($event_array) != 0) {
            foreach ($event_array as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k => $v) {
                        $event = mc_create_tags($v);
                        if ($v->category_private == 1 && !is_user_logged_in()) {
                            // this event is private.
                        } else {
                            $temp_array[] = $event;
                        }
                    }
                }
            }
        }
        $i = 0;
        $last_item = $last_id = $last_date = '';
        $skips = array();
        foreach (reverse_array($temp_array, true, $order) as $details) {
            $item = jd_draw_template($details, $template);
            if ($i < $skip && $skip != 0) {
                $i++;
            } else {
                // if same group, and same date, use it.
                if ($details['group'] !== $last_id || $details['date'] == $last_date || $details['group'] == '0') {
                    if (!in_array($details['dateid'], $skips)) {
                        $output .= $item == $last_item ? '' : "<li>{$item}</li>";
                    }
                }
            }
            $skips[] = $details['dateid'];
            // prevent the same event from showing more than once.
            $last_id = $details['group'];
            // prevent group events from displaying in a row. Not if there are intervening events.
            $last_item = $item;
            $last_date = $details['date'];
        }
    } else {
        $caching = apply_filters('mc_cache_enabled', false);
        if ($caching) {
            $cache = get_transient('mc_cache_upcoming');
            $output .= "<!-- Cached -->";
            if ($cache) {
                if (isset($cache[$category])) {
                    $events = $cache[$category];
                } else {
                    $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue);
                    $cache[$category] = $events;
                    set_transient('mc_cache_upcoming', $cache, 60 * 30);
                }
            } else {
                $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue);
                $cache[$category] = $events;
                set_transient('mc_cache_upcoming', $cache, 60 * 30);
            }
        } else {
            $events = mc_get_all_events($category, $before, $after, $show_today, $author, $host, $ltype, $lvalue);
            // grab all events within reasonable proximity
        }
        $holiday_array = array();
        if (!get_option('mc_skip_holidays_category') || get_option('mc_skip_holidays_category') == '') {
            $holidays = array();
        } else {
            $holidays = mc_get_all_holidays($before, $after, $show_today);
            $holiday_array = mc_set_date_array($holidays);
        }
        if (is_array($events) && !empty($events)) {
            $event_array = mc_set_date_array($events);
            if (is_array($holidays) && count($holidays) > 0) {
                $event_array = mc_holiday_limit($event_array, $holiday_array);
                // if there are holidays, rejigger.
            }
        }
        if (!empty($event_array)) {
            $output .= mc_produce_upcoming_events($event_array, $template, 'list', $order, $skip, $before, $after, $show_today);
        } else {
            $output = '';
        }
    }
    if ($output != '') {
        $output = $header . $output . $footer;
        return $output;
    } else {
        return stripcslashes($no_event_text);
    }
}
function mc_search_results($query)
{
    $before = apply_filters('mc_past_search_results', 0, 'basic');
    $after = apply_filters('mc_future_search_results', 10, 'basic');
    // return only future events, nearest 10
    if (is_string($query)) {
        $search = mc_prepare_search_query($query);
        $term = $query;
    } else {
        $search = apply_filters('mc_advanced_search', '', $query);
        $term = $query['mcs'];
        $before = apply_filters('mc_past_search_results', 10, 'advanced');
        $after = apply_filters('mc_future_search_results', 10, 'advanced');
    }
    $event_array = mc_get_search_results($search);
    //$event_array = mc_flatten_array( $event_array );
    if (!empty($event_array)) {
        $template = '<strong>{date}</strong> {title} {details}';
        $template = apply_filters('mc_search_template', $template);
        // no filters parameter prevents infinite looping on the_content filters.
        $output = mc_produce_upcoming_events($event_array, $template, 'list', 'ASC', 0, $before, $after, 'yes', 'nofilters');
    } else {
        $output = apply_filters('mc_search_no_results', "<li class='no-results'>" . __('Sorry, your search produced no results.', 'my-calendar') . "</li>");
    }
    $header = apply_filters('mc_search_before', '<ol class="mc-search-results">', $term);
    $footer = apply_filters('mc_search_after', '</ol>', $term);
    return $header . $output . $footer;
}