kithkin/database/seeders/DatabaseSeeder.php

155 lines
4.6 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');
$firstname = env('ADMIN_FIRSTNAME', 'Admin');
$lastname = env('ADMIN_LASTNAME', 'Account');
$timezone = env('APP_TIMEZONE', 'UTC');
/** create or update the admin user */
$user = User::updateOrCreate(
['email' => $email],
[
'firstname' => $firstname,
'lastname' => $lastname,
'timezone' => $timezone,
'password' => Hash::make($password),
]
);
/** fill the sabre-friendly columns */
$user->update([
'uri' => 'principals/'.$user->email,
'displayname' => $firstname.' '.$lastname,
]);
/** 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(),
]
);
/** create cards */
$bookId = DB::table('addressbooks')->insertGetId([
'principaluri' => $user->uri,
'uri' => 'default',
'displayname' => 'Default Address Book',
]);
$vcard = <<<VCF
BEGIN:VCARD
VERSION:3.0
FN:Seeded Contact
EMAIL:seeded@example.com
TEL:+1-555-123-4567
UID:seeded-contact-001
END:VCARD
VCF;
DB::table('addressbook_meta')->insert([
'addressbook_id' => $bookId,
'color' => '#ff40ff',
'is_default' => true,
'settings' => null,
'created_at' => now(),
'updated_at' => now(),
]);
$cardId = DB::table('cards')->insertGetId([
'addressbookid' => $bookId,
'uri' => Str::uuid().'.vcf',
'lastmodified' => now()->timestamp,
'etag' => md5($vcard),
'size' => strlen($vcard),
'carddata' => $vcard,
]);
DB::table('contact_meta')->insert([
'card_id' => $cardId,
'avatar_url' => null,
'tags' => 'demo,seed',
'notes' => 'Seeded contact from DatabaseSeeder.',
'created_at' => now(),
'updated_at' => now(),
]);
}
}