/** * Get bookings that match the array of arguments passed. * @return array * @static */ function get($args = array(), $count = false) { global $wpdb, $current_user; $bookings_table = EM_BOOKINGS_TABLE; $events_table = EM_EVENTS_TABLE; $locations_table = EM_LOCATIONS_TABLE; //Quick version, we can accept an array of IDs, which is easy to retrieve if (self::array_is_numeric($args)) { //Array of numbers, assume they are event IDs to retreive //We can just get all the events here and return them $sql = "\r\n\t\t\t\tSELECT * FROM {$bookings_table} b \r\n\t\t\t\tLEFT JOIN {$events_table} e ON e.event_id=b.event_id \r\n\t\t\t\tWHERE booking_id" . implode(" OR booking_id=", $args); $results = $wpdb->get_results(apply_filters('em_bookings_get_sql', $sql), ARRAY_A); $bookings = array(); foreach ($results as $result) { $bookings[] = new EM_Booking($result); } return $bookings; //We return all the bookings matched as an EM_Booking array. } //We assume it's either an empty array or array of search arguments to merge with defaults $args = self::get_default_search($args); $limit = $args['limit'] && is_numeric($args['limit']) ? "LIMIT {$args['limit']}" : ''; $offset = $limit != "" && is_numeric($args['offset']) ? "OFFSET {$args['offset']}" : ''; //Get the default conditions $conditions = self::build_sql_conditions($args); //Put it all together $where = count($conditions) > 0 ? " WHERE " . implode(" AND ", $conditions) : ''; //Get ordering instructions $EM_Booking = new EM_Booking(); $accepted_fields = $EM_Booking->get_fields(true); $orderby = self::build_sql_orderby($args, $accepted_fields); //Now, build orderby sql $orderby_sql = count($orderby) > 0 ? 'ORDER BY ' . implode(', ', $orderby) : ''; //Selector $selectors = $count ? 'COUNT(*)' : '*'; //Create the SQL statement and execute $sql = "\r\n\t\t\tSELECT {$selectors} FROM {$bookings_table} \r\n\t\t\tLEFT JOIN {$events_table} ON {$events_table}.event_id={$bookings_table}.event_id \r\n\t\t\tLEFT JOIN {$locations_table} ON {$locations_table}.location_id={$events_table}.location_id\r\n\t\t\t{$where}\r\n\t\t\t{$orderby_sql}\r\n\t\t\t{$limit} {$offset}\r\n\t\t"; //If we're only counting results, return the number of results if ($count) { return apply_filters('em_bookings_get_count', $wpdb->get_var($sql), $args); } $results = $wpdb->get_results(apply_filters('em_events_get_sql', $sql, $args), ARRAY_A); //If we want results directly in an array, why not have a shortcut here? if ($args['array'] == true) { return $results; } //Make returned results EM_Booking objects $results = is_array($results) ? $results : array(); $bookings = array(); foreach ($results as $booking) { $bookings[] = new EM_Booking($booking); } $EM_Bookings = new EM_Bookings($bookings); return apply_filters('em_bookings_get', $EM_Bookings); }