From 512511df29423fc943b566bc20d997cd20ee9e03 Mon Sep 17 00:00:00 2001 From: AndreaChirulescu Date: Mon, 12 Apr 2021 23:34:32 +0200 Subject: Moved a lot of concert related logic from import/admin into the GiglogAdmin_Concert --- includes/admin/views/giglog_admin_page.php | 90 +++++------ includes/admin/views/giglog_import_gigs.php | 24 ++- includes/concert.php | 236 ++++++++++++++++++---------- 3 files changed, 214 insertions(+), 136 deletions(-) (limited to 'includes') diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 9322c9a..8deba81 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -41,8 +41,8 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) {

- - + '; return($select); } - + static function get_filters() { $cities = array_merge(["ALL"], GiglogAdmin_Venue::all_cities()); @@ -133,48 +133,36 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return $select; } - + static function editforms() { global $wpdb; - $cid = filter_input(INPUT_POST, "cid"); - if(($_POST['edit']=="EDIT")&&!empty($cid)) - { - $query = "SELECT * FROM wpg_concerts where id = ".$cid; - $results = $wpdb->get_results($query); - foreach($results as $row) - { - $band = $row -> band; - $venue = $row-> venue; - $cdate = $row ->wpgconcert_date; - $ticket = $row->wpgconcert_tickets; - $link = $row->wpgconcert_event; - } - - } + $cid = filter_input(INPUT_POST, "cid"); + $c = GiglogAdmin_Concert::find_or_create($cid,'','','','',''); + $content='
Form to create/edit concerts, bands, venues
' - .'' - .''.GiglogAdmin_AdminPage::get_allbands($band).'
' - .''.GiglogAdmin_AdminPage::get_allvenues($venue).'
' - .'
' - .'
' - .'
'; - if ($band=='') //actions differ if we update or create a concert, hence two buttons needed + .'' + .''.GiglogAdmin_AdminPage::get_allbands($c->band()).'
' + .''.GiglogAdmin_AdminPage::get_allvenues($c->venue()).'
' + .'
' + .'
' + .'
'; + if ($c->band()=='') //actions differ if we update or create a concert, hence two buttons needed $content.='

'; - else + else $content.='

'; $content.='
'; - + $content.='

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

' .'
'; - + $content.='

' .'
' .'
' .'

' - .'
'; + .''; return $content; } @@ -302,7 +290,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { // return the table return $content; } - + static function update() { @@ -342,49 +330,49 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { //$url2=$_SERVER['REQUEST_URI']; //doesn't seem to be needed actually, leaving here just in case //header("Refresh: 1; URL=$url2"); //reload page } - + if(isset($_POST['newconcert'])) { IF (empty($_POST['selectband']) || empty($_POST['selectvenueadmin']) || empty($_POST['cdate']) || empty($_POST['ticket']) || empty($_POST['eventurl'])) echo ''; - else - { + else + { GiglogAdmin_Concert::create($_POST['selectband'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); echo ''; - } + } } if(isset($_POST['editconcert'])) { IF (empty($_POST['selectband']) || empty($_POST['selectvenueadmin']) || empty($_POST['cdate']) || empty($_POST['ticket']) || empty($_POST['eventurl'])) echo ''; - else - { + else + { GiglogAdmin_Concert::updatec($_POST['pid'],$_POST['selectband'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); echo ''; - } - } - + } + } + if(isset($_POST['newband'])) { - IF (empty($_POST['bandname'])) //country is not checked as it is set to Norway by default + IF (empty($_POST['bandname'])) //country is not checked as it is set to Norway by default echo ''; - else - { - GiglogAdmin_Band::create($_POST['bandname'],$_POST['selectcountry']); + else + { + GiglogAdmin_Band::create($_POST['bandname'],$_POST['selectcountry']); echo ''; - } + } } - + if(isset($_POST['newvenue'])) { IF (empty($_POST['venuename']) || empty($_POST['venuecity'])) echo ''; - else - { - GiglogAdmin_Venue::create($_POST['venuename'],$_POST['venuecity']); + else + { + GiglogAdmin_Venue::create($_POST['venuename'],$_POST['venuecity']); echo ''; - } - } + } + } } static function assignconcert($p1, $c) diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index 04b2351..d0520e7 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -85,7 +85,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { if (is_numeric($venue)) $newconcert[1] = $venue; else { - $v = GiglogAdmin_Venue::find_or_create($venue,'Oslo'); + $v = GiglogAdmin_Venue::find_or_create($venue,'Oslo'); //phase 666 of the project should maybe consider both city and band country when creating concerts/importing files $newconcert[1] = $v->id(); } @@ -98,22 +98,34 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { . ', VENUE ' . $venue . ' with venue ID ' . $newconcert[1] . ', CONCERTDATE ' . $condate); } else { - $id = GiglogAdmin_Concert::create( + $id = GiglogAdmin_Concert::find_or_create( + '', $newconcert[0], $newconcert[1], $condate, $ticketlink, - $eventlink); - + $eventlink); + error_log( 'NEW CONCERT ADDED: ' - . ' ID: ' . $id + . ' ID: ' . $id->id() . ' BAND ' . $band . ' with band ID ' . $newconcert[0] . ', VENUE ' . $venue . ' with venue ID ' . $newconcert[1] . ', CONCERTDATE ' . $condate . ', Ticket LINK ' . $ticketlink . ', Event LINK ' . $eventlink); - GiglogAdmin_Concertlogs::add($id); + GiglogAdmin_Concertlogs::add($id->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) + */ } } } diff --git a/includes/concert.php b/includes/concert.php index 774b72a..5c50d43 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -1,79 +1,157 @@ -. - */ - -if ( !class_exists('GiglogAdmin_Concert') ) { - class GiglogAdmin_Concert - { - public static function create($band, $venue, $date, $ticketlink, $eventlink) - { - global $wpdb; - - $res = $wpdb->insert('wpg_concerts', array( - 'band' => $band, - 'venue' => $venue, - 'wpgconcert_date' => $date, - 'wpgconcert_tickets' => $ticketlink, - 'wpgconcert_event' => $eventlink - )); - - if ( !$res ) { - error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); - die; - } - - return $wpdb->insert_id; - } - - public static function updatec($id, $band, $venue, $cdate, $ticketlink, $eventlink) - { - global $wpdb; - - $res = $wpdb->update('wpg_concerts', array( - 'band' => $band, - 'venue' => $venue, - 'wpgconcert_date' => $cdate, - 'wpgconcert_tickets' => $ticketlink, - 'wpgconcert_event' => $eventlink - ), - array('id' => $id) - ); - - 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); //not sure what to return here? - } - - public static function get($band, $venue, $date) - { - global $wpdb; - - $sql = 'SELECT id from wpg_concerts' - . ' where band = ' . $band - . ' and venue = ' . $venue - . ' and wpgconcert_date ="' . $date . '"'; - - error_log(__CLASS__ . '::' . __FUNCTION__ . ": {$sql}"); - return $wpdb->get_results($sql); - } - } -} -?> +. + */ + +if ( !class_exists('GiglogAdmin_Concert') ) { + class GiglogAdmin_Concert + { + private $id; + private $band; + private $venue; + private $cdate; + private $tickets; + private $eventlink; + + /* + * Constructs a new venue 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. + */ + private function __construct($attrs) + { + $this->id = isset($attrs->id) ? $attrs->id : NULL; + $this->band = isset($attrs->band) ? $attrs->band : NULL; + $this->venue = isset($attrs->venue) ? $attrs->venue : NULL; + $this->cdate = isset($attrs->wpgconcert_date) ? $attrs->wpgconcert_date : NULL; + $this->tickets = isset($attrs->wpgconcert_tickets) ? $attrs->wpgconcert_tickets : NULL; + $this->eventlink = isset($attrs->wpgconcert_event) ? $attrs->wpgconcert_event : NULL; + } + + static function find_or_create($id,$band, $venue, $cdate, $ticketlink, $eventlink) + { + global $wpdb; + if($id) + { + $csql = 'SELECT * FROM wpg_concerts WHERE id="' . $id . '"'; + $results = $wpdb->get_results($csql); + + if ($results) + return new GiglogAdmin_Concert($results[0]); + } + else { + + return GiglogAdmin_Concert::create($band, $venue, $cdate, $ticketlink, $eventlink); + } + } + + public static function create($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(); + + return $cid; + } + + + public static function updatec($id, $band, $venue, $cdate, $ticketlink, $eventlink) + { + global $wpdb; + + $res = $wpdb->update('wpg_concerts', array( + 'band' => $band, + 'venue' => $venue, + 'wpgconcert_date' => $cdate, + 'wpgconcert_tickets' => $ticketlink, + 'wpgconcert_event' => $eventlink + ), + array('id' => $id) + ); + + 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); //not sure what to return here? + } + + public static function get($band, $venue, $date) + { + global $wpdb; + + $sql = 'SELECT id from wpg_concerts' + . ' where band = ' . $band + . ' and venue = ' . $venue + . ' and wpgconcert_date ="' . $date . '"'; + + error_log(__CLASS__ . '::' . __FUNCTION__ . ": {$sql}"); + return $wpdb->get_results($sql); + } + + public function save() + { + global $wpdb; + + $wpdb->insert('wpg_concerts', array( + 'id' => '', + 'band' => $this->band, + 'venue' => $this->venue, + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink + )); + + $this->id = $wpdb->insert_id; + } + + public function id() + { + return $this->id; + } + + public function band() + { + return $this->band; + } + public function venue() + { + return $this->venue; + } + public function cdate() + { + return $this->cdate; + } + public function tickets() + { + return $this->tickets; + } + public function eventlink() + { + return $this->eventlink; + } + } +} +?> \ No newline at end of file -- cgit v1.2.3