From ef1da20cb88b94d7c4781cb3bcf86ccb67c5005f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 21 Feb 2022 21:04:38 +0100 Subject: Add sample ical export module. Currently just uses sample from iCalcreator docs, but shows how it can be done, and how to hook it into WordPress. --- includes/ical_export.php | 176 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 includes/ical_export.php (limited to 'includes/ical_export.php') diff --git a/includes/ical_export.php b/includes/ical_export.php new file mode 100644 index 0000000..4543ce0 --- /dev/null +++ b/includes/ical_export.php @@ -0,0 +1,176 @@ + +// SPDX-FileCopyrightText: 2022 Harald Eilertsen +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use Kigkonsult\Icalcreator\Vcalendar; + +if ( ! class_exists( "GiglogAdmin_IcalExport" ) ) +{ + class Giglogadmin_IcalExport + { + public static function export_ical() + { + // 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" + ) + ->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' ); + + // 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:' ] + ) + ->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 ) + ); + + $vcalendarString = + // apply appropriate Vtimezone with Standard/DayLight components + $vcalendar->vtimezonePopulate() + // and create the (string) calendar + ->createCalendar(); + + header( 'Content-Type: text/calendar' ); + echo $vcalendarString; + } + } + + add_action( 'wp_ajax_nopriv_giglog_export_ical', [ 'GiglogAdmin_IcalExport', 'export_ical' ] ); +} -- cgit v1.2.3