summaryrefslogtreecommitdiffstats
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/class-giglogadmin-venue.php149
1 files changed, 106 insertions, 43 deletions
diff --git a/includes/class-giglogadmin-venue.php b/includes/class-giglogadmin-venue.php
index ed148c2..0b73d24 100644
--- a/includes/class-giglogadmin-venue.php
+++ b/includes/class-giglogadmin-venue.php
@@ -16,25 +16,50 @@ if ( ! class_exists( 'GiglogAdmin_Venue' ) ) {
*/
class GiglogAdmin_Venue {
+ /**
+ * The internal database ID of the entry.
+ *
+ * @var int|null
+ */
private ?int $id;
/**
+ * Venue name.
+ *
+ * @var string
* @psalm-suppress PropertyNotSetInConstructor
*/
private string $name;
/**
+ * Venue city.
+ *
+ * @var string|null
* @psalm-suppress PropertyNotSetInConstructor
*/
private string $city;
+
+ /**
+ * Venue address, optional.
+ *
+ * @var string|null
+ */
private ?string $address;
+
+ /**
+ * Venue website, optional.
+ *
+ * @var string|null
+ */
private ?string $webpage;
- /*
+ /**
* Constructs a new venue object from an array of attributes.
* The attributes are expected to be named as in the database,
* so this constructor can be used to construct the object
* directly from the database row.
+ *
+ * @param object $attrs The attributes describing this venue.
*/
public function __construct( object $attrs ) {
$this->id = isset( $attrs->id ) ? $attrs->id : null;
@@ -54,19 +79,38 @@ if ( ! class_exists( 'GiglogAdmin_Venue' ) ) {
/**
* Get venue by given id.
*
- * @param int $id
+ * @param int $id The DB id of the venue to get.
* @return null|self
*/
- static function get( int $id ) : ?self {
+ public static function get( int $id ) : ?self {
global $wpdb;
- $query = $wpdb->prepare( "SELECT * from {$wpdb->prefix}giglogadmin_venues WHERE id = %d", $id );
- $results = $wpdb->get_results( $query );
+ $results = $wpdb->get_results(
+ $wpdb->prepare( "SELECT * from {$wpdb->prefix}giglogadmin_venues WHERE id = %d", $id )
+ );
+
+ if ( ! $result ) {
+ error_log( "GiglogAdmin: Query for Venue with id={$id} returned null." );
+ return null;
+ } elseif ( count( $results ) > 1 ) {
+ error_log(
+ "GiglogAdmin: Query for venye with id={$id} returned "
+ . count( $result )
+ . 'entries.'
+ );
+ return null;
+ }
- return $results ? new GiglogAdmin_Venue( $results[0] ) : null;
+ return new GiglogAdmin_Venue( $results[0] );
}
- static function create( string $name, string $city = 'Oslo' ): self {
+ /**
+ * Create a new venue and store it in the database.
+ *
+ * @param string $name The name of the venue.
+ * @param string $city The city where the venue is located (defaults to Oslo).
+ */
+ public static function create( string $name, string $city = 'Oslo' ): self {
$venue = new GiglogAdmin_Venue(
(object) array(
'wpgvenue_name' => $name,
@@ -78,74 +122,84 @@ if ( ! class_exists( 'GiglogAdmin_Venue' ) ) {
return $venue;
}
- static function find_or_create( string $name, string $city = 'Oslo' ): self {
+ /**
+ * Find a venue by name and city, or create it if it does not exist.
+ *
+ * @param string $name The name of the venue.
+ * @param string $city The city of the venue (defaults to Oslo).
+ */
+ public static function find_or_create( string $name, string $city = 'Oslo' ): self {
global $wpdb;
- $venuesql = "SELECT * FROM {$wpdb->prefix}giglogadmin_venues "
- . $wpdb->prepare( 'WHERE upper(wpgvenue_name)=upper(%s) and upper(wpgvenue_city)=upper(%s)', $name, $city );
- $results = $wpdb->get_results( $venuesql );
+ $results = $wpdb->get_results(
+ $wpdb->prepare(
+ "SELECT * FROM {$wpdb->prefix}giglogadmin_venues "
+ . 'WHERE upper(wpgvenue_name)=upper(%s) and upper(wpgvenue_city)=upper(%s)',
+ $name,
+ $city
+ )
+ );
- if ( $results ) {
- return new GiglogAdmin_Venue( $results[0] );
- } else {
+ if ( ! $results ) {
return self::create( $name, $city );
+ } elseif ( count( $results ) > 1 ) {
+ error_log(
+ "GiglogAdmin: Query for venye with id={$id} returned "
+ . count( $result )
+ . 'entries.'
+ );
+ } else {
+ return new GiglogAdmin_Venue( $results[0] );
}
}
- static function all_cities(): array {
+ /**
+ * Return a list of all cities with venues.
+ */
+ public static function all_cities(): array {
global $wpdb;
+
$results = $wpdb->get_results(
"select distinct wpgvenue_city from {$wpdb->prefix}giglogadmin_venues"
);
- return array_map(
- function ( $r ) {
- return $r->wpgvenue_city;
- },
- $results
- );
+ return array_map( fn( $r ) => $r->wpgvenue_city, $results );
}
/**
- * @return self[]
- *
- * @psalm-return array<array-key, self>
+ * Return a list of all venues.
*/
- static function all_venues(): array {
+ public static function all_venues(): array {
global $wpdb;
$results = $wpdb->get_results(
"select * from {$wpdb->prefix}giglogadmin_venues ORDER BY wpgvenue_name"
);
- return array_map(
- function ( $r ) {
- return new GiglogAdmin_Venue( $r );
- },
- $results
- );
+ return array_map( fn( $r ) => new GiglogAdmin_Venue( $r ), $results );
}
/**
- * @return self[]
+ * Return all venues within the given city.
*
- * @psalm-return array<array-key, self>
+ * @param string $city The city whose venues to return.
*/
- static function venues_in_city( string $city ): array {
+ public static function venues_in_city( string $city ): array {
global $wpdb;
- $q = $wpdb->prepare( "select * from {$wpdb->prefix}giglogadmin_venues where wpgvenue_city=%s", $city )
- . ' ORDER BY wpgvenue_name';
- $results = $wpdb->get_results( $q );
-
- return array_map(
- function ( $r ) {
- return new GiglogAdmin_Venue( $r );
- },
- $results
+ $results = $wpdb->get_results(
+ $wpdb->prepare(
+ "SELECT * FROM {$wpdb->prefix}giglogadmin_venues WHERE wpgvenue_city=%s ORDER BY wpgvenue_name",
+ $city
+ )
);
+
+ return array_map( fn( $r ) => new GiglogAdmin_Venue( $r ), $results );
}
+ /**
+ * Save the venue to the database.
+ */
public function save(): void {
global $wpdb;
@@ -161,14 +215,23 @@ if ( ! class_exists( 'GiglogAdmin_Venue' ) ) {
$this->id = $wpdb->insert_id;
}
+ /**
+ * Return database ID of venue.
+ */
public function id() : int {
return $this->id;
}
+ /**
+ * Return name of venue.
+ */
public function name() : string {
return $this->name;
}
+ /**
+ * Return city of venue
+ */
public function city() : string {
return $this->city;
}