summaryrefslogtreecommitdiffstats
path: root/includes/giglogadmin-export-ical-handler.php
blob: 94309fca18f22271fcf0b405869dd2e793acb329 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?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 ( ! function_exists( 'giglogadmin_export_ical_handler' ) ) {
    /**
     * Function to handle ICAL export of concert data.
     */
    function giglogadmin_export_ical_handler() {
        if ( ! isset( $_GET['evid'] ) || ! is_numeric( $_GET['evid'] ) ) {
            wp_die( 'Invalid request, invalid or missing event id.' );
        }

        $evid = intval( $_GET['evid'] );

        $concert = GiglogAdmin_Concert::get( $evid );
        if ( ! $concert ) {
            wp_die( 'Invalid request, unknown event id.' );
        }

        $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 = sanitize_file_name(
            "Concert-{$concert->venue()->name()}-{$newformat}-"
            . substr( $concert->cname(), 0, 30 )
        );

        // 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' );
        $output =
            // 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 $output;
        die();
    }

    add_action( 'wp_ajax_nopriv_giglog_export_ical', 'giglogadmin_export_ical_handler' );
    add_action( 'wp_ajax_giglog_export_ical', 'giglogadmin_export_ical_handler' );
}