From 82c4a8f2c4e5acd80b813829cecc40f621da3b21 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 14 Jun 2021 10:07:55 +0200 Subject: Begin move roles and status field to concerts table. There's no need to have a separate table (concertlogs) for these fields. --- includes/admin/register_db_tables.php | 18 ++++++++-- includes/concert.php | 68 +++++++++++++++++++++++++++++------ tests/ConcertTest.php | 24 +++++++++++++ 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/includes/admin/register_db_tables.php b/includes/admin/register_db_tables.php index 11223b3..64123d9 100644 --- a/includes/admin/register_db_tables.php +++ b/includes/admin/register_db_tables.php @@ -260,7 +260,7 @@ if ( !function_exists( "giglog_register_db_tables") ) function giglog_register_db_tables() { $db_version = get_option('giglogadmin_db_version'); - if ($db_version == 5) { + if ($db_version == 6) { return; } @@ -445,7 +445,21 @@ if ( !function_exists( "giglog_register_db_tables") ) "ALTER TABLE `wpg_concerts` DROP FOREIGN KEY `wpgconcert_band`;"); } - update_option("giglogadmin_db_version", 5); + if ($db_version == NULL || $db_version < 6) + { + /* + * Move status and roles from concertlogs to main concerts table + * Don't really see the value in a separate table for these items + * Also make the roles fiels a JSON field instead of separate + * fields for each role. + */ + $wpdb->query( + "ALTER TABLE `wpg_concerts` ADD COLUMN IF NOT EXISTS ( + wpgconcert_status INT DEFAULT 1, + wpgconcert_roles JSON CHECK (JSON_VALID(wpgconcert_roles)))"); + } + + update_option("giglogadmin_db_version", 6); } giglog_register_db_tables(); diff --git a/includes/concert.php b/includes/concert.php index d370de9..88a20da 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -16,6 +16,15 @@ if ( !class_exists('GiglogAdmin_Concert') ) { private $cdate; private $tickets; private $eventlink; + private int $status; + private array $roles; + + public const STATUS_NONE = 1; + public const STATUS_ACCRED_REQ = 2; + public const STATUS_PHOTO_APPROVED = 3; + public const STATUS_TEXT_APPROVED = 4; + public const STATUS_ALL_APPROVED = 5; + public const STATUS_REJECTED = 6; /* * Constructs a new concert object from an array of attributes. @@ -30,7 +39,8 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $this->cdate = isset($attrs->wpgconcert_date) ? $attrs->wpgconcert_date : NULL; $this->tickets = isset($attrs->wpgconcert_tickets) ? $attrs->wpgconcert_tickets : NULL; $this->eventlink = isset($attrs->wpgconcert_event) ? $attrs->wpgconcert_event : NULL; - + $this->status = isset($attrs->wpgconcert_status) ? $attrs->wpgconcert_status : 1; + $this->roles = isset($attrs->wpgconcert_roles) ? json_decode($attrs->wpgconcert_roles, true) : []; if ( isset( $attrs->venue ) ) { if (isset($attrs->wpgvenue_name) && isset($attrs->wpgvenue_city)) { @@ -65,6 +75,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { . 'WHERE ' . $wpdb->prepare('wpg_concerts.id = %d', $id); $results = $wpdb->get_results($query); + var_dump($results); return $results ? new GiglogAdmin_Concert($results[0]) : NULL; } @@ -188,20 +199,40 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); } - public function save(): void + public function save() : void { global $wpdb; - $wpdb->insert('wpg_concerts', array( - 'id' => '', - 'wpgconcert_name' => $this->cname, - 'venue' => $this->venue->id(), - 'wpgconcert_date' => $this->cdate, - 'wpgconcert_tickets' => $this->tickets, - 'wpgconcert_event' => $this->eventlink - )); + if ( $this->id !== NULL ) { + $res = $wpdb->update('wpg_concerts', array( + 'id' => $this->id, + 'wpgconcert_name' => $this->cname, + 'venue' => $this->venue->id(), + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink, + 'wpgconcert_status' => $this->status, + ), + array( 'id' => $this->id ) ); + + } + else { + $res = $wpdb->insert('wpg_concerts', array( + 'wpgconcert_name' => $this->cname, + 'venue' => $this->venue->id(), + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink, + 'wpgconcert_status' => $this->status, + )); + } - $this->id = $wpdb->insert_id; + if ( $res === false ) { + $wpdb->print_error( __METHOD__ ); + } + else { + $this->id = $wpdb->insert_id; + } } public function id() @@ -229,6 +260,21 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { return $this->eventlink; } + + public function status() + { + return $this->status; + } + + public function set_status( int $new_status ) + { + $this->status = $new_status; + } + + public function roles() + { + return $this->roles; + } } } ?> diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 2c747b8..7edda46 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -86,6 +86,30 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals($gig->id(), $fetched_gig->id()); $this->assertEquals($gig->cname(), $fetched_gig->cname()); $this->assertEquals($venue->id(), $fetched_gig->venue()->id()); + $this->assertEquals(GiglogAdmin_Concert::STATUS_NONE, $fetched_gig->status()); + $this->assertEquals([], $fetched_gig->roles()); + } + + public function testSetConcertStatus() : void + { + $venue = GiglogAdmin_Venue::create("a venue"); + $today = date("Y-m-d"); + + $gig = GiglogAdmin_Concert::create( + "a concert123", + $venue->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + + $fetched_gig = GiglogAdmin_Concert::get($gig->id()); + $fetched_gig->set_status( GiglogAdmin_Concert::STATUS_ACCRED_REQ ); + $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig->status() ); + + $fetched_gig->save(); + + $fetched_gig_2 = GiglogAdmin_Concert::get($gig->id()); + $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig_2->status() ); } public function testOnlyFetchConcertsFromGivenCity() : void -- cgit v1.2.3