diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2021-09-06 20:00:13 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2021-09-06 20:00:13 +0200 |
commit | 0bfbca60e451f8c9e627dd698d4a58ddae73c874 (patch) | |
tree | ed34c4b623b2f86607ff3753ecdfa3cb4a47a607 | |
parent | f01f957a0d622e37f1a32e2d88dc152ec5524c35 (diff) | |
download | gigologadmin-0bfbca60e451f8c9e627dd698d4a58ddae73c874.tar.gz gigologadmin-0bfbca60e451f8c9e627dd698d4a58ddae73c874.tar.bz2 gigologadmin-0bfbca60e451f8c9e627dd698d4a58ddae73c874.zip |
Refactor Concert::find_concerts.
Make it a bit more compact and fix use of $wpdb->prepare for
`currentuser` filter.
-rw-r--r-- | includes/concert.php | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/includes/concert.php b/includes/concert.php index 089e9a7..936154f 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -37,6 +37,17 @@ if ( !class_exists('GiglogAdmin_Concert') ) { public const STATUS_ALL_APPROVED = 4; public const STATUS_REJECTED = 5; + // Table to translate from filter keys to db columns used by + // find_Concerts + private const KEY_TRANS_TABLE = [ + 'name' => 'wpgconcert_name', + 'date' => 'wpgconcert_date', + 'venue_id' => 'wpg_venues.id', + 'venue' => 'wpg_venues.wpgvenue_name', + 'city' => 'wpg_venues.wpgvenue_city', + 'currentuser' => 'wpgconcert_roles', + ]; + private const BASE_QUERY = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name wpg_venues_wpgvenue_city ' . 'FROM wpg_concerts ' @@ -195,29 +206,23 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $query = self::BASE_QUERY; $where = []; - - if ( isset( $filter['name'] ) ) { - array_push($where, "wpgconcert_name = {$wpdb->prepare('%s', $filter['name'])}"); - } - - if ( isset( $filter['date'] ) ) { - array_push($where, "wpgconcert_date = {$wpdb->prepare('%s', $filter['date'])}"); - } - - if ( isset( $filter["city"] ) ) { - array_push($where, 'wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $filter["city"])); - } - - if ( isset( $filter["venue_id"] ) ) { - array_push($where, 'wpg_venues.id = ' . $wpdb->prepare('%s', $filter["venue_id"])); - } - - if ( isset( $filter['venue'] ) ) { - array_push($where, "wpg_venues.wpgvenue_name = {$wpdb->prepare('%s', $filter['venue'])}"); - } - - if ( isset( $filter["currentuser"] ) ) { - array_push($where , 'wpgconcert_roles like "%'.$filter["currentuser"].'%"'); + foreach( $filter as $key => $value ) { + switch ($key) { + case 'name': + case 'date': + case 'venue': + case 'city': + array_push($where, $wpdb->prepare(self::KEY_TRANS_TABLE[$key] . '=%s', $value)); + break; + + case 'venue_id': + array_push($where, $wpdb->prepare(self::KEY_TRANS_TABLE[$key] . '=%d', $value)); + break; + + case 'currentuser': + array_push($where , $wpdb->prepare(self::KEY_TRANS_TABLE[$key] . ' like %%%s%%', $value)); + break; + } } if ( ! empty( $where ) ) { |