From ae72f1b2f0c73ded5277300f0d15914e6e10ecae Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 10 May 2021 10:45:04 +0200 Subject: Make it possible to list all concerts. --- includes/concert.php | 9 ++++++--- tests/ConcertTest.php | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index 050c924..455bc82 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -171,14 +171,17 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public static function find_concerts_in(string $city) : array + public static function find_concerts_in(?string $city = null) : array { global $wpdb; $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' . 'FROM wpg_concerts ' - . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' - . 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city); + . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '; + + if ( $city ) { + $query .= 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city); + } $results = $wpdb->get_results($query); diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 96fc72b..ec7bc13 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -168,4 +168,27 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Revolver", $gig->venue()->name()); } } + + public function testFetchAllConcerts() : void + { + $venue1 = GiglogAdmin_Venue::create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::create("Meieriet", "Sogndal"); + + for ($i = 0; $i < 4; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); + } + + for ($i = 4; $i < 6; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue2->id(), '', '', ''); + } + + for ($i = 6; $i < 11; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); + } + + $gigs = GiglogAdmin_Concert::find_concerts_in(); + + $this->assertEquals(11, count($gigs)); + } } -- cgit v1.2.3 From c65980879a990e6d02a8ab262b014e670f314477 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 May 2021 18:04:18 +0200 Subject: Add function to generate selection boxes. --- giglogadmin.php | 1 + includes/view-helpers/select_field.php | 26 +++++++++++++++++ tests/SelectFieldTest.php | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 includes/view-helpers/select_field.php create mode 100644 tests/SelectFieldTest.php diff --git a/giglogadmin.php b/giglogadmin.php index c8ae9b0..c35c470 100644 --- a/giglogadmin.php +++ b/giglogadmin.php @@ -33,6 +33,7 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { require_once __DIR__ . '/includes/admin/helpfiles/instrunctions.php'; require_once __DIR__ . '/includes/admin/helpfiles/instr_reviewers.php'; require_once __DIR__ . '/includes/admin/helpfiles/instr_photog.php'; + require_once __DIR__ . '/includes/view-helpers/select_field.php'; class GiglogAdmin_Plugin { diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php new file mode 100644 index 0000000..7cff3ef --- /dev/null +++ b/includes/view-helpers/select_field.php @@ -0,0 +1,26 @@ + +// SPDX-FileCopyrightText: 2021 Harald Eilertsen +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +namespace EternalTerror\ViewHelpers; + +/** + * Return HTML code for a selction box with the given options and preselected value. + * + * @param string $name The name attribute for the selection box + * @param array $opts The options as arrays of [value, label] pairs + * @param mixed|int $selected The value of the preselected option, or null if no + * option is preselected. + * @return string + */ +function select_field(string $name, ?array $opts = [], $selected = null) : string +{ + $body = ''; + foreach ($opts as $opt) { + $sel = selected($selected, $opt[0], false); + $body .= ""; + } + return ""; +} diff --git a/tests/SelectFieldTest.php b/tests/SelectFieldTest.php new file mode 100644 index 0000000..1c1a8c2 --- /dev/null +++ b/tests/SelectFieldTest.php @@ -0,0 +1,53 @@ + +// SPDX-FileCopyrightText: 2021 Harald Eilertsen +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +declare(strict_types=1); + +use \EternalTerror\ViewHelpers; + +final class SelectFieldTest extends WP_UnitTestCase +{ + public function testEmptySelectField() + { + $res = ViewHelpers\select_field("fooselect"); + $this->assertStringStartsWith('', $res); + } + + public function testSelectFieldWithOneOption() + { + $res = ViewHelpers\select_field("fooselect", [[42, 'An option']]); + $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); + } + + public function testSelectFieldWithMultipleOption() + { + $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']]; + + $res = ViewHelpers\select_field("fooselect", $opts); + + $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + } + + public function testSelectFieldWithSelectedOption() + { + $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']]; + + $res = ViewHelpers\select_field("fooselect", $opts, 666); + + $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + } +} -- cgit v1.2.3 From 9694c61f301989cdbbfd9e303e74b30812752cab Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 May 2021 19:33:07 +0200 Subject: Allow custom text for no selection in select_fields. --- includes/view-helpers/select_field.php | 10 ++++++++-- tests/SelectFieldTest.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php index 7cff3ef..816f8ef 100644 --- a/includes/view-helpers/select_field.php +++ b/includes/view-helpers/select_field.php @@ -13,11 +13,17 @@ namespace EternalTerror\ViewHelpers; * @param array $opts The options as arrays of [value, label] pairs * @param mixed|int $selected The value of the preselected option, or null if no * option is preselected. + * @param string $blank Text to use for "no selection", defaults to "Please + * select..." * @return string */ -function select_field(string $name, ?array $opts = [], $selected = null) : string +function select_field( + string $name, + ?array $opts = [], + $selected = null, + string $blank = "Please select...") : string { - $body = ''; + $body = ""; foreach ($opts as $opt) { $sel = selected($selected, $opt[0], false); $body .= ""; diff --git a/tests/SelectFieldTest.php b/tests/SelectFieldTest.php index 1c1a8c2..fb6c298 100644 --- a/tests/SelectFieldTest.php +++ b/tests/SelectFieldTest.php @@ -22,6 +22,7 @@ final class SelectFieldTest extends WP_UnitTestCase $res = ViewHelpers\select_field("fooselect", [[42, 'An option']]); $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); } @@ -33,6 +34,7 @@ final class SelectFieldTest extends WP_UnitTestCase $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); @@ -46,6 +48,21 @@ final class SelectFieldTest extends WP_UnitTestCase $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + $this->assertStringContainsString('', $res); + } + + public function testSelectFieldWithCustomBlankSelectionText() + { + $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']]; + + $res = ViewHelpers\select_field("fooselect", $opts, 666, "None"); + + $this->assertStringStartsWith('', $res); + $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); $this->assertStringContainsString('', $res); -- cgit v1.2.3 From 0d3a225b3203b8a8fe0ad24138c09a4f1f738f54 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 May 2021 21:09:34 +0200 Subject: Use select_field for venue selector in new/edit form --- includes/admin/views/giglog_admin_page.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 9d9d759..7de52ce 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -38,19 +38,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { static function get_venue_selector( ?GiglogAdmin_Venue $invenue ): string { - $select = ''; - return($select); + return \EternalTerror\ViewHelpers\select_field( + "selectvenueadmin", + array_map(fn($venue) => [$venue->id(), $venue->name()], GiglogAdmin_Venue::all_venues()), + $invenue ? $invenue->id() : null); } -- cgit v1.2.3 From 543bddc03fbf9dc90429d4d77dd11affaea4f356 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 May 2021 09:13:28 +0200 Subject: Use select_field helper for city/venue filter. --- includes/admin/views/giglog_admin_page.php | 49 ++++++++++-------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 7de52ce..8ae30c3 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -70,45 +70,28 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } - static function get_filters(): string + static function get_filters() : string { - $cities = array_merge(["ALL"], GiglogAdmin_Venue::all_cities()); - $cty = filter_input( INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS ); - $selected_city = - filter_input(INPUT_POST, "selectcity", FILTER_SANITIZE_SPECIAL_CHARS) - || $cities[0]; - - $select = '
FILTER DATA: '; + $select = 'FILTER DATA:'; + $select .= \EternalTerror\ViewHelpers\select_field( + "selectcity", + array_map(fn($city) => [$city, $city], GiglogAdmin_Venue::all_cities()), + $cty, + "Select city..."); - if ( $cty && $cty!= "ALL" ) { + if ( !empty($cty) ) { //second drop down for venue - $venues = GiglogAdmin_Venue::venues_in_city($cty); - - $venue_list = array_merge( - [0, "ALL"], + $select .= \EternalTerror\ViewHelpers\select_field( + "selectvenue", array_map( - function($v) { return [$v->id(), $v->name()]; }, - $venues)); - - $selected_venue = filter_input(INPUT_POST, "selectvenue", FILTER_SANITIZE_SPECIAL_CHARS) - || $venue_list[0]; - - $select .= ''; - + fn($venue) => [$venue->id(), $venue->name()], + GiglogAdmin_Venue::venues_in_city($cty) + ), + filter_input(INPUT_POST, 'selectvenue', FILTER_SANITIZE_SPECIAL_CHARS), + "Select venue..."); } //option to select own concerts only $select .= ' Date: Tue, 25 May 2021 16:43:05 +0200 Subject: Use select_field in adminactions form. Also add a `get_status` method to the Concertlogs class, returning the press status for a given concert_id. --- includes/admin/views/giglog_admin_page.php | 16 +++++----------- includes/concertlogs.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 8ae30c3..f84b497 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -152,22 +152,16 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $query = "SELECT id,wpgs_name from wpg_pressstatus" ; $statuses = $wpdb->get_results($query); - $select = + return '' . '' - . '' + . \EternalTerror\ViewHelpers\select_field( + 'selectstatus', + array_map(fn($status) => [ $status->id, $status->wpgs_name ], $statuses), + GiglogAdmin_Concertlogs::get_status($concert_id)) . '' . '' . '
'; - - return $select; } //function to calculate if the concert has been added in the past 10 days or before that and show a green NEW for the newest rows diff --git a/includes/concertlogs.php b/includes/concertlogs.php index 3b8083a..9546d21 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -25,5 +25,17 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) $wpdb->query($q); } + + public static function get_status(int $concert_id) : ?int + { + global $wpdb; + + $q = $wpdb->prepare( + 'select wpgcl_status from wpg_concertlogs where id = %d', + $concert_id); + $res = $wpdb->get_results($q); + + return $res ? $res[0]->wpgcl_status : null; + } } } -- cgit v1.2.3 From 40fd00f9b5988690d09fb4f760577e54b51a9c5b Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 May 2021 20:23:54 +0200 Subject: Refactor the AdminPage::get_user method. No functional change, just trying to make sense of it. --- includes/admin/views/giglog_admin_page.php | 34 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index f84b497..7ae7628 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -49,21 +49,33 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { { $hf_user = wp_get_current_user(); $hf_username = $hf_user->user_login; + $users = array_map( + fn($usr) => $usr->user_login, + get_users( array( 'fields' => array( 'user_login' ) ) ) ); + + // This renders a user form, which we don't really use for anything + // other than to check which user (if any) the form was made for. + // Seems this could be done a bit simpler... + $userform = GiglogAdmin_AdminPage::returnuser($ctype, $cid); + $select = ''; return($select); -- cgit v1.2.3 From 7ef3a3cbbe723e7e56f47ed1c85ac39488815bfd Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 May 2021 21:02:42 +0200 Subject: Add method to get assigned user for concertlogs --- includes/concertlogs.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/includes/concertlogs.php b/includes/concertlogs.php index 9546d21..583cc8d 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -37,5 +37,25 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) return $res ? $res[0]->wpgcl_status : null; } + + public static function get_assigned_user( int $concert_id, string $role ) : ?string + { + global $wpdb; + + if ( ! in_array( $role, [ 'photo1', 'photo2', 'rev1', 'rev2' ] ) ) { + error_log(__METHOD__ . ": invalid \$role ({$role}) given."); + return null; + } + + $column = "wpgcl_{$role}"; + $q = $wpdb->prepare( + "select {$column} from wpg_concertlogs where id = %d", + $concert_id); + + $res = $wpdb->get_row($q, ARRAY_A); + var_dump($res); + + return array_shift( $res ); + } } } -- cgit v1.2.3 From aaa154aec8b446fb7123f0dd7a37bdd18490248e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 May 2021 21:03:24 +0200 Subject: Simplify AdminPage::get_user Now queries the user from the concertlogs table instead of going by generating a form that is thrown away. --- includes/admin/views/giglog_admin_page.php | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 7ae7628..4f211fc 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -53,29 +53,20 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { fn($usr) => $usr->user_login, get_users( array( 'fields' => array( 'user_login' ) ) ) ); - // This renders a user form, which we don't really use for anything - // other than to check which user (if any) the form was made for. - // Seems this could be done a bit simpler... - $userform = GiglogAdmin_AdminPage::returnuser($ctype, $cid); + $current_user = $cid ? GiglogAdmin_Concertlogs::get_assigned_user( $cid, $ctype ) : null; $select = ''; return($select); -- cgit v1.2.3 From 1d1f5696e79d7d5be03948fdeceb925ae22e8f10 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 May 2021 21:05:56 +0200 Subject: Remove debug log from concertlogs --- includes/concertlogs.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/concertlogs.php b/includes/concertlogs.php index 583cc8d..70baf26 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -53,7 +53,6 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) $concert_id); $res = $wpdb->get_row($q, ARRAY_A); - var_dump($res); return array_shift( $res ); } -- cgit v1.2.3 From 72a0df55f1a5858ac088cfce34183c740c544663 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 May 2021 21:15:09 +0200 Subject: Use select_field in AdminPage::get_user --- includes/admin/views/giglog_admin_page.php | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 4f211fc..bb42aa7 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -55,21 +55,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $current_user = $cid ? GiglogAdmin_Concertlogs::get_assigned_user( $cid, $ctype ) : null; - $select = ''; - return($select); + return \EternalTerror\ViewHelpers\select_field( + $ctype, + array_map( fn($user) => [ $user, $user ], $users ), + $current_user); } -- cgit v1.2.3 From 8e1da1f405a5f307c84e12b9a40b4a657c16c05d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 26 May 2021 21:33:52 +0200 Subject: Add instance methods to Concertlogs. This allows us to instantiate a Concertlogs objects just as with Concert and Venue objects. Also add a few instance methods to get the assigned user for a given role, and get the role assigned to a given user. --- includes/concertlogs.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/includes/concertlogs.php b/includes/concertlogs.php index 70baf26..8b485ca 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -9,6 +9,16 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) { class GiglogAdmin_Concertlogs { + private array $roles; + + private function __construct( $attr = [] ) + { + $this->roles['photo1'] = $attr->{"wpgcl_photo1"}; + $this->roles['photo2'] = $attr->{"wpgcl_photo2"}; + $this->roles['rev1'] = $attr->{"wpgcl_rev1"}; + $this->roles['rev2'] = $attr->{"wpgcl_rev2"}; + } + /** * Adds a default entry for the given concert id in the * concert logs table. @@ -56,5 +66,28 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) return array_shift( $res ); } + + public static function get(int $concert_id) : ?self + { + global $wpdb; + + $q = $wpdb->prepare( + "select * from wpg_concertlogs where id = %d", + $concert_id); + + $res = $wpdb->get_row($q); + + return $res ? new self($res) : null; + } + + public function get_assigned_role(string $username) : ?string + { + return array_search( $username, $this->roles ); + } + + public function assigned_user(string $role) : ?string + { + return $this->roles[$role]; + } } } -- cgit v1.2.3 From b3c2e40c2ebfbfc6098495293c322140fd48250c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 26 May 2021 21:35:28 +0200 Subject: Refactor and reformat AdminPage::returnuser. Now use a Concertlog object to render the correct subform instead of messing with the db directly. --- includes/admin/views/giglog_admin_page.php | 49 +++++++++++++++++++----------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index bb42aa7..c1887dc 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -384,31 +384,44 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { wp_mail( $to, $subject, $body, $headers ); } - static function returnuser(string $p1, ?int $c): string + static function returnuser(string $p1, ?int $c) : ?string { - global $wpdb; $hf_user = wp_get_current_user(); $hf_username = $hf_user->user_login; + if (!$c) { + return null; + } - if (!empty($c)) - { - $sql = "select * from wpg_concertlogs where wpgcl_concertid=".$c; - $crow = $wpdb->get_results($sql); - $array = array('photo1' => $crow[0]->wpgcl_photo1, 'photo2'=> $crow[0]->wpgcl_photo2, 'rev1' => $crow[0]->wpgcl_rev1, 'rev2'=> $crow[0]->wpgcl_rev2); - - //first check if current slot is taken by current user - if ($array[$p1] == $hf_username) return ('
'); - else //check if slot is taken by another user - if (!empty($array[$p1])) return ('Taken
Taken by '.$array[$p1].'
'); - else //check if other slots for this concert are taken by user - if (in_array($hf_username,$array)) return ('-'); - else //not taken by anyone - return ('
-
'); + $cl = GiglogAdmin_Concertlogs::get($c); + $role = $cl->get_assigned_role( $hf_username ); + $assigned_user = $cl->assigned_user( $p1 ); + + //first check if current slot is taken by current user + if ( $role == $p1 ) { + $f = '
' + . ' ' + . ' ' + . ' ' + . '
'; + } + elseif ( $assigned_user ) { //check if slot is taken by another user + $f = 'Taken' + . '
Taken by ' . $assigned_user . '
'; + } + elseif ( $role ) { + // other slots for this concert are taken by user + $f = '-'; + } + else { //not taken by anyone + $f = '
' + . ' ' + . ' ' + . ' ' + . '
'; } - else return ('no concert selected'); + return $f; } } } -- cgit v1.2.3 From 1c8667a1d6e56ac359b18b4b364296d12b2e41a0 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 29 May 2021 13:59:03 +0200 Subject: Make AdminPage a proper object. --- includes/admin/views/giglog_admin_page.php | 57 ++++++++++++++++++------------ 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index c1887dc..c2c84e9 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -9,7 +9,18 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { require_once __DIR__ . '/../../venue.php'; class GiglogAdmin_AdminPage { - static function render_html(): void { + public function __construct() + { + } + + public static function render_html() : void + { + $page = new self(); + $page->render_page(); + } + + public function render_page() : void + { ?>

Giglog Admin

@@ -28,15 +39,15 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { what the accreditation status is. You will get personal message if this is really close to the concert date.

-

-

+

get_filters() ?>

+

get_concerts() ?>

user_login; @@ -62,7 +73,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } - static function get_filters() : string + private function get_filters() : string { $cty = filter_input(INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS); @@ -95,7 +106,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return $select; } - static function editforms(): string + private function editforms(): string { $cid = filter_input(INPUT_POST, "cid"); $editing = filter_input(INPUT_POST, "edit") == "EDIT"; @@ -110,7 +121,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { .'
CONCERT DETAILS

' .'' .'
' - .'' . GiglogAdmin_AdminPage::get_venue_selector($c->venue()) . '
' + .'' . $this->get_venue_selector($c->venue()) . '
' .'
' .'
' .'
' @@ -123,10 +134,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content.='
'; $content.='
ASSIGNMENT DETAILS

' - .''.GiglogAdmin_AdminPage::get_user($c->id(),'photo1').'
' - .''.GiglogAdmin_AdminPage::get_user($c->id(),'photo2').'
' - .''.GiglogAdmin_AdminPage::get_user($c->id(),'rev1').'
' - .''.GiglogAdmin_AdminPage::get_user($c->id(),'rev2').'
'; + .''.$this->get_user($c->id(),'photo1').'
' + .''.$this->get_user($c->id(),'photo2').'
' + .''.$this->get_user($c->id(),'rev1').'
' + .''.$this->get_user($c->id(),'rev2').'
'; $content.='
'; $content.='
VENUE DETAILS

' @@ -138,7 +149,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return $content; } - static function adminactions( int $concert_id ) : string + private function adminactions( int $concert_id ) : string { global $wpdb; $query = "SELECT id,wpgs_name from wpg_pressstatus" ; @@ -160,7 +171,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { /** * @return null|string */ - static function getpublishstatus(int $concert_id) + private function getpublishstatus(int $concert_id) { global $wpdb; $date1 = new DateTime("now"); @@ -177,7 +188,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } - static function get_concerts(): string + private function get_concerts(): string { $hf_user = wp_get_current_user(); $hf_username = $hf_user->user_login; @@ -237,17 +248,17 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); $content .= '' .$newformat. ''; - $content .= ''.GiglogAdmin_AdminPage::getpublishstatus($row->id ).''; - $content .= ''.GiglogAdmin_AdminPage::returnuser('photo1', $row->id ).''; - $content .= ''.GiglogAdmin_AdminPage::returnuser('photo2', $row->id ).''; - $content .= ''.GiglogAdmin_AdminPage::returnuser('rev1', $row->id ).''; - $content .= ''.GiglogAdmin_AdminPage::returnuser('rev2', $row->id ).''; + $content .= ''.$this->getpublishstatus($row->id ).''; + $content .= ''.$this->returnuser('photo1', $row->id ).''; + $content .= ''.$this->returnuser('photo2', $row->id ).''; + $content .= ''.$this->returnuser('rev1', $row->id ).''; + $content .= ''.$this->returnuser('rev2', $row->id ).''; $content .= ''.$row -> wpgs_name.''; if (current_user_can('administrator')) { $content .= '' - . GiglogAdmin_AdminPage::adminactions($row->id) + . $this->adminactions($row->id) . ''; } $content .= ''; @@ -294,7 +305,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } //handling the admin drop down menu - if(isset($_POST['selectstatus']) && $_POST['edit']!="EDIT" && !empty($_POST['cid'])) + if(isset($_POST['selectstatus']) && (isset($_POST['edit']) && $_POST['edit']!="EDIT") && !empty($_POST['cid'])) { $usql = "UPDATE wpg_concertlogs SET wpgcl_status=".$_POST['selectstatus']." WHERE wpgcl_concertid=".$_POST['cid']; $uresults = $wpdb->get_results($usql); @@ -384,7 +395,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { wp_mail( $to, $subject, $body, $headers ); } - static function returnuser(string $p1, ?int $c) : ?string + private function returnuser(string $p1, ?int $c) : ?string { $hf_user = wp_get_current_user(); $hf_username = $hf_user->user_login; -- cgit v1.2.3 From ca846023ab96299d53c7f806810f1b09bc45c5bb Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 29 May 2021 14:07:19 +0200 Subject: Get current users username in AdminPage constructor. --- includes/admin/views/giglog_admin_page.php | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index c2c84e9..4a0e348 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -11,6 +11,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { class GiglogAdmin_AdminPage { public function __construct() { + $this->username = wp_get_current_user()->user_login; } public static function render_html() : void @@ -58,8 +59,6 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { private function get_user( ?int $cid, string $ctype): string { - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; $users = array_map( fn($usr) => $usr->user_login, get_users( array( 'fields' => array( 'user_login' ) ) ) ); @@ -190,8 +189,6 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { private function get_concerts(): string { - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; $roles = $hf_user->roles; global $wpdb; @@ -202,7 +199,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { CITYNAMEVENUEDATE PHOTO1PHOTO2TEXT1TEXT2 STATUS'; - if (current_user_can('administrator')) //($hf_username == 'etadmin') + if (current_user_can('administrator')) $content .= 'AdminOptions'; $content .= ''; @@ -223,7 +220,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $query .= ($cty == "ALL") ? "" : " and wpgv.wpgvenue_city='" .$cty ."'"; $query .= ($venue == "0") ? "" : " and wpgv.id='" .$venue ."'"; - $query.= (empty($_POST['my_checkbox'])) ? "": " and (wpgcl_photo1 ='".$hf_username."' or wpgcl_photo2 ='".$hf_username."' or wpgcl_rev1 ='".$hf_username."' or wpgcl_rev2 ='".$hf_username."')"; + $query.= (empty($_POST['my_checkbox'])) ? "": " and (wpgcl_photo1 ='".$this->username."' or wpgcl_photo2 ='".$this->username."' or wpgcl_rev1 ='".$this->username."' or wpgcl_rev2 ='".$this->username."')"; $query .=" order by wpgv.wpgvenue_city, wpgconcert_date, wpgc.id" ; $results = $wpdb->get_results($query); $lastType = ''; @@ -357,17 +354,15 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { { global $wpdb; - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; $to = 'live@eternal-terror.com'; - $subject = $hf_username.' has taken '.$p1. 'for a concert with id '.$c; + $subject = $this->username.' has taken '.$p1. 'for a concert with id '.$c; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); - $usql = "UPDATE wpg_concertlogs SET wpgcl_".$p1."='".$hf_username."' WHERE wpgcl_concertid=".$c; + $usql = "UPDATE wpg_concertlogs SET wpgcl_".$p1."='".$this->username."' WHERE wpgcl_concertid=".$c; $uresults = $wpdb->get_results($usql); $wpdb->insert( 'wpg_logchanges', array ( 'id' => '', - 'userid' => $hf_username, + 'userid' => $this->username, 'action' => 'assigned '.$p1, 'concertid' => $c)); echo ($wpdb->last_error ); @@ -378,17 +373,15 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { { global $wpdb; - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; $to = 'live@eternal-terror.com'; - $subject = $hf_username.' has UNASSINED '.$p1. 'for a concert with id '.$c; + $subject = $this->username.' has UNASSINED '.$p1. 'for a concert with id '.$c; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); $usql = "UPDATE wpg_concertlogs SET wpgcl_".$p1."='' WHERE wpgcl_concertid=".$c; $uresults = $wpdb->get_results($usql); $wpdb->insert( 'wpg_logchanges', array ( 'id' => '', - 'userid' => $hf_username, + 'userid' => $this->username, 'action' => 'unassigned '.$p1, 'concertid' => $c)); echo ($wpdb->last_error ); @@ -397,15 +390,12 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { private function returnuser(string $p1, ?int $c) : ?string { - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; - if (!$c) { return null; } $cl = GiglogAdmin_Concertlogs::get($c); - $role = $cl->get_assigned_role( $hf_username ); + $role = $cl->get_assigned_role( $this->username ); $assigned_user = $cl->assigned_user( $p1 ); //first check if current slot is taken by current user -- cgit v1.2.3 From b5c2165f0369391bd2367f2df4a3ee9a903b7eb9 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Jun 2021 18:11:06 +0200 Subject: Drop obsolete line from AdminPage::get_concerts. This must have been forgotten in the previous commit. --- includes/admin/views/giglog_admin_page.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 4a0e348..6f0b2ba 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -189,7 +189,6 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { private function get_concerts(): string { - $roles = $hf_user->roles; global $wpdb; $content = ''; -- cgit v1.2.3 From 5af89bca3bd17777fae94e681882d6eabf152303 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Jun 2021 18:24:01 +0200 Subject: Fix misc Psalm issues. --- includes/admin/views/giglog_admin_page.php | 2 ++ includes/admin/views/giglog_import_gigs.php | 2 +- includes/concert.php | 2 ++ includes/concertlogs.php | 2 +- includes/venue.php | 4 ++-- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 6f0b2ba..ab71270 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -9,6 +9,8 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { require_once __DIR__ . '/../../venue.php'; class GiglogAdmin_AdminPage { + private string $username; + public function __construct() { $this->username = wp_get_current_user()->user_login; diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index a8daa3f..aeaf974 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -51,7 +51,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { * * @return void * - * @param ArrayAccess|array $file + * @param array */ static function process_upload(array $file): void { $newconcert= []; diff --git a/includes/concert.php b/includes/concert.php index 455bc82..56021f5 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -6,6 +6,8 @@ // SPDX-License-Identifier: AGPL-3.0-or-later if ( !class_exists('GiglogAdmin_Concert') ) { + require_once __DIR__ . '/venue.php'; + class GiglogAdmin_Concert { private $id; diff --git a/includes/concertlogs.php b/includes/concertlogs.php index 8b485ca..cd0a330 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -82,7 +82,7 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) public function get_assigned_role(string $username) : ?string { - return array_search( $username, $this->roles ); + return array_search( $username, $this->roles ) || NULL; } public function assigned_user(string $role) : ?string diff --git a/includes/venue.php b/includes/venue.php index f9f7e4f..aedb6a7 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -31,8 +31,8 @@ if ( !class_exists('GiglogAdmin_Venue') ) { /** * Get venue by given id. * - * @param int $id. - * @return null|self. + * @param int $id + * @return null|self */ static function get(int $id) : ?self { -- cgit v1.2.3 From 72340c709e50a2383a8c651d72bb4396e9477c9a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Jun 2021 18:24:37 +0200 Subject: Streamline Concert api. Reduce to one find_concerts function taking a filter to limit the selection. --- includes/concert.php | 37 ++++++++++++++++++++++--------------- tests/ConcertTest.php | 14 +++++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index 56021f5..fcb3934 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -173,7 +173,18 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public static function find_concerts_in(?string $city = null) : array + /** + * 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 $filter + * @return array + */ + public static function find_concerts(array $filter = []) : array { global $wpdb; @@ -181,23 +192,19 @@ if ( !class_exists('GiglogAdmin_Concert') ) { . 'FROM wpg_concerts ' . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '; - if ( $city ) { - $query .= 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city); - } - - $results = $wpdb->get_results($query); + $where = []; - return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); - } + if ( isset( $filter["city"] ) ) { + array_push($where, 'wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $filter["city"])); + } - public static function find_concerts_at(GiglogAdmin_Venue $venue) : array - { - global $wpdb; + if ( isset( $filter["venue_id"] ) ) { + array_push($where, 'wpg_venues.id = ' . $wpdb->prepare('%s', $filter["venue_id"])); + } - $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' - . 'FROM wpg_concerts ' - . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' - . 'WHERE wpg_concerts.venue = ' . $wpdb->prepare('%d', $venue->id()); + if ( ! empty( $where ) ) { + $query .= 'WHERE ' . implode(' and ', $where); + } $results = $wpdb->get_results($query); diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index ec7bc13..2c747b8 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -106,7 +106,7 @@ final class ConcertTest extends WP_UnitTestCase GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); } - $gigs_in_svene = GiglogAdmin_Concert::find_concerts_in("Svene"); + $gigs_in_svene = GiglogAdmin_Concert::find_concerts([ "city" => "Svene"]); $this->assertEquals(4, count($gigs_in_svene)); while ($gig = array_pop($gigs_in_svene)) { @@ -114,14 +114,14 @@ final class ConcertTest extends WP_UnitTestCase } - $gigs_in_oslo = GiglogAdmin_Concert::find_concerts_in("Oslo"); + $gigs_in_oslo = GiglogAdmin_Concert::find_concerts(["city" => "Oslo"]); $this->assertEquals(2, count($gigs_in_oslo)); while ($gig = array_pop($gigs_in_oslo)) { $this->assertEquals("Oslo", $gig->venue()->city()); } - $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts_in("Sogndal"); + $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts(["city" => "Sogndal"]); $this->assertEquals(5, count($gigs_in_sogndal)); while ($gig = array_pop($gigs_in_sogndal)) { @@ -147,21 +147,21 @@ final class ConcertTest extends WP_UnitTestCase GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); } - $gigs_at_ss = GiglogAdmin_Concert::find_concerts_at($venue1); + $gigs_at_ss = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue1->id()]); $this->assertEquals(4, count($gigs_at_ss)); while ($gig = array_pop($gigs_at_ss)) { $this->assertEquals("Sentrum Scene", $gig->venue()->name()); } - $gigs_at_rmh = GiglogAdmin_Concert::find_concerts_at($venue2); + $gigs_at_rmh = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue2->id()]); $this->assertEquals(2, count($gigs_at_rmh)); while ($gig = array_pop($gigs_at_rmh)) { $this->assertEquals("Rockefeller Music Hall", $gig->venue()->name()); } - $gigs_at_r = GiglogAdmin_Concert::find_concerts_at($venue3); + $gigs_at_r = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue3->id()]); $this->assertEquals(5, count($gigs_at_r)); while ($gig = array_pop($gigs_at_r)) { @@ -187,7 +187,7 @@ final class ConcertTest extends WP_UnitTestCase GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); } - $gigs = GiglogAdmin_Concert::find_concerts_in(); + $gigs = GiglogAdmin_Concert::find_concerts(); $this->assertEquals(11, count($gigs)); } -- cgit v1.2.3 From bb353e8e61156b0c9fdab673e2485502a01b8434 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 13 Jun 2021 16:05:48 +0200 Subject: Move method to update Concertlogs to Concertlogs class. --- includes/admin/views/giglog_admin_page.php | 2 +- includes/concert.php | 23 ----------------------- includes/concertlogs.php | 22 ++++++++++++++++++++++ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index ab71270..6c1f059 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -332,7 +332,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { else { GiglogAdmin_Concert::update_concert($_POST['pid'],$_POST['cname'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); - GiglogAdmin_Concert::update_concertlog($_POST['pid'],$_POST['photo1'], $_POST['photo2'], $_POST['rev1'], $_POST['rev2']); + GiglogAdmin_Concertlogs::update($_POST['pid'],$_POST['photo1'], $_POST['photo2'], $_POST['rev1'], $_POST['rev2']); echo ''; } diff --git a/includes/concert.php b/includes/concert.php index fcb3934..6d568c8 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -137,29 +137,6 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return ($wpdb->last_error); } - static function update_concertlog($cid, $ph1, $ph2, $rev1, $rev2) - { - global $wpdb; - - $res = $wpdb->update('wpg_concertlogs', array( - 'wpgcl_photo1' => $ph1, - 'wpgcl_photo2' => $ph2, - 'wpgcl_rev1' => $rev1, - 'wpgcl_rev2' => $rev2 - ), - array('wpgcl_concertid' => $cid) - ); - - if ( !$res ) { - // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed - error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); - die; - } - - return ($wpdb->last_error); - - } - public static function find($cname, $venue, $date) { global $wpdb; diff --git a/includes/concertlogs.php b/includes/concertlogs.php index cd0a330..8b1ca63 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -36,6 +36,28 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) $wpdb->query($q); } + static function update($cid, $ph1, $ph2, $rev1, $rev2) + { + global $wpdb; + + $res = $wpdb->update('wpg_concertlogs', array( + 'wpgcl_photo1' => $ph1, + 'wpgcl_photo2' => $ph2, + 'wpgcl_rev1' => $rev1, + 'wpgcl_rev2' => $rev2 + ), + array('wpgcl_concertid' => $cid) + ); + + if ( !$res ) { + // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed + error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); + die; + } + + return ($wpdb->last_error); + } + public static function get_status(int $concert_id) : ?int { global $wpdb; -- cgit v1.2.3 From 68652c0546845de2f216d7fb285205e2eddf9d41 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 13 Jun 2021 16:07:12 +0200 Subject: Fix detecting error in update_concert method. --- includes/concert.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/concert.php b/includes/concert.php index 6d568c8..d370de9 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -128,7 +128,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { array('id' => $id) ); - if ( !$res ) { + if ( $res === false ) { // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); die; -- cgit v1.2.3 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