71 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Models;
 | 
						|
 | 
						|
use Illuminate\Database\Eloquent\Model;
 | 
						|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 | 
						|
use Illuminate\Database\Eloquent\Relations\HasOne;
 | 
						|
 | 
						|
class Event extends Model
 | 
						|
{
 | 
						|
    protected $table = 'calendarobjects'; // Sabre table
 | 
						|
    public $timestamps = false;
 | 
						|
    protected $primaryKey = 'id';
 | 
						|
 | 
						|
    /* add mass-assignment for these cols */
 | 
						|
    protected $fillable = [
 | 
						|
        'calendarid',
 | 
						|
        'uri',
 | 
						|
        'lastmodified',
 | 
						|
        'etag',
 | 
						|
        'size',
 | 
						|
        'componenttype',
 | 
						|
        'uid',
 | 
						|
        'calendardata',
 | 
						|
    ];
 | 
						|
 | 
						|
    /* casts */
 | 
						|
    protected $casts = [
 | 
						|
        'start_at' => 'datetime',
 | 
						|
        'end_at' => 'datetime',
 | 
						|
    ];
 | 
						|
 | 
						|
    /* owning calendar */
 | 
						|
    public function calendar(): BelongsTo
 | 
						|
    {
 | 
						|
        return $this->belongsTo(Calendar::class, 'calendarid');
 | 
						|
    }
 | 
						|
 | 
						|
    /* ui-specific metadata (category, reminders, extra json) */
 | 
						|
    public function meta(): HasOne
 | 
						|
    {
 | 
						|
        return $this->hasOne(EventMeta::class, 'event_id');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * filter events in event_meta by start and end time
 | 
						|
     */
 | 
						|
    public function scopeInRange($query, $start, $end)
 | 
						|
    {
 | 
						|
        return $query->whereHas('meta', function ($q) use ($start, $end) {
 | 
						|
            $q->where('start_at', '<=', $end)
 | 
						|
              ->where(function ($qq) use ($start) {
 | 
						|
                  $qq->where('end_at', '>=', $start)
 | 
						|
                     ->orWhereNull('end_at');          // open-ended events
 | 
						|
              });
 | 
						|
        });
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * ccnvenience wrapper for calendar controller
 | 
						|
     */
 | 
						|
    public static function forCalendarsInRange($calendarIds, $start, $end)
 | 
						|
    {
 | 
						|
        return static::query()
 | 
						|
            ->with('meta')                           // eager-load meta once
 | 
						|
            ->whereIn('calendarid', $calendarIds)
 | 
						|
            ->inRange($start, $end)                  // ← the scope above
 | 
						|
            ->get();
 | 
						|
    }
 | 
						|
}
 |