132 lines
4.4 KiB
PHP
132 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Subscription;
|
|
use App\Models\Calendar;
|
|
use App\Models\CalendarInstance;
|
|
use App\Models\CalendarMeta;
|
|
|
|
class SubscriptionController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$subs = Subscription::where(
|
|
'principaluri',
|
|
auth()->user()->principal_uri
|
|
)->get();
|
|
|
|
return view('subscription.index', compact('subs'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('subscription.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'source' => 'required|url',
|
|
'displayname' => 'nullable|string|max:255',
|
|
'color' => 'nullable|regex:/^#[0-9A-Fa-f]{6}$/',
|
|
'refreshrate' => 'nullable|string|max:10',
|
|
]);
|
|
|
|
DB::transaction(function () use ($request, $data) {
|
|
|
|
/* add entry into calendarsubscriptions first */
|
|
$sub = Subscription::create([
|
|
'uri' => Str::uuid(),
|
|
'principaluri' => 'principals/'.$request->user()->email,
|
|
'source' => $data['source'],
|
|
'displayname' => $data['displayname'] ?: $data['source'],
|
|
'calendarcolor' => $data['color'] ?? '#1a1a1a',
|
|
'refreshrate' => 'P1D',
|
|
'lastmodified' => now()->timestamp,
|
|
]);
|
|
|
|
/* create the empty "shadow" calendar container */
|
|
$calId = Calendar::create([
|
|
'synctoken' => 1,
|
|
'components' => 'VEVENT',
|
|
])->id;
|
|
|
|
/* create the calendarinstance row attached to the user */
|
|
CalendarInstance::create([
|
|
'calendarid' => $calId,
|
|
'principaluri' => $sub->principaluri,
|
|
'uri' => Str::uuid(),
|
|
'displayname' => $sub->displayname,
|
|
'description' => 'Remote feed: '.$sub->source,
|
|
'calendarcolor' => $sub->calendarcolor,
|
|
'timezone' => config('app.timezone', 'UTC'),
|
|
]);
|
|
|
|
/* create our calendar_meta entry */
|
|
CalendarMeta::create([
|
|
'calendar_id' => $calId,
|
|
'subscription_id' => $sub->id,
|
|
'title' => $sub->displayname,
|
|
'color' => $sub->calendarcolor,
|
|
'color_fg' => contrast_text_color($sub->calendarcolor),
|
|
'is_shared' => true,
|
|
'is_remote' => true,
|
|
]);
|
|
});
|
|
|
|
return redirect()
|
|
->route('calendar.index')
|
|
->with('toast', __('Subscription added!'));
|
|
}
|
|
|
|
public function edit(Subscription $subscription)
|
|
{
|
|
$this->authorize('update', $subscription);
|
|
return view('subscription.edit', ['subscription' => $subscription]);
|
|
}
|
|
|
|
public function update(Request $request, Subscription $subscription)
|
|
{
|
|
$this->authorize('update', $subscription);
|
|
|
|
$data = $request->validate([
|
|
'displayname' => 'nullable|string|max:255',
|
|
'calendarcolor' => 'nullable|regex:/^#[0-9A-Fa-f]{6}$/',
|
|
'refreshrate' => 'nullable|string|max:10',
|
|
'striptodos' => 'sometimes|boolean',
|
|
'stripalarms' => 'sometimes|boolean',
|
|
'stripattachments' => 'sometimes|boolean',
|
|
]);
|
|
|
|
// update calendarsubscriptions record
|
|
$subscription->update($data);
|
|
|
|
// update corresponding calendar_meta record
|
|
$subscription->meta()->updateOrCreate(
|
|
[], // no “where” clause → look at subscription_id FK
|
|
[
|
|
'title' => $subscription->displayname,
|
|
'color' => $subscription->calendarcolor ?? '#1a1a1a',
|
|
'color_fg' => contrast_text_color(
|
|
$subscription->calendarcolor ?? '#1a1a1a'
|
|
),
|
|
'updated_at'=> now(),
|
|
]
|
|
);
|
|
|
|
return back()->with('toast', __('Subscription updated!'));
|
|
}
|
|
|
|
public function destroy(Subscription $subscription)
|
|
{
|
|
$this->authorize('delete', $subscription);
|
|
$subscription->delete();
|
|
|
|
return back()->with('toast', __('Subscription removed!'));
|
|
}
|
|
}
|