summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2022-02-12 23:01:54 +0100
committerHarald Eilertsen <haraldei@anduin.net>2022-02-12 23:04:17 +0100
commit21b1bc75bee176f77576abde970b2bfcee2b4c4e (patch)
tree1e112859dbf4367ee69d7bbeeb9ad2d19127f55d
parenta826c6ab358e64524c4a1c1ce180404fb937993a (diff)
downloadgigologadmin-21b1bc75bee176f77576abde970b2bfcee2b4c4e.tar.gz
gigologadmin-21b1bc75bee176f77576abde970b2bfcee2b4c4e.tar.bz2
gigologadmin-21b1bc75bee176f77576abde970b2bfcee2b4c4e.zip
Make a count method for concerts.
This allows querying the database for the count of concerts that will match a given filter. Useful to find the total number of pages in a pagination setting, for instance.
-rw-r--r--includes/concert.php81
1 files changed, 54 insertions, 27 deletions
diff --git a/includes/concert.php b/includes/concert.php
index 11949f7..91fbe57 100644
--- a/includes/concert.php
+++ b/includes/concert.php
@@ -175,27 +175,21 @@ if ( !class_exists('GiglogAdmin_Concert') ) {
][$key];
}
- /**
- * 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
+ private static function _build_query(array $filter = [], $count = false) : string
{
global $wpdb;
$ct = "{$wpdb->prefix}giglogadmin_concerts";
$vt = "{$wpdb->prefix}giglogadmin_venues";
- $query = "SELECT {$ct}.*, {$vt}.wpgvenue_name, {$vt}.wpgvenue_city "
- . "FROM {$ct} "
- . "LEFT JOIN {$vt} ON {$ct}.venue = {$vt}.id ";
+ if ( $count ) {
+ $query = "SELECT count({$ct}.id) ";
+ }
+ else {
+ $query = "SELECT {$ct}.*, {$vt}.wpgvenue_name, {$vt}.wpgvenue_city ";
+ }
+
+ $query .= "FROM {$ct} LEFT JOIN {$vt} ON {$ct}.venue = {$vt}.id ";
$keymap = [
'id' => $wpdb->prefix . 'giglogadmin_concerts.id',
@@ -215,8 +209,6 @@ if ( !class_exists('GiglogAdmin_Concert') ) {
case 'name':
case 'date':
case 'month':
- array_push($where, $wpdb->prepare($keymap[$key] . '=%s', $value));
- break;
case 'venue':
case 'city':
array_push($where, $wpdb->prepare($keymap[$key] . '=%s', $value));
@@ -224,20 +216,20 @@ if ( !class_exists('GiglogAdmin_Concert') ) {
case 'id':
case 'venue_id':
- array_push($where, $wpdb->prepare($keymap[$key] . '=%d', $value));
- break;
+ array_push($where, $wpdb->prepare($keymap[$key] . '=%d', $value));
+ break;
case 'currentuser':
- array_push($where , $wpdb->prepare($keymap[$key] . ' like "%%%s%%"', $value));
- break;
+ array_push($where, $wpdb->prepare($keymap[$key] . ' like "%%%s%%"', $value));
+ break;
case 'offset':
- array_push($lmt , $value);
- break;
+ array_push($lmt, $value);
+ break;
case 'recperpage':
- array_push($lmt , $value);
- break;
+ array_push($lmt, $value);
+ break;
}
}
@@ -245,17 +237,52 @@ if ( !class_exists('GiglogAdmin_Concert') ) {
$query .= 'WHERE ' . implode(' and ', $where);
}
- $query.= ' ORDER BY wpgconcert_date';
+ $query .= ' ORDER BY wpgconcert_date';
if ( ! empty( $lmt ) ) {
$query .= ' LIMIT ' . implode(', ', $lmt);
}
- $results = $wpdb->get_results($query);
+ return $query;
+ }
+
+ /**
+ * 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;
+
+ $query = Self::_build_query($filter, false);
+ $results = $wpdb->get_results($query);
return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results);
}
+ /**
+ * Return the number of objects matching the given filter.
+ *
+ * @param array<string, mixed> $filter
+ * @return int
+ */
+ public static function count(array $filter = []) : int
+ {
+ global $wpdb;
+
+ $query = Self::_build_query($filter, true);
+ $count = $wpdb->get_var($query);
+
+ return $count ? $count : 0;
+ }
+
public function save() : void
{
global $wpdb;