From c5d786a1c72911e4352a6e40ea19aff98711d353 Mon Sep 17 00:00:00 2001 From: AndreaChirulescu Date: Sun, 27 Feb 2022 13:35:59 +0100 Subject: iCal file download fixes to generate one file per concert giglog import fixes to catch errors and ignore old concerts --- includes/admin/views/_concerts_cal_dld.php | 162 ----------------- includes/admin/views/_concerts_table.php | 268 ++++++++++++++-------------- includes/admin/views/_edit_concert_form.php | 2 +- includes/admin/views/giglog_import_gigs.php | 102 +++++++---- includes/ical_export.php | 203 ++++++--------------- includes/venue.php | 2 +- 6 files changed, 263 insertions(+), 476 deletions(-) delete mode 100644 includes/admin/views/_concerts_cal_dld.php diff --git a/includes/admin/views/_concerts_cal_dld.php b/includes/admin/views/_concerts_cal_dld.php deleted file mode 100644 index f306a69..0000000 --- a/includes/admin/views/_concerts_cal_dld.php +++ /dev/null @@ -1,162 +0,0 @@ - "kigkonsult.se", ] ) - - // with calendaring info - ->setMethod( Vcalendar::PUBLISH ) - ->setXprop( - Vcalendar::X_WR_CALNAME, - "Calendar Sample" - ) - ->setXprop( - Vcalendar::X_WR_CALDESC, - "This is a demo calendar" - ) - ->setXprop( - Vcalendar::X_WR_RELCALID, - "3E26604A-50F4-4449-8B3E-E4F4932D05B5" - ) - ->setXprop( - Vcalendar::X_WR_TIMEZONE, - "Europe/Stockholm" - ); - - // create a new event -$event1 = $vcalendar->newVevent() - ->setTransp( Vcalendar::OPAQUE ) - ->setClass( Vcalendar::P_BLIC ) - ->setSequence( 1 ) - // describe the event - ->setSummary( 'Scheduled meeting with five occurrences' ) - ->setDescription( - 'Agenda for the the meeting...', - [ Vcalendar::ALTREP => - 'CID:' ] - ) - ->setComment( 'It\'s going to be fun..' ) - // place the event - ->setLocation( 'Kafé Ekorren Stockholm' ) - ->setGeo( '59.32206', '18.12485' ) - // set the time - ->setDtstart( - new DateTime( - '20220421T090000', - new DateTimezone( 'Europe/Stockholm' ) - ) - ) - ->setDtend( - new DateTime( - '20220421T100000', - new DateTimezone( 'Europe/Stockholm' ) - ) - ) - // with recurrence rule - - -; - - // add alarm for the event -$alarm = $event1->newValarm() - ->setAction( Vcalendar::DISPLAY ) - // copy description from event - ->setDescription( $event1->getDescription()) - // fire off the alarm one day before - ->setTrigger( '-P1D' ); - - // alter day and time for one event in recurrence set -$event2 = $vcalendar->newVevent() - ->setTransp( Vcalendar::OPAQUE ) - ->setClass( Vcalendar::P_BLIC ) - // reference to event in recurrence set - ->setUid( $event1->getUid()) - ->setSequence( 2 ) - // pointer to event in the recurrence set - ->setRecurrenceid( '20220505T090000 Europe/Stockholm' ) - // reason text - ->setDescription( - 'Altered day and time for event 2022-05-05', - [ Vcalendar::ALTREP => - 'CID:' ] - ) - ->setComment( 'Now we are working hard for two hours' ) - // the altered day and time with duration - ->setDtstart( - new DateTime( - '20220504T100000', - new DateTimezone( 'Europe/Stockholm' ) - ) - ) - ->setDuration( 'PT2H' ) - // add alarm (copy from event1) - ->setComponent( - $event1->getComponent( Vcalendar::VALARM ) - ); - -$vcalendarString = - // apply appropriate Vtimezone with Standard/DayLight components - $vcalendar->vtimezonePopulate() - // and create the (string) calendar - ->createCalendar(); - -return($vcalendarString); - } - - - -function dldCal() -{ - - -if(isset($_POST['download'])){ - $file = "test.ical"; - $txt = fopen($file, "w") or die("Unable to open file!"); -fwrite($txt, icalvalue()); -fclose($txt); - - $filename = "myzipfile.zip"; - - if (file_exists($file)) { - header('Content-Type: application/zip'); - header('Content-Disposition: attachment; filename="'.basename($file).'"'); - header('Content-Length: ' . filesize($file)); - ob_clean(); - flush(); - echo readfile($file); - // delete file - // unlink($file); - - } -} -/* - - - - -header('Content-Type: text/calendar'); -header('Content-Disposition: attachment; filename="'.basename($file).'"'); -header('Content-Transfer-Encoding: binary'); -header('Expires: 0'); -header('Content-Length:'. filesize($file)); -header('Pragma: public'); -ob_clean(); -//flush(); -readfile('test.ical'); -//echo file_get_contents($file); - -*/ - - - - -} - diff --git a/includes/admin/views/_concerts_table.php b/includes/admin/views/_concerts_table.php index 8d38eab..066e14c 100644 --- a/includes/admin/views/_concerts_table.php +++ b/includes/admin/views/_concerts_table.php @@ -34,18 +34,21 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) public function render(): string { return $this->render_filters() - . $this->render_concerts_table(); + . $this->render_concerts_table(); } private function render_concert_table_header() : string { - $content = - '
' - . '' - . ' '; + + + $content = '
CITYDATENAMEVENUE
'; + $content.= ' Note: the iCal link will download a file with extension .ical which can be used to add the event to your calendar. For convenience, we set all events with start time at 19:00 but please check the actual event for the correct time.'; + + $content.= ''; + $content.= ''; if (!is_admin()) { - $content .= ''; + $content .= ''; } else { $content .= ''; @@ -121,42 +124,42 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) private function render_pagination() : string { $content = - '
' - . ''; + '
' + . ''; if($this->page_no > 1) { $content .= - '' - . '' - . 'First Page -' - . '' - . '' - . '' - . ' Previous'; + '' + . '' + . 'First Page -' + . '' + . '' + . '' + . ' Previous'; } $content .= '' - . '' - . 'Page ' . $this->page_no . ' of ' . $this->total_no_of_pages . '' - . ''; + . '' + . 'Page ' . $this->page_no . ' of ' . $this->total_no_of_pages . '' + . ''; $content .= ''; if ($this->page_no < $this->total_no_of_pages) { $content .= - '' - . '' - . 'Next - ' - . '' - . '' - . '' - . 'Last Page' - . ''; + '' + . '' + . 'Next - ' + . '' + . '' + . '' + . 'Last Page' + . ''; } $content .= - '' - . '
'; + '
' + . '
'; return $content; } @@ -209,10 +212,11 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) if (current_user_can('administrator')) { $content .= ''; + . $this->adminactions($concert) + . ''; } } + $content .= ''; $content .= ''; $lastType = $concert->venue()->city(); } @@ -240,116 +244,116 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) } $select .= ''; + . '" value="' . esc_attr( $val ) . '">'; } $cty = $this->get_filter('city'); $select .= \EternalTerror\ViewHelpers\select_field( - "city", - array_map(fn($city) => [$city, $city], GiglogAdmin_Venue::all_cities()), - $cty, - "Select city..."); - - - if ( !empty($cty) ) { - //second drop down for venue - $select .= \EternalTerror\ViewHelpers\select_field( - "venue", - array_map( - fn($venue) => [$venue->id(), $venue->name()], - GiglogAdmin_Venue::venues_in_city($cty) - ), - $this->get_filter('venue_id'), - "Select venue..."); - } - - $select .= \EternalTerror\ViewHelpers\select_field( - "month", - array_map( - fn($m) => [ $m, $wp_locale->get_month( $m ) ], - range( 1, 12 ) - ), - $this->get_filter('month'), - "Select month..."); - - $select.=''; - - if(is_admin()) { - //option to select own concerts only - $select .= 'get_filter( 'current_user' ) ) - . '>'; + "city", + array_map(fn($city) => [$city, $city], GiglogAdmin_Venue::all_cities()), + $cty, + "Select city..."); + + + if ( !empty($cty) ) { + //second drop down for venue + $select .= \EternalTerror\ViewHelpers\select_field( + "venue", + array_map( + fn($venue) => [$venue->id(), $venue->name()], + GiglogAdmin_Venue::venues_in_city($cty) + ), + $this->get_filter('venue_id'), + "Select venue..."); + } - } - //NOTE that I remvoed

and mvoed them up to render_concerts_table function - $select .= ''; + $select .= \EternalTerror\ViewHelpers\select_field( + "month", + array_map( + fn($m) => [ $m, $wp_locale->get_month( $m ) ], + range( 1, 12 ) + ), + $this->get_filter('month'), + "Select month..."); - return $select; - } + $select.=''; - private function adminactions( GiglogAdmin_Concert $concert ) : string - { - return - '' - . '' - . \EternalTerror\ViewHelpers\select_field( - 'selectstatus', - array_map(fn($i) => [ $i, self::STATUS_LABELS[$i] ], range(1, count(self::STATUS_LABELS) - 1)), - $concert->status()) - . '' - . '' - . ''; - } + if(is_admin()) { + //option to select own concerts only + $select .= 'get_filter( 'current_user' ) ) + . '>'; - /** - * Display a mark on the concert if it is new. - * I.e. imported/created within the last ten days. - * - * @return null|string - */ - private function mark_new_concert(GiglogAdmin_Concert $concert) : string - { - $now = new DateTime(); - $new_entry = $now->diff($concert->created())->days <= 10; - if ($new_entry) { - return 'NEW'; - } - else { - return ''; - } - } + } + //NOTE that I remvoed

and mvoed them up to render_concerts_table function + $select .= ''; - 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; - - //first check if current slot is taken by current user - if ( $assigned_user == $this->username ) { - $f = '' - . ' ' - . ' ' - . ' ' - . ''; - } - elseif ( $assigned_user ) { //check if slot is taken by another user - $f = 'Taken' - . '
Taken by ' . $assigned_user . '
'; - } - elseif ( array_search($this->username, $roles) ) { - // other slots for this concert are taken by user - $f = '-'; - } - else { //not taken by anyone - $f = '' - . ' ' - . ' ' - . ' ' - . ''; - } + return $select; + } - return $f; - } - } -} + private function adminactions( GiglogAdmin_Concert $concert ) : string + { + return + '' + . '' + . \EternalTerror\ViewHelpers\select_field( + 'selectstatus', + array_map(fn($i) => [ $i, self::STATUS_LABELS[$i] ], range(1, count(self::STATUS_LABELS) - 1)), + $concert->status()) + . '' + . '' + . ''; + } + + /** + * Display a mark on the concert if it is new. + * I.e. imported/created within the last ten days. + * + * @return null|string + */ + private function mark_new_concert(GiglogAdmin_Concert $concert) : string + { + $now = new DateTime(); + $new_entry = $now->diff($concert->created())->days <= 10; + if ($new_entry) { + return 'NEW'; + } + else { + return ''; + } + } + + 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; + + //first check if current slot is taken by current user + if ( $assigned_user == $this->username ) { + $f = '' + . ' ' + . ' ' + . ' ' + . ''; + } + elseif ( $assigned_user ) { //check if slot is taken by another user + $f = 'Taken' + . '
Taken by ' . $assigned_user . '
'; + } + elseif ( array_search($this->username, $roles) ) { + // other slots for this concert are taken by user + $f = '-'; + } + else { //not taken by anyone + $f = '' + . ' ' + . ' ' + . ' ' + . ''; + } + + return $f; + } + } + } diff --git a/includes/admin/views/_edit_concert_form.php b/includes/admin/views/_edit_concert_form.php index 6e4f5e5..9bddfba 100644 --- a/includes/admin/views/_edit_concert_form.php +++ b/includes/admin/views/_edit_concert_form.php @@ -53,7 +53,7 @@ if (!class_exists("GiglogAdmin_EditConcertForm")) .'
' .'' . $this->get_venue_selector($c->venue()) . '
' //date has to be formatted else it is not red in the date field of html form - .'
' + .'
' .'
' .'
' .''; diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index 1a72fe1..e523dee 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -38,51 +38,93 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { } /** - * Imports concert data from a file with tab separated values. - * - * The file must contain the following columns each separated by _one_ - * tab character: - * - * 1. Concertname - * 2. Venuename or numeric venue id - * 3. Concert date - * 4. Ticket link - * 5. Event info link - * - * Empty lines are ignored. - * - * @return void - * - * @param array - */ + * Imports concert data from a file with tab separated values. + * + * The file must contain the following columns each separated by _one_ + * tab character: + * + * 1. Concertname + * 2. Venuename or numeric venue id + * 3. Concert date + * 4. Ticket link + * 5. Event info link + * + * Empty lines are ignored. + * + * @return void + * + * @param array + */ static function process_upload(array $file): void { + global $wpdb; $newconcert= []; $fo = new SplFileObject($file['tmp_name']); - + $importerrors = ''; + $rid=0; foreach ($fo as $line) { + $rid++; $line = trim( $line ); if ( empty($line) ) { // Skip empty lines continue; } - $resultArray = explode("\t", $line); - $cname = trim($resultArray[0]); - $venue = trim($resultArray[1]); + if (empty(trim($resultArray[0]))) + { $importerrors.= 'Row '.$rid.' is missing concert name!'."
"; + continue; + } + else if (empty(trim($resultArray[1]))) + { $importerrors.= 'Row '.$rid.' is missing venue name!'."
"; + continue; + } - if (is_numeric($venue)) { - $venue = GiglogAdmin_Venue::get($venue); + else if (empty(trim($resultArray[2]))) + { $importerrors.= 'Row '.$rid.' is missing city name!' ."
"; + continue; } + else { - $venue = GiglogAdmin_Venue::find_or_create($venue,'Oslo'); - } + $condate = date('Y-m-d', strtotime($resultArray[3])); + if ($condate"; + continue; } + + else { + + + $cname = trim($resultArray[0]); + $venue = trim($resultArray[1]); - $condate = date('Y-m-d', strtotime($resultArray[2])); - $ticketlink = trim($resultArray[3]); - $eventlink = trim($resultArray[4]); - GiglogAdmin_Concert::create($cname, $venue->id(), $condate, $ticketlink, $eventlink); + if (is_numeric($venue)) { + $venue = GiglogAdmin_Venue::get($venue); + } + else { + $venue = GiglogAdmin_Venue::find_or_create($venue,trim($resultArray[2])); + } + + + $ticketlink = trim($resultArray[4]); + $eventlink = trim($resultArray[5]); + + + + + try { + GiglogAdmin_Concert::create($cname, $venue->id(), $condate, $ticketlink, $eventlink); + + } + + + catch(Exception $e) { + $importerrors.= 'Row '.$rid.' is duplicate (or failed due unknown error)!'."
"; + } + } + } + + } + if (!empty($importerrors)) echo ($importerrors); + else echo ('All rows imported ok'); } } } -} diff --git a/includes/ical_export.php b/includes/ical_export.php index 4543ce0..c56e349 100644 --- a/includes/ical_export.php +++ b/includes/ical_export.php @@ -12,165 +12,68 @@ if ( ! class_exists( "GiglogAdmin_IcalExport" ) ) class Giglogadmin_IcalExport { public static function export_ical() + { + $evid = $_GET['evid']; + + $concert = GiglogAdmin_Concert::get($evid); + $cfullname = $concert->cname().' live at '. $concert->venue()->name() .', '.$concert->venue()->city(); + $fdate = strtotime($concert->cdate()); + $newformat = date('Ymd',$fdate); // create a new calendar $vcalendar = Vcalendar::factory( [ Vcalendar::UNIQUE_ID => "kigkonsult.se", ] ) - // with calendaring info - ->setMethod( Vcalendar::PUBLISH ) - ->setXprop( - Vcalendar::X_WR_CALNAME, - "Calendar Sample" + // with calendaring info + ->setMethod( Vcalendar::PUBLISH ) + ->setXprop( + Vcalendar::X_WR_CALNAME, + "Calendar Sample" ) ->setXprop( Vcalendar::X_WR_CALDESC, - "This is a demo calendar" - ) - ->setXprop( - Vcalendar::X_WR_RELCALID, - "3E26604A-50F4-4449-8B3E-E4F4932D05B5" - ) - ->setXprop( - Vcalendar::X_WR_TIMEZONE, - "Europe/Stockholm" - ); - - // create a new event - $event1 = $vcalendar->newVevent() - ->setTransp( Vcalendar::OPAQUE ) - ->setClass( Vcalendar::P_BLIC ) - ->setSequence( 1 ) - // describe the event - ->setSummary( 'Scheduled meeting with five occurrences' ) - ->setDescription( - 'Agenda for the the meeting...', - [ Vcalendar::ALTREP => - 'CID:' ] - ) - ->setComment( 'It\'s going to be fun..' ) - // place the event - ->setLocation( 'Kafé Ekorren Stockholm' ) - ->setGeo( '59.32206', '18.12485' ) - // set the time - ->setDtstart( - new DateTime( - '20190421T090000', - new DateTimezone( 'Europe/Stockholm' ) - ) - ) - ->setDtend( - new DateTime( - '20190421T100000', - new DateTimezone( 'Europe/Stockholm' ) - ) - ) - // with recurrence rule - ->setRrule( - [ - Vcalendar::FREQ => Vcalendar::WEEKLY, - Vcalendar::COUNT => 5, - ] - ) - // and set another on a specific date - ->setRdate( - [ - new DateTime( - '20190609T090000', - new DateTimezone( 'Europe/Stockholm' ) - ), - new DateTime( - '20190609T110000', - new DateTimezone( 'Europe/Stockholm' ) - ), - ], - [ Vcalendar::VALUE => Vcalendar::PERIOD ] - ) - // and revoke a recurrence date - ->setExdate( - new DateTime( - '2019-05-12 09:00:00', - new DateTimezone( 'Europe/Stockholm' ) - ) - ) - // organizer, chair and some participants - ->setOrganizer( - 'secretary@coffeebean.com', - [ Vcalendar::CN => 'Secretary CoffeeBean' ] - ) - ->setAttendee( - 'president@coffeebean.com', - [ - Vcalendar::ROLE => Vcalendar::CHAIR, - Vcalendar::PARTSTAT => Vcalendar::ACCEPTED, - Vcalendar::RSVP => Vcalendar::FALSE, - Vcalendar::CN => 'President CoffeeBean', - ] - ) - ->setAttendee( - 'participant1@coffeebean.com', - [ - Vcalendar::ROLE => Vcalendar::REQ_PARTICIPANT, - Vcalendar::PARTSTAT => Vcalendar::NEEDS_ACTION, - Vcalendar::RSVP => Vcalendar::TRUE, - Vcalendar::CN => 'Participant1 CoffeeBean', - ] - ) - ->setAttendee( - 'participant2@coffeebean.com', - [ - Vcalendar::ROLE => Vcalendar::REQ_PARTICIPANT, - Vcalendar::PARTSTAT => Vcalendar::NEEDS_ACTION, - Vcalendar::RSVP => Vcalendar::TRUE, - Vcalendar::CN => 'Participant2 CoffeeBean', - ] - ); - - // add alarm for the event - $alarm = $event1->newValarm() - ->setAction( Vcalendar::DISPLAY ) - // copy description from event - ->setDescription( $event1->getDescription()) - // fire off the alarm one day before - ->setTrigger( '-P1D' ); + "Concert ".$cfullname . "" + ) + ->setXprop( + Vcalendar::X_WR_RELCALID, + "3E26604A-50F4-4449-8B3E-E4F4932D05B5" + ) + ->setXprop( + Vcalendar::X_WR_TIMEZONE, + "Europe/Oslo" + ); - // alter day and time for one event in recurrence set - $event2 = $vcalendar->newVevent() - ->setTransp( Vcalendar::OPAQUE ) - ->setClass( Vcalendar::P_BLIC ) - // reference to event in recurrence set - ->setUid( $event1->getUid()) - ->setSequence( 2 ) - // pointer to event in the recurrence set - ->setRecurrenceid( '20190505T090000 Europe/Stockholm' ) - // reason text - ->setDescription( - 'Altered day and time for event 2019-05-05', - [ Vcalendar::ALTREP => - 'CID:' ] + // create a new event + $event1 = $vcalendar->newVevent() + ->setTransp( Vcalendar::OPAQUE ) + ->setClass( Vcalendar::P_BLIC ) + ->setSequence( 1 ) + // describe the event + ->setSummary("".$cfullname."" ) + ->setDescription("".$cfullname."" ) + ->setComment ("".$cfullname."" ) + // place the event + ->setLocation( "".$concert->venue()->name() .', '.$concert->venue()->city() ."" ) + // set the time + ->setDtstart( + new DateTime( + $newformat.'T190000', + new DateTimezone( 'Europe/Oslo' ) ) - ->setComment( 'Now we are working hard for two hours' ) - // the altered day and time with duration - ->setDtstart( - new DateTime( - '20190504T100000', - new DateTimezone( 'Europe/Stockholm' ) - ) ) - ->setDuration( 'PT2H' ) - // add alarm (copy from event1) - ->setComponent( - $event1->getComponent( Vcalendar::VALARM ) - ); + ->setDuration ("PT4H") - $vcalendarString = - // apply appropriate Vtimezone with Standard/DayLight components - $vcalendar->vtimezonePopulate() - // and create the (string) calendar - ->createCalendar(); + ; + $vcalendarString = + // apply appropriate Vtimezone with Standard/DayLight components + $vcalendar->vtimezonePopulate() + // and create the (string) calendar + ->createCalendar(); - header( 'Content-Type: text/calendar' ); - echo $vcalendarString; - } - } + header( 'Content-Type: text/calendar' ); + echo $vcalendarString; + die(); + } + } - add_action( 'wp_ajax_nopriv_giglog_export_ical', [ 'GiglogAdmin_IcalExport', 'export_ical' ] ); -} + add_action( 'wp_ajax_nopriv_giglog_export_ical', [ 'GiglogAdmin_IcalExport', 'export_ical' ] ); + add_action( 'wp_ajax_giglog_export_ical', [ 'GiglogAdmin_IcalExport', 'export_ical' ] ); + } diff --git a/includes/venue.php b/includes/venue.php index 177ba2d..5e9ecf9 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -75,7 +75,7 @@ if ( !class_exists('GiglogAdmin_Venue') ) { { global $wpdb; $venuesql = "SELECT * FROM {$wpdb->prefix}giglogadmin_venues " - . $wpdb->prepare("WHERE upper(wpgvenue_name)=upper(%s) and wpgvenue_city=%s", $name, $city); + . $wpdb->prepare("WHERE upper(wpgvenue_name)=upper(%s) and upper(wpgvenue_city)=upper(%s)", $name, $city); $results = $wpdb->get_results($venuesql); -- cgit v1.2.3
CITYDATENAMEVENUEEVENTTICKETSEVENTTICKETSCalendarPHOTO1PHOTO2TEXT1TEXT2STATUS' - . $this->adminactions($concert) - . ' iCal