Esempio n. 1
0
function mc_dateclass($now, $current)
{
    if (date("Ymd", $now) == date("Ymd", $current)) {
        $dateclass = 'current-day';
    } else {
        if (my_calendar_date_comp(date('Y-m-d', $now), date('Y-m-d', $current))) {
            $dateclass = 'future-day';
        } else {
            $dateclass = 'past-day past-date';
            // stupid legacy classes.
        }
    }
    return $dateclass;
}
function mc_produce_upcoming_events($events, $template, $type = 'list', $order = 'asc', $skip = 0, $before, $after, $show_today = 'yes', $context = 'filters')
{
    // $events has +5 before and +5 after if those values are non-zero.
    // $events equals array of events based on before/after queries. Nothing skipped, order is not set, holiday conflicts removed.
    $output = array();
    $near_events = $temp_array = array();
    $past = $future = 1;
    $now = current_time('timestamp');
    $today = date('Y', $now) . '-' . date('m', $now) . '-' . date('d', $now);
    @uksort($events, "my_calendar_timediff_cmp");
    // sort all events by proximity to current date
    $count = count($events);
    $group = array();
    $spans = array();
    $extra = 0;
    $i = 0;
    // create near_events array
    $last_events = $last_group = array();
    if (is_array($events)) {
        foreach ($events as $k => $event) {
            if ($i < $count) {
                if (is_array($event)) {
                    foreach ($event as $e) {
                        if ($e->category_private == 1 && !is_user_logged_in()) {
                        } else {
                            $beginning = $e->occur_begin;
                            $end = $e->occur_end;
                            // store span time in an array to avoid repeating database query
                            if ($e->event_span == 1 && !isset($spans[$e->occur_group_id])) {
                                // this is a multi-day event: treat each event as if it spanned the entire range of the group.
                                $span_time = mc_span_time($e->occur_group_id);
                                $beginning = $span_time[0];
                                $end = $span_time[1];
                                $spans[$e->occur_group_id] = $span_time;
                            } else {
                                if ($e->event_span == 1 && isset($spans[$e->occur_group_id])) {
                                    $span_time = $spans[$e->occur_group_id];
                                    $beginning = $span_time[0];
                                    $end = $span_time[1];
                                }
                            }
                            $current = date('Y-m-d H:i:00', current_time('timestamp'));
                            if ($e) {
                                // if a multi-day event, show only once.
                                if ($e->occur_group_id != 0 && $e->event_span == 1 && in_array($e->occur_group_id, $group)) {
                                    $md = true;
                                } else {
                                    $group[] = $e->occur_group_id;
                                    $md = false;
                                }
                                // end multi-day reduction
                                if (!$md) {
                                    // check if this event instance or this event group has already been displayed
                                    $same_event = in_array($e->occur_id, $last_events) ? true : false;
                                    $same_group = in_array($e->occur_group_id, $last_group) ? true : false;
                                    if ($show_today == 'yes' && my_calendar_date_equal($beginning, $current)) {
                                        $in_total = 'yes';
                                        // count todays events in total
                                        if ($in_total != 'no') {
                                            $near_events[] = $e;
                                            $future++;
                                        } else {
                                            $near_events[] = $e;
                                        }
                                    } else {
                                        if ($past <= $before && $future <= $after) {
                                            $near_events[] = $e;
                                            // if neither limit is reached, split off freely
                                        } else {
                                            if ($past <= $before && my_calendar_date_comp($beginning, $current)) {
                                                $near_events[] = $e;
                                                // split off another past event
                                            } else {
                                                if ($future <= $after && !my_calendar_date_comp($end, $current)) {
                                                    $near_events[] = $e;
                                                    // split off another future event
                                                }
                                            }
                                        }
                                    }
                                    if (my_calendar_date_comp($beginning, $current)) {
                                        if (!$same_event && !$same_group) {
                                            $past++;
                                        }
                                    } else {
                                        if (my_calendar_date_equal($beginning, $current)) {
                                            if ($show_today == 'yes') {
                                                $extra++;
                                            }
                                        } else {
                                            if (!my_calendar_date_comp($end, $current)) {
                                                if (!$same_event && !$same_group) {
                                                    $future++;
                                                }
                                            }
                                        }
                                    }
                                    $last_events[] = $e->occur_id;
                                    $last_group[] = $e->occur_group_id;
                                    $last_date = $beginning;
                                }
                                if ($past > $before && $future > $after && $show_today != 'yes') {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    $events = $near_events;
    @usort($events, "my_calendar_datetime_cmp");
    // sort split events by date
    if (is_array($events)) {
        foreach (array_keys($events) as $key) {
            $event =& $events[$key];
            //echo $event->event_title . " " . $event->event_group_id."<br />";
            $event_details = mc_create_tags($event, $context);
            if (get_option('mc_event_approve') == 'true') {
                if ($event->event_approved != 0) {
                    $temp_array[] = $event_details;
                }
            } else {
                $temp_array[] = $event_details;
            }
        }
        $i = 0;
        $groups = array();
        $skips = array();
        foreach (reverse_array($temp_array, true, $order) as $details) {
            if (!in_array($details['group'], $groups)) {
                $date = date('Y-m-d', strtotime($details['dtstart']));
                $class = my_calendar_date_comp($date, $today) === true ? "past-event" : "future-event";
                if (my_calendar_date_equal($date, $today)) {
                    $class = "today";
                }
                if ($details['event_span'] == 1) {
                    $class = "multiday";
                }
                if ($type == 'list') {
                    $prepend = "\n<li class=\"{$class}\">";
                    $append = "</li>\n";
                } else {
                    $prepend = $append = '';
                }
                if ($i < $skip && $skip != 0) {
                    $i++;
                } else {
                    if (!in_array($details['dateid'], $skips)) {
                        $output[] = apply_filters('mc_event_upcoming', "{$prepend}" . jd_draw_template($details, $template, $type) . "{$append}", $event);
                        $skips[] = $details['dateid'];
                    }
                }
                if ($details['event_span'] == 1) {
                    $groups[] = $details['group'];
                }
            }
        }
    }
    // If more items than there should be (due to handling of current-day's events), pop off.
    $intended = $before + $after + $extra;
    $actual = count($output);
    if ($actual > $intended) {
        for ($i = 0; $i < $actual - $intended; $i++) {
            array_pop($output);
        }
    }
    $html = '';
    foreach ($output as $out) {
        $html .= $out;
    }
    return $html;
}