Esempio n. 1
0
function eme_ical_single_event($event, $title_format, $description_format)
{
    $title = eme_sanitize_ical(eme_replace_placeholders($title_format, $event, "text"));
    $description = eme_sanitize_ical(eme_replace_placeholders($description_format, $event, "text"));
    $html_description = eme_sanitize_ical(eme_replace_placeholders($description_format, $event, "html"), 1);
    $event_link = eme_event_url($event);
    $startstring = $event['event_start_date'] . " " . $event['event_start_time'];
    $dtstartdate = eme_localised_date($startstring, "Ymd");
    $dtstarthour = eme_localised_date($startstring, "His");
    //$dtstart=$dtstartdate."T".$dtstarthour."Z";
    // we'll use localtime, so no "Z"
    $dtstart = $dtstartdate . "T" . $dtstarthour;
    if ($event['event_end_date'] == "") {
        $event['event_end_date'] = $event['event_start_date'];
    }
    if ($event['event_end_time'] == "") {
        $event['event_end_time'] = $event['event_start_time'];
    }
    $endstring = $event['event_end_date'] . " " . $event['event_end_time'];
    $dtenddate = eme_localised_date($endstring, "Ymd");
    $dtendhour = eme_localised_date($endstring, "His");
    //$dtend=$dtenddate."T".$dtendhour."Z";
    // we'll use localtime, so no "Z"
    $dtend = $dtenddate . "T" . $dtendhour;
    $tzstring = get_option('timezone_string');
    $res = "";
    $res .= "BEGIN:VEVENT\r\n";
    //DTSTAMP must be in UTC format, so adding "Z" as well
    $res .= "DTSTAMP:" . gmdate('Ymd') . 'T' . gmdate('His') . "Z\r\n";
    if ($event['event_properties']['all_day']) {
        // ical standard for an all day event: specify only the day, meaning
        // an 'all day' event is flagged as starting at the beginning of one day and lasting until the beginning of the next
        // so it is the same as adding "T000000" as time spec to the start/end datestring
        // But since it "ends" at the beginning of the next day, we should add 24 hours, otherwise the event ends one day too soon
        $dtenddate = eme_localised_date(eme_date_calc("+1 day", $endstring), "Ymd");
        $res .= "DTSTART;VALUE=DATE:{$dtstartdate}\r\n";
        $res .= "DTEND;VALUE=DATE:{$dtenddate}\r\n";
    } else {
        $res .= "DTSTART;TZID={$tzstring}:{$dtstart}\r\n";
        $res .= "DTEND;TZID={$tzstring}:{$dtend}\r\n";
    }
    $res .= "UID:{$dtstart}-{$dtend}-" . $event['event_id'] . "@" . $_SERVER['SERVER_NAME'] . "\r\n";
    $res .= "SUMMARY:{$title}\r\n";
    $res .= "DESCRIPTION:{$description}\r\n";
    $res .= "X-ALT-DESC;FMTTYPE=text/html:{$html_description}\r\n";
    $res .= "URL:{$event_link}\r\n";
    $res .= "ATTACH:{$event_link}\r\n";
    if ($event['event_image_id']) {
        $thumb_array = image_downsize($event['event_image_id'], get_option('eme_thumbnail_size'));
        $thumb_url = $thumb_array[0];
        $res .= "ATTACH:{$thumb_url}\r\n";
    }
    if (isset($event['location_id']) && $event['location_id']) {
        $location = eme_sanitize_ical(eme_replace_placeholders("#_LOCATION, #_ADDRESS, #_TOWN", $event, "text"));
        $res .= "LOCATION:{$location}\r\n";
    }
    $res .= "END:VEVENT\r\n";
    return $res;
}
Esempio n. 2
0
function eme_create_month_scope($count)
{
    $scope = array();
    $scope[0] = __('Select Month', 'eme');
    for ($i = 0; $i < $count; $i++) {
        $limit_start = eme_date_calc("first day of {$i} month");
        $limit_end = eme_date_calc("last day of {$i} month");
        $this_scope = "{$limit_start}--{$limit_end}";
        $scope_text = eme_localised_date($limit_start, get_option('eme_show_period_monthly_dateformat'));
        $scope[$this_scope] = $scope_text;
    }
    return $scope;
}
Esempio n. 3
0
function eme_global_map($atts)
{
    global $eme_need_gmap_js;
    if (get_option('eme_gmap_is_active') == '1') {
        // the locations shortcode has been deteced, so we indicate
        // that we want the javascript in the footer as well
        $eme_need_gmap_js = 1;
        extract(shortcode_atts(array('show_locations' => true, 'show_events' => false, 'eventful' => false, 'scope' => 'all', 'paging' => 0, 'category' => '', 'width' => 450, 'height' => 300, 'list_location' => 'after'), $atts));
        $eventful = $eventful === "true" || $eventful === "1" ? true : $eventful;
        $show_events = $show_events === "true" || $show_events === "1" ? true : $show_events;
        $show_locations = $show_locations === "true" || $show_locations === "1" ? true : $show_locations;
        $eventful = $eventful === "false" || $eventful === "0" ? false : $eventful;
        $show_events = $show_events === "false" || $show_events === "0" ? false : $show_events;
        $show_locations = $show_locations === "false" || $show_locations === "0" ? false : $show_locations;
        $events_page_link = eme_get_events_page(true, false);
        $prev_text = "";
        $next_text = "";
        $scope_offset = 0;
        // for browsing: if paging=1 and only for this_week,this_month or today
        if ($eventful && $paging == 1) {
            if (isset($_GET['eme_offset'])) {
                $scope_offset = $_GET['eme_offset'];
            }
            $prev_offset = $scope_offset - 1;
            $next_offset = $scope_offset + 1;
            if ($scope == "this_week") {
                $start_of_week = get_option('start_of_week');
                $day_offset = date('w') - $start_of_week;
                if ($day_offset < 0) {
                    $day_offset += 7;
                }
                $limit_start = eme_date_calc("-{$day_offset} days +{$scope_offset} week");
                $limit_end = eme_date_calc("+6 days", $start_day);
                $scope = "{$limit_start}--{$limit_end}";
                $scope_text = eme_localised_date($limit_start) . " -- " . eme_localised_date($limit_end);
                $prev_text = __('Previous week', 'eme');
                $next_text = __('Next week', 'eme');
            } elseif ($scope == "this_month") {
                $limit_start = eme_date_calc("first day of {$scope_offset} month");
                $limit_end = eme_date_calc("last day of {$scope_offset} month");
                $scope = "{$limit_start}--{$limit_end}";
                $scope_text = eme_localised_date($limit_start, get_option('eme_show_period_monthly_dateformat'));
                $prev_text = __('Previous month', 'eme');
                $next_text = __('Next month', 'eme');
            } elseif ($scope == "this_year") {
                $year = date('Y') + $scope_offset;
                $limit_start = "{$year}-01-01";
                $limit_end = "{$year}-12-31";
                $scope = "{$limit_start}--{$limit_end}";
                $scope = "{$limit_start}--{$limit_end}";
                $scope_text = eme_localised_date($limit_start, get_option('eme_show_period_yearly_dateformat'));
                $prev_text = __('Previous year', 'eme');
                $next_text = __('Next year', 'eme');
            } elseif ($scope == "today") {
                $scope = date('Y-m-d', strtotime("{$scope_offset} days"));
                $limit_start = $scope;
                $limit_end = $scope;
                $scope_text = eme_localised_date($limit_start);
                $prev_text = __('Previous day', 'eme');
                $next_text = __('Next day', 'eme');
            } elseif ($scope == "tomorrow") {
                $scope_offset++;
                $scope = date('Y-m-d', strtotime("{$scope_offset} days"));
                $limit_start = $scope;
                $limit_end = $scope;
                $scope_text = eme_localised_date($limit_start);
                $prev_text = __('Previous day', 'eme');
                $next_text = __('Next day', 'eme');
            }
            // to prevent going on indefinitely and thus allowing search bots to go on for ever,
            // we stop providing links if there are no more events left
            $older_events = eme_get_events(1, "--" . $limit_start, "ASC", 0, $location['location_id'], $category);
            $newer_events = eme_get_events(1, "++" . $limit_end, "ASC", 0, $location['location_id'], $category);
            if (count($older_events) == 0) {
                $prev_text = "";
            }
            if (count($newer_events) == 0) {
                $next_text = "";
            }
        }
        $id_base = preg_replace("/\\D/", "_", microtime(1));
        $id_base = rand() . "_" . $id_base;
        if (!preg_match('/\\%$|px$/', $width)) {
            $width = $width . "px";
        }
        if (!preg_match('/\\%$|px$/', $height)) {
            $height = $height . "px";
        }
        $result = "<div id='eme_global_map_{$id_base}' class='eme_global_map' style='width: {$width}; height: {$height}'>map</div>";
        // get the paging output ready
        if ($paging == 1) {
            $pagination_top = "<div id='locations-pagination-top'> ";
            $this_page_url = $_SERVER['REQUEST_URI'];
            // remove the offset info
            $this_page_url = remove_query_arg('eme_offset', $this_page_url);
            if ($prev_text != "") {
                $pagination_top .= "<a class='eme_nav_left' href='" . add_query_arg(array('eme_offset' => $prev_offset), $this_page_url) . "'>&lt;&lt; {$prev_text}</a>";
            }
            if ($next_text != "") {
                $pagination_top .= "<a class='eme_nav_right' href='" . add_query_arg(array('eme_offset' => $next_offset), $this_page_url) . "'>{$next_text} &gt;&gt;</a>";
            }
            $pagination_top .= "<span class='eme_nav_center'>{$scope_text}</span>";
            $pagination_top .= "</div>";
            $pagination_bottom = str_replace("locations-pagination-top", "locations-pagination-bottom", $pagination_top);
            $result = $pagination_top . $result . $pagination_bottom;
        }
        $eventful_string = "eventful_" . $id_base;
        $scope_string = "scope_" . $id_base;
        $category_string = "category_" . $id_base;
        $result .= "<script type='text/javascript'>\n         <!--// \n      {$eventful_string} = '{$eventful}';\n      {$scope_string} = '{$scope}';\n      {$category_string} = '{$category}';\n      events_page_link = '{$events_page_link}';\n         //-->\n         </script>";
        // we add the list if wanted (only for "before" or "after")
        $locations = eme_get_locations((bool) $eventful, $scope, $category, $scope_offset);
        $loc_list = "<div id='eme_div_locations_list_{$id_base}' class='eme_div_locations_list'><ol id='eme_locations_list_{$id_base}' class='eme_locations_list'>";
        $firstletter = "A";
        foreach ($locations as $location) {
            if ($show_locations) {
                $loc_list .= "<li id='location-" . $location['location_id'] . "_{$id_base}" . "' style='list-style-type: upper-alpha'><a>" . eme_trans_sanitize_html($location['location_name']) . "</a></li>";
            }
            if ($show_events) {
                $events = eme_get_events(0, $scope, "ASC", $scope_offset, $location['location_id'], $category);
                $loc_list .= "<ol id='eme_events_list'>";
                foreach ($events as $event) {
                    if ($show_locations) {
                        $loc_list .= "<li id='location-" . $location['location_id'] . "_{$id_base}" . "' style='list-style-type: none'>- <a>" . eme_trans_sanitize_html($event['event_name']) . "</a></li>";
                    } else {
                        $loc_list .= "<li id='location-" . $location['location_id'] . "_{$id_base}" . "' style='list-style-type: none'>{$firstletter}. <a>" . eme_trans_sanitize_html($event['event_name']) . "</a></li>";
                    }
                }
                $loc_list .= "</ol>";
            }
            // cool: we can increment strings in php, so we can mimic the CSS "style='list-style-type: upper-alpha'" thingie
            // usefull when we show events (more than one event per location)
            $firstletter++;
        }
        $loc_list .= "</ol></div>";
        if ($list_location == "before") {
            $result = $loc_list . $result;
        } elseif ($list_location == "after") {
            $result .= $loc_list;
        }
    } else {
        $result = "";
    }
    return $result;
}
Esempio n. 4
0
function eme_get_events($o_limit, $scope = "future", $order = "ASC", $o_offset = 0, $location_id = "", $category = "", $author = "", $contact_person = "", $show_ongoing = 1, $notcategory = "", $show_recurrent_events_once = 0, $extra_conditions = "")
{
    global $wpdb;
    $events_table = $wpdb->prefix . EVENTS_TBNAME;
    $bookings_table = $wpdb->prefix . BOOKINGS_TBNAME;
    if ($o_limit === "") {
        $o_limit = get_option('eme_event_list_number_items');
    }
    if ($o_limit > 0) {
        $limit = "LIMIT " . intval($o_limit);
    } else {
        $limit = "";
    }
    if ($o_offset > 0) {
        if ($o_limit == 0) {
            $limit = "LIMIT " . intval($o_offset);
        }
        $offset = "OFFSET " . intval($o_offset);
    } else {
        $offset = "";
    }
    if ($order != "DESC") {
        $order = "ASC";
    }
    $today = date("Y-m-d");
    $this_time = date("H:i:00");
    $conditions = array();
    // extra sql conditions we put in front, most of the time this is the most
    // effective place
    if ($extra_conditions != "") {
        $conditions[] = $extra_conditions;
    }
    // if we're not in the admin itf, we don't want draft events
    if (!is_admin()) {
        if (is_user_logged_in()) {
            $conditions[] = "event_status IN (" . STATUS_PUBLIC . "," . STATUS_PRIVATE . ")";
        } else {
            $conditions[] = "event_status=" . STATUS_PUBLIC;
        }
        if (get_option('eme_rsvp_hide_full_events')) {
            // COALESCE is used in case the SUM returns NULL
            // this is a correlated subquery, so the FROM clause should specify events_table again, so it will search in the outer query for events_table.event_id
            $conditions[] = "(event_rsvp=0 OR (event_rsvp=1 AND event_seats > (SELECT COALESCE(SUM(booking_seats),0) AS booked_seats FROM {$bookings_table} WHERE {$bookings_table}.event_id = {$events_table}.event_id)))";
        }
        if (get_option('eme_rsvp_hide_rsvp_ended_events')) {
            $conditions[] = "(event_rsvp=0 OR (event_rsvp=1 AND (event_end_date < '{$today}' OR UNIX_TIMESTAMP(CONCAT(event_start_date,' ',event_start_time))-rsvp_number_days*60*60*24-rsvp_number_hours*60*60 > UNIX_TIMESTAMP()) ))";
        }
    }
    if (preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}\$/", $scope)) {
        //$conditions[] = " event_start_date like '$scope'";
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date LIKE '{$scope}') OR (event_start_date <= '{$scope}' AND event_end_date >= '{$scope}'))";
        } else {
            $conditions[] = " (event_start_date LIKE '{$scope}') ";
        }
    } elseif (preg_match("/^--([0-9]{4}-[0-9]{2}-[0-9]{2})\$/", $scope, $matches)) {
        $limit_start = $matches[1];
        if ($show_ongoing) {
            $conditions[] = " (event_start_date < '{$limit_start}') ";
        } else {
            $conditions[] = " (event_end_date < '{$limit_start}') ";
        }
    } elseif (preg_match("/^\\+\\+([0-9]{4}-[0-9]{2}-[0-9]{2})\$/", $scope, $matches)) {
        $limit_start = $matches[1];
        $conditions[] = " (event_start_date > '{$limit_start}') ";
    } elseif (preg_match("/^0000-([0-9]{2})\$/", $scope, $matches)) {
        $year = date('Y');
        $month = $matches[1];
        $number_of_days_month = eme_days_in_month($month, $year);
        $limit_start = "{$year}-{$month}-01";
        $limit_end = "{$year}-{$month}-{$number_of_days_month}";
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "this_week") {
        // this comes from global wordpress preferences
        $start_of_week = get_option('start_of_week');
        $day_offset = date('w') - $start_of_week;
        if ($day_offset < 0) {
            $day_offset += 7;
        }
        $limit_start = eme_date_calc("-{$day_offset} days");
        $limit_end = eme_date_calc("+6 days", $start_day);
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "next_week") {
        // this comes from global wordpress preferences
        $start_of_week = get_option('start_of_week');
        $day_offset = date('w') - $start_of_week;
        if ($day_offset < 0) {
            $day_offset += 7;
        }
        $day_offset += 7;
        // add 7 days for next week
        $limit_start = eme_date_calc("{$day_offset} days");
        $limit_end = eme_date_calc("+6 days", $start_day);
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "this_month") {
        $limit_start = eme_date_calc("first day of this month");
        $limit_end = eme_date_calc("last day of this month");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "this_year") {
        $year = date('Y');
        $limit_start = "{$year}-01-01";
        $limit_end = "{$year}-12-31";
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "next_month") {
        // the year/month should be based on the first of the month, so if we are the 13th, we substract 12 days to get to day 1
        // Reason: monthly offsets needs to be calculated based on the first day of the current month, not the current day,
        //    otherwise if we're now on the 31st we'll skip next month since it has only 30 days
        $limit_start = eme_date_calc("first day of next month");
        $limit_end = eme_date_calc("last day of next month");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^([0-9]{4}-[0-9]{2}-[0-9]{2})--([0-9]{4}-[0-9]{2}-[0-9]{2})\$/", $scope, $matches)) {
        $limit_start = $matches[1];
        $limit_end = $matches[2];
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^([0-9]{4}-[0-9]{2}-[0-9]{2})--today\$/", $scope, $matches)) {
        $limit_start = $matches[1];
        $limit_end = $today;
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^today--([0-9]{4}-[0-9]{2}-[0-9]{2})\$/", $scope, $matches)) {
        $limit_start = $today;
        $limit_end = $matches[1];
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^\\+(\\d+)d\$/", $scope, $matches)) {
        $limit_start = $today;
        $days = $matches[1];
        $limit_end = eme_date_calc("+{$days} days");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^\\-(\\d+)d\$/", $scope, $matches)) {
        $limit_end = $today;
        $days = $matches[1];
        $limit_start = eme_date_calc("-{$days} days");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^(\\-?\\+?\\d+)d--(\\-?\\+?\\d+)d\$/", $scope, $matches)) {
        $day1 = $matches[1];
        $day2 = $matches[2];
        $limit_start = eme_date_calc("{$day1} days");
        $limit_end = eme_date_calc("{$day2} days");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^relative\\-(\\d+)d--([0-9]{4}-[0-9]{2}-[0-9]{2})\$/", $scope, $matches)) {
        $days = $matches[1];
        $limit_end = $matches[2];
        $limit_start = eme_date_calc("-{$days} days", strtotime($limit_end));
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^([0-9]{4}-[0-9]{2}-[0-9]{2})--relative\\+(\\d+)d\$/", $scope, $matches)) {
        $limit_start = $matches[1];
        $days = $matches[2];
        $limit_end = eme_date_calc("+{$days} days", strtotime($limit_start));
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^\\+(\\d+)m\$/", $scope, $matches)) {
        // the year/month should be based on the first of the month, so if we are the 13th, we substract 12 days to get to day 1
        // Reason: monthly offsets needs to be calculated based on the first day of the current month, not the current day,
        //    otherwise if we're now on the 31st we'll skip next month since it has only 30 days
        $day_offset = date('j') - 1;
        $months_in_future = $matches[1]++;
        $limit_start = eme_date_calc("first day of this month");
        $limit_end = eme_date_calc("last day of +{$months_in_future} month");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^\\-(\\d+)m\$/", $scope, $matches)) {
        // the year/month should be based on the first of the month, so if we are the 13th, we substract 12 days to get to day 1
        // Reason: monthly offsets needs to be calculated based on the first day of the current month, not the current day,
        //    otherwise if we're now on the 31st we'll skip next month since it has only 30 days
        $day_offset = date('j') - 1;
        $months_in_past = $matches[1]++;
        $limit_start = eme_date_calc("first day of -{$months_in_past} month");
        $limit_end = eme_date_calc("last day of last month");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif (preg_match("/^(\\-?\\+?\\d+)m--(\\-?\\+?\\d+)m\$/", $scope, $matches)) {
        $day_offset = date('j') - 1;
        $months1 = $matches[1];
        $months2 = $matches[2];
        $limit_start = eme_date_calc("first day of {$months1} month");
        $limit_end = eme_date_calc("last day of {$months2} month");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "today--this_week") {
        $start_of_week = get_option('start_of_week');
        $end_day = 6 - date('w') + $start_of_week;
        $limit_start = $today;
        $limit_end = eme_date_calc("+{$end_day} days");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "today--this_week_plus_one") {
        $start_of_week = get_option('start_of_week');
        $end_day = 7 - date('w') + $start_of_week;
        $limit_start = $today;
        $limit_end = eme_date_calc("+{$end_day} days");
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "today--this_month") {
        $limit_start = $today;
        $limit_end = date('Y-m-d', strtotime("last day of this month"));
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "today--this_year") {
        $limit_start = $today;
        $limit_end = "{$year}-12-31";
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "this_week--today") {
        $start_of_week = get_option('start_of_week');
        $day_offset = date('w') - $start_of_week;
        if ($day_offset < 0) {
            $day_offset += 7;
        }
        $limit_start = eme_date_calc("-{$day_offset} days");
        $limit_end = $today;
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "this_month--today") {
        $limit_start = eme_date_calc("first day of this month");
        $limit_end = $today;
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "this_year--today") {
        $limit_start = "{$year}-01-01";
        $limit_end = $today;
        if ($show_ongoing) {
            $conditions[] = " ((event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_end_date BETWEEN '{$limit_start}' AND '{$limit_end}') OR (event_start_date <= '{$limit_start}' AND event_end_date >= '{$limit_end}'))";
        } else {
            $conditions[] = " (event_start_date BETWEEN '{$limit_start}' AND '{$limit_end}')";
        }
    } elseif ($scope == "tomorrow--future") {
        if ($show_ongoing) {
            $conditions[] = " (event_start_date > '{$today}' OR (event_end_date > '{$today}' AND event_end_date != '0000-00-00' AND event_end_date IS NOT NULL))";
        } else {
            $conditions[] = " (event_start_date > '{$today}')";
        }
    } elseif ($scope == "past") {
        //$conditions[] = " (event_end_date < '$today' OR (event_end_date = '$today' and event_end_time < '$this_time' )) ";
        // not taking the hour into account until we can enter timezone info as well
        if ($show_ongoing) {
            $conditions[] = " event_end_date < '{$today}'";
        } else {
            $conditions[] = " event_start_date < '{$today}'";
        }
    } elseif ($scope == "today") {
        if ($show_ongoing) {
            $conditions[] = " (event_start_date = '{$today}' OR (event_start_date <= '{$today}' AND event_end_date >= '{$today}'))";
        } else {
            $conditions[] = " (event_start_date = '{$today}')";
        }
    } elseif ($scope == "tomorrow") {
        $tomorrow = eme_date_calc("tomorrow");
        if ($show_ongoing) {
            $conditions[] = " (event_start_date = '{$tomorrow}' OR (event_start_date <= '{$tomorrow}' AND event_end_date >= '{$tomorrow}'))";
        } else {
            $conditions[] = " (event_start_date = '{$tomorrow}')";
        }
    } elseif ($scope == "ongoing") {
        // only shows ongoing events, for this we try to use the date and time, but it might be incorrect since there's no user timezone info
        $conditions[] = " (CONCAT(event_start_date,' ',event_start_time)<='{$today} {$this_time}' AND CONCAT(event_end_date,' ',event_end_time)>= '{$today} {$this_time}')";
    } else {
        if ($scope != "all") {
            $scope = "future";
        }
        if ($scope == "future") {
            //$conditions[] = " ((event_start_date = '$today' AND event_start_time >= '$this_time') OR (event_start_date > '$today') OR (event_end_date > '$today' AND event_end_date != '0000-00-00' AND event_end_date IS NOT NULL) OR (event_end_date = '$today' AND event_end_time >= '$this_time'))";
            // not taking the hour into account until we can enter timezone info as well
            if ($show_ongoing) {
                $conditions[] = " (event_start_date >= '{$today}' OR (event_end_date >= '{$today}' AND event_end_date != '0000-00-00' AND event_end_date IS NOT NULL))";
            } else {
                $conditions[] = " (event_start_date >= '{$today}')";
            }
        }
    }
    // when used inside a location description, you can use this_location to indicate the current location being viewed
    if ($location_id == "this_location" && get_query_var('location_id')) {
        $location_id = get_query_var('location_id');
    }
    if (is_numeric($location_id)) {
        if ($location_id > 0) {
            $conditions[] = " location_id = {$location_id}";
        }
    } elseif ($location_id == "none") {
        $conditions[] = " location_id = ''";
    } elseif (preg_match('/,/', $location_id)) {
        $location_ids = explode(',', $location_id);
        $location_conditions = array();
        foreach ($location_ids as $loc) {
            if (is_numeric($loc) && $loc > 0) {
                $location_conditions[] = " location_id = {$loc}";
            } elseif ($loc == "none") {
                $location_conditions[] = " location_id = ''";
            }
        }
        $conditions[] = "(" . implode(' OR', $location_conditions) . ")";
    } elseif (preg_match('/\\+/', $location_id)) {
        $location_ids = explode('+', $location_id);
        $location_conditions = array();
        foreach ($location_ids as $loc) {
            if (is_numeric($loc) && $loc > 0) {
                $location_conditions[] = " location_id = {$loc}";
            }
        }
        $conditions[] = "(" . implode(' AND', $location_conditions) . ")";
    } elseif (preg_match('/ /', $location_id)) {
        // url decoding of '+' is ' '
        $location_ids = explode(' ', $location_id);
        $location_conditions = array();
        foreach ($location_ids as $loc) {
            if (is_numeric($loc) && $loc > 0) {
                $location_conditions[] = " location_id = {$loc}";
            }
        }
        $conditions[] = "(" . implode(' AND', $location_conditions) . ")";
    }
    if (get_option('eme_categories_enabled')) {
        if (is_numeric($category)) {
            if ($category > 0) {
                $conditions[] = " FIND_IN_SET({$category},event_category_ids)";
            }
        } elseif ($category == "none") {
            $conditions[] = "event_category_ids = ''";
        } elseif (preg_match('/,/', $category)) {
            $category = explode(',', $category);
            $category_conditions = array();
            foreach ($category as $cat) {
                if (is_numeric($cat) && $cat > 0) {
                    $category_conditions[] = " FIND_IN_SET({$cat},event_category_ids)";
                } elseif ($cat == "none") {
                    $category_conditions[] = " event_category_ids = ''";
                }
            }
            $conditions[] = "(" . implode(' OR', $category_conditions) . ")";
        } elseif (preg_match('/\\+/', $category)) {
            $category = explode('+', $category);
            $category_conditions = array();
            foreach ($category as $cat) {
                if (is_numeric($cat) && $cat > 0) {
                    $category_conditions[] = " FIND_IN_SET({$cat},event_category_ids)";
                }
            }
            $conditions[] = "(" . implode(' AND ', $category_conditions) . ")";
        } elseif (preg_match('/ /', $category)) {
            // url decoding of '+' is ' '
            $category = explode(' ', $category);
            $category_conditions = array();
            foreach ($category as $cat) {
                if (is_numeric($cat) && $cat > 0) {
                    $category_conditions[] = " FIND_IN_SET({$cat},event_category_ids)";
                }
            }
            $conditions[] = "(" . implode(' AND ', $category_conditions) . ")";
        }
    }
    if (get_option('eme_categories_enabled')) {
        if (is_numeric($notcategory)) {
            if ($notcategory > 0) {
                $conditions[] = " NOT FIND_IN_SET({$notcategory},event_category_ids)";
            }
        } elseif ($notcategory == "none") {
            $conditions[] = "event_category_ids != ''";
        } elseif (preg_match('/,/', $notcategory)) {
            $notcategory = explode(',', $notcategory);
            $category_conditions = array();
            foreach ($notcategory as $cat) {
                if (is_numeric($cat) && $cat > 0) {
                    $category_conditions[] = " NOT FIND_IN_SET({$cat},event_category_ids)";
                } elseif ($cat == "none") {
                    $category_conditions[] = " event_category_ids != ''";
                }
            }
            $conditions[] = "(" . implode(' OR', $category_conditions) . ")";
        } elseif (preg_match('/\\+/', $notcategory)) {
            $notcategory = explode('+', $notcategory);
            $category_conditions = array();
            foreach ($notcategory as $cat) {
                if (is_numeric($cat) && $cat > 0) {
                    $category_conditions[] = " NOT FIND_IN_SET({$cat},event_category_ids)";
                }
            }
            $conditions[] = "(" . implode(' AND ', $category_conditions) . ")";
        } elseif (preg_match('/ /', $notcategory)) {
            // url decoding of '+' is ' '
            $notcategory = explode(' ', $notcategory);
            $category_conditions = array();
            foreach ($notcategory as $cat) {
                if (is_numeric($cat) && $cat > 0) {
                    $category_conditions[] = " NOT FIND_IN_SET({$cat},event_category_ids)";
                }
            }
        }
    }
    // now filter the author ID
    if ($author != '' && !preg_match('/,/', $author)) {
        $authinfo = get_user_by('login', $author);
        $conditions[] = " event_author = " . $authinfo->ID;
    } elseif (preg_match('/,/', $author)) {
        $authors = explode(',', $author);
        $author_conditions = array();
        foreach ($authors as $authname) {
            $authinfo = get_user_by('login', $authname);
            $author_conditions[] = " event_author = " . $authinfo->ID;
        }
        $conditions[] = "(" . implode(' OR ', $author_conditions) . ")";
    }
    // now filter the contact ID
    if ($contact_person != '' && !preg_match('/,/', $contact_person)) {
        $authinfo = get_user_by('login', $contact_person);
        $conditions[] = " event_contactperson_id = " . $authinfo->ID;
    } elseif (preg_match('/,/', $contact_person)) {
        $contact_persons = explode(',', $contact_person);
        $contact_person_conditions = array();
        foreach ($contact_persons as $authname) {
            $authinfo = get_user_by('login', $authname);
            $contact_person_conditions[] = " event_contactperson_id = " . $authinfo->ID;
        }
        $conditions[] = "(" . implode(' OR ', $contact_person_conditions) . ")";
    }
    // extra conditions for authors: if we're in the admin itf, return only the events for which you have the right to change anything
    $current_userid = get_current_user_id();
    if (is_admin() && !current_user_can(get_option('eme_cap_edit_events')) && !current_user_can(get_option('eme_cap_list_events')) && current_user_can(get_option('eme_cap_author_event'))) {
        $conditions[] = "(event_author = {$current_userid} OR event_contactperson_id= {$current_userid})";
    }
    $where = implode(" AND ", $conditions);
    if ($show_recurrent_events_once) {
        if ($where != "") {
            $where = " AND " . $where;
        }
        $sql = "SELECT * FROM {$events_table}\n         WHERE (recurrence_id>0 {$where})\n         group by recurrence_id union all\n         SELECT * FROM {$events_table}\n         WHERE (recurrence_id=0 {$where})\n         ORDER BY event_start_date {$order} , event_start_time {$order}\n         {$limit} \n         {$offset}";
    } else {
        if ($where != "") {
            $where = " WHERE " . $where;
        }
        $sql = "SELECT * FROM {$events_table}\n         {$where}\n         ORDER BY event_start_date {$order} , event_start_time {$order}\n         {$limit} \n         {$offset}";
    }
    $wpdb->show_errors = true;
    $events = $wpdb->get_results($sql, ARRAY_A);
    $inflated_events = array();
    if (!empty($events)) {
        //$wpdb->print_error();
        foreach ($events as $this_event) {
            $this_event = eme_get_event_data($this_event);
            array_push($inflated_events, $this_event);
        }
        if (has_filter('eme_event_list_filter')) {
            $inflated_events = apply_filters('eme_event_list_filter', $inflated_events);
        }
    }
    return $inflated_events;
}