Example #1
0
function eme_get_events($o_limit = 0, $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, $eme_timezone;
    $events_table = $wpdb->prefix . EVENTS_TBNAME;
    $bookings_table = $wpdb->prefix . BOOKINGS_TBNAME;
    if (strpos($o_limit, "=")) {
        // allows the use of arguments
        $defaults = array('o_limit' => 0, 'scope' => 'future', 'order' => 'ASC', 'o_offset' => 0, 'location_id' => '', 'category' => '', 'author' => '', 'contact_person' => '', 'show_ongoing' => 1, 'notcategory' => '', 'show_recurrent_events_once' => 0, 'extra_conditions' => '');
        $r = wp_parse_args($o_limit, $defaults);
        extract($r);
    }
    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";
    }
    $eme_date_obj = new ExpressiveDate(null, $eme_timezone);
    $start_of_week = get_option('start_of_week');
    $eme_date_obj->setWeekStartDay($start_of_week);
    $today = $eme_date_obj->getDate();
    $this_time = $eme_date_obj->getTime();
    $this_datetime = $eme_date_obj->getDateTime();
    $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)) {
        $month = $matches[1];
        $eme_date_obj->setMonth($month);
        $number_of_days_month = $eme_date_obj->getDaysInMonth();
        $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');
        $eme_date_obj->setWeekStartDay($start_of_week);
        $limit_start = $eme_date_obj->startOfWeek()->format('Y-m-d');
        $limit_end = $eme_date_obj->endOfWeek()->format('Y-m-d');
        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');
        $eme_date_obj->setWeekStartDay($start_of_week);
        $eme_date_obj->addOneWeek();
        $limit_start = $eme_date_obj->startOfWeek()->format('Y-m-d');
        $limit_end = $eme_date_obj->endOfWeek()->format('Y-m-d');
        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_obj->startOfMonth()->format('Y-m-d');
        $limit_end = $eme_date_obj->endOfMonth()->format('Y-m-d');
        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 = $eme_date_obj->getYear();
        $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") {
        $eme_date_obj->addOneMonth();
        $limit_start = $eme_date_obj->startOfMonth()->format('Y-m-d');
        $limit_end = $eme_date_obj->endOfMonth()->format('Y-m-d');
        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)) {
        $days = $matches[1];
        $limit_start = $today;
        $limit_end = $eme_date_obj->addDays($days)->getDate();
        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)) {
        $days = $matches[1];
        $limit_start = $eme_date_obj->minusDays($days)->getDate();
        $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("/^(\\-?\\+?\\d+)d--(\\-?\\+?\\d+)d\$/", $scope, $matches)) {
        $day1 = $matches[1];
        $day2 = $matches[2];
        $limit_start = $eme_date_obj->copy()->modifyDays($day1)->getDate();
        $limit_end = $eme_date_obj->copy()->modifyDays($day2)->getDate();
        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];
        $eme_date_obj->setTimestampFromString($limit_end . " " . $eme_timezone);
        $limit_start = $eme_date_obj->minusDays($days)->getDate() . " " . $eme_timezone;
        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];
        $eme_date_obj->setTimestampFromString($limit_start . " " . $eme_timezone);
        $limit_end = $eme_date_obj->addDays($days)->getDate() . " " . $eme_timezone;
        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)) {
        $months_in_future = $matches[1]++;
        $limit_start = $eme_date_obj->startOfMonth()->format('Y-m-d');
        $eme_date_obj->addMonths($months_in_future);
        $limit_end = $eme_date_obj->endOfMonth()->format('Y-m-d');
        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)) {
        $months_in_past = $matches[1]++;
        $limit_start = $eme_date_obj->copy()->minusMonths($months_in_past)->startOfMonth()->format('Y-m-d');
        $limit_end = $eme_date_obj->copy()->minusOneMonth()->endOfMonth()->format('Y-m-d');
        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)) {
        $months1 = $matches[1];
        $months2 = $matches[2];
        $limit_start = $eme_date_obj->copy()->modifyMonths($months1)->startOfMonth()->format('Y-m-d');
        $limit_end = $eme_date_obj->copy()->modifyMonths($months2)->endOfMonth()->format('Y-m-d');
        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") {
        $limit_start = $today;
        $limit_end = $eme_date_obj->endOfWeek()->format('Y-m-d');
        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") {
        $limit_start = $today;
        $limit_end = $eme_date_obj->endOfWeek()->addOneDay()->format('Y-m-d');
        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 = $eme_date_obj->endOfMonth()->format('Y-m-d');
        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") {
        $limit_start = $eme_date_obj->startOfWeek()->format('Y-m-d');
        $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_obj->startOfMonth()->format('Y-m-d');
        $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_obj->addOneDay()->getDate();
        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)<='{$this_datetime}' AND CONCAT(event_end_date,' ',event_end_time)>= '{$this_datetime}')";
    } 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;
}
Example #2
0
function eme_get_calendar($args = "")
{
    global $wp_locale;
    global $wpdb, $eme_timezone;
    // the calendar is being used, so we need the jquery for the calendar
    global $eme_need_calendar_js;
    $eme_need_calendar_js = 1;
    $defaults = array('category' => 0, 'notcategory' => 0, 'full' => 0, 'month' => '', 'year' => '', 'echo' => 1, 'long_events' => 0, 'author' => '', 'contact_person' => '', 'location_id' => '', 'template_id' => 0);
    $r = wp_parse_args($args, $defaults);
    extract($r);
    $echo = $echo === "true" || $echo === "1" ? true : $echo;
    $full = $full === "true" || $full === "1" ? true : $full;
    $long_events = $long_events === "true" || $long_events === "1" ? true : $long_events;
    $echo = $echo === "false" || $echo === "0" ? false : $echo;
    $full = $full === "false" || $full === "0" ? false : $full;
    $long_events = $long_events === "false" || $long_events === "0" ? false : $long_events;
    // this comes from global wordpress preferences
    $start_of_week = get_option('start_of_week');
    $eme_date_obj = new ExpressiveDate(null, $eme_timezone);
    if (get_option('eme_use_client_clock') && isset($_SESSION['eme_client_mday']) && isset($_SESSION['eme_client_month']) && isset($_SESSION['eme_client_fullyear'])) {
        // these come from client unless their clock is wrong
        $iNowDay = sprintf("%02d", $_SESSION['eme_client_mday']);
        $iNowMonth = sprintf("%02d", $_SESSION['eme_client_month']);
        $iNowYear = sprintf("%04d", $_SESSION['eme_client_fullyear']);
    } else {
        // Get current year, month and day
        list($iNowYear, $iNowMonth, $iNowDay) = explode('-', $eme_date_obj->getDate());
    }
    $iSelectedYear = $year;
    $iSelectedMonth = $month;
    if ($iSelectedMonth == '') {
        $iSelectedMonth = $iNowMonth;
    }
    if ($iSelectedYear == '') {
        $iSelectedYear = $iNowYear;
    }
    $iSelectedMonth = sprintf("%02d", $iSelectedMonth);
    // Get name and number of days of specified month
    $eme_date_obj->setDay(1);
    $eme_date_obj->setMonth($iSelectedMonth);
    $eme_date_obj->setYear($iSelectedYear);
    // Get friendly month name, but since DateTime::format doesn't respect the locale, we need eme_localised_date
    if ($full) {
        list($sMonthName, $iDaysInMonth) = explode('-', eme_localised_date($eme_date_obj->getDate(), 'F-t'));
    } else {
        list($sMonthName, $iDaysInMonth) = explode('-', eme_localised_date($eme_date_obj->getDate(), 'M-t'));
    }
    // take into account some locale info: some always best show full month name, some show month after year, some have a year suffix
    $locale_code = substr(get_locale(), 0, 2);
    $showMonthAfterYear = 0;
    $yearSuffix = "";
    switch ($locale_code) {
        case "hu":
            $showMonthAfterYear = 1;
            break;
        case "ja":
            $showMonthAfterYear = 1;
            $sMonthName = eme_localised_date($eme_date_obj->getDate(), 'F');
            $yearSuffix = "年";
            break;
        case "ko":
            $showMonthAfterYear = 1;
            $sMonthName = eme_localised_date($eme_date_obj->getDate(), 'F');
            $yearSuffix = "년";
            break;
        case "zh":
            $showMonthAfterYear = 1;
            $sMonthName = eme_localised_date($eme_date_obj->getDate(), 'F');
            $yearSuffix = "年";
            break;
    }
    if ($showMonthAfterYear) {
        $cal_datestring = "{$iSelectedYear}{$yearSuffix} {$sMonthName}";
    } else {
        $cal_datestring = "{$sMonthName} {$iSelectedYear}{$yearSuffix}";
    }
    // Get previous year and month
    $iPrevYear = $iSelectedYear;
    $iPrevMonth = $iSelectedMonth - 1;
    if ($iPrevMonth <= 0) {
        $iPrevYear--;
        $iPrevMonth = 12;
        // set to December
    }
    $iPrevMonth = sprintf("%02d", $iPrevMonth);
    // Get next year and month
    $iNextYear = $iSelectedYear;
    $iNextMonth = $iSelectedMonth + 1;
    if ($iNextMonth > 12) {
        $iNextYear++;
        $iNextMonth = 1;
    }
    $iNextMonth = sprintf("%02d", $iNextMonth);
    // Get number of days of previous month
    $eme_date_obj2 = new ExpressiveDate(null, $eme_timezone);
    $eme_date_obj2->setDay(1);
    $eme_date_obj2->setMonth($iPrevMonth);
    $eme_date_obj2->setYear($iPrevYear);
    $iPrevDaysInMonth = (int) $eme_date_obj2->getDaysInMonth();
    // Get numeric representation of the day of the week of the first day of specified (current) month
    // remember: first day of week is a Sunday
    // if you want the day of the week to begin on Monday: start_of_week=1, Tuesday: start_of_week=2, etc ...
    // So, if e.g. the month starts on a Sunday and start_of_week=1 (Monday), then $iFirstDayDow is 6
    $iFirstDayDow = (int) $eme_date_obj->getDayOfWeekAsNumeric() - $start_of_week;
    if ($iFirstDayDow < 0) {
        $iFirstDayDow += 7;
    }
    // On what day the previous month begins
    $iPrevShowFrom = $iPrevDaysInMonth - $iFirstDayDow + 1;
    // we'll look for events in the requested month and 7 days before and after
    $calbegin = "{$iPrevYear}-{$iPrevMonth}-{$iPrevShowFrom}";
    $calend = "{$iNextYear}-{$iNextMonth}-07";
    $events = eme_get_events(0, "{$calbegin}--{$calend}", "ASC", 0, $location_id, $category, $author, $contact_person, 1, $notcategory);
    $eventful_days = array();
    if ($events) {
        //Go through the events and slot them into the right d-m index
        foreach ($events as $event) {
            if ($event['event_status'] == STATUS_PRIVATE && !is_user_logged_in()) {
                continue;
            }
            $eme_date_obj_end = new ExpressiveDate($event['event_end_date'] . " " . $event['event_end_time'], $eme_timezone);
            $eme_date_obj_now = new ExpressiveDate(null, $eme_timezone);
            if (get_option('eme_cal_hide_past_events') && $eme_date_obj_end->lessThan($eme_date_obj_now)) {
                continue;
            }
            if ($long_events) {
                //If $long_events is set then show a date as eventful if there is an multi-day event which runs during that day
                $eme_date_obj_tmp = new ExpressiveDate($event['event_start_date'] . " " . $event['event_start_time'], $eme_timezone);
                if ($eme_date_obj_end->lessThan($eme_date_obj_tmp)) {
                    $eme_date_obj_end = $eme_date_obj_tmp->copy();
                }
                $day_count = 0;
                while ($eme_date_obj_tmp->lessOrEqualTo($eme_date_obj_end)) {
                    $event_eventful_date = $eme_date_obj_tmp->getDate();
                    //Only show events on the day that they start
                    if (isset($eventful_days[$event_eventful_date]) && is_array($eventful_days[$event_eventful_date])) {
                        $eventful_days[$event_eventful_date][] = $event;
                    } else {
                        $eventful_days[$event_eventful_date] = array($event);
                    }
                    $eme_date_obj_tmp->addOneDay();
                }
            } else {
                //Only show events on the day that they start
                if (isset($eventful_days[$event['event_start_date']]) && is_array($eventful_days[$event['event_start_date']])) {
                    $eventful_days[$event['event_start_date']][] = $event;
                } else {
                    $eventful_days[$event['event_start_date']] = array($event);
                }
            }
        }
    }
    // we found all the events for the wanted days, now get them in the correct format with a good link
    if ($template_id) {
        $event_format = eme_get_template_format($template_id);
    } else {
        $event_format = get_option('eme_full_calendar_event_format');
    }
    $event_title_format = get_option('eme_small_calendar_event_title_format');
    $event_title_separator_format = get_option('eme_small_calendar_event_title_separator');
    $cells = array();
    foreach ($eventful_days as $day_key => $events) {
        // Set the date into the key
        $events_titles = array();
        foreach ($events as $event) {
            $events_titles[] = eme_replace_placeholders($event_title_format, $event, "text");
        }
        $link_title = implode($event_title_separator_format, $events_titles);
        $cal_day_link = eme_calendar_day_url($day_key);
        // Let's add the possible options
        // template_id is not being used per event
        if (!empty($location_id)) {
            $cal_day_link = add_query_arg(array('location_id' => $location_id), $cal_day_link);
        }
        if (!empty($category)) {
            $cal_day_link = add_query_arg(array('category' => $category), $cal_day_link);
        }
        if (!empty($notcategory)) {
            $cal_day_link = add_query_arg(array('notcategory' => $scope), $cal_day_link);
        }
        if (!empty($author)) {
            $cal_day_link = add_query_arg(array('author' => $author), $cal_day_link);
        }
        if (!empty($contact_person)) {
            $cal_day_link = add_query_arg(array('contact_person' => $contact_person), $cal_day_link);
        }
        $event_date = explode('-', $day_key);
        $event_day = ltrim($event_date[2], '0');
        $cells[$day_key] = "<a title='{$link_title}' href='{$cal_day_link}'>{$event_day}</a>";
        if ($full) {
            $cells[$day_key] .= "<ul class='eme-calendar-day-event'>";
            foreach ($events as $event) {
                $cells[$day_key] .= eme_replace_placeholders($event_format, $event);
            }
            $cells[$day_key] .= "</ul>";
        }
    }
    // If previous month
    $isPreviousMonth = $iFirstDayDow > 0;
    // Initial day on the calendar
    $iCalendarDay = $isPreviousMonth ? $iPrevShowFrom : 1;
    $isNextMonth = false;
    $sCalTblRows = '';
    // Generate rows for the calendar
    for ($i = 0; $i < 6; $i++) {
        // 6-weeks range
        if ($isNextMonth) {
            continue;
        }
        $sCalTblRows .= "<tr>";
        for ($j = 0; $j < 7; $j++) {
            // 7 days a week
            // we need the calendar day with 2 digits for the planned events
            $iCalendarDay_padded = sprintf("%02d", $iCalendarDay);
            if ($isPreviousMonth) {
                $calstring = "{$iPrevYear}-{$iPrevMonth}-{$iCalendarDay_padded}";
            } elseif ($isNextMonth) {
                $calstring = "{$iNextYear}-{$iNextMonth}-{$iCalendarDay_padded}";
            } else {
                $calstring = "{$iSelectedYear}-{$iSelectedMonth}-{$iCalendarDay_padded}";
            }
            // each day in the calendar has the name of the day as a class by default
            $eme_date_obj = new ExpressiveDate($calstring, $eme_timezone);
            $sClass = $eme_date_obj->format('D');
            if (isset($cells[$calstring])) {
                if ($isPreviousMonth) {
                    $sClass .= " eventful-pre event-day-{$iCalendarDay}";
                } elseif ($isNextMonth) {
                    $sClass .= " eventful-post event-day-{$iCalendarDay}";
                } elseif ($calstring == "{$iNowYear}-{$iNowMonth}-{$iNowDay}") {
                    $sClass .= " eventful-today event-day-{$iCalendarDay}";
                } else {
                    $sClass .= " eventful event-day-{$iCalendarDay}";
                }
                $sCalTblRows .= '<td class="' . $sClass . '">' . $cells[$calstring] . "</td>\n";
            } else {
                if ($isPreviousMonth) {
                    $sClass .= " eventless-pre";
                } elseif ($isNextMonth) {
                    $sClass .= " eventless-post";
                } elseif ($calstring == "{$iNowYear}-{$iNowMonth}-{$iNowDay}") {
                    $sClass .= " eventless-today";
                } else {
                    $sClass .= " eventless";
                }
                $sCalTblRows .= '<td class="' . $sClass . '">' . $iCalendarDay . "</td>\n";
            }
            // Next day
            $iCalendarDay++;
            if ($isPreviousMonth && $iCalendarDay > $iPrevDaysInMonth) {
                $isPreviousMonth = false;
                $iCalendarDay = 1;
            }
            if (!$isPreviousMonth && !$isNextMonth && $iCalendarDay > $iDaysInMonth) {
                $isNextMonth = true;
                $iCalendarDay = 1;
            }
        }
        $sCalTblRows .= "</tr>\n";
    }
    $weekdays = array(__('Sunday'), __('Monday'), __('Tuesday'), __('Wednesday'), __('Thursday'), __('Friday'), __('Saturday'));
    $sCalDayNames = "";
    // respect the beginning of the week offset
    for ($i = $start_of_week; $i < $start_of_week + 7; $i++) {
        $j = $i;
        if ($j == 7) {
            $j -= 7;
        }
        if ($full) {
            $sCalDayNames .= "<td>" . $wp_locale->get_weekday_abbrev($weekdays[$j]) . "</td>";
        } else {
            $sCalDayNames .= "<td>" . $wp_locale->get_weekday_initial($weekdays[$j]) . "</td>";
        }
    }
    // the real links are created via jquery when clicking on the prev-month or next-month class-links
    $previous_link = "<a class='prev-month' href=\"#\">&lt;&lt;</a>";
    $next_link = "<a class='next-month' href=\"#\">&gt;&gt;</a>";
    $random = rand(100, 200);
    $full ? $class = 'eme-calendar-full' : ($class = 'eme-calendar');
    $calendar = "<div class='{$class}' id='eme-calendar-{$random}'>";
    if ($full) {
        $fullclass = 'fullcalendar';
        $head = "<td class='month_name' colspan='7'>{$previous_link} {$next_link} {$cal_datestring}</td>\n";
    } else {
        $fullclass = '';
        $head = "<td>{$previous_link}</td><td class='month_name' colspan='5'>{$cal_datestring}</td><td>{$next_link}</td>\n";
    }
    // Build the heading portion of the calendar table
    $calendar .= "<table class='eme-calendar-table {$fullclass}'>\n" . "<thead>\n<tr>\n" . $head . "</tr>\n</thead>\n" . "<tr class='days-names'>\n" . $sCalDayNames . "</tr>\n";
    $calendar .= $sCalTblRows;
    $calendar .= "</table>\n</div>";
    // we generate the onclick javascript per calendar div
    // this is important if more than one calendar exists on the page
    $calendar .= "<script type='text/javascript'>\n         jQuery('#eme-calendar-" . $random . " a.prev-month').click(function(e){\n            e.preventDefault();\n            tableDiv = jQuery('#eme-calendar-" . $random . "');\n            loadCalendar(tableDiv, '{$full}', '{$long_events}','{$iPrevMonth}','{$iPrevYear}','{$category}','{$author}','{$contact_person}','{$location_id}','{$notcategory}','{$template_id}');\n         } );\n         jQuery('#eme-calendar-" . $random . " a.next-month').click(function(e){\n            e.preventDefault();\n            tableDiv = jQuery('#eme-calendar-" . $random . "');\n            loadCalendar(tableDiv, '{$full}', '{$long_events}','{$iNextMonth}','{$iNextYear}','{$category}','{$author}','{$contact_person}','{$location_id}','{$notcategory}','{$template_id}');\n         } );\n         </script>";
    $output = $calendar;
    if ($echo) {
        echo $output;
    } else {
        return $output;
    }
}