From 72340c709e50a2383a8c651d72bb4396e9477c9a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Jun 2021 18:24:37 +0200 Subject: Streamline Concert api. Reduce to one find_concerts function taking a filter to limit the selection. --- includes/concert.php | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'includes/concert.php') diff --git a/includes/concert.php b/includes/concert.php index 56021f5..fcb3934 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -173,7 +173,18 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public static function find_concerts_in(?string $city = null) : array + /** + * Return an array of concert objects optionally limited by a specified + * filter. + * + * Valid filters are: + * - 'venue_id' => int : only include concerts at the given venue + * - 'city' => string : only include concerts in the given city + * + * @param array $filter + * @return array + */ + public static function find_concerts(array $filter = []) : array { global $wpdb; @@ -181,23 +192,19 @@ if ( !class_exists('GiglogAdmin_Concert') ) { . 'FROM wpg_concerts ' . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '; - if ( $city ) { - $query .= 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city); - } - - $results = $wpdb->get_results($query); + $where = []; - return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); - } + if ( isset( $filter["city"] ) ) { + array_push($where, 'wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $filter["city"])); + } - public static function find_concerts_at(GiglogAdmin_Venue $venue) : array - { - global $wpdb; + if ( isset( $filter["venue_id"] ) ) { + array_push($where, 'wpg_venues.id = ' . $wpdb->prepare('%s', $filter["venue_id"])); + } - $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' - . 'FROM wpg_concerts ' - . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' - . 'WHERE wpg_concerts.venue = ' . $wpdb->prepare('%d', $venue->id()); + if ( ! empty( $where ) ) { + $query .= 'WHERE ' . implode(' and ', $where); + } $results = $wpdb->get_results($query); -- cgit v1.2.3