summaryrefslogtreecommitdiffstats
path: root/includes/concert.php
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-06-12 18:24:37 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-06-12 18:31:39 +0200
commit72340c709e50a2383a8c651d72bb4396e9477c9a (patch)
tree8843929b57fa9c4bba74abe16384e61090ccad0a /includes/concert.php
parent5af89bca3bd17777fae94e681882d6eabf152303 (diff)
downloadgigologadmin-72340c709e50a2383a8c651d72bb4396e9477c9a.tar.gz
gigologadmin-72340c709e50a2383a8c651d72bb4396e9477c9a.tar.bz2
gigologadmin-72340c709e50a2383a8c651d72bb4396e9477c9a.zip
Streamline Concert api.
Reduce to one find_concerts function taking a filter to limit the selection.
Diffstat (limited to 'includes/concert.php')
-rw-r--r--includes/concert.php37
1 files changed, 22 insertions, 15 deletions
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<string, mixed> $filter
+ * @return array<GiglogAdmin_Concert>
+ */
+ 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);