kithkin/database/seeders/DatabaseSeeder.php

106 lines
3.1 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\User;
class DatabaseSeeder extends Seeder
{
public function run(): void
{
/** credentials from .env (with sensible fall-backs) */
$email = env('ADMIN_EMAIL', 'admin@example.com');
$password = env('ADMIN_PASSWORD', 'changeme');
$name = env('ADMIN_NAME', 'Admin');
/** create or update the admin user */
$user = User::updateOrCreate(
['email' => $email],
[
'name' => $name,
'password' => Hash::make($password),
]
);
/** fill the Sabre-friendly columns once we know the ULID */
$user->update([
'uri' => 'principals/'.$user->id,
'displayname' => $user->name,
]);
/** sample caldav data */
$calId = DB::table('calendars')->insertGetId([
'synctoken' => 1,
'components' => 'VEVENT',
]);
$instanceId = DB::table('calendarinstances')->insertGetId([
'calendarid' => $calId,
'principaluri' => $user->uri, // uses new column
'uri' => Str::uuid(),
'displayname' => 'Sample Calendar',
'description' => 'Seeded calendar',
'calendarorder' => 0,
'calendarcolor' => '#007db6',
'timezone' => config('app.timezone', 'UTC'),
]);
DB::table('calendar_meta')->updateOrInsert(
['calendar_id' => $instanceId],
['color' => '#007AFF']
);
/** sample vevent */
$uid = Str::uuid().'@kithkin.lan';
$start = Carbon::now();
$end = Carbon::now()->addHour();
$ical = <<<ICS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Kithkin//Laravel CalDAV//EN
BEGIN:VEVENT
UID:$uid
DTSTAMP:{$start->utc()->format('Ymd\\THis\\Z')}
DTSTART:{$start->format('Ymd\\THis')}
DTEND:{$end->format('Ymd\\THis')}
SUMMARY:Seed Event
DESCRIPTION:Automatically seeded event
LOCATION:Home Office
END:VEVENT
END:VCALENDAR
ICS;
$eventId = DB::table('calendarobjects')->insertGetId([
'calendarid' => $calId,
'uri' => Str::uuid().'.ics',
'lastmodified' => time(),
'etag' => md5($ical),
'size' => strlen($ical),
'componenttype' => 'VEVENT',
'uid' => $uid,
'calendardata' => $ical,
]);
DB::table('event_meta')->updateOrInsert(
['event_id' => $eventId],
[
'title' => 'Seed Event',
'description' => 'Automatically seeded event',
'location' => 'Home Office',
'all_day' => false,
'category' => 'Demo',
'start_at' => $start,
'end_at' => $end,
'created_at' => now(),
'updated_at' => now(),
]
);
}
}