summaryrefslogtreecommitdiffstats
path: root/includes/class-giglogadmin-icalexport.php
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2023-01-19 20:17:55 +0100
committerHarald Eilertsen <haraldei@anduin.net>2023-01-19 20:17:55 +0100
commitf499d9e657fe79e4413eec9e20ae13d616fac6f5 (patch)
tree616588346d0dc9337b2cd096cf2af7320dcc060f /includes/class-giglogadmin-icalexport.php
parenta5a9bb640306a69ec8f9a3c3701c49f34d3e7ebc (diff)
downloadgigologadmin-f499d9e657fe79e4413eec9e20ae13d616fac6f5.tar.gz
gigologadmin-f499d9e657fe79e4413eec9e20ae13d616fac6f5.tar.bz2
gigologadmin-f499d9e657fe79e4413eec9e20ae13d616fac6f5.zip
Rename and restructure source files to conform to common namin schemes.
- Source files containing a class should only contain _one_ class. - Source files containing a class should be names class-[name of the class].php - Use dashes instead of underscores in file names. - Fix source file comments - Some nitpicking...
Diffstat (limited to 'includes/class-giglogadmin-icalexport.php')
-rw-r--r--includes/class-giglogadmin-icalexport.php95
1 files changed, 95 insertions, 0 deletions
diff --git a/includes/class-giglogadmin-icalexport.php b/includes/class-giglogadmin-icalexport.php
new file mode 100644
index 0000000..ba1d7f0
--- /dev/null
+++ b/includes/class-giglogadmin-icalexport.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Class to handle ICAL export of concert data.
+ *
+ * @package giglogadmin
+ *
+ * SPDX-FileCopyrightText: 2022 Andrea Chirulescu <andrea.chirulescu@gmail.com>
+ * SPDX-FileCopyrightText: 2022 Harald Eilertsen <haraldei@anduin.net>
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+use Kigkonsult\Icalcreator\Vcalendar;
+
+if ( ! class_exists( 'GiglogAdmin_IcalExport' ) ) {
+ /**
+ * Class to handle ICAL export of concert data.
+ */
+ class Giglogadmin_IcalExport {
+
+ static function textclean( string $txt ) : string {
+ $cleantext = preg_replace( '/[^A-Za-z0-9 ]/', '', $txt );
+ $cleantext = str_replace( ' ', '_', $cleantext );
+ return( $cleantext );
+ }
+
+ public static function export_ical() {
+ $evid = $_GET['evid'];
+
+ $concert = GiglogAdmin_Concert::get( $evid );
+ $cfullname = $concert->cname() . ' live at ' . $concert->venue()->name() . ', ' . $concert->venue()->city();
+ $cshortname = substr( $cfullname, 0, 20 );
+ $fdate = strtotime( $concert->cdate() );
+ $newformat = date( 'Ymd', $fdate );
+ $filename = 'Concert ' . $concert->venue()->name() . ' ' . $newformat . ' ' . substr( $concert->cname(), 0, 30 );
+ $filename = self::textclean( $filename );
+ // create a new calendar
+ $vcalendar = Vcalendar::factory( array( Vcalendar::UNIQUE_ID => 'kigkonsult.se' ) )
+ // with calendaring info
+ ->setMethod( Vcalendar::PUBLISH )
+ ->setXprop(
+ Vcalendar::X_WR_CALNAME,
+ 'Calendar Sample'
+ )
+ ->setXprop(
+ Vcalendar::X_WR_CALDESC,
+ 'Concert ' . $cfullname . ''
+ )
+ ->setXprop(
+ Vcalendar::X_WR_RELCALID,
+ '3E26604A-50F4-4449-8B3E-E4F4932D05B5'
+ )
+ ->setXprop(
+ Vcalendar::X_WR_TIMEZONE,
+ 'Europe/Oslo'
+ );
+
+ // 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' )
+ )
+ )
+ ->setDuration( 'PT4H' );
+ $vcalendarString =
+ // apply appropriate Vtimezone with Standard/DayLight components
+ $vcalendar->vtimezonePopulate()
+ // and create the (string) calendar
+ ->createCalendar();
+
+ header( 'Content-Type: text/calendar' );
+ header( 'content-disposition: attachment;filename=' . $filename . '.ics' );
+ echo $vcalendarString;
+ die();
+ }
+ }
+
+ /** @psalm-suppress HookNotFound */
+ add_action( 'wp_ajax_nopriv_giglog_export_ical', array( 'GiglogAdmin_IcalExport', 'export_ical' ) );
+
+ /** @psalm-suppress HookNotFound */
+ add_action( 'wp_ajax_giglog_export_ical', array( 'GiglogAdmin_IcalExport', 'export_ical' ) );
+}