Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iCal/ICS support #96

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions app/Http/Controllers/ICalController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Http\Controllers;

use \App\Models\Actie;

class ICalController extends Controller
{

/**
* Gets the events data from the database
* and populates the iCal object.
*
* @return void
*/
public function generate($id = null)
{
if ($id === null) {
$events = Actie::published()->nietAfgelopen()->get();
} else {
$events = Actie::findOrFail($id);
if ($events === false) {
dd($events);
}
}


define('ICAL_FORMAT', 'Ymd\THis\Z');

$icalObject = "BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
PRODID:-//Watkanikdoen.nl//Acties//NL\n";

// loop over events
foreach ($events as $event) {
$icalObject .=
"BEGIN:VEVENT
DTSTART:" . date(ICAL_FORMAT, strtotime($event->starts)) . "
DTEND:" . date(ICAL_FORMAT, strtotime($event->ends)) . "
DTSTAMP:" . date(ICAL_FORMAT, strtotime($event->created_at)) . "
SUMMARY:$event->excerpt
UID:$event->id
STATUS:" . strtoupper($event->status) . "
LAST-MODIFIED:" . date(ICAL_FORMAT, strtotime($event->updated_at)) . "
LOCATION:$event->location
END:VEVENT\n";
}

// close calendar
$icalObject .= "END:VCALENDAR";

// Set the headers
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="cal.ics"');

$icalObject = str_replace(' ', '', $icalObject);

echo $icalObject;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"livewire/livewire": "^3.4",
"matanyadaev/laravel-eloquent-spatial": "^4.2",
"orangehill/iseed": "^3.0",
"spatie/icalendar-generator": "^2.6",
"tcg/voyager": "^1.7",
"tymon/jwt-auth": "^2.1"
},
Expand Down
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Http\Controllers\BlogController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ICalController;
use App\Http\Controllers\ImageController;
use App\Http\Controllers\LanguageController;
use App\Http\Controllers\NotificationController;
Expand Down Expand Up @@ -78,6 +79,10 @@
Route::get('subscriber/verify/{id}/{hash}', [SubscriberController::class, 'verify'])->name('subscribers.verify');
Route::get('subscriber/verified', [SubscriberController::class, 'verified'])->name('subscribers.verified');

// iCal route
Route::get('ical/feed', [ICalController::class, 'generate'])->name('ical.feed');
Route::get('ical/{id}', [ICalController::class, 'generate'])->name('ical.single');

// Widget route
Route::get('widget', [WidgetController::class, 'index'])->name('widget');

Expand Down
21 changes: 21 additions & 0 deletions test.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:spatie/icalendar-generator
NAME:Laracon online
X-WR-CALNAME:Laracon online
BEGIN:VTIMEZONE
TZID:UTC
BEGIN:STANDARD
DTSTART:20230610T150000Z
TZOFFSETFROM:+0000
TZOFFSETTO:+0000
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
UID:65fdb4771b7ea
DTSTAMP:20240322T164023Z
SUMMARY:Creating calender feeds
DTSTART:20240306T150000Z
DTEND:20240306T160000Z
END:VEVENT
END:VCALENDAR
Loading