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

Customizable iCal Feeds for Dynamically Adding Events to Calendar Apps #487

Merged
merged 7 commits into from
Feb 4, 2025
Merged
Changes from 1 commit
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
Next Next commit
ll
bogdankharchenko committed Jan 31, 2025
commit a226af424fda4e92d90f307bf7add20e92a7cfbd
82 changes: 82 additions & 0 deletions app/Http/Controllers/CalendarFeedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers;

use App\Enums\OrganizationStatus;
use App\Models\Event;
use App\Models\Org;
use Illuminate\Http\Request;
use Spatie\IcalendarGenerator\Components\Calendar;
use Spatie\IcalendarGenerator\Components\Event as CalendarEvent;

class CalendarFeedController extends Controller
{
public function index()
{
return view('calendar-feed.index', [
'organizations' => Org::query()
->with('category')
->where('status', OrganizationStatus::Active)
->orderBy('title')
->get()
->map(fn(Org $org) => [
'id' => $org->id,
'title' => $org->title,
'checked' => true,
]),
]);
}

public function show(Request $request)
{
$organization_ids = collect(explode('-', $request->input('orgs')));

$organizations = Org::query()
->where('status', OrganizationStatus::Active)
->when($organization_ids->isNotEmpty(), fn($query) => $query->whereIn('id', $organization_ids))
->get();

$events = Event::query()
->with('organization', 'venue.state')
->future()
->when($organization_ids->isNotEmpty(), fn($query) => $query->whereIn('organization_id', $organization_ids))
->get()
->mapWithKeys(function(Event $event, $i) {
return [
$i => CalendarEvent::create($event->event_name)
->uniqueIdentifier($event->uniqueIdentifierHash())
->startsAt($event->active_at)
->endsAt($event->expire_at)
->address($event->venue?->fullAddress() ?? 'Virtual Event')
->description($event->description)
->url($event->url),
];
})
->toArray();

// Calendar
$calendar = Calendar::create()
->timezone(['America/New_York']);

if ($organizations->count() === 1) {
$organization = $organizations->first();

$calendar
->productIdentifier($organization->title.' Event Calendar')
->name($organization->title.' Event Calendar')
->description($organization->description ?? '')
->source(route('orgs.show', $organization));
} else {
$calendar
->productIdentifier('HackGreenville.com Event Calendar')
->name('HackGreenville Event Calendar')
->description('Tech Events in Greenville SC!')
->source(route('calendar.index'));
}

$data = $calendar->event($events)->get();

return response($data)
->header('Content-Type', 'text/calendar; charset=utf-8');
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
"laravel/tinker": "^2.0",
"malzariey/filament-daterangepicker-filter": "^2.7",
"scyllaly/hcaptcha": "^4.4",
"spatie/icalendar-generator": "^2.9",
"spatie/laravel-data": "*"
},
"require-dev": {
139 changes: 137 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading