kithkin/app/Http/Controllers/CalendarSettingsController.php

63 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
class CalendarSettingsController extends Controller
{
/* landing page list of settings choices */
public function index()
{
return $this->frame('calendar.settings.subscribe');
}
/* show “Subscribe to a calendar” form */
public function subscribeForm()
{
return $this->frame(
'calendar.settings.subscribe',
[
'title' => 'Subscribe to a calendar',
'sub' => 'Add an `.ics` calender from another service'
]);
}
/* handle POST from the subscribe form */
public function subscribeStore(Request $request)
{
$data = $request->validate([
'source' => ['required', 'url'],
'displayname' => ['nullable', 'string', 'max:100'],
'color' => ['nullable', 'regex:/^#[0-9A-F]{6}$/i'],
]);
DB::table('calendarsubscriptions')->insert([
'uri' => Str::uuid(), // local id
'principaluri' => 'principals/'.$request->user()->email,
'source' => $data['source'],
'displayname' => $data['displayname'] ?: $data['source'],
'calendarcolor' => $data['color'],
'refreshrate' => 'P1D', // daily
'lastmodified' => now()->timestamp,
]);
return redirect()
->route('calendar.settings')
->with('toast', __('Subscription added successfully!'));
}
/**
* content frame handler
*/
private function frame(?string $view = null, array $data = [])
{
return view('calendar.settings.index', [
'view' => $view,
'data' => $data,
]);
}
}