80 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Models;
 | 
						|
 | 
						|
use Illuminate\Database\Eloquent\Model;
 | 
						|
use Illuminate\Support\Facades\DB;
 | 
						|
use Illuminate\Support\Str;
 | 
						|
use App\Models\User;
 | 
						|
 | 
						|
class Subscription extends Model
 | 
						|
{
 | 
						|
    /** basic table mapping */
 | 
						|
    protected $table      = 'calendarsubscriptions';
 | 
						|
    public    $timestamps = false;           // sabre table
 | 
						|
 | 
						|
    protected $fillable = [
 | 
						|
        'uri',
 | 
						|
        'principaluri',
 | 
						|
        'source',
 | 
						|
        'displayname',
 | 
						|
        'calendarcolor',
 | 
						|
        'refreshrate',
 | 
						|
        'calendarorder',
 | 
						|
        'striptodos',
 | 
						|
        'stripalarms',
 | 
						|
        'stripattachments',
 | 
						|
    ];
 | 
						|
 | 
						|
    protected $casts = [
 | 
						|
        'striptodos'       => 'bool',
 | 
						|
        'stripalarms'      => 'bool',
 | 
						|
        'stripattachments' => 'bool',
 | 
						|
    ];
 | 
						|
 | 
						|
    /** relationship to meta row */
 | 
						|
    public function meta()
 | 
						|
    {
 | 
						|
        return $this->hasOne(\App\Models\CalendarMeta::class,
 | 
						|
                             'subscription_id');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Create a remote calendar subscription and its UI metadata (calendar_meta).
 | 
						|
     */
 | 
						|
    public static function createWithMeta(User $user, array $data): self
 | 
						|
    {
 | 
						|
        return DB::transaction(function () use ($user, $data) {
 | 
						|
 | 
						|
            /** insert into calendarsubscriptions */
 | 
						|
            $sub = self::create([
 | 
						|
                'uri'           => (string) Str::uuid(),
 | 
						|
                'principaluri'  => $user->principal_uri,
 | 
						|
                'source'        => $data['source'],
 | 
						|
                'displayname'   => $data['displayname'] ?? $data['source'],
 | 
						|
                'calendarcolor' => $data['calendarcolor'] ?? null,
 | 
						|
                'refreshrate'   => $data['refreshrate'] ?? 'P1D',
 | 
						|
                'calendarorder' => 0,
 | 
						|
                'striptodos'       => false,
 | 
						|
                'stripalarms'      => false,
 | 
						|
                'stripattachments' => false,
 | 
						|
                'lastmodified'  => now()->timestamp,
 | 
						|
            ]);
 | 
						|
 | 
						|
            /** create corresponding calendar_meta row */
 | 
						|
            DB::table('calendar_meta')->insert([
 | 
						|
                'subscription_id' => $sub->id,
 | 
						|
                'is_remote'       => true,
 | 
						|
                'title'           => $sub->displayname,
 | 
						|
                'color'           => $sub->calendarcolor ?? '#1a1a1a',
 | 
						|
                'color_fg'        => contrast_text_color($sub->calendarcolor ?? '#1a1a1a'),
 | 
						|
                'is_shared'       => true,
 | 
						|
                'created_at'      => now(),
 | 
						|
                'updated_at'      => now(),
 | 
						|
            ]);
 | 
						|
 | 
						|
            return $sub;
 | 
						|
        });
 | 
						|
    }
 | 
						|
}
 |