| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Models; |
| 4 |
|
| 5 |
use FluentBooking\App\Services\Helper; |
| 6 |
|
| 7 |
class Availability extends Model |
| 8 |
{ |
| 9 |
protected $table = 'fcal_meta'; |
| 10 |
|
| 11 |
protected $guarded = ['id']; |
| 12 |
|
| 13 |
protected $fillable = [ |
| 14 |
'object_type', |
| 15 |
'object_id', |
| 16 |
'key', |
| 17 |
'value' |
| 18 |
]; |
| 19 |
|
| 20 |
public static function boot() |
| 21 |
{ |
| 22 |
parent::boot(); |
| 23 |
|
| 24 |
static::creating(function ($model) { |
| 25 |
$model->object_type = 'availability'; |
| 26 |
}); |
| 27 |
|
| 28 |
static::updating(function ($model) { |
| 29 |
$model->object_type = 'availability'; |
| 30 |
}); |
| 31 |
|
| 32 |
static::addGlobalScope('object_type', function ($query) { |
| 33 |
$query->where('object_type', 'availability'); |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
public function setValueAttribute($value) |
| 38 |
{ |
| 39 |
$this->attributes['value'] = \maybe_serialize($value); |
| 40 |
} |
| 41 |
|
| 42 |
public function getValueAttribute($value) |
| 43 |
{ |
| 44 |
return \maybe_unserialize($value); |
| 45 |
} |
| 46 |
|
| 47 |
public function getAuthor() |
| 48 |
{ |
| 49 |
$user = get_user_by('ID', $this->object_id); |
| 50 |
if(!$user) { |
| 51 |
return [ |
| 52 |
'name' => __('Deleted user', 'fluent-booking'), |
| 53 |
'avatar' => '' |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
$name = trim($user->first_name . ' ' . $user->last_name); |
| 58 |
if(!$name) { |
| 59 |
$name = $user->display_name; |
| 60 |
} |
| 61 |
|
| 62 |
return [ |
| 63 |
'name' => $name, |
| 64 |
'avatar' => Helper::fluentBookingUserAvatar($user->user_email, $user) |
| 65 |
]; |
| 66 |
} |
| 67 |
} |
| 68 |
|