66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class Calendar extends Model
|
|
{
|
|
protected $table = 'calendars'; // Sabre table
|
|
public $timestamps = false; // no created_at/updated_at
|
|
|
|
/* add mass-assignment for these cols */
|
|
protected $fillable = [
|
|
'displayname',
|
|
'description',
|
|
];
|
|
|
|
/* all event components (VEVENT, VTODO, …) */
|
|
public function events(): HasMany
|
|
{
|
|
return $this->hasMany(Event::class, 'calendarid'); // FK in calendarobjects
|
|
}
|
|
|
|
/* ui-specific metadata (color, sharing flags, json settings) */
|
|
public function meta(): HasOne
|
|
{
|
|
return $this->hasOne(CalendarMeta::class, 'calendar_id');
|
|
}
|
|
|
|
/* get instances */
|
|
public function instances()
|
|
{
|
|
return $this->hasMany(CalendarInstance::class, 'calendarid');
|
|
}
|
|
|
|
/* get the primary? instance for a user */
|
|
public function instanceForUser(?User $user = null)
|
|
{
|
|
$user = $user ?? auth()->user();
|
|
|
|
return $this->instances()
|
|
->where('principaluri', 'principals/' . $user->email)
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* convert "/calendar/{slug}" into the correct calendar instance (uri column)
|
|
*
|
|
* @param mixed $value The URI segment (instance UUID).
|
|
* @param string|null $field Ignored in our override.
|
|
*/
|
|
public function resolveRouteBinding($value, $field = null): mixed
|
|
{
|
|
return $this->whereHas('instances', function (Builder $q) use ($value) {
|
|
$q->where('uri', $value)
|
|
->where('principaluri', Auth::user()->principal_uri);
|
|
})
|
|
->with('instances')
|
|
->firstOrFail();
|
|
}
|
|
}
|