From 02caa8be1541d11a65dc00cdea08d1b0bc932ba5 Mon Sep 17 00:00:00 2001 From: AndreaChirulescu Date: Wed, 14 Apr 2021 22:42:38 +0200 Subject: Got rid of find or create for concert and band Refactored band --- includes/admin/views/giglog_admin_page.php | 39 +++++++--- includes/admin/views/giglog_import_gigs.php | 4 +- includes/band.php | 110 ++++++++++++++++++++++++---- includes/concert.php | 81 ++++++++++---------- 4 files changed, 172 insertions(+), 62 deletions(-) diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index b59ade3..b6fe098 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -62,12 +62,13 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return($select); } - static function get_countries() + static function get_countries($incountry) { $select = ''; @@ -152,10 +153,18 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content.=''; $content.='

' - .'
' - .''.GiglogAdmin_AdminPage::get_countries().'
' - .'

' - .'
'; + .'' + .'
' + .''.GiglogAdmin_AdminPage::get_countries(GiglogAdmin_Band::get_band($c->band())[1]).'
'; + + if ($editing) { + $content.='

'; + } + else { + $content.='

'; + } + + $content.=''; $content.='

' .'
' @@ -336,8 +345,9 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { echo ''; else { - GiglogAdmin_Concert::find_or_create($_POST['selectband'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); - echo ''; + $ret = GiglogAdmin_Concert::create($_POST['selectband'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); + if ($ret!='dup') echo ''; + else echo ''; } } if(isset($_POST['editconcert'])) @@ -346,7 +356,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { echo ''; else { - GiglogAdmin_Concert::updatec($_POST['pid'],$_POST['selectband'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); + GiglogAdmin_Concert::update_concert($_POST['pid'],$_POST['selectband'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); echo ''; } } @@ -362,6 +372,17 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } } + if(isset($_POST['editband'])) + { + IF (empty($_POST['bandname'])) //country is not checked as it is set to Norway by default + echo ''; + else + { + GiglogAdmin_Band::update_band($_POST['bid'],$_POST['bandname'],$_POST['selectcountry']); + echo ''; + } + } + if(isset($_POST['newvenue'])) { IF (empty($_POST['venuename']) || empty($_POST['venuecity'])) diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index c2dfd2a..ca1c7b8 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -69,7 +69,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { //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]; - $newconcert[0] = GiglogAdmin_Band::find_or_create($band,'NO'); + $newconcert[0] = GiglogAdmin_Band::create($band,''); if (is_numeric($venue)) $newconcert[1] = $venue; @@ -80,7 +80,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { //not sure how to check dates, hopefully manual verification of files will take care of it - GiglogAdmin_Concert::find_or_create( + GiglogAdmin_Concert::create( $newconcert[0], $newconcert[1], $condate, diff --git a/includes/band.php b/includes/band.php index f631b08..ad5ce28 100644 --- a/includes/band.php +++ b/includes/band.php @@ -8,29 +8,52 @@ if ( !class_exists('GiglogAdmin_Band') ) { class GiglogAdmin_Band { - static function create($name, $country) - { - global $wpdb; - if (empty($country)) $country = 'NO'; - $wpdb->insert('wpg_bands', array( - 'id' => '', - 'wpgband_name' => $name, - 'wpgband_country' => $country - )); + private $id; + private $bandname; + private $country; - return $wpdb->insert_id; + + public function __construct($attrs = []) + { + $this->id = isset($attrs->id) ? $attrs->id : NULL; + $this->bandname = isset($attrs->bandname) ? $attrs->bandname : NULL; + $this->country = isset($attrs->country) ? $attrs->country : 'NO'; } - static function find_or_create($name, $country) + static function create($bandname, $country) { global $wpdb; - if(empty($country)) $country = 'NO'; - $bandsql = 'SELECT id FROM wpg_bands WHERE upper(wpgband_name)="' . $name . '"'; + if (empty($country)) $country = 'NO'; + $bandsql = 'SELECT id FROM wpg_bands WHERE upper(wpgband_name)="' . $bandname . '" and wpgband_country = "'.$country.'"'; $results = $wpdb->get_results($bandsql); + $attrs = new stdClass(); + $attrs->bandname = $bandname; + $attrs->country = $country; + + if ($results) + { + $attrs->id = $results[0]->id; + $bid = new GiglogAdmin_Band($attrs); + } + else + { + + $attrs->id = ''; + + $bid = new GiglogAdmin_Band($attrs); + $bid->save(); + + + error_log( 'NEW BAND ADDED: ' + . ' ID: ' . $bid -> id() + . ' BAND NAME ' . $bandname + . ', COUNTRY ' . $country); + } + return ($bid->id()); - return $results ? $results[0]->id : GiglogAdmin_Band::create($name, $country); } + static function all_bands() { global $wpdb; @@ -49,5 +72,64 @@ if ( !class_exists('GiglogAdmin_Band') ) { return ($results); } + 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() + { + 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/includes/concert.php b/includes/concert.php index 89d28bd..7afedca 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -32,21 +32,6 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - static function find_or_create($band, $venue, $cdate, $ticketlink, $eventlink) - { - global $wpdb; - $cresults = GiglogAdmin_Concert::get($band, $venue, $cdate); - if ($cresults) { - error_log( 'DUPLICATE ROW detected: ' - . ' BAND ID ' . $band - . ', VENUE ID ' . $venue - . ', CONCERTDATE ' . $cdate); - } - else - return GiglogAdmin_Concert::create($band, $venue, $cdate, $ticketlink, $eventlink); - } - - static function find_cid($id) { global $wpdb; @@ -66,26 +51,39 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } } - public static function create($band, $venue, $cdate, $ticketlink, $eventlink) + static function check_duplicate($band, $venue, $cdate, $ticketlink, $eventlink) { - $attrs = new stdClass(); - $attrs->id = ''; - $attrs->band = $band; - $attrs->venue = $venue; - $attrs->wpgconcert_date = $cdate; - $attrs->wpgconcert_tickets = $ticketlink; - $attrs->wpgconcert_event = $eventlink; - $cid = new GiglogAdmin_Concert($attrs); - $cid->save(); - - error_log( 'NEW CONCERT ADDED: ' - . ' ID: ' . $cid -> id() - . ' BAND ID ' . $band - . ', VENUE ID ' . $venue - . ', CONCERTDATE ' . $cdate - . ', Ticket LINK ' . $ticketlink - . ', Event LINK ' . $eventlink); - GiglogAdmin_Concertlogs::add($cid->id()); + global $wpdb; + + $cresults = GiglogAdmin_Concert::get($band, $venue, $cdate); + if ($cresults) + return($cresults); + else + return ('new'); + + } + public static function create($band, $venue, $cdate, $ticketlink, $eventlink) + { $c = GiglogAdmin_Concert::check_duplicate($band, $venue, $cdate, $ticketlink, $eventlink); + if ($c=='new') + { + $attrs = new stdClass(); + $attrs->id = ''; + $attrs->band = $band; + $attrs->venue = $venue; + $attrs->wpgconcert_date = $cdate; + $attrs->wpgconcert_tickets = $ticketlink; + $attrs->wpgconcert_event = $eventlink; + $cid = new GiglogAdmin_Concert($attrs); + $cid->save(); + + error_log( 'NEW CONCERT ADDED: ' + . ' ID: ' . $cid -> id() + . ' BAND ID ' . $band + . ', VENUE ID ' . $venue + . ', CONCERTDATE ' . $cdate + . ', Ticket LINK ' . $ticketlink + . ', Event LINK ' . $eventlink); + GiglogAdmin_Concertlogs::add($cid->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 ( @@ -96,11 +94,20 @@ if ( !class_exists('GiglogAdmin_Concert') ) { VALUES (null, new.id, 1) */ - return $cid; + return $cid; + } + else + { + error_log( 'DUPLICATE ROW detected: ' + . ' BAND ID ' . $band + . ', VENUE ID ' . $venue + . ', CONCERTDATE ' . $cdate); + return('dup'); + } } - public static function updatec($id, $band, $venue, $cdate, $ticketlink, $eventlink) + public static function update_concert($id, $band, $venue, $cdate, $ticketlink, $eventlink) { global $wpdb; @@ -120,7 +127,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { die; } - return ($wpdb->last_error); //not sure what to return here? + return ($wpdb->last_error); } public static function get($band, $venue, $date) -- cgit v1.2.3