diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2021-05-09 15:42:58 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2021-05-09 15:42:58 +0200 |
commit | d116e23de0efcd528226acf6dbe4c7cbc5c5698b (patch) | |
tree | a2249944821123a35de6e593c713c06648900c9d | |
parent | 883fbfab5dd0f7fabfed008341acaa63f365b1dc (diff) | |
download | gigologadmin-d116e23de0efcd528226acf6dbe4c7cbc5c5698b.tar.gz gigologadmin-d116e23de0efcd528226acf6dbe4c7cbc5c5698b.tar.bz2 gigologadmin-d116e23de0efcd528226acf6dbe4c7cbc5c5698b.zip |
Include info from venue in concerts.
This makes the concert a full object containing all relevant info, while
we can still segment the data in the db.
Instead of this:
$concert = GiglogAdmin_Concert::get($concert_id);
$venue = GiglogAdmin_Venue::get($concert->venue());
echo "{$concert->name()} @ {$venue->name()} : {$concert->cdate()}"
You can now do:
$concert = GiglogAdmin_Concert::get($concert_id);
echo "{$concert->name()} @ {$concert->venue()->name()} : {$concert->cdate()}"
And yeah, renamed Concert::find_cid() to Concert::get() and changed it's
semantics somewhat. It now either returns the given concert if it
exists, or NULL if it does not. Simpler function; simpler to use.
-rw-r--r-- | includes/admin/views/giglog_admin_page.php | 14 | ||||
-rw-r--r-- | includes/concert.php | 43 | ||||
-rw-r--r-- | tests/ConcertTest.php | 21 |
3 files changed, 54 insertions, 24 deletions
diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index b78b4db..2971a22 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -36,13 +36,17 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { echo(GiglogAdmin_AdminPage::editforms()); //not sure why it doesn't show without the echo? } - static function get_allvenues( ?int $invenue ): string + static function get_allvenues( ?GiglogAdmin_Venue $invenue ): string { $select = '<select name="selectvenueadmin">'; $select .= '<option value="">Please Select..</option>'; foreach ( GiglogAdmin_Venue::all_venues() AS $venue ) { - if($invenue==$venue ->id() ) $select .= '<option value="' . $venue -> id(). '" selected="selected">'.$venue->name(); - else $select .= '<option value="' . $venue->id() . '">'. $venue->name(); + if($invenue && $invenue->id() == $venue->id() ) { + $select .= '<option value="' . $venue->id(). '" selected="selected">'.$venue->name(); + } + else { + $select .= '<option value="' . $venue->id() . '">'. $venue->name(); + } $select .='</option>'; } $select .= '</select>'; @@ -131,7 +135,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $editing = filter_input(INPUT_POST, "edit") == "EDIT"; if ($editing && !empty($cid)) //A bit overdoing with the checks if concert ID is empty both here and in find_cid. But based on that, things are NULL or not. Better ideas? - $c = GiglogAdmin_Concert::find_cid($cid); + $c = GiglogAdmin_Concert::get($cid); else $c = new GiglogAdmin_Concert(); @@ -140,7 +144,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { .'<div class="concertitems"><strong>CONCERT DETAILS</strong><br><br><fieldset>' .'<input type="hidden" name="pid" value="' .$c->id(). '" />' .'<label for="cname">Concert Name:</label><textarea id="cname" name="cname" value="'.$c->cname().'">'.$c->cname().'</textarea><br>' - .'<label for="venue">Venue:</label>'.GiglogAdmin_AdminPage::get_allvenues($c->venue()).'<br>' + .'<label for="venue">Venue:</label>' . GiglogAdmin_AdminPage::get_allvenues($c->venue()) . '<br>' .'<label for="cdate">Date:</label><input type="date" id="cdate" name="cdate" value="'.$c->cdate().'"><br>' .'<label for="ticket">Tickets:</label><input type="text" id="ticket" name="ticket" value="'.$c->tickets().'"><br>' .'<label for="eventurl">Event link:</label><input type="text" id="eventurl" name="eventurl" value="'.$c->eventlink().'"><br>' diff --git a/includes/concert.php b/includes/concert.php index 5c17933..8c99c42 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -25,33 +25,41 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { $this->id = isset($attrs->id) ? $attrs->id : NULL; $this->cname = isset($attrs->wpgconcert_name) ? $attrs->wpgconcert_name : NULL; - $this->venue = isset($attrs->venue) ? $attrs->venue : NULL; $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; + + + if ( isset( $attrs->venue ) ) { + $venue_attrs = (object) [ + "id" => $attrs->venue, + "wpgvenue_name" => $attrs->wpgvenue_name, + "wpgvenue_city" => $attrs->wpgvenue_city, + ]; + + $this->venue = new GiglogAdmin_Venue($venue_attrs); + } } /** - * @return null|self + * Get concert with given id. + * + * @param int $id. + * @return null|self. */ - static function find_cid($id) + static function get( int $id ) : ?self { global $wpdb; - if(!empty($id)) - { - $csql = 'SELECT * FROM wpg_concerts WHERE id="' . $id . '"'; - $results = $wpdb->get_results($csql); - if ($results) - { - return new GiglogAdmin_Concert($results[0]); - } - } - else - { - return new GiglogAdmin_Concert(); - } + $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' + . 'FROM wpg_concerts ' + . 'LEFT JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' + . 'WHERE ' . $wpdb->prepare('wpg_concerts.id = %d', $id); + + $results = $wpdb->get_results($query); + + return $results ? new GiglogAdmin_Concert($results[0]) : NULL; } public static function create(string $name, $venue, string $date, string $ticketlink, string $eventlink): ?self @@ -154,7 +162,6 @@ if ( !class_exists('GiglogAdmin_Concert') ) { . ' and venue = ' . $venue . ' and wpgconcert_date ="' . $date . '"'; - error_log(__CLASS__ . '::' . __FUNCTION__ . ": {$sql}"); return $wpdb->get_results($sql); } @@ -165,7 +172,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $wpdb->insert('wpg_concerts', array( 'id' => '', 'wpgconcert_name' => $this->cname, - 'venue' => $this->venue, + 'venue' => $this->venue->id(), 'wpgconcert_date' => $this->cdate, 'wpgconcert_tickets' => $this->tickets, 'wpgconcert_event' => $this->eventlink diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 6d14b68..0517ad8 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -19,7 +19,7 @@ final class ConcertTest extends WP_UnitTestCase "https://example.com/events/93"); $this->assertEquals("a concert", $concert->cname()); - $this->assertEquals($venue->id(), $concert->venue()); + $this->assertEquals($venue->id(), $concert->venue()->id()); $this->assertEquals($today, $concert->cdate()); $this->assertEquals("https://example.com/tickets/42", $concert->tickets()); $this->assertEquals("https://example.com/events/93", $concert->eventlink()); @@ -68,4 +68,23 @@ final class ConcertTest extends WP_UnitTestCase $this->assertNull($new); } + + public function testGetConcertByIdReturnsFullConcertObject() : 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()); + + $this->assertEquals($gig->id(), $fetched_gig->id()); + $this->assertEquals($gig->cname(), $fetched_gig->cname()); + $this->assertEquals($venue->id(), $fetched_gig->venue()->id()); + } } |