<?php
/**
* Class representing venues.
*
* @package giglogadmin
*
* SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
* SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
if ( ! class_exists( 'GiglogAdmin_Venue' ) ) {
/**
* Class representing venues.
*/
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;
if ( isset( $attrs->wpgvenue_name, $attrs->wpgvenue_city ) ) {
$this->name = $attrs->wpgvenue_name;
$this->city = $attrs->wpgvenue_city;
} else {
error_log( 'Trying to construct a Venue without a name or a city' );
wp_die();
}
$this->address = isset( $attrs->wpgvenue_address ) ? $attrs->wpgvenue_address : null;
$this->webpage = isset( $attrs->wpgvenue_webpage ) ? $attrs->wpgvenue_webpage : null;
}
/**
* Get venue by given id.
*
* @param int $id The DB id of the venue to get.
* @return null|self
*/
public static function get( int $id ) : ?self {
global $wpdb;
$res = $wpdb->get_results(
$wpdb->prepare( "SELECT * from {$wpdb->prefix}giglogadmin_venues WHERE id = %d", $id )
);
if ( ! $res ) {
error_log( "GiglogAdmin: Query for Venue with id={$id} returned null." );
return null;
} elseif ( count( $res ) > 1 ) {
error_log(
"GiglogAdmin: Query for venye with id={$id} returned "
. count( $result )
. 'entries.'
);
return null;
}
return new GiglogAdmin_Venue( $res[0] );
}
/**
* 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,
'wpgvenue_city' => $city,
)
);
$venue->save();
return $venue;
}
/**
* 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;
$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 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] );
}
}
/**
* 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( fn( $r ) => $r->wpgvenue_city, $results );
}
/**
* Return a list of all venues.
*/
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( fn( $r ) => new GiglogAdmin_Venue( $r ), $results );
}
/**
* Return all venues within the given city.
*
* @param string $city The city whose venues to return.
*/
public static function venues_in_city( string $city ): array {
global $wpdb;
$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;
$wpdb->insert(
$wpdb->prefix . 'giglogadmin_venues',
array(
'id' => '',
'wpgvenue_name' => $this->name,
'wpgvenue_city' => $this->city,
)
);
$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;
}
}
}