| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Models; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Model; |
| 6 |
|
| 7 |
class User extends Model |
| 8 |
{ |
| 9 |
protected $table = 'users'; |
| 10 |
|
| 11 |
protected $guarded = ['ID', 'user_pass', 'user_activation_key']; |
| 12 |
|
| 13 |
protected $hidden = ['user_pass', 'user_activation_key']; |
| 14 |
|
| 15 |
protected $appends = ['full_name']; |
| 16 |
|
| 17 |
protected $primaryKey = 'ID'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @return \FluentBooking\Framework\Database\Orm\Relations\HasMany |
| 21 |
*/ |
| 22 |
public function calendars() |
| 23 |
{ |
| 24 |
return $this->hasMany(Calendar::class, 'user_id'); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @return \FluentBooking\Framework\Database\Orm\Relations\BelongsToMany |
| 29 |
*/ |
| 30 |
public function bookings() |
| 31 |
{ |
| 32 |
return $this->belongsToMany(CalendarSlot::class, 'fcal_booking_hosts', 'user_id', 'booking_id') |
| 33 |
->withPivot('status'); |
| 34 |
} |
| 35 |
|
| 36 |
public function user() { |
| 37 |
return get_user_by('ID', $this->ID); |
| 38 |
} |
| 39 |
|
| 40 |
public function getFullNameAttribute() { |
| 41 |
$user = $this->user(); |
| 42 |
$name = trim($user->first_name . ' ' . $user->last_name); |
| 43 |
if(!$name) { |
| 44 |
$name = $user->display_name; |
| 45 |
} |
| 46 |
return $name; |
| 47 |
} |
| 48 |
|
| 49 |
public function staff() { |
| 50 |
return $this->hasOne(Staff::class, 'object_id'); |
| 51 |
} |
| 52 |
|
| 53 |
public function metas() |
| 54 |
{ |
| 55 |
return $this->hasMany(Meta::class, 'object_id', 'ID') |
| 56 |
->where('object_type', 'user_meta'); |
| 57 |
} |
| 58 |
|
| 59 |
public function getMeta($key, $default = null) |
| 60 |
{ |
| 61 |
if ($this->relationLoaded('metas')) { |
| 62 |
$meta = $this->metas->firstWhere('key', $key); |
| 63 |
} else { |
| 64 |
$meta = Meta::where('object_type', 'user_meta') |
| 65 |
->where('object_id', $this->ID) |
| 66 |
->where('key', $key) |
| 67 |
->first(); |
| 68 |
} |
| 69 |
|
| 70 |
if (!$meta) { |
| 71 |
return $default; |
| 72 |
} |
| 73 |
|
| 74 |
return $meta->value; |
| 75 |
} |
| 76 |
|
| 77 |
public function updateMeta($key, $value) |
| 78 |
{ |
| 79 |
$exist = Meta::where('object_type', 'user_meta') |
| 80 |
->where('object_id', $this->ID) |
| 81 |
->where('key', $key) |
| 82 |
->first(); |
| 83 |
|
| 84 |
if ($exist) { |
| 85 |
$exist->value = $value; |
| 86 |
$exist->save(); |
| 87 |
} else { |
| 88 |
$exist = Meta::create([ |
| 89 |
'object_type' => 'user_meta', |
| 90 |
'object_id' => $this->ID, |
| 91 |
'key' => $key, |
| 92 |
'value' => $value |
| 93 |
]); |
| 94 |
} |
| 95 |
|
| 96 |
return $exist; |
| 97 |
} |
| 98 |
} |
| 99 |
|