97 lines
2.5 KiB
PHP
Executable File
97 lines
2.5 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* Represents the relationship between a user and a subject they are subscribed to.
|
|
*
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int $subject_id
|
|
* @property bool $is_active
|
|
* @property string $frequency Frequency of insight delivery (daily, weekly, biweekly, monthly)
|
|
* @property array|null $preferred_times Array of preferred delivery times in H:i format
|
|
* @property string|null $preferred_time (deprecated) Single preferred time, use preferred_times instead
|
|
* @property array|null $weekdays Days of the week for delivery (0=Sunday, 6=Saturday)
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read User $user
|
|
* @property-read Subject $subject
|
|
*/
|
|
class UserSubject extends Model
|
|
{
|
|
/** @var array<int, string> */
|
|
protected $fillable = [
|
|
'user_id',
|
|
'subject_id',
|
|
'is_active',
|
|
'frequency',
|
|
'preferred_times',
|
|
'weekdays',
|
|
'insight_language',
|
|
'delivery_channels',
|
|
];
|
|
|
|
/** @var array<string, string> */
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'weekdays' => 'array',
|
|
'preferred_times' => 'array',
|
|
'delivery_channels' => 'array',
|
|
];
|
|
|
|
/**
|
|
* Get default delivery channels.
|
|
*/
|
|
public static function getDefaultChannels(): array
|
|
{
|
|
return ['dashboard'];
|
|
}
|
|
|
|
/**
|
|
* Check if a specific delivery channel is enabled.
|
|
*/
|
|
public function hasChannel(string $channel): bool
|
|
{
|
|
$channels = $this->delivery_channels ?? self::getDefaultChannels();
|
|
return in_array($channel, $channels, true);
|
|
}
|
|
|
|
/**
|
|
* Get the user associated with this user subject.
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* Get the subject associated with this user subject.
|
|
*/
|
|
public function subject(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Subject::class);
|
|
}
|
|
|
|
/**
|
|
* Get the preferred times as a formatted string for display.
|
|
*/
|
|
public function getFormattedTimesAttribute(): string
|
|
{
|
|
if (empty($this->preferred_times)) {
|
|
return __('Any time');
|
|
}
|
|
|
|
$times = collect($this->preferred_times)
|
|
->map(fn ($time) => substr($time, 0, 5))
|
|
->implode(', ');
|
|
|
|
return $times;
|
|
}
|
|
}
|