| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
/** |
| 6 |
* NotificationPreference Model - DB Model for fcom_notification_prefs table |
| 7 |
* |
| 8 |
* Stores a user's explicit notification overrides as an (event x channel) matrix. |
| 9 |
* A missing row means "inherit the site default" - see NotificationPref::isEnabled(). |
| 10 |
* Only explicit overrides are stored, so a member on all defaults costs zero rows. |
| 11 |
* |
| 12 |
* Unlike the legacy pref rows in fcom_notification_users, the channel is a real |
| 13 |
* column here. Adding push (or any future channel) is new rows, not new key names. |
| 14 |
* |
| 15 |
* @package FluentCommunity\App\Models |
| 16 |
* |
| 17 |
* @property int $id |
| 18 |
* @property int $user_id |
| 19 |
* @property string $channel |
| 20 |
* @property string $event_key |
| 21 |
* @property int $object_id |
| 22 |
* @property int $value |
| 23 |
* |
| 24 |
* @version 1.0.0 |
| 25 |
*/ |
| 26 |
class NotificationPreference extends Model |
| 27 |
{ |
| 28 |
protected $table = 'fcom_notification_prefs'; |
| 29 |
|
| 30 |
protected $primaryKey = 'id'; |
| 31 |
|
| 32 |
protected $guarded = ['id']; |
| 33 |
|
| 34 |
protected $fillable = [ |
| 35 |
'user_id', |
| 36 |
'channel', |
| 37 |
'event_key', |
| 38 |
'object_id', |
| 39 |
'value' |
| 40 |
]; |
| 41 |
|
| 42 |
public function user() |
| 43 |
{ |
| 44 |
return $this->belongsTo(User::class, 'user_id'); |
| 45 |
} |
| 46 |
|
| 47 |
public function xprofile() |
| 48 |
{ |
| 49 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 50 |
} |
| 51 |
|
| 52 |
public function scopeForChannel($query, $channel) |
| 53 |
{ |
| 54 |
return $query->where('channel', $channel); |
| 55 |
} |
| 56 |
|
| 57 |
public function scopeForEvent($query, $eventKey) |
| 58 |
{ |
| 59 |
return $query->where('event_key', $eventKey); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Global prefs (not scoped to a space or other object) use object_id = 0 |
| 64 |
* rather than NULL so the unique key can enforce one row per cell. |
| 65 |
*/ |
| 66 |
public function scopeGlobalScoped($query) |
| 67 |
{ |
| 68 |
return $query->where('object_id', 0); |
| 69 |
} |
| 70 |
|
| 71 |
public function scopeEnabled($query) |
| 72 |
{ |
| 73 |
return $query->where('value', 1); |
| 74 |
} |
| 75 |
} |
| 76 |
|