phpvms/app/Models/User.php

319 lines
7.9 KiB
PHP
Raw Normal View History

2017-06-09 02:28:26 +08:00
<?php
namespace App\Models;
2017-06-09 02:28:26 +08:00
use App\Models\Enums\JournalType;
use App\Models\Traits\JournalTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2017-06-09 02:28:26 +08:00
use Illuminate\Foundation\Auth\User as Authenticatable;
2018-02-21 12:33:09 +08:00
use Illuminate\Notifications\Notifiable;
2017-12-16 02:36:13 +08:00
use Laratrust\Traits\LaratrustUserTrait;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
2017-06-09 02:28:26 +08:00
/**
* @property int id
* @property int pilot_id
* @property int airline_id
* @property string name
* @property string name_private Only first name, rest are initials
* @property string email
* @property string password
* @property string api_key
* @property mixed timezone
* @property string ident
* @property string curr_airport_id
* @property string home_airport_id
* @property string avatar
* @property Airline airline
* @property Flight[] flights
* @property int flight_time
* @property int transfer_time
* @property string remember_token
* @property \Carbon\Carbon created_at
* @property \Carbon\Carbon updated_at
* @property Rank rank
* @property Journal journal
* @property int rank_id
* @property string discord_id
* @property int state
* @property string last_ip
* @property bool opt_in
* @property Pirep[] pireps
* @property string last_pirep_id
* @property Pirep last_pirep
* @property UserFieldValue[] fields
* @property Role[] roles
* @property Subfleet[] subfleets
* @property TypeRating[] typeratings
*
2019-12-22 12:46:41 +08:00
* @mixin \Illuminate\Database\Eloquent\Builder
* @mixin \Illuminate\Notifications\Notifiable
2017-12-16 02:36:13 +08:00
* @mixin \Laratrust\Traits\LaratrustUserTrait
2017-06-09 02:28:26 +08:00
*/
class User extends Authenticatable
{
use HasFactory;
use HasRelationships;
use JournalTrait;
2017-12-16 02:36:13 +08:00
use LaratrustUserTrait;
use Notifiable;
2017-06-09 02:28:26 +08:00
public $table = 'users';
/**
* The journal type for when it's being created
*/
public $journal_type = JournalType::USER;
protected $fillable = [
'id',
'name',
'email',
'password',
'pilot_id',
'airline_id',
'rank_id',
'discord_id',
'discord_private_channel_id',
2017-12-31 02:15:26 +08:00
'api_key',
2018-01-10 09:08:16 +08:00
'country',
'home_airport_id',
'curr_airport_id',
'last_pirep_id',
'flights',
'flight_time',
2018-09-05 08:09:33 +08:00
'transfer_time',
2018-04-02 19:47:31 +08:00
'avatar',
'timezone',
'state',
'status',
'toc_accepted',
2018-05-25 11:17:41 +08:00
'opt_in',
'last_ip',
'notes',
'created_at',
'updated_at',
];
2017-06-09 02:28:26 +08:00
/**
* The attributes that should be hidden for arrays.
*/
protected $hidden = [
'api_key',
2022-02-05 03:17:07 +08:00
'email',
'name',
'discord_id',
2022-02-05 03:17:07 +08:00
'discord_private_channel_id',
'password',
'last_ip',
'remember_token',
'notes',
];
protected $casts = [
'id' => 'integer',
'pilot_id' => 'integer',
2018-09-05 09:27:16 +08:00
'flights' => 'integer',
'flight_time' => 'integer',
'transfer_time' => 'integer',
'balance' => 'double',
'state' => 'integer',
'status' => 'integer',
'toc_accepted' => 'boolean',
2018-09-05 09:27:16 +08:00
'opt_in' => 'boolean',
];
2017-06-29 08:56:10 +08:00
public static $rules = [
'name' => 'required',
'email' => 'required|email',
2019-07-17 23:00:30 +08:00
'pilot_id' => 'required|integer',
];
2018-01-01 03:08:41 +08:00
/**
* Format the pilot ID/ident
*
* @return Attribute
*/
public function ident(): Attribute
{
return Attribute::make(
get: function ($_, $attrs) {
$length = setting('pilots.id_length');
$ident_code = filled(setting('pilots.id_code')) ? setting(
'pilots.id_code'
) : optional($this->airline)->icao;
return $ident_code.str_pad($attrs['pilot_id'], $length, '0', STR_PAD_LEFT);
}
);
}
2018-02-07 00:33:39 +08:00
/**
* Return a "privatized" version of someones name - First and middle names full, last name initials
2018-08-27 00:40:04 +08:00
*
* @return Attribute
2018-02-07 00:33:39 +08:00
*/
public function namePrivate(): Attribute
2018-02-07 00:33:39 +08:00
{
return Attribute::make(
get: function ($_, $attrs) {
$name_parts = explode(' ', $attrs['name']);
$count = count($name_parts);
if ($count === 1) {
return $name_parts[0];
}
$gdpr_name = '';
$last_name = $name_parts[$count - 1];
$loop_count = 0;
while ($loop_count < ($count - 1)) {
$gdpr_name .= $name_parts[$loop_count].' ';
$loop_count++;
}
$gdpr_name .= mb_substr($last_name, 0, 1);
return mb_convert_case($gdpr_name, MB_CASE_TITLE);
}
);
2018-02-07 00:33:39 +08:00
}
2018-02-07 01:02:40 +08:00
/**
* Shortcut for timezone
2018-08-27 00:40:04 +08:00
*
* @return Attribute
2018-02-07 01:02:40 +08:00
*/
public function tz(): Attribute
2018-02-07 01:02:40 +08:00
{
return Attribute::make(
get: fn ($_, $attrs) => $attrs['timezone'],
set: fn ($value) => [
'timezone' => $value,
]
);
2018-02-07 01:02:40 +08:00
}
2018-05-25 11:17:41 +08:00
/**
2018-08-27 00:40:04 +08:00
* Return a File model
*/
public function avatar(): Attribute
{
return Attribute::make(
get: function ($_, $attrs) {
if (!$attrs['avatar']) {
return null;
}
return new File([
'path' => $attrs['avatar'],
]);
}
);
}
2018-02-07 01:02:40 +08:00
2018-01-01 03:08:41 +08:00
/**
* @param mixed $size Size of the gravatar, in pixels
2018-08-27 00:40:04 +08:00
*
2018-01-01 03:08:41 +08:00
* @return string
*/
public function gravatar($size = null)
{
2018-01-08 23:22:12 +08:00
$default = config('gravatar.default');
$uri = config('gravatar.url').md5(strtolower(trim($this->email))).'?d='.urlencode($default);
if ($size !== null) {
$uri .= '&s='.$size;
}
return $uri;
}
public function resolveAvatarUrl()
{
/** @var File $avatar */
$avatar = $this->avatar;
if (empty($avatar)) {
return $this->gravatar();
}
2020-03-30 03:05:56 +08:00
return $avatar->url;
}
2017-06-29 08:56:10 +08:00
/**
* Foreign Keys
*/
public function airline()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airline::class, 'airline_id');
2017-06-29 08:56:10 +08:00
}
/**
* @return \App\Models\Award[]|mixed
*/
public function awards()
{
return $this->belongsToMany(Award::class, 'user_awards');
}
/**
* The bid rows
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function bids()
{
return $this->hasMany(Bid::class, 'user_id');
}
2017-06-29 08:56:10 +08:00
public function home_airport()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'home_airport_id');
2017-06-29 08:56:10 +08:00
}
public function current_airport()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Airport::class, 'curr_airport_id');
2017-06-29 08:56:10 +08:00
}
public function last_pirep()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Pirep::class, 'last_pirep_id');
}
public function fields()
2017-08-04 10:02:02 +08:00
{
return $this->hasMany(UserFieldValue::class, 'user_id');
2017-08-04 10:02:02 +08:00
}
2017-06-30 04:50:16 +08:00
public function pireps()
{
return $this->hasMany(Pirep::class, 'user_id');
2017-06-30 04:50:16 +08:00
}
2017-06-29 08:56:10 +08:00
public function rank()
{
2018-01-08 23:22:12 +08:00
return $this->belongsTo(Rank::class, 'rank_id');
2017-06-29 08:56:10 +08:00
}
public function typeratings()
{
return $this->belongsToMany(
Typerating::class,
'typerating_user',
'user_id',
'typerating_id'
);
}
public function rated_subfleets()
{
return $this->hasManyDeep(
Subfleet::class,
['typerating_user', Typerating::class, 'typerating_subfleet']
);
}
2017-06-09 02:28:26 +08:00
}