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/band.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 14 deletions(-) (limited to 'includes/band.php') 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; + } } } -- cgit v1.2.3