From 349ce7054ac8caccdc3d370365461931ff7a7b96 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 14:51:01 +0200 Subject: Clean tests env before running tests Since we're using our own database tables, these are not cleaned by the default WP_PHPUnit setup. To ensure that we start on a clean slate when running the tests, clean the env before the tests are run. --- run-tests | 1 + run-tests-windoze.bat | 1 + 2 files changed, 2 insertions(+) diff --git a/run-tests b/run-tests index 9f881d6..8c1eeb5 100755 --- a/run-tests +++ b/run-tests @@ -5,4 +5,5 @@ # # SPDX-License-Identifier: CC0-1.0 +npm run wp-env clean npm run wp-env run phpunit "phpunit -c /var/www/html/wp-content/plugins/giglogadmin/phpunit.xml $*" diff --git a/run-tests-windoze.bat b/run-tests-windoze.bat index 107f943..8654d40 100644 --- a/run-tests-windoze.bat +++ b/run-tests-windoze.bat @@ -5,4 +5,5 @@ REM SPDX-FileCopyrightText: 2021 Harald Eilertsen REM REM SPDX-License-Identifier: CC0-1.0 +npm run wp-env clean npm run wp-env run phpunit "phpunit -c /var/www/html/wp-content/plugins/giglogadmin/phpunit.xml" -- cgit v1.2.3 From f3c31f5e3b6f57bbc887e6b4e435e5202eb41208 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 14:53:57 +0200 Subject: Refactor ConcertTests and some cleanup The commit changes the way we populate the database for the tests by creating more entries up front. This reduces the amount of duplicated code between the tests, but also introduce some challenges. As modifications to the database done in the wpSetUpBeforeClass hook are not cleaned up automatically by the WP_PHPUnit framework, we also have to add a wpTearDownAfterClass hook so anything we set up in this class does not disturb any other tests in other classes. --- tests/ConcertTest.php | 143 ++++++++++++++++++++++++++------------------------ 1 file changed, 75 insertions(+), 68 deletions(-) diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 7edda46..bde488b 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -6,37 +6,70 @@ final class ConcertTest extends WP_UnitTestCase { - public function testCreateConcert() : void + const VENUES = [ + [ "a venue", "Somewhere" ], + [ "Svene Bedehus", "Svene" ], + [ "Rockefeller Music Hall", "Oslo" ], + [ "Sentrum Scene", "Oslo" ], + [ "Revolver", "Oslo" ], + [ "Meieriet", "Sogndal" ], + ]; + + const CONCERTS = [ + [ "a concert", 0 ], + ]; + + private static $concerts = []; + + /* This function runs _once_ before all the test cases. + * + * Use it to set up a common state that all test cases can + * use + */ + static function wpSetUpBeforeClass() : void { - $venue = GiglogAdmin_Venue::create("a venue"); + $created_venues = []; + foreach (self::VENUES as $venue) { + $created_venues[] = GiglogAdmin_Venue::find_or_create($venue[0], $venue[1]); + } + $today = date("Y-m-d"); - $concert = GiglogAdmin_Concert::create( - "a concert", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); + foreach (self::CONCERTS as $concert) { + self::$concerts[] = GiglogAdmin_Concert::create( + $concert[0], + $created_venues[$concert[1]]->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + } + } - $this->assertEquals("a concert", $concert->cname()); - $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()); + /* This function runs _once_ after all the test cases in this class. + * + * It is needed to clean up changes in the database that we don't want + * to disturb any other tests. + */ + static function wpTearDownAfterClass() : void + { + global $wpdb; + + $tables = [ + "wpg_concerts", + "wpg_venues", + "wpg_concertlogs", + ]; + + foreach( $tables as $table ) { + $wpdb->query("DELETE FROM {$table}"); + } } - public function testCreateExistingConcert() : void + public function testCreateExistingConcertShouldFail() : void { - $venue = GiglogAdmin_Venue::create("a venue"); + $venue = GiglogAdmin_Venue::find_or_create("a venue", "Somewhere"); $today = date("Y-m-d"); - GiglogAdmin_Concert::create( - "a concert", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - $new = GiglogAdmin_Concert::create( "a concert", $venue->id(), @@ -49,18 +82,11 @@ final class ConcertTest extends WP_UnitTestCase public function testCreateExistingConcertVariableCase() : void { - $venue = GiglogAdmin_Venue::create("a venue"); + $venue = GiglogAdmin_Venue::find_or_create("a venue", "Somewhere"); $today = date("Y-m-d"); - GiglogAdmin_Concert::create( - "a concert123", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - $new = GiglogAdmin_Concert::create( - "a CoNceRt123", + "a CoNceRt", $venue->id(), $today, "https://example.com/tickets/42", @@ -71,52 +97,33 @@ final class ConcertTest extends WP_UnitTestCase public function testGetConcertByIdReturnsFullConcertObject() : void { - $venue = GiglogAdmin_Venue::create("a venue"); - $today = date("Y-m-d"); + $id = self::$concerts[0]->id(); + $fetched_gig = GiglogAdmin_Concert::get($id); - $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()); + $this->assertEquals($id, $fetched_gig->id()); + $this->assertEquals("a concert", $fetched_gig->cname()); + $this->assertEquals("a venue", $fetched_gig->venue()->name()); $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"); + $id = self::$concerts[0]->id(); + $fetched_gig = GiglogAdmin_Concert::get($id); - $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()); + $fetched_gig_2 = GiglogAdmin_Concert::get($id); $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig_2->status() ); } public function testOnlyFetchConcertsFromGivenCity() : void { - $venue1 = GiglogAdmin_Venue::create("Svene Bedehus", "Svene"); - $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::create("Meieriet", "Sogndal"); + $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::find_or_create("Meieriet", "Sogndal"); for ($i = 0; $i < 4; $i++) { GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); @@ -155,9 +162,9 @@ final class ConcertTest extends WP_UnitTestCase public function testOnlyFetchConcertsAtGivenVenue() : void { - $venue1 = GiglogAdmin_Venue::create("Sentrum Scene", "Oslo"); - $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::create("Revolver", "Oslo"); + $venue1 = GiglogAdmin_Venue::find_or_create("Sentrum Scene", "Oslo"); + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::find_or_create("Revolver", "Oslo"); for ($i = 0; $i < 4; $i++) { GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); @@ -195,9 +202,9 @@ final class ConcertTest extends WP_UnitTestCase 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"); + $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::find_or_create("Meieriet", "Sogndal"); for ($i = 0; $i < 4; $i++) { GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); @@ -213,6 +220,6 @@ final class ConcertTest extends WP_UnitTestCase $gigs = GiglogAdmin_Concert::find_concerts(); - $this->assertEquals(11, count($gigs)); + $this->assertEquals(count(self::$concerts) + 11, count($gigs)); } } -- cgit v1.2.3 From 06ec509ce40d3ca04c770e542f57891a3540c7cd Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 15:18:39 +0200 Subject: Refactor ConcertTest some more. Create more of the concerts used by tests into the wpSetupBeforeClass hook. --- tests/ConcertTest.php | 83 ++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 58 deletions(-) diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index bde488b..77b1ed3 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -16,7 +16,12 @@ final class ConcertTest extends WP_UnitTestCase ]; const CONCERTS = [ - [ "a concert", 0 ], + [ "a concert", 0, 1 ], + [ "Concert in Svene #", 1, 4 ], + [ "Concert at Rockefeller #", 2, 2 ], + [ "Concert at Sentrum Scene #", 3, 4 ], + [ "Concert at Revolver #", 4, 5 ], + [ "Concert at Meieriet #", 5, 5 ], ]; private static $concerts = []; @@ -36,12 +41,21 @@ final class ConcertTest extends WP_UnitTestCase $today = date("Y-m-d"); foreach (self::CONCERTS as $concert) { - self::$concerts[] = GiglogAdmin_Concert::create( - $concert[0], - $created_venues[$concert[1]]->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); + for ($i = 0; $i < $concert[2]; $i++) { + if ($concert[2] > 1) { + $concert_name = $concert[0] . ($i + 1); + } + else { + $concert_name = $concert[0]; + } + + self::$concerts[] = GiglogAdmin_Concert::create( + $concert_name, + $created_venues[$concert[1]]->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + } } } @@ -121,22 +135,6 @@ final class ConcertTest extends WP_UnitTestCase public function testOnlyFetchConcertsFromGivenCity() : void { - $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); - $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::find_or_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_in_svene = GiglogAdmin_Concert::find_concerts([ "city" => "Svene"]); $this->assertEquals(4, count($gigs_in_svene)); @@ -144,10 +142,9 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Svene", $gig->venue()->city()); } - $gigs_in_oslo = GiglogAdmin_Concert::find_concerts(["city" => "Oslo"]); - $this->assertEquals(2, count($gigs_in_oslo)); + $this->assertEquals(11, count($gigs_in_oslo)); while ($gig = array_pop($gigs_in_oslo)) { $this->assertEquals("Oslo", $gig->venue()->city()); } @@ -163,21 +160,6 @@ final class ConcertTest extends WP_UnitTestCase public function testOnlyFetchConcertsAtGivenVenue() : void { $venue1 = GiglogAdmin_Venue::find_or_create("Sentrum Scene", "Oslo"); - $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::find_or_create("Revolver", "Oslo"); - - 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_at_ss = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue1->id()]); $this->assertEquals(4, count($gigs_at_ss)); @@ -185,6 +167,7 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Sentrum Scene", $gig->venue()->name()); } + $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); $gigs_at_rmh = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue2->id()]); $this->assertEquals(2, count($gigs_at_rmh)); @@ -192,6 +175,7 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals("Rockefeller Music Hall", $gig->venue()->name()); } + $venue3 = GiglogAdmin_Venue::find_or_create("Revolver", "Oslo"); $gigs_at_r = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue3->id()]); $this->assertEquals(5, count($gigs_at_r)); @@ -202,24 +186,7 @@ final class ConcertTest extends WP_UnitTestCase public function testFetchAllConcerts() : void { - $venue1 = GiglogAdmin_Venue::find_or_create("Svene Bedehus", "Svene"); - $venue2 = GiglogAdmin_Venue::find_or_create("Rockefeller Music Hall", "Oslo"); - $venue3 = GiglogAdmin_Venue::find_or_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(); - - $this->assertEquals(count(self::$concerts) + 11, count($gigs)); + $this->assertEquals(count(self::$concerts), count($gigs)); } } -- cgit v1.2.3 From f1fced27a60d656c70c0c060f894679773158f9c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 15:21:28 +0200 Subject: Remove some debug output. --- includes/concert.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/concert.php b/includes/concert.php index 88a20da..6257fab 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -75,7 +75,6 @@ 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; } -- cgit v1.2.3 From 184f1e3f971423fad351de2b2fbde6f436e6d9ce Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 15:23:35 +0200 Subject: Remove Band class and tests. --- includes/band.php | 151 ----------------------------------------------------- tests/BandTest.php | 69 ------------------------ 2 files changed, 220 deletions(-) delete mode 100644 includes/band.php delete mode 100644 tests/BandTest.php diff --git a/includes/band.php b/includes/band.php deleted file mode 100644 index 3460acb..0000000 --- a/includes/band.php +++ /dev/null @@ -1,151 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -if ( !class_exists('GiglogAdmin_Band') ) { - class GiglogAdmin_Band - { - private $id; - private $bandname; - private $country; - - /* - * Constructs a new band 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. - */ - public function __construct($attrs = []) - { - $this->id = isset($attrs->id) ? $attrs->id : NULL; - $this->bandname = isset($attrs->wpgband_name) ? $attrs->wpgband_name : NULL; - $this->country = isset($attrs->wpgband_country) ? $attrs->wpgband_country : 'NO'; - } - - static function create($bandname, $country = 'NO'): self - { - $band = GiglogAdmin_Band::find($bandname, $country); - - if ( ! $band ) { - $band = new GiglogAdmin_Band((object) [ - 'wpgband_name' => $bandname, - 'wpgband_country' => $country, - ]); - - $band->save(); - - error_log( 'NEW BAND ADDED: ' - . ' ID: ' . $band->id() - . ' BAND NAME ' . $band->bandname() - . ', COUNTRY ' . $band->country()); - } - - return $band; - } - - static function find($name, $country): ?self - { - global $wpdb; - - $q = 'SELECT * FROM wpg_bands ' - . 'WHERE upper(wpgband_name)="' . $name - . '" and wpgband_country = "' . $country.'"'; - - $results = $wpdb->get_results($q); - - if ($results) { - return new GiglogAdmin_Band($results[0]); - } - else { - return NULL; - } - } - - - static function all_bands() - { - global $wpdb; - - $results = $wpdb->get_results("select id, wpgband_name as vname from wpg_bands order by wpgband_name"); - - return ($results); - } - - static function all_countries() - { - global $wpdb; - - $results = $wpdb->get_results("select id, wpgcountry_name as cname from wpg_countries order by id"); - - return ($results); - } - - /** - * @return array|string - * - * @psalm-return array{0: mixed, 1: mixed}|string - */ - static function get_band($bid) - { - global $wpdb; - if(!empty($bid)) - { - - $results = $wpdb->get_results('select wpgband_name as bname, wpgband_country as cname from wpg_bands where wpg_bands.id = '.$bid ); - - return array ($results[0]->bname,$results[0]->cname); - } - else return(''); - } - - static function update_band($bid,$bname,$bcountry) - { - global $wpdb; - - $res = $wpdb->update('wpg_bands', array( - 'wpgband_name' => $bname, - 'wpgband_country' => $bcountry - ), - array('id' => $bid) - ); - - 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 function save(): void - { - global $wpdb; - - $wpdb->insert('wpg_bands', array( - 'id' => '', - 'wpgband_name' => $this->bandname, - 'wpgband_country' => $this->country - )); - - $this->id = $wpdb->insert_id; - } - - public function id() - { - return $this->id; - } - - public function bandname() - { - return $this->bandname; - } - public function country() - { - return $this->country; - } - } -} diff --git a/tests/BandTest.php b/tests/BandTest.php deleted file mode 100644 index e4fccbe..0000000 --- a/tests/BandTest.php +++ /dev/null @@ -1,69 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -declare(strict_types=1); - -require __DIR__ . '/../includes/band.php'; - -final class BandTest extends WP_UnitTestCase -{ - /* This function runs _once_ before all the test cases. - * - * Use it to set up a common state that all test cases can - * use - */ - static function wpSetUpBeforeClass() : void - { - GiglogAdmin_Band::create("The Flamboyant Blasphemers"); - } - - public function testCreatingBandWithName() : void - { - $count = count(GiglogAdmin_Band::all_bands()); - - $band = GiglogAdmin_Band::create("Tullerusk"); - - $this->assertEquals("Tullerusk", $band->bandname()); - $this->assertEquals($count + 1, count(GiglogAdmin_Band::all_bands())); - } - - public function testCreateExistingBand() : void - { - $count = count(GiglogAdmin_Band::all_bands()); - - $existing_band = GiglogAdmin_Band::find("The Flamboyant Blasphemers", "NO"); - $new_band = GiglogAdmin_Band::create("The Flamboyant Blasphemers"); - - $this->assertEquals($count, count(GiglogAdmin_Band::all_bands())); - $this->assertEquals($existing_band->id(), $new_band->id()); - $this->assertEquals($existing_band->bandname(), $new_band->bandname()); - } - - public function testCreateBandsWithSameNameInDifferentCountry() : void - { - $existing_band = GiglogAdmin_Band::find("The Flamboyant Blasphemers", "NO"); - $new_band = GiglogAdmin_Band::create("The Flamboyant Blasphemers", "RO"); - - $this->assertNotEquals($existing_band->id(), $new_band->id()); - } - - public function testFindExistingBandReturnsObject() : void - { - $found = GiglogAdmin_Band::find("The Flamboyant Blasphemers", "NO"); - - $this->assertNotNull($found); - $this->assertEquals("The Flamboyant Blasphemers", $found->bandname()); - } - - public function testFindNonExistingBandReturnsNULL() : void - { - // Nice, UK isn't in the country list, so let's move Venom to Azerbajan - // for now... - $found = GiglogAdmin_Band::find("Venom", "AZ"); - - $this->assertNull($found); - } -} -- cgit v1.2.3 From 224f0149ea513146b164736f461e6cbba4b86add Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 27 Jun 2021 13:05:08 +0200 Subject: Begin move roles and status field to concerts table. --- giglogadmin.php | 1 + includes/concert.php | 5 +++++ includes/roles.php | 12 ++++++++++++ tests/ConcertTest.php | 25 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 includes/roles.php diff --git a/giglogadmin.php b/giglogadmin.php index c35c470..dec54a7 100644 --- a/giglogadmin.php +++ b/giglogadmin.php @@ -34,6 +34,7 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { 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'; + require_once __DIR__ . '/includes/roles.php'; class GiglogAdmin_Plugin { diff --git a/includes/concert.php b/includes/concert.php index 6257fab..db8d68a 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -274,6 +274,11 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { return $this->roles; } + + public function assign_role( int $role, string $username ) : void + { + $this->roles[$role] = $username; + } } } ?> diff --git a/includes/roles.php b/includes/roles.php new file mode 100644 index 0000000..dbbedd2 --- /dev/null +++ b/includes/roles.php @@ -0,0 +1,12 @@ +assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig_2->status() ); } + public function testAssignConcertRoles() : 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"); + + $gig->assign_role( GiglogAdmin_Roles::PHOTO1, 'user1' ); + $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $gig->roles() ); + + $gig->save(); + + var_dump($gig); + + $fetched_gig = GiglogAdmin_Concert::get( $gig->id() ); + global $wpdb; + $wpdb->print_error(); + $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $fetched_gig->roles() ); + } + public function testOnlyFetchConcertsFromGivenCity() : void { $gigs_in_svene = GiglogAdmin_Concert::find_concerts([ "city" => "Svene"]); -- cgit v1.2.3 From fb8d2649e17c94458db247925b796f5ae2141b0d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 1 Sep 2021 22:01:42 +0200 Subject: Fix saving and fetching roles from Concerts table. --- includes/concert.php | 42 ++++++++++++++++++++---------------------- tests/ConcertTest.php | 23 ++++------------------- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index db8d68a..53cd00e 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -76,7 +76,12 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $results = $wpdb->get_results($query); - return $results ? new GiglogAdmin_Concert($results[0]) : NULL; + if ( !$results ) { + $wpdb->print_error( __METHOD__ ); + return null; + } + + return new GiglogAdmin_Concert($results[0]); } public static function create(string $name, $venue, string $date, string $ticketlink, string $eventlink): ?self @@ -202,34 +207,27 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { global $wpdb; - 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 ) ); + $columns = [ + 'wpgconcert_name' => $this->cname, + 'venue' => $this->venue->id(), + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink, + 'wpgconcert_status' => $this->status, + 'wpgconcert_roles' => wp_json_encode( $this->roles ), + ]; + if ( $this->id !== NULL ) { + $res = $wpdb->update( 'wpg_concerts', $columns, [ '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, - )); + $res = $wpdb->insert('wpg_concerts', $columns); } if ( $res === false ) { $wpdb->print_error( __METHOD__ ); } - else { + elseif ( $this->id === NULL ) { $this->id = $wpdb->insert_id; } } @@ -275,7 +273,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return $this->roles; } - public function assign_role( int $role, string $username ) : void + public function assign_role( string $role, string $username ) : void { $this->roles[$role] = $username; } diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 0bf345c..5e6ebde 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -135,27 +135,12 @@ final class ConcertTest extends WP_UnitTestCase public function testAssignConcertRoles() : 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"); - - $gig->assign_role( GiglogAdmin_Roles::PHOTO1, 'user1' ); - $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $gig->roles() ); - + $gig = GiglogAdmin_Concert::get(self::$concerts[0]->id()); + $gig->assign_role( 'photo1' , 'user1' ); $gig->save(); - var_dump($gig); - - $fetched_gig = GiglogAdmin_Concert::get( $gig->id() ); - global $wpdb; - $wpdb->print_error(); - $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $fetched_gig->roles() ); + $fetched_gig = GiglogAdmin_Concert::get( self::$concerts[0]->id() ); + $this->assertEquals( [ 'photo1' => 'user1' ], $fetched_gig->roles() ); } public function testOnlyFetchConcertsFromGivenCity() : void -- cgit v1.2.3 From 9c4793526c46deed2b20f1a1ed494d69c4f58e97 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Sep 2021 08:50:08 +0200 Subject: psalm: No need for null $opts param in select_field. It's initialized to an empty array if not specified, that should be good enough, and don't trip up iterating over it. --- includes/view-helpers/select_field.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php index 816f8ef..e72fc40 100644 --- a/includes/view-helpers/select_field.php +++ b/includes/view-helpers/select_field.php @@ -19,7 +19,7 @@ namespace EternalTerror\ViewHelpers; */ function select_field( string $name, - ?array $opts = [], + array $opts = [], $selected = null, string $blank = "Please select...") : string { -- cgit v1.2.3 From ecd1eb7848d7657267a55cbee03eb2f90a1ba1b7 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Sep 2021 08:54:58 +0200 Subject: psalm: Ad types to attrs and constructor for Venue --- includes/venue.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/venue.php b/includes/venue.php index aedb6a7..6de14c6 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -7,11 +7,11 @@ if ( !class_exists('GiglogAdmin_Venue') ) { class GiglogAdmin_Venue { - private $id; - private $name; - private $city; - private $address; - private $webpage; + private int $id; + private string $name; + private string $city; + private string $address; + private string $webpage; /* * Constructs a new venue object from an array of attributes. @@ -19,7 +19,7 @@ if ( !class_exists('GiglogAdmin_Venue') ) { * so this constructor can be used to construct the object * directly from the database row. */ - public function __construct($attrs) + public function __construct(object $attrs) { $this->id = isset($attrs->id) ? $attrs->id : NULL; $this->name = isset($attrs->wpgvenue_name) ? $attrs->wpgvenue_name : NULL; -- cgit v1.2.3 From 3668e685566bed2d3623a3cd276d93f79319a008 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Sep 2021 09:10:19 +0200 Subject: psalm: Add type info to attrs and constructor for Concert. Also make sure we explicitly set the venue attribute in the constructor. --- includes/concert.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index 53cd00e..cc1efaa 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -5,17 +5,19 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later +require_once __DIR__ . '/venue.php'; + if ( !class_exists('GiglogAdmin_Concert') ) { require_once __DIR__ . '/venue.php'; class GiglogAdmin_Concert { - private $id; - private $cname; - private $venue; - private $cdate; - private $tickets; - private $eventlink; + private int $id; + private string $cname; + private ?GiglogAdmin_Venue $venue; + private string $cdate; + private string $tickets; + private string $eventlink; private int $status; private array $roles; @@ -32,7 +34,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { * so this constructor can be used to construct the object * directly from the database row. */ - public function __construct($attrs = []) + public function __construct(object $attrs) { $this->id = isset($attrs->id) ? $attrs->id : NULL; $this->cname = isset($attrs->wpgconcert_name) ? $attrs->wpgconcert_name : NULL; @@ -56,6 +58,9 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $this->venue = GiglogAdmin_Venue::get($attrs->venue); } } + else { + $this->venue = NULL; + } } @@ -268,7 +273,12 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $this->status = $new_status; } - public function roles() + /** + * Return the roles defined for this concert. + * + * @return array + */ + public function roles() : array { return $this->roles; } -- cgit v1.2.3 From 52980d074809a18d150a88313ab86054870c416e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Sep 2021 09:20:44 +0200 Subject: Fix more type issues in Concert and Venue classes --- includes/concert.php | 2 +- includes/venue.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index cc1efaa..f2f5535 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -12,7 +12,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { class GiglogAdmin_Concert { - private int $id; + private ?int $id; private string $cname; private ?GiglogAdmin_Venue $venue; private string $cdate; diff --git a/includes/venue.php b/includes/venue.php index 6de14c6..541680f 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -7,11 +7,11 @@ if ( !class_exists('GiglogAdmin_Venue') ) { class GiglogAdmin_Venue { - private int $id; - private string $name; - private string $city; - private string $address; - private string $webpage; + private ?int $id; + private ?string $name; + private ?string $city; + private ?string $address; + private ?string $webpage; /* * Constructs a new venue object from an array of attributes. -- cgit v1.2.3 From dbdcf4c486fb526861d040ed482d2dc4cdb06969 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Sep 2021 09:52:38 +0200 Subject: Make name and city required attributes for Venues. It does not make sense to have anonymous venues nowhere. --- includes/venue.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/includes/venue.php b/includes/venue.php index 541680f..3ad0f06 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -8,8 +8,16 @@ if ( !class_exists('GiglogAdmin_Venue') ) { class GiglogAdmin_Venue { private ?int $id; - private ?string $name; - private ?string $city; + + /** + * @psalm-suppress PropertyNotSetInConstructor + */ + private string $name; + + /** + * @psalm-suppress PropertyNotSetInConstructor + */ + private string $city; private ?string $address; private ?string $webpage; @@ -22,8 +30,16 @@ if ( !class_exists('GiglogAdmin_Venue') ) { public function __construct(object $attrs) { $this->id = isset($attrs->id) ? $attrs->id : NULL; - $this->name = isset($attrs->wpgvenue_name) ? $attrs->wpgvenue_name : NULL; - $this->city = isset($attrs->wpgvenue_city) ? $attrs->wpgvenue_city : 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; } @@ -108,8 +124,7 @@ if ( !class_exists('GiglogAdmin_Venue') ) { static function venues_in_city(string $city): array { global $wpdb; - $q = $wpdb->prepare( - "select id, wpgvenue_name from wpg_venues where wpgvenue_city=%s", $city); + $q = $wpdb->prepare("select * from wpg_venues where wpgvenue_city=%s", $city); $results = $wpdb->get_results($q); return array_map(function ($r) { return new GiglogAdmin_Venue($r); }, $results); -- cgit v1.2.3 From 18edd0852b02a2027f24e6644e81bf111371c26f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 09:14:20 +0200 Subject: Allow empty concerts to be created. --- includes/concert.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index f2f5535..26722ed 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -13,12 +13,12 @@ if ( !class_exists('GiglogAdmin_Concert') ) { class GiglogAdmin_Concert { private ?int $id; - private string $cname; + private ?string $cname; private ?GiglogAdmin_Venue $venue; - private string $cdate; - private string $tickets; - private string $eventlink; - private int $status; + private ?string $cdate; + private ?string $tickets; + private ?string $eventlink; + private ?int $status; private array $roles; public const STATUS_NONE = 1; -- cgit v1.2.3 From 742ef8ee58b5b2dd59e17648f31f2f8ed9d4f3d0 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 09:14:56 +0200 Subject: emove dependency on ConcertLogs in gig import code. --- includes/admin/views/giglog_import_gigs.php | 2 -- includes/concert.php | 11 ----------- 2 files changed, 13 deletions(-) diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index aeaf974..4bd59da 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -6,9 +6,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { - // require_once __DIR__ . '/../../band.php'; require_once __DIR__ . '/../../concert.php'; - require_once __DIR__ . '/../../concertlogs.php'; require_once __DIR__ . '/../../venue.php'; class GiglogAdmin_ImportGigsPage { diff --git a/includes/concert.php b/includes/concert.php index 26722ed..61959b2 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -118,17 +118,6 @@ if ( !class_exists('GiglogAdmin_Concert') ) { . ', Ticket LINK ' . $ticketlink . ', Event LINK ' . $eventlink); - GiglogAdmin_Concertlogs::add( $concert->id() ); - /*the last line can be replaced by a trigger - CREATE TRIGGER `insertIntoPhotoLogs` AFTER INSERT ON `wpg_concerts` - FOR EACH ROW INSERT INTO wpg_concertlogs ( - wpg_concertlogs.id, - wpg_concertlogs.wpgcl_concertid, - wpg_concertlogs.wpgcl_status) - - VALUES - (null, new.id, 1) - */ return $concert; } } -- cgit v1.2.3 From 74b06505888d69602b32d792ba4a815cd604ae41 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 09:15:46 +0200 Subject: Redo concert list to use the Concert class. Instead of directly accessing the database with a custom query, we now just use the Concert::find_concerts method to fetch the concerts that are to be displayed. This became much easier now that we don't rely on the extra concertlogs table. There's still stuff missing to be functionally equivalent to the old code, but this should be a lot easier to get in place now. --- includes/admin/views/giglog_admin_page.php | 70 ++++++++++++++---------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 6c1f059..5a16b66 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -115,7 +115,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { 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::get($cid); else - $c = new GiglogAdmin_Concert(); + $c = new GiglogAdmin_Concert((object)[]); $content='

Form to create/edit concerts and venues


'; $content.='
' @@ -204,63 +204,58 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content .= 'AdminOptions'; $content .= ''; + $filter = []; + // Use the submitted "city" if any. Otherwise, use the default/static value. $cty = filter_input( INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS ); - $cty = $cty ? $cty: 'ALL'; + if ($cty) $filter['city'] = $cty; $venue = filter_input( INPUT_POST, 'selectvenue', FILTER_SANITIZE_SPECIAL_CHARS ); - $venue = $venue ? $venue : '0'; + if ($venue) $filter['venue_id'] = $venue; + $concerts = GiglogAdmin_Concert::find_concerts($filter); - $query = "SELECT wpgc.id, wpgconcert_name, wpgv.wpgvenue_name as venue, wpgc.wpgconcert_date, wpgc.wpgconcert_tickets, wpgc.wpgconcert_event, wpgv.wpgvenue_city, wpgv.wpgvenue_webpage, wpgps.wpgs_name - FROM wpg_concerts wpgc, wpg_venues wpgv, wpg_pressstatus wpgps, wpg_concertlogs wpgcl - where wpgc.venue = wpgv.id - and wpgconcert_date >= CURDATE() - and wpgps.id = wpgcl.wpgcl_status - and wpgcl.wpgcl_concertid=wpgc.id"; - - $query .= ($cty == "ALL") ? "" : " and wpgv.wpgvenue_city='" .$cty ."'"; - $query .= ($venue == "0") ? "" : " and wpgv.id='" .$venue ."'"; - $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 = ''; - foreach ( $results AS $row ) { + foreach ( $concerts AS $concert ) { $content .= ''; - if($lastType != '' && $lastType != $row->wpgvenue_city) { - $content .= ''.$row->wpgvenue_city.''; + if ($lastType != '' && $lastType != $concert->venue()->city()) { + $content .= '' . $concert->city() . ''; } if ($lastType == '' ) { - $content .= ''.$row->wpgvenue_city.''; + $content .= '' . $concert->venue()->city() . ''; } // Modify these to match the database structure // $content .= '' . $row->id. ''; $content .= ''; - $content .= '' . $row->wpgconcert_name. ''; - $content .= '' . $row->venue. ''; - $fdate = strtotime($row->wpgconcert_date); + $content .= '' . $concert->cname() . ''; + $content .= '' . $concert->venue()->name() . ''; + $fdate = strtotime($concert->cdate()); $newformat = date('d.M.Y',$fdate); //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); - $content .= '' .$newformat. ''; - $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 .= - '' - . $this->adminactions($row->id) - . ''; - } + $content .= '' . $newformat . ''; + $content .= '' . /* $concert->isnew() */ '' . ''; + + $roles = $concert->roles(); + + $content .= '' . (array_key_exists('photo1', $roles) ? $roles['photo1'] : '') . ''; + $content .= '' . (array_key_exists('photo1', $roles) ? $roles['photo2'] : '') . ''; + $content .= '' . (array_key_exists('rev1', $roles) ? $roles['rev1'] : '') . ''; + $content .= '' . (array_key_exists('rev2', $roles) ? $roles['rev2'] : '') . ''; + + $content .= '' . $concert->status() . ''; + + // if (current_user_can('administrator')) { + // $content .= + // '' + // . $this->adminactions($row->id) + // . ''; + // } $content .= ''; - $lastType = $row->wpgvenue_city; + $lastType = $concert->venue()->city(); } $content .= ''; @@ -395,7 +390,6 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return null; } - $cl = GiglogAdmin_Concertlogs::get($c); $role = $cl->get_assigned_role( $this->username ); $assigned_user = $cl->assigned_user( $p1 ); -- cgit v1.2.3 From ce7584bd6362d4eed9160c361498e624aede6a06 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 11:40:59 +0200 Subject: Render adminactions in concert list view. Clicking the buttons don't work quite yet. --- includes/admin/views/giglog_admin_page.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 5a16b66..3fe3237 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -150,7 +150,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return $content; } - private function adminactions( int $concert_id ) : string + private function adminactions( GiglogAdmin_Concert $concert ) : string { global $wpdb; $query = "SELECT id,wpgs_name from wpg_pressstatus" ; @@ -158,11 +158,11 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return '' - . '' + . '' . \EternalTerror\ViewHelpers\select_field( 'selectstatus', array_map(fn($status) => [ $status->id, $status->wpgs_name ], $statuses), - GiglogAdmin_Concertlogs::get_status($concert_id)) + $concert->status()) . '' . '' . ''; @@ -248,12 +248,12 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content .= '' . $concert->status() . ''; - // if (current_user_can('administrator')) { - // $content .= - // '' - // . $this->adminactions($row->id) - // . ''; - // } + if (current_user_can('administrator')) { + $content .= + '' + . $this->adminactions($concert) + . ''; + } $content .= ''; $lastType = $concert->venue()->city(); } -- cgit v1.2.3 From 92ee799aab8d9ee41564b346f3a89bb688cf53bd Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 13:08:07 +0200 Subject: Shift value of Concert statuses to be 0-based. This will trip up any existing records in the db, but that should not matter, since we're changing how this entire stuff works now. --- includes/concert.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index 61959b2..d6fe7c4 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -21,12 +21,12 @@ if ( !class_exists('GiglogAdmin_Concert') ) { 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; + public const STATUS_NONE = 0; + public const STATUS_ACCRED_REQ = 1; + public const STATUS_PHOTO_APPROVED = 2; + public const STATUS_TEXT_APPROVED = 3; + public const STATUS_ALL_APPROVED = 4; + public const STATUS_REJECTED = 5; /* * Constructs a new concert object from an array of attributes. @@ -41,7 +41,7 @@ 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->status = isset($attrs->wpgconcert_status) ? $attrs->wpgconcert_status : 0; $this->roles = isset($attrs->wpgconcert_roles) ? json_decode($attrs->wpgconcert_roles, true) : []; if ( isset( $attrs->venue ) ) { -- cgit v1.2.3 From c09de43860ac6d57c6af31c971424e9c957e75c2 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 13:12:43 +0200 Subject: Drop query to press status table in adminactions. Replace the table with hardcoded strings in the AdminPage class. This makes it a pure presentation issue, while the statuses themselves are just mnemonics. There's one smell here, and that is that the status values and their textual representation is split across two modules. (Values in Concert, and textual representation in AdminPage.) This should probably be addressed later by refactoring both into a separate AccredStatus class or something. --- includes/admin/views/giglog_admin_page.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 3fe3237..7490122 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -11,6 +11,15 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { class GiglogAdmin_AdminPage { private string $username; + const STATUS_LABELS = [ + '', + 'Accred Requested', + 'Photo Approved', + 'Text Approved', + 'Photo and Text Approved', + 'Rejected' + ]; + public function __construct() { $this->username = wp_get_current_user()->user_login; @@ -152,16 +161,12 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { private function adminactions( GiglogAdmin_Concert $concert ) : string { - global $wpdb; - $query = "SELECT id,wpgs_name from wpg_pressstatus" ; - $statuses = $wpdb->get_results($query); - return '
' . '' . \EternalTerror\ViewHelpers\select_field( 'selectstatus', - array_map(fn($status) => [ $status->id, $status->wpgs_name ], $statuses), + array_map(fn($i) => [ $i, self::STATUS_LABELS[$i] ], range(1, count(self::STATUS_LABELS) - 1)), $concert->status()) . '' . '' @@ -246,7 +251,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content .= '' . (array_key_exists('rev1', $roles) ? $roles['rev1'] : '') . ''; $content .= '' . (array_key_exists('rev2', $roles) ? $roles['rev2'] : '') . ''; - $content .= '' . $concert->status() . ''; + $content .= '' . self::STATUS_LABELS[$concert->status()] . ''; if (current_user_can('administrator')) { $content .= -- cgit v1.2.3 From 2ca81303a9b892e179a790dc9e4ed14c08307610 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 14:37:37 +0200 Subject: Drop ref to ConcertLogs in AdminPage::get_user method. --- includes/admin/views/giglog_admin_page.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 7490122..0db72b2 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -68,16 +68,18 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } - private function get_user( ?int $cid, string $ctype): string + private function get_user( GiglogAdmin_Concert $concert, string $role): string { $users = array_map( fn($usr) => $usr->user_login, get_users( array( 'fields' => array( 'user_login' ) ) ) ); - $current_user = $cid ? GiglogAdmin_Concertlogs::get_assigned_user( $cid, $ctype ) : null; + $roles = $concert->roles(); + + $current_user = array_key_exists($role, $roles) ? $roles[$role] : NULL; return \EternalTerror\ViewHelpers\select_field( - $ctype, + $role, array_map( fn($user) => [ $user, $user ], $users ), $current_user); } @@ -143,11 +145,12 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content.='

'; $content.='
'; + $content.='
ASSIGNMENT DETAILS

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

' -- cgit v1.2.3 From 60af9af46e4c80db1bd41d77991e2cf24ed69641 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 15:04:58 +0200 Subject: Rename AdminPage::get_user -> user_dropdown_for_role The original name did not make much sense. The function didn't return a user, but a dropdown list of users, where the user currently holding the given role for the given concert was preselected in the list. --- includes/admin/views/giglog_admin_page.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 0db72b2..248f175 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -68,7 +68,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } - private function get_user( GiglogAdmin_Concert $concert, string $role): string + private function user_dropdown_for_role( GiglogAdmin_Concert $concert, string $role): string { $users = array_map( fn($usr) => $usr->user_login, @@ -147,10 +147,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content.='
'; $content.='
ASSIGNMENT DETAILS

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

' -- cgit v1.2.3 From c9d80308ccb63a74f7dab723dd4e73a67b7fb597 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 15:07:40 +0200 Subject: Handle updating concert status again. --- includes/admin/views/giglog_admin_page.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 248f175..4cb7dad 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -305,13 +305,14 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { header("Refresh: 1; URL=$url3"); //reload page } - //handling the admin drop down menu - if(isset($_POST['selectstatus']) && (isset($_POST['edit']) && $_POST['edit']!="EDIT") && !empty($_POST['cid'])) + // handle the status drop down + if (isset($_POST['selectstatus']) && !empty($_POST['selectstatus']) && !empty($_POST['cid'])) { - $usql = "UPDATE wpg_concertlogs SET wpgcl_status=".$_POST['selectstatus']." WHERE wpgcl_concertid=".$_POST['cid']; - $uresults = $wpdb->get_results($usql); - //$url2=$_SERVER['REQUEST_URI']; //doesn't seem to be needed actually, leaving here just in case - //header("Refresh: 1; URL=$url2"); //reload page + if ($_POST['selectstatus'] > 0 && $_POST['selectstatus'] < count(self::STATUS_LABELS)) { + $concert = GiglogAdmin_Concert::get(intval($_POST['cid'])); + $concert->set_status(intval($_POST['selectstatus'])); + $concert->save(); + } } if(isset($_POST['newconcert'])) { -- cgit v1.2.3 From 74d39366640683914ab44db039ab48959c7dc91b Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Sep 2021 19:32:28 +0200 Subject: Editing concerts now work again. --- includes/admin/views/giglog_admin_page.php | 32 ++++++++++++----- includes/concert.php | 55 +++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 4cb7dad..56376a8 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -331,15 +331,31 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { if(isset($_POST['editconcert'])) { - IF (empty($_POST['cname']) || empty($_POST['selectvenueadmin']) || empty($_POST['cdate']) || empty($_POST['ticket']) || empty($_POST['eventurl'])) - echo ''; - else - { - GiglogAdmin_Concert::update_concert($_POST['pid'],$_POST['cname'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); - GiglogAdmin_Concertlogs::update($_POST['pid'],$_POST['photo1'], $_POST['photo2'], $_POST['rev1'], $_POST['rev2']); - echo ''; + $roles = array_reduce( + ['photo1', 'photo1', 'rev1', 'rev2'], + function($roles, $r) { + if (isset($_POST[$r])) { + $roles[$r] = sanitize_user($_POST[$r]); + } + return $roles; + }, + [] + ); + + $attributes = [ + 'wpgconcert_name' => sanitize_text_field($_POST['cname']), + 'venue' => intval($_POST['selectvenueadmin']), + 'wpgconcert_date' => sanitize_text_field($_POST['cdate']), + 'wpgconcert_ticket' => esc_url_raw($_POST['ticket']), + 'wpgconcert_event' => esc_url_raw($_POST['eventurl']), + 'wpgconcert_roles' => $roles, + ]; + + $concert = GiglogAdmin_Concert::get(intval($_POST['pid'])); + if ($concert->update((object) $attributes)) { + // let user know the concert was updated. + // Look into admin_notices } - } diff --git a/includes/concert.php b/includes/concert.php index d6fe7c4..f0c69c8 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -123,27 +123,50 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public static function update_concert($id, $cname, $venue, $cdate, $ticketlink, $eventlink) + public function update(object $attrs) : bool { - global $wpdb; + $need_update = false; - $res = $wpdb->update('wpg_concerts', array( - 'wpgconcert_name' => $cname, - 'venue' => $venue, - 'wpgconcert_date' => $cdate, - 'wpgconcert_tickets' => $ticketlink, - 'wpgconcert_event' => $eventlink - ), - array('id' => $id) - ); + if (isset($attrs->wpgconcert_name) && $attrs->wpgconcert_name != $this->cname) { + $this->cname = $attrs->wpgconcert_name; + $need_update = true; + } - if ( $res === false ) { - // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed - error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); - die; + if (isset($attrs->wpgconcert_date) && $attrs->wpgconcert_date != $this->cdate) { + $this->cdate = $attrs->wpgconcert_date; + $need_update = true; + } + + if (isset($attrs->wpgconcert_tickets) && $attrs->wpgconcert_tickets != $this->tickets) { + $this->tickets = $attrs->wpgconcert_tickets; + $need_update = true; + } + + if (isset($attrs->wpgconcert_event) && $attrs->wpgconcert_event != $this->eventlink) { + $this->eventling = $attrs->wpgconcert_eventlink; + $need_update = true; + } + + if (isset($attrs->wpgconcert_status) && $attrs->wpgconcert_status != $this->status) { + $this->status = $attrs->wpgconcert_status; + $need_update = true; + } + + if (isset($attrs->wpgconcert_roles) && $attrs->wpgconcert_roles != $this->roles) { + $this->roles = $attrs->wpgconcert_roles; + $need_update = true; + } + + if (isset($attrs->venue) && $attrs->venue != $this->venue()->id()) { + $this->venue = GiglogAdmin_Venue::get($attrs->venue); + $need_update = true; + } + + if ($need_update) { + $this->save(); } - return ($wpdb->last_error); + return $need_update; } public static function find($cname, $venue, $date) -- cgit v1.2.3 From c5e16119e6315ba1e05f85103206238898e50803 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 10:35:30 +0200 Subject: Make click to assign concert/role to self work again. --- includes/admin/views/giglog_admin_page.php | 54 ++++++++++++++---------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 56376a8..dd21a32 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -249,10 +249,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $roles = $concert->roles(); - $content .= '' . (array_key_exists('photo1', $roles) ? $roles['photo1'] : '') . ''; - $content .= '' . (array_key_exists('photo1', $roles) ? $roles['photo2'] : '') . ''; - $content .= '' . (array_key_exists('rev1', $roles) ? $roles['rev1'] : '') . ''; - $content .= '' . (array_key_exists('rev2', $roles) ? $roles['rev2'] : '') . ''; + $content .= '' . $this->returnuser('photo1', $concert) . ''; + $content .= '' . $this->returnuser('photo2', $concert) . ''; + $content .= '' . $this->returnuser('rev1', $concert) . ''; + $content .= '' . $this->returnuser('rev2', $concert) . ''; $content .= '' . self::STATUS_LABELS[$concert->status()] . ''; @@ -291,7 +291,12 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { if(isset($_POST['assignitem'])) { - GiglogAdmin_AdminPage::assignconcert($_POST['pid'],$_POST['cid']); + $concert = GiglogAdmin_Concert::get(intval($_POST['cid'])); + $role = sanitize_text_field($_POST['pid']); + + if ($concert) { + GiglogAdmin_AdminPage::assignconcert($role, $concert); + } $url2=$_SERVER['REQUEST_URI']; header("Refresh: 1; URL=$url2"); //reload page @@ -371,22 +376,17 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } } - static function assignconcert($p1, $c): void + static function assignconcert($p1, GiglogAdmin_Concert $concert): void { - global $wpdb; + $username = wp_get_current_user()->user_login; + $concert->assign_role($p1, $username); + $concert->save(); $to = 'live@eternal-terror.com'; - $subject = $this->username.' has taken '.$p1. 'for a concert with id '.$c; + $subject = $username.' has taken '.$p1. 'for a concert with id '.$concert->id(); $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); - $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' => $this->username, - 'action' => 'assigned '.$p1, - 'concertid' => $c)); - echo ($wpdb->last_error ); + wp_mail( $to, $subject, $body, $headers ); } @@ -409,20 +409,16 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { wp_mail( $to, $subject, $body, $headers ); } - private function returnuser(string $p1, ?int $c) : ?string + private function returnuser(string $role, GiglogAdmin_Concert $concert) : ?string { - if (!$c) { - return null; - } - - $role = $cl->get_assigned_role( $this->username ); - $assigned_user = $cl->assigned_user( $p1 ); + $roles = $concert->roles(); + $assigned_user = array_key_exists($role, $roles) ? $roles[$role] : NULL; //first check if current slot is taken by current user - if ( $role == $p1 ) { + if ( $assigned_user == $this->username ) { $f = '' - . ' ' - . ' ' + . ' ' + . ' ' . ' ' . ''; } @@ -430,14 +426,14 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $f = 'Taken' . '
Taken by ' . $assigned_user . '
'; } - elseif ( $role ) { + elseif ( array_search($this->username, $roles) ) { // other slots for this concert are taken by user $f = '-'; } else { //not taken by anyone $f = '
' - . ' ' - . ' ' + . ' ' + . ' ' . ' ' . '
'; } -- cgit v1.2.3 From 4f7cfd164d8200d586334fdc5d6eada2a219565e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 10:40:08 +0200 Subject: Rename AdminPage::returnuser to assign_role_for_user_form. It did not return any users, but a form so name it for what it does. --- includes/admin/views/giglog_admin_page.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index dd21a32..7c77b2a 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -249,10 +249,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $roles = $concert->roles(); - $content .= '' . $this->returnuser('photo1', $concert) . ''; - $content .= '' . $this->returnuser('photo2', $concert) . ''; - $content .= '' . $this->returnuser('rev1', $concert) . ''; - $content .= '' . $this->returnuser('rev2', $concert) . ''; + $content .= '' . $this->assign_role_for_user_form('photo1', $concert) . ''; + $content .= '' . $this->assign_role_for_user_form('photo2', $concert) . ''; + $content .= '' . $this->assign_role_for_user_form('rev1', $concert) . ''; + $content .= '' . $this->assign_role_for_user_form('rev2', $concert) . ''; $content .= '' . self::STATUS_LABELS[$concert->status()] . ''; @@ -409,7 +409,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { wp_mail( $to, $subject, $body, $headers ); } - private function returnuser(string $role, GiglogAdmin_Concert $concert) : ?string + private function assign_role_for_user_form(string $role, GiglogAdmin_Concert $concert) : ?string { $roles = $concert->roles(); $assigned_user = array_key_exists($role, $roles) ? $roles[$role] : NULL; -- cgit v1.2.3 From f57acecbae89a2782acf6f6b539025385c198366 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 10:58:07 +0200 Subject: Make click to unassign from concert work again. As a user can only be assigned to one role at the time, we remove the current user from any role that they may have when clearing the assignment. --- includes/admin/views/giglog_admin_page.php | 28 +++++++++++++--------------- includes/concert.php | 5 +++++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 7c77b2a..0f8fe76 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -136,7 +136,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { .'' . $this->get_venue_selector($c->venue()) . '
' .'
' .'
' - .'
' + .'
' .''; // actions differ if we update or create a concert, hence two buttons needed if ($editing) @@ -304,7 +304,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { if(isset($_POST['unassignitem'])) { - GiglogAdmin_AdminPage::unassignconcert($_POST['pid'],$_POST['cid']); + $concert = GiglogAdmin_Concert::get(intval($_POST['cid'])); + $role = sanitize_text_field($_POST['pid']); + + GiglogAdmin_AdminPage::unassignconcert($role, $concert); $url3=$_SERVER['REQUEST_URI']; header("Refresh: 1; URL=$url3"); //reload page @@ -390,22 +393,17 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { wp_mail( $to, $subject, $body, $headers ); } - static function unassignconcert($p1, $c): void + static function unassignconcert($p1, GiglogAdmin_Concert $concert): void { - global $wpdb; + $username = wp_get_current_user()->user_login; + $concert->remove_user_from_roles($username); + $concert->save(); $to = 'live@eternal-terror.com'; - $subject = $this->username.' has UNASSINED '.$p1. 'for a concert with id '.$c; + $subject = $username.' has UNASSINED '.$p1. 'for a concert with id '.$concert->id(); $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' => $this->username, - 'action' => 'unassigned '.$p1, - 'concertid' => $c)); - echo ($wpdb->last_error ); + wp_mail( $to, $subject, $body, $headers ); } @@ -417,8 +415,8 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { //first check if current slot is taken by current user if ( $assigned_user == $this->username ) { $f = '
' - . ' ' - . ' ' + . ' ' + . ' ' . ' ' . '
'; } diff --git a/includes/concert.php b/includes/concert.php index f0c69c8..dce15f0 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -299,6 +299,11 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { $this->roles[$role] = $username; } + + public function remove_user_from_roles( string $username ) : void + { + $this->roles = array_filter($this->roles, fn($u) => $u != $username); + } } } ?> -- cgit v1.2.3 From 82fff1107a46f9cdaab589e619b03a3c76431c4f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 14:45:57 +0200 Subject: Update db tables if needed on every load. No need to deactivate/activate plugin to get the right version of the tables. --- giglogadmin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/giglogadmin.php b/giglogadmin.php index dec54a7..2db1f3b 100644 --- a/giglogadmin.php +++ b/giglogadmin.php @@ -24,6 +24,8 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { + + require_once __DIR__ . '/includes/admin/register_db_tables.php'; require_once __DIR__ . '/includes/public/shortcodes/giglog_bands.php'; require_once __DIR__ . '/includes/public/shortcodes/giglog_display_unprocessed.php'; require_once __DIR__ . '/includes/public/shortcodes/giglog_photographers.php'; @@ -52,7 +54,6 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { } static function activate(): void { - require_once __DIR__ . '/includes/admin/register_db_tables.php'; } static function deactivate(): void { -- cgit v1.2.3 From 64f4141378ea6fb524df015abdef5393d8d589bb Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 14:47:03 +0200 Subject: Add timestamp columns to concerts and venues. To keep track of creation and modification times for each record. --- includes/admin/register_db_tables.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/includes/admin/register_db_tables.php b/includes/admin/register_db_tables.php index 64123d9..8c6dc98 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 == 6) { + if ($db_version == 8) { return; } @@ -459,7 +459,22 @@ if ( !function_exists( "giglog_register_db_tables") ) wpgconcert_roles JSON CHECK (JSON_VALID(wpgconcert_roles)))"); } - update_option("giglogadmin_db_version", 6); + if ($db_version == NULL || $db_version < 8) + { + // Add automatically updated `created` and `updated` colomns to keep + // track of creation time and modification times for a record. + $wpdb->query( + "ALTER TABLE `wpg_concerts` ADD COLUMN IF NOT ExISTS ( + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)"); + + $wpdb->query( + "ALTER TABLE `wpg_venues` ADD COLUMN IF NOT ExISTS ( + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)"); + } + + update_option("giglogadmin_db_version", 8); } giglog_register_db_tables(); -- cgit v1.2.3 From 57c9ed78112fb33f8a552e9b76d4fedac14f08d9 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 15:25:13 +0200 Subject: Mark recently added concerts as new again. --- includes/admin/views/giglog_admin_page.php | 26 +++++++++----------------- includes/concert.php | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 0f8fe76..447dcb2 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -180,27 +180,21 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { /** * @return null|string */ - private function getpublishstatus(int $concert_id) + private function getpublishstatus(GiglogAdmin_Concert $concert) : string { - global $wpdb; - $date1 = new DateTime("now"); - $dsql = "select wpgcl_createddate from wpg_concertlogs where wpgcl_concertid=".$concert_id; - $results = $wpdb->get_results($dsql); - foreach ( $results AS $row ) { - //$x = strtotime($row -> filedate); - $x= date('Y-m-d H:i:s', strtotime($row -> wpgcl_createddate)); - $date2 = new DateTime($x, new DateTimeZone('Europe/London')); - $dd = $date2 -> diff($date1) ->format("%a"); + $now = new DateTime(); + $new_entry = $now->diff($concert->created())->days <= 10; + if ($new_entry) { + return 'NEW'; + } + else { + return ''; } - - if ($dd <= 10) return ('NEW'); } private function get_concerts(): string { - global $wpdb; - $content = ''; // $content .= ''; @@ -245,9 +239,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); $content .= ''; - $content .= ''; - - $roles = $concert->roles(); + $content .= ''; $content .= ''; $content .= ''; diff --git a/includes/concert.php b/includes/concert.php index dce15f0..c0c13ee 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -20,6 +20,8 @@ if ( !class_exists('GiglogAdmin_Concert') ) { private ?string $eventlink; private ?int $status; private array $roles; + private ?DateTimeImmutable $created; + private ?DateTimeImmutable $updated; public const STATUS_NONE = 0; public const STATUS_ACCRED_REQ = 1; @@ -43,6 +45,8 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $this->eventlink = isset($attrs->wpgconcert_event) ? $attrs->wpgconcert_event : NULL; $this->status = isset($attrs->wpgconcert_status) ? $attrs->wpgconcert_status : 0; $this->roles = isset($attrs->wpgconcert_roles) ? json_decode($attrs->wpgconcert_roles, true) : []; + $this->created = isset($attrs->created) ? new DateTimeImmutable($attrs->created) : NULL; + $this->updated = isset($attrs->updated) ? new DateTimeImmutable($attrs->updated) : NULL; if ( isset( $attrs->venue ) ) { if (isset($attrs->wpgvenue_name) && isset($attrs->wpgvenue_city)) { @@ -74,8 +78,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { global $wpdb; - $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' - . 'FROM wpg_concerts ' + $query = 'SELECT * FROM wpg_concerts ' . 'LEFT JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' . 'WHERE ' . $wpdb->prepare('wpg_concerts.id = %d', $id); @@ -197,8 +200,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { global $wpdb; - $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' - . 'FROM wpg_concerts ' + $query = 'SELECT * FROM wpg_concerts ' . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '; $where = []; @@ -304,6 +306,14 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { $this->roles = array_filter($this->roles, fn($u) => $u != $username); } + + public function created() : DateTimeImmutable { + return $this->created; + } + + public function updated() : DateTimeImmutable { + return $this->updated; + } } } ?> -- cgit v1.2.3 From 2b54c76bec008bed8b910183cf4e98795c7a446a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 16:07:48 +0200 Subject: Remove dangling $wpdb ref in AdminPage. AdminPage now references the database only through the Concert (and Venue) models. --- includes/admin/views/giglog_admin_page.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 447dcb2..dfea3fd 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -269,8 +269,6 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { */ static function update() { - global $wpdb; - if ('POST' !== $_SERVER['REQUEST_METHOD']) return; -- cgit v1.2.3 From 08f09666f7028463bfd9b69ac5fb9e8e06aa79ad Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 16:09:07 +0200 Subject: Remove obsolete shortcodes. These are no longer in use, and have been replaced by the admin screens. --- giglogadmin.php | 11 - includes/public/shortcodes/giglog_bands.php | 118 ------- .../shortcodes/giglog_display_unprocessed.php | 145 -------- .../public/shortcodes/giglog_photographers.php | 385 --------------------- .../public/shortcodes/giglog_process_files.php | 72 ---- 5 files changed, 731 deletions(-) delete mode 100644 includes/public/shortcodes/giglog_bands.php delete mode 100644 includes/public/shortcodes/giglog_display_unprocessed.php delete mode 100644 includes/public/shortcodes/giglog_photographers.php delete mode 100644 includes/public/shortcodes/giglog_process_files.php diff --git a/giglogadmin.php b/giglogadmin.php index 2db1f3b..e9adbe2 100644 --- a/giglogadmin.php +++ b/giglogadmin.php @@ -26,10 +26,6 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { require_once __DIR__ . '/includes/admin/register_db_tables.php'; - require_once __DIR__ . '/includes/public/shortcodes/giglog_bands.php'; - require_once __DIR__ . '/includes/public/shortcodes/giglog_display_unprocessed.php'; - require_once __DIR__ . '/includes/public/shortcodes/giglog_photographers.php'; - require_once __DIR__ . '/includes/public/shortcodes/giglog_process_files.php'; require_once __DIR__ . '/includes/admin/views/giglog_admin_page.php'; require_once __DIR__ . '/includes/admin/views/giglog_import_gigs.php'; require_once __DIR__ . '/includes/admin/helpfiles/instrunctions.php'; @@ -41,12 +37,6 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { class GiglogAdmin_Plugin { static public function init(): void { - add_shortcode('giglog_cities', 'giglogadmin_getfilters'); - add_shortcode('giglog_bands', 'giglogadmin_getconcerts'); - add_shortcode('giglog_unprocessed', 'giglogadmin_display_unprocessed'); - add_shortcode('giglog_upload', 'giglogadmin_upload_files'); - add_shortcode('giglog_photog', 'giglogadmin_photographers'); - add_action( 'admin_menu', array( 'GiglogAdmin_Plugin', 'add_admin_pages' )); add_action( 'admin_menu', array( 'GiglogAdmin_Plugin', 'add_help_pages' )); @@ -143,7 +133,6 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { register_activation_hook( __FILE__, array( 'GiglogAdmin_Plugin', 'activate' )); register_deactivation_hook( __FILE__, array( 'GiglogAdmin_Plugin', 'deactivate' )); - GiglogAdmin_Plugin::init(); } ?> diff --git a/includes/public/shortcodes/giglog_bands.php b/includes/public/shortcodes/giglog_bands.php deleted file mode 100644 index e4fe71d..0000000 --- a/includes/public/shortcodes/giglog_bands.php +++ /dev/null @@ -1,118 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -/* - * code used for giglogadmin for the open page where everyone sees the list of - * concerts. First function displays filters by city, venue and the second one - * builds the table with concerts - */ - -function giglogadmin_getfilters(): string -{ - global $wpdb; - - //echo (var_dump($_POST["selectvenue"])); - - $results = $wpdb->get_results('select distinct wpgvenue_city from wpg_venues'); - $select = '
'; - //second drop down for venue - - $vquery = "select id, wpgvenue_name from wpg_venues"; - $vquery .= " where wpgvenue_city='" . $_POST["selectcity"] . "'"; - $resultsv = $wpdb->get_results($vquery); - $select .= ''; - return $select; -} - - -function giglogadmin_getconcerts(): string -{ - global $wpdb; - // Shortcodes RETURN content, so store in a variable to return - $content = '
CITYIDBANDVENUEDATE
' . $newformat . '' . /* $concert->isnew() */ '' . '' . $this->getpublishstatus($concert) . '' . $this->assign_role_for_user_form('photo1', $concert) . '' . $this->assign_role_for_user_form('photo2', $concert) . '
'; - // $content .= ''; - $content .= ''; - // Use the submitted "city" if any. Otherwise, use the default/static value. - $cty = filter_input(INPUT_POST, 'selectcity'); - $cty = $cty ? $cty : 'ALL'; - - $venue = filter_input(INPUT_POST, 'selectvenue'); - //echo($_POST['selectvenue']); - $venue = $venue ? $venue : '0'; - - - $query = "SELECT wpgc.id, wpgc.wpgconcert_name ,wpgv.wpgvenue_name as venue ,wpgc.wpgconcert_date, wpgc.wpgconcert_tickets, wpgc.wpgconcert_event, wpgv.wpgvenue_city, wpgv.wpgvenue_webpage - FROM wpg_concerts wpgc, wpg_venues wpgv -where wpgc.venue = wpgv.id -and wpgconcert_date >= CURDATE()"; - $query .= ($cty == "ALL") ? "" : " and wpgv.wpgvenue_city='" . $cty . "'"; - $query .= ($venue == "0") ? "" : " and wpgv.id='" . $venue . "'"; - $query .= " order by wpgv.wpgvenue_city, wpgconcert_date, wpgc.id"; - //echo($query); - $results = $wpdb->get_results($query); - - - $lastType = ''; - foreach ($results AS $row) { - $content .= ''; - - if ($lastType != '' && $lastType != $row->wpgvenue_city) { - $content .= ''; - } - - if ($lastType == '') { - $content .= ''; - } - // Modify these to match the database structure - // $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $fdate = strtotime($row->wpgconcert_date); - $newformat = date('d.M.Y', $fdate); - - //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $lastType = $row->wpgvenue_city; - } - $content .= '
CITYIDBANDVENUEDATETICKETSEVENT
CITYTITLEVENUEDATETICKETSEVENT
' . $row->wpgvenue_city . '
' . $row->wpgvenue_city . '
' . $row->id. '' . $row->wpgconcert_name . '' . $row->venue . '' . $newformat . 'TicketsEvent link
'; - // return the table - return $content; -} diff --git a/includes/public/shortcodes/giglog_display_unprocessed.php b/includes/public/shortcodes/giglog_display_unprocessed.php deleted file mode 100644 index 90ef69f..0000000 --- a/includes/public/shortcodes/giglog_display_unprocessed.php +++ /dev/null @@ -1,145 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -/* - * I kinda overloaded this snippet. Added comments for each function. But this - * is used in the giglog admin page, which should only be available to admin - * users. After the file is being uploaded into the concertlists folder, its - * content is written into wpg_files. Then the content is split into lines and - * each line is transformed intoa concert - */ - -/* this checks th wpg_files table to see if any file is uploaded but hasn't - * gone through the processing process - aka fetching each line and - * transforming it into a concert line - */ -function giglogadmin_getunprocessed(): string -{ - global $wpdb; - - $content = '

UNPROCESSED ROWS

'; - $content .= ''; - $query = 'SELECT rowid,filename,filedate,rowcontent from wpg_files where processed="N"'; - $results = $wpdb->get_results($query); - foreach ($results AS $row) { - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - } - $content .= '
FilerowFILENAMEDATEUploadedContent
' . $row->rowid . '' . $row->filename . '' . $row->filedate . '' . $row->rowcontent . '
'; - return $content; -} - -/* function that goes through each line of the unprocessed file. Each line is - * checked against the concerts table. if it exists - concert and date and - * venue - it does nothing with it. If it doesn't exist, it checks if band or - * venue exists. If they don't, they get created, if they do, their ID from - * their table is fetchd and used in concerts table - */ -function giglogadmin_insertconcerts(): string -{ - global $wpdb; - $concertlist = '

Inserted the following:

'; - $newconcert= []; - $query1 = 'SELECT id,rowid,filename,filedate,rowcontent from wpg_files where processed="N"'; - $cresults = $wpdb->get_results($query1); - foreach ($cresults AS $row) { - $rowfileid = $row->id; - $resultArray = explode("\t", $row->rowcontent); - $cname = $resultArray[0]; - $venue = $resultArray[1]; - $condate = date('Y-m-d', strtotime($resultArray[2])); - $ticketlink = $resultArray[3]; - $eventlink = $resultArray[4]; - //first item in the row should be band $resultArray[0]; second should be venue $resultArray[1]; third should be concert date $resultArray[2]; - //fourth item is ticketlink $resultArray[3]; fifth item is eventlink $resultArray[4]; - - - - //processing venue - if (is_numeric($venue)) - $newconcert[1] = $venue; - else { - $venuesql = 'SELECT id FROM wpg_venues WHERE upper(wpgvenue_name)="' . $venue . '"'; - $results = $wpdb->get_results($venuesql); - if ($results) - $newconcert[1] = $results[0]->id; - else { - $wpdb->insert('wpg_venues', array( - 'id' => '', - 'wpgvenue_name' => $venue - )); - echo ($wpdb->last_error); - $newconcert[1] = $wpdb->insert_id; - } - } - //done processing venue - - //not sure how to check dates, hopefully manual verification of files will take care of it - - //check if concert already exists and return ID if it does. Not checking by date, to be considered - $csql = 'SELECT id from wpg_concerts where wpgconcert_name = ' . $cname . ' and venue = ' . $newconcert[1] . ' and wpgconcert_date ="' . $condate . '"'; - - $cresults = $wpdb->get_results($csql); - if ($cresults) { - $usql = 'UPDATE wpg_files SET processed="D", wpgc_id = ' . $cresults[0]->id . ' WHERE id = ' . $rowfileid; - - $uresults = $wpdb->get_results($usql); - $concertlist .= 'DUPLICATE ROW detected Title ' . $cname . ' with band ID ' . $newconcert[0]; - $concertlist .= ', VENUE ' . $venue . ' with venue ID ' . $newconcert[1]; - $concertlist .= ', CONCERTDATE ' . $condate; - $concertlist .= '
'; - } else { - $wpdb->insert('wpg_concerts', array( - 'id' => '', - 'wpgconcert_name' => cname, - 'venue' => $newconcert[1], - 'wpgconcert_date' => $condate, - 'wpgconcert_tickets' => $ticketlink, - 'wpgconcert_event' => $eventlink - )); - echo ($wpdb->last_error); - $newconcertid = $wpdb->insert_id; - - $usql = 'UPDATE wpg_files SET processed="Y", wpgc_id = ' . $newconcertid . ' WHERE id = ' . $rowfileid; - - $uresults = $wpdb->get_results($usql); - $concertlist .= 'name ' . $cname ; - $concertlist .= ', VENUE ' . $venue . ' with venue ID ' . $newconcert[1]; - $concertlist .= ', CONCERTDATE ' . $condate . ', Ticket LINK ' . $ticketlink . ', event LINK' . $eventlink; - $concertlist .= '
'; - - } - - //end check if concert exists - - - //remember to add the concert ID when displaying - - - } //end looping through unprocessed rows - - return $concertlist; -} - -function giglogadmin_display_unprocessed(): string { - $output = giglogadmin_getunprocessed(); - - $output .= '
'; - - - if (isset($_POST['ProcessConcerts'])) { - $output .= giglogadmin_insertconcerts(); - - //$url2 = $_SERVER['REQUEST_URI']; - //header("Refresh: 5; URL=$url2"); //reload page - } //end if button for process concerts is pressed - - return $output; -} diff --git a/includes/public/shortcodes/giglog_photographers.php b/includes/public/shortcodes/giglog_photographers.php deleted file mode 100644 index 829c53e..0000000 --- a/includes/public/shortcodes/giglog_photographers.php +++ /dev/null @@ -1,385 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -/* - * code used for giglogadmin for the page where users such as photographers/concert reviewers - * check what gigs are available and show interest for them - * Admin users also control concert statuses here - */ - -function giglogadmin_assignconcert($p1, $c): void -{ - 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; - $body = 'The email body content'; - $headers = array('Content-Type: text/html; charset=UTF-8'); - - - - if ($p1 == 'photo1') $usql = "UPDATE wpg_concertlogs SET wpgcl_photo1='".$hf_username."' WHERE wpgcl_concertid=".$c; - if ($p1 == 'photo2') $usql = "UPDATE wpg_concertlogs SET wpgcl_photo2='".$hf_username."' WHERE wpgcl_concertid=".$c; - if ($p1 == 'rev1') $usql = "UPDATE wpg_concertlogs SET wpgcl_rev1='".$hf_username."' WHERE wpgcl_concertid=".$c; - if ($p1 == 'rev2') $usql = "UPDATE wpg_concertlogs SET wpgcl_rev2='".$hf_username."' WHERE wpgcl_concertid=".$c; - - $uresults = $wpdb->get_results($usql); - $wpdb->insert( 'wpg_logchanges', array ( - 'id' => '', - 'userid' => $hf_username, - 'action' => 'assigned '.$p1, - 'concertid' => $c)); - echo ($wpdb->last_error ); - wp_mail( $to, $subject, $body, $headers ); - - -} - -function giglogadmin_unassignconcert($p1, $c): void -{ - 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; - $body = 'The email body content'; - $headers = array('Content-Type: text/html; charset=UTF-8'); - - - - if ($p1 == 'photo1') $usql = "UPDATE wpg_concertlogs SET wpgcl_photo1='' WHERE wpgcl_concertid=".$c; - if ($p1 == 'photo2') $usql = "UPDATE wpg_concertlogs SET wpgcl_photo2='' WHERE wpgcl_concertid=".$c; - if ($p1 == 'rev1') $usql = "UPDATE wpg_concertlogs SET wpgcl_rev1='' WHERE wpgcl_concertid=".$c; - if ($p1 == 'rev2') $usql = "UPDATE wpg_concertlogs SET wpgcl_rev2='' WHERE wpgcl_concertid=".$c; - - - $uresults = $wpdb->get_results($usql); - $wpdb->insert( 'wpg_logchanges', array ( - 'id' => '', - 'userid' => $hf_username, - 'action' => 'unassigned '.$p1, - 'concertid' => $c)); - echo ($wpdb->last_error ); - wp_mail( $to, $subject, $body, $headers ); - - -} - -/** - * @return null|string - */ -function giglogadmin_getpublishstatus ($c) -{ - global $wpdb; - $date1 = new DateTime("now"); - $dsql = "select filedate from wpg_files where wpgc_id=".$c; - $results = $wpdb->get_results($dsql); - foreach ( $results AS $row ) - { //$x = strtotime($row -> filedate); - $x= date('Y-m-d H:i:s', strtotime($row -> filedate)); - $date2 = new DateTime($x, new DateTimeZone('Europe/London')); - $dd = date_diff ($date1, $date2); - $datediff = $dd ->format('%d'); - } - if ($datediff <= 10) return ('NEW'); -} - - -/** - * @return null|string - */ -function giglogadmin_returnuser($p1, $c) -{ - global $wpdb; - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; - - //PHOTO1 - if ($p1 == 'photo1') - { - //checking if taken - $vquery0 = "select wpgcl_photo1 from wpg_concertlogs where wpgcl_concertid=".$c ; - $results = $wpdb->get_results($vquery0); - foreach ( $results AS $row ) $x= $row -> wpgcl_photo1; - if ($x !='' and $x!=$hf_username) { return ('Taken
Taken by '.$x.'
'); } - else - if ($x==$hf_username) //if current user - return ('
-
'); - else //not taken by anyone - return ('
-
'); - } - //PHOTO2 - if ($p1 == 'photo2') - { - $vquery0 = "select wpgcl_photo2 from wpg_concertlogs where wpgcl_concertid=".$c ; - $results = $wpdb->get_results($vquery0); - foreach ( $results AS $row ) $x= $row -> wpgcl_photo2; - if ($x !='' and $x!=$hf_username) { return ('Taken
Taken by '.$x.'
'); } - else - if ($x==$hf_username) //if current user - return ('
-
'); - - else //not taken by anyone - return ('
-
'); - - } - //TEXT1 - if ($p1 == 'rev1') - { - $vquery0 = "select wpgcl_rev1 from wpg_concertlogs where wpgcl_concertid=".$c ; - $results = $wpdb->get_results($vquery0); - foreach ( $results AS $row ) $x= $row -> wpgcl_rev1; - if ($x !='' and $x!=$hf_username) { ('Taken
Taken by '.$x.'
'); } - else - if ($x==$hf_username) //if current user - return ('
-
'); - else //not taken by anyone - return ('
-
'); - - } - //TEXT2 - if ($p1 == 'rev2') - { - $vquery0 = "select wpgcl_rev2 from wpg_concertlogs where wpgcl_concertid=".$c ; - $results = $wpdb->get_results($vquery0); - foreach ( $results AS $row ) $x= $row -> wpgcl_rev2; - if ($x !='' and $x!=$hf_username) { ('Taken
Taken by '.$x.'
'); } - else - if ($x==$hf_username) //if current user - return ('
-
'); - else //not taken by anyone - return ('
-
'); - - } - - -} - - -function giglogadmin_getfiltersphotog(): string { - global $wpdb; - - //echo (var_dump($_POST["selectvenue"])); - - $results = $wpdb->get_results('select distinct wpgvenue_city from wpg_venues'); - $select= '
'; - //second drop down for venue - - $vquery = "select id, wpgvenue_name from wpg_venues"; - $vquery.= " where wpgvenue_city='".$_POST["selectcity"]."'"; - $resultsv = $wpdb->get_results($vquery); - $select.= '
'; - return $select; -} - - -function giglogadmin_getconcertsphotog ( ): string { - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; - $roles = ( array ) $hf_user->roles; - global $wpdb; - - // Shortcodes RETURN content, so store in a variable to return - $content = '

The available slots are marked with the green checkbox. - If you click on it, it will be assigned to you and if you no longer - wish to cover that concert, click on the red icon and you will be - unassigned. A mail should be sent to the admin when this happens, - but in order for the accreditation request to be sent, you have to - mail live@eternal-terror.com with the template containing concert - information. There might be some exceptions, but those are discussed - case by case. So whenever you want a concert, assign yourself and send - the template no later than 3 weeks before the concert.

- -

Admin will try to keep the concert status updated so that you know - what the accreditation status is. You will get personal message if this - is really close to the concert date.

'; - - $content .= ''; - // $content .= ''; - - $content .= ' - - - '; - if (current_user_can('administrator')) //($hf_username == 'etadmin') - $content .= ''; - $content .= ''; - - // Use the submitted "city" if any. Otherwise, use the default/static value. - $cty= filter_input( INPUT_POST, 'selectcity' ); - $cty= $cty? $cty: 'ALL'; - - $venue= filter_input( INPUT_POST, 'selectvenue' ); - //echo($_POST['selectvenue']); - $venue= $venue? $venue: '0'; - - - $query = "SELECT wpgc.id, wpgc.wpgconcert_name, wpgv.wpgvenue_name as venue, wpgc.wpgconcert_date, wpgc.wpgconcert_tickets, wpgc.wpgconcert_event, wpgv.wpgvenue_city, wpgv.wpgvenue_webpage, wpgps.wpgs_name - FROM wpg_concerts wpgc, wpg_venues wpgv, wpg_pressstatus wpgps, wpg_concertlogs wpgcl - where wpgc.venue = wpgv.id - and wpgconcert_date >= CURDATE() - and wpgps.id = wpgcl.wpgcl_status - and wpgcl.wpgcl_concertid=wpgc.id"; - - $query .= ($cty == "ALL") ? "" : " and wpgv.wpgvenue_city='" .$cty ."'"; - $query .= ($venue == "0") ? "" : " and wpgv.id='" .$venue ."'"; - $query .=" order by wpgv.wpgvenue_city, wpgconcert_date" ; - //echo($query); - $results = $wpdb->get_results($query); - - - $lastType = ''; - foreach ( $results AS $row ) { - $content .= ''; - - if($lastType != '' && $lastType != $row->wpgvenue_city) { - $content .= ''; - } - - if ($lastType == '' ) - {$content .= ''; - } - // Modify these to match the database structure - // $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $fdate = strtotime($row->wpgconcert_date); - $newformat = date('d.M.Y',$fdate); - - //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - if (current_user_can('administrator')) //($hf_username == 'etadmin') - { $content .= ''; - } - $content .= ''; - $lastType = $row->wpgvenue_city; - } - $content .= '
CITYIDBANDVENUEDATE
CITYTITLEVENUEDATE PHOTO1PHOTO2TEXT1TEXT2STATUSAdminButtons
'.$row->wpgvenue_city.'
'.$row->wpgvenue_city.'
' . $row->id. '' . $row->wpgconcert_name. '' . $row->venue. '' .$newformat. ''.giglogadmin_getpublishstatus($row->id ).''.giglogadmin_returnuser('photo1', $row->id ).''.giglogadmin_returnuser('photo2', $row->id ).''.giglogadmin_returnuser('rev1', $row->id ).''.giglogadmin_returnuser('rev2', $row->id ).''.$row -> wpgs_name; - $content .= ''; - $content .= '
-
'; - $content .= '
'; - - if(isset($_POST['assignitem'])) - { - echo (giglogadmin_assignconcert($_POST['pid'],$_POST['cid'])); - - $url2=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url2"); //reload page - } - - if(isset($_POST['unassignitem'])) - { - echo (giglogadmin_unassignconcert($_POST['pid'],$_POST['cid'])); - - $url3=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url3"); //reload page - } - - if(isset($_POST['reqsent'])) - { - echo (giglogadmin_assignconcert($_POST['pid'],$_POST['cid'])); - $usql = "UPDATE wpg_concertlogs SET wpgcl_status=2 WHERE wpgcl_concertid=".$_POST['cid']; - $uresults = $wpdb->get_results($usql); - $url2=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url2"); //reload page - } - - if(isset($_POST['phok'])) - { - $usql = "UPDATE wpg_concertlogs SET wpgcl_status=3 WHERE wpgcl_concertid=".$_POST['cid']; - $uresults = $wpdb->get_results($usql); - - $url2=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url2"); //reload page - } - - if(isset($_POST['txtok'])) - { - $usql = "UPDATE wpg_concertlogs SET wpgcl_status=4 WHERE wpgcl_concertid=".$_POST['cid']; - $uresults = $wpdb->get_results($usql); - - $url2=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url2"); //reload page - } - - if(isset($_POST['allok'])) - { - $usql = "UPDATE wpg_concertlogs SET wpgcl_status=5 WHERE wpgcl_concertid=".$_POST['cid']; - $uresults = $wpdb->get_results($usql); - - $url2=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url2"); //reload page - } - - if(isset($_POST['rej'])) - { - $usql = "UPDATE wpg_concertlogs SET wpgcl_status=6 WHERE wpgcl_concertid=".$_POST['cid']; - $uresults = $wpdb->get_results($usql); - - $url2=$_SERVER['REQUEST_URI']; - header("Refresh: 1; URL=$url2"); //reload page - } - - // return the table - return $content; -} - -function giglogadmin_photographers(): string -{ - $output = giglogadmin_getfiltersphotog(); - $output .= giglogadmin_getconcertsphotog(); - - return $output; -} diff --git a/includes/public/shortcodes/giglog_process_files.php b/includes/public/shortcodes/giglog_process_files.php deleted file mode 100644 index 9443cda..0000000 --- a/includes/public/shortcodes/giglog_process_files.php +++ /dev/null @@ -1,72 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -/* - * snippet used to upload files with concerts. File is tab delimited file. - * Band Venue Date TicketLink Eventlink. The form is at the end of this snippet - */ - -function giglogadmin_upload_files(): string { - global $wpdb; - $output = ""; - $dir = wp_upload_dir()['basedir'].'/concertlists/'; //the basedir is from file uploader plugin, namely the uploads folder in which I created a concertlist folder - if ( !file_exists($dir) ) { - mkdir( $dir ); - } - - $cfiles = scandir($dir); - foreach ($cfiles as $value) { //list all files in directory - $my_id = 0; //reset my_id which is used to check if anything was inserted - if (strlen($value) > 3 and (is_dir($dir . '/' . $value) == false)) { - $output .= 'Filename: ' . $value . '
'; - $filecontent = file_get_contents($dir . '/' . $value); - $listcontent = str_replace(array( - "\r", - "\n" - ), '
', $filecontent); //tring to replace end of lines with brs for html - - $output .= 'FILE CONTENT
'; - $r = 1; - //processing each line of the file into a new row in wpg_files table - if (isset($_POST['InsertFileContent'])) { - $lines = new SplFileObject($dir . '/' . $value); - //and then execute a sql query here - $table = 'wpg_files'; - foreach ($lines as $newconcert) { - $output .= '
  • ' . $newconcert . '
  • '; - $wpdb->insert($table, array( - 'id' => '', - 'filename' => $value, - 'rowid' => $r, - 'rowcontent' => $newconcert - )); - $r++; - //$wpdb->print_error(); - $output .= $wpdb->last_error; - $my_id = $wpdb->insert_id; - $output .= '
    ---------------
    Inserted rowID ' . $my_id . '
    '; - } //end processing each line - } //end file processing - - - } //end if that checks whether filename is longer than 3 and is actually a file - - if ($my_id > 0) //if anything was inserted, move file to handled - { - $output .= '
    File ' . $value . ' will be movedto handled folder'; - rename($dir . '/' . $value, $dir . '/handled/' . $value); - } - - } //end looping through all folder content - - if ($my_id > 0) { - $url1 = $_SERVER['REQUEST_URI']; - header("Refresh: 5; URL=$url1"); - } //reload page - - $output .= '
    '; - return $output; -} -- cgit v1.2.3 From f9f97dc3eae4980d63ce78ccc56b73aeab28af84 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 16:22:44 +0200 Subject: Drop obsolete database tables. These tables are no longer being used, so let's remove them and the code to add them. --- includes/admin/register_db_tables.php | 103 ++-------------------------------- 1 file changed, 6 insertions(+), 97 deletions(-) diff --git a/includes/admin/register_db_tables.php b/includes/admin/register_db_tables.php index 8c6dc98..8080578 100644 --- a/includes/admin/register_db_tables.php +++ b/includes/admin/register_db_tables.php @@ -259,35 +259,17 @@ if ( !function_exists( "giglog_register_db_tables") ) */ function giglog_register_db_tables() { + global $wpdb; + + // Clean out obsolete tables if they exist. + $wpdb->query("DROP TABLE IF EXISTS " + . "wpg_bands, wpg_concertlogs, wpg_files, wpg_logchanges, wpg_pressstatus"); + $db_version = get_option('giglogadmin_db_version'); if ($db_version == 8) { return; } - $bands_table = - "CREATE TABLE IF NOT EXISTS `wpg_bands` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `wpgband_name` varchar(500) NOT NULL, - `wpgband_country` varchar(4) DEFAULT 'NO', - PRIMARY KEY (`id`), - KEY `wpgband_country` (`wpgband_country`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - - $concertlogs_table = - "CREATE TABLE IF NOT EXISTS `wpg_concertlogs` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `wpgcl_concertid` int(11) NOT NULL, - `wpgcl_photo1` varchar(200) DEFAULT NULL, - `wpgcl_photo2` varchar(200) DEFAULT NULL, - `wpgcl_rev1` varchar(200) DEFAULT NULL, - `wpgcl_rev2` varchar(200) DEFAULT NULL, - `wpgcl_int` varchar(200) DEFAULT NULL, - `wpgcl_status` int(11) DEFAULT 1, - PRIMARY KEY (`id`), - KEY `wpglog_status` (`wpgcl_status`), - KEY `wpglog_concerts` (`wpgcl_concertid`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - $concerts_table = "CREATE TABLE IF NOT EXISTS `wpg_concerts` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -311,35 +293,6 @@ if ( !function_exists( "giglog_register_db_tables") ) PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - $files_table = - "CREATE TABLE IF NOT EXISTS `wpg_files` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `filename` varchar(2000) NOT NULL, - `filedate` datetime NOT NULL DEFAULT current_timestamp(), - `rowid` int(11) NOT NULL, - `rowcontent` text NOT NULL, - `processed` char(1) NOT NULL DEFAULT 'N', - `wpgc_id` int(11) NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - - $logchanges_table = - "CREATE TABLE IF NOT EXISTS `wpg_logchanges` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `userid` varchar(500) NOT NULL, - `action` varchar(500) NOT NULL, - `actiondate` date NOT NULL DEFAULT current_timestamp(), - `concertid` int(11) NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - - $pressstatus_table = - "CREATE TABLE IF NOT EXISTS `wpg_pressstatus` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `wpgs_name` varchar(50) NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - $venues_table = "CREATE TABLE IF NOT EXISTS `wpg_venues` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -350,15 +303,9 @@ if ( !function_exists( "giglog_register_db_tables") ) PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - global $wpdb; $tables = array( - $bands_table, - $concertlogs_table, $concerts_table, $countries_table, - $files_table, - $logchanges_table, - $pressstatus_table, $venues_table); foreach($tables as $tabledef) { @@ -377,51 +324,13 @@ if ( !function_exists( "giglog_register_db_tables") ) ADD FULLTEXT KEY `id` (`id`,`wpgc_fullname`,`wpgcountry_name`,`wpgc_iso3`,`wpgc_numcode`);"); - $wpdb->query( - "ALTER TABLE `wpg_bands` - ADD CONSTRAINT `wpgband_country` - FOREIGN KEY (`wpgband_country`) - REFERENCES `wpg_countries` (`id`) ON DELETE NO ACTION;"); - - $wpdb->query( - "ALTER TABLE `wpg_concertlogs` - ADD CONSTRAINT `wpglog_concerts` - FOREIGN KEY (`wpgcl_concertid`) - REFERENCES `wpg_concerts` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION, - ADD CONSTRAINT `wpglog_status` - FOREIGN KEY (`wpgcl_status`) - REFERENCES `wpg_pressstatus` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;"); - $wpdb->query( "ALTER TABLE `wpg_concerts` - ADD CONSTRAINT `wpgconcert_band` - FOREIGN KEY (`band`) - REFERENCES `wpg_bands` (`id`) ON DELETE NO ACTION, ADD CONSTRAINT `wpgconcert_venue` FOREIGN KEY (`venue`) REFERENCES `wpg_venues` (`id`) ON DELETE NO ACTION;"); } - if ($db_version == NULL || $db_version < 2) - { - $wpdb->query( - "INSERT INTO `wpg_pressstatus` (`id`, `wpgs_name`) VALUES - (1, ' '), - (2, 'Accred Requested'), - (3, 'Photo Approved'), - (4, 'Text Approved'), - (5, 'Photo and Text approved'), - (6, 'Rejected');"); - } - - if ($db_version == NULL || $db_version < 3) - { - $wpdb->query( - "ALTER TABLE `wpg_concertlogs` - ADD COLUMN IF NOT EXISTS - `wpgcl_createddate` date NOT NULL DEFAULT current_timestamp();"); - } - if ($db_version == NULL || $db_version < 4) { $wpdb->query( -- cgit v1.2.3 From 74fda7b89101ad47b77b486f4e38792db473cb00 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Sep 2021 16:29:44 +0200 Subject: Remove obsolete source files. --- includes/concertlogs.php | 115 ----------------------------------------------- includes/roles.php | 12 ----- 2 files changed, 127 deletions(-) delete mode 100644 includes/concertlogs.php delete mode 100644 includes/roles.php diff --git a/includes/concertlogs.php b/includes/concertlogs.php deleted file mode 100644 index 8b1ca63..0000000 --- a/includes/concertlogs.php +++ /dev/null @@ -1,115 +0,0 @@ - -// SPDX-FileCopyrightText: 2021 Harald Eilertsen -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -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. - * - * @return void - */ - public static function add($concert_id): void - { - global $wpdb; - - $q = $wpdb->prepare( - "INSERT INTO wpg_concertlogs SET wpgcl_concertid = %d", - intval($concert_id)); - - $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; - - $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; - } - - 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); - - 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 ) || NULL; - } - - public function assigned_user(string $role) : ?string - { - return $this->roles[$role]; - } - } -} diff --git a/includes/roles.php b/includes/roles.php deleted file mode 100644 index dbbedd2..0000000 --- a/includes/roles.php +++ /dev/null @@ -1,12 +0,0 @@ -