2017-06-09 02:28:26 +08:00
|
|
|
<?php
|
|
|
|
|
2017-06-11 09:10:31 +08:00
|
|
|
namespace App\Models;
|
2017-06-09 02:28:26 +08:00
|
|
|
|
2018-03-06 09:55:48 +08:00
|
|
|
use App\Models\Enums\JournalType;
|
2018-02-11 03:25:40 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
2018-03-02 06:20:13 +08:00
|
|
|
use App\Models\Traits\JournalTrait;
|
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;
|
2017-06-09 02:28:26 +08:00
|
|
|
|
|
|
|
/**
|
2020-08-12 05:48:51 +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 int state
|
|
|
|
* @property bool opt_in
|
|
|
|
* @property string last_pirep_id
|
2020-10-23 04:25:52 +08:00
|
|
|
* @property Pirep last_pirep
|
2020-08-12 05:48:51 +08:00
|
|
|
* @property UserFieldValue[] fields
|
2021-04-23 22:33:13 +08:00
|
|
|
* @property Role[] roles
|
|
|
|
* @property Subfleet[] subfleets
|
2019-11-27 22:19:20 +08:00
|
|
|
*
|
2019-12-22 12:46:41 +08:00
|
|
|
* @mixin \Illuminate\Database\Eloquent\Builder
|
2017-12-13 06:58:27 +08:00
|
|
|
* @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
|
|
|
|
{
|
2018-03-02 06:20:13 +08:00
|
|
|
use JournalTrait;
|
2017-12-16 02:36:13 +08:00
|
|
|
use LaratrustUserTrait;
|
2018-03-02 06:20:13 +08:00
|
|
|
use Notifiable;
|
2017-06-09 02:28:26 +08:00
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
public $table = 'users';
|
|
|
|
|
2018-03-06 09:55:48 +08:00
|
|
|
/**
|
|
|
|
* The journal type for when it's being created
|
|
|
|
*/
|
|
|
|
public $journal_type = JournalType::USER;
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
protected $fillable = [
|
2019-07-17 01:54:14 +08:00
|
|
|
'id',
|
2017-12-13 06:58:27 +08:00
|
|
|
'name',
|
|
|
|
'email',
|
|
|
|
'password',
|
2019-07-17 01:54:14 +08:00
|
|
|
'pilot_id',
|
2017-12-13 06:58:27 +08:00
|
|
|
'airline_id',
|
2018-01-04 05:41:21 +08:00
|
|
|
'rank_id',
|
2017-12-31 02:15:26 +08:00
|
|
|
'api_key',
|
2018-01-10 09:08:16 +08:00
|
|
|
'country',
|
2017-12-13 06:58:27 +08:00
|
|
|
'home_airport_id',
|
|
|
|
'curr_airport_id',
|
2017-12-26 05:19:34 +08:00
|
|
|
'last_pirep_id',
|
2018-01-04 05:41:21 +08:00
|
|
|
'flights',
|
|
|
|
'flight_time',
|
2018-09-05 08:09:33 +08:00
|
|
|
'transfer_time',
|
2018-04-02 19:47:31 +08:00
|
|
|
'avatar',
|
2017-12-13 06:58:27 +08:00
|
|
|
'timezone',
|
2017-12-23 02:46:46 +08:00
|
|
|
'state',
|
|
|
|
'status',
|
2018-09-21 00:14:18 +08:00
|
|
|
'toc_accepted',
|
2018-05-25 11:17:41 +08:00
|
|
|
'opt_in',
|
2018-01-30 08:14:55 +08:00
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
2017-12-13 06:58:27 +08:00
|
|
|
];
|
2017-06-09 02:28:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*/
|
2017-12-13 02:43:58 +08:00
|
|
|
protected $hidden = [
|
2018-01-31 02:31:59 +08:00
|
|
|
'api_key',
|
2017-12-13 02:43:58 +08:00
|
|
|
'password',
|
|
|
|
'remember_token',
|
|
|
|
];
|
2017-07-04 14:05:37 +08:00
|
|
|
|
2017-12-13 02:43:58 +08:00
|
|
|
protected $casts = [
|
2019-07-17 01:54:14 +08:00
|
|
|
'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',
|
2018-09-21 00:14:18 +08:00
|
|
|
'toc_accepted' => 'boolean',
|
2018-09-05 09:27:16 +08:00
|
|
|
'opt_in' => 'boolean',
|
2017-12-13 02:43:58 +08:00
|
|
|
];
|
2017-06-29 08:56:10 +08:00
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
public static $rules = [
|
2019-07-17 21:48:20 +08:00
|
|
|
'name' => 'required',
|
|
|
|
'email' => 'required|email',
|
2019-07-17 23:00:30 +08:00
|
|
|
'pilot_id' => 'required|integer',
|
2017-12-23 06:32:21 +08:00
|
|
|
];
|
|
|
|
|
2018-01-01 03:08:41 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-04-07 00:03:19 +08:00
|
|
|
public function getIdentAttribute(): string
|
2017-07-03 10:25:48 +08:00
|
|
|
{
|
2017-12-13 02:43:58 +08:00
|
|
|
$length = setting('pilots.id_length');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2019-07-17 02:06:13 +08:00
|
|
|
return $this->airline->icao.str_pad($this->pilot_id, $length, '0', STR_PAD_LEFT);
|
2018-01-01 03:08:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:03:19 +08:00
|
|
|
/**
|
|
|
|
* Return a "privatized" version of someones name - First name full, rest of the names are initials
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNamePrivateAttribute(): string
|
|
|
|
{
|
|
|
|
$name_parts = explode(' ', $this->attributes['name']);
|
|
|
|
$count = count($name_parts);
|
|
|
|
if ($count === 1) {
|
|
|
|
return $name_parts[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$first_name = $name_parts[0];
|
|
|
|
$last_name = $name_parts[$count - 1];
|
|
|
|
|
|
|
|
return $first_name.' '.$last_name[0];
|
|
|
|
}
|
|
|
|
|
2018-02-07 00:33:39 +08:00
|
|
|
/**
|
2018-02-07 01:02:40 +08:00
|
|
|
* Shorthand for getting the timezone
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-07 01:02:40 +08:00
|
|
|
* @return string
|
2018-02-07 00:33:39 +08:00
|
|
|
*/
|
2018-02-07 01:02:40 +08:00
|
|
|
public function getTzAttribute(): string
|
2018-02-07 00:33:39 +08:00
|
|
|
{
|
|
|
|
return $this->timezone;
|
|
|
|
}
|
|
|
|
|
2018-02-07 01:02:40 +08:00
|
|
|
/**
|
|
|
|
* Shorthand for setting the timezone
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-07 01:02:40 +08:00
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setTzAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['timezone'] = $value;
|
|
|
|
}
|
2018-05-25 11:17:41 +08:00
|
|
|
|
2018-05-01 23:28:47 +08:00
|
|
|
/**
|
2018-08-27 00:40:04 +08:00
|
|
|
* Return a File model
|
|
|
|
*/
|
2018-05-01 23:28:47 +08:00
|
|
|
public function getAvatarAttribute()
|
|
|
|
{
|
|
|
|
if (!$this->attributes['avatar']) {
|
2020-03-25 22:58:03 +08:00
|
|
|
return null;
|
2018-05-01 23:28:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new File([
|
2020-01-07 02:44:43 +08:00
|
|
|
'path' => $this->attributes['avatar'],
|
2018-05-01 23:28:47 +08:00
|
|
|
]);
|
|
|
|
}
|
2018-02-07 01:02:40 +08:00
|
|
|
|
2018-01-01 03:08:41 +08:00
|
|
|
/**
|
2018-01-20 06:07:31 +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
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function gravatar($size = null)
|
2017-08-12 01:06:17 +08:00
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
$default = config('gravatar.default');
|
2018-01-20 06:07:31 +08:00
|
|
|
|
|
|
|
$uri = config('gravatar.url')
|
2018-03-20 09:50:40 +08:00
|
|
|
.md5(strtolower(trim($this->email))).'?d='.urlencode($default);
|
2018-01-20 06:07:31 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($size !== null) {
|
2018-01-20 06:07:31 +08:00
|
|
|
$uri .= '&s='.$size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uri;
|
2017-08-12 01:06:17 +08:00
|
|
|
}
|
|
|
|
|
2020-03-30 01:33:14 +08:00
|
|
|
public function resolveAvatarUrl()
|
|
|
|
{
|
|
|
|
$avatar = $this->getAvatarAttribute();
|
|
|
|
if (empty($avatar)) {
|
|
|
|
return $this->gravatar();
|
|
|
|
}
|
2020-03-30 03:05:56 +08:00
|
|
|
return $avatar->url;
|
2020-03-30 01:33:14 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-12 04:06:09 +08:00
|
|
|
/**
|
|
|
|
* @return \App\Models\Award[]|mixed
|
|
|
|
*/
|
2018-03-17 13:18:03 +08:00
|
|
|
public function awards()
|
|
|
|
{
|
2020-10-12 04:06:09 +08:00
|
|
|
return $this->belongsToMany(Award::class, 'user_awards');
|
2018-03-17 13:18:03 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 05:48:51 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
public function last_pirep()
|
|
|
|
{
|
2018-01-08 23:22:12 +08:00
|
|
|
return $this->belongsTo(Pirep::class, 'last_pirep_id');
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 05:48:51 +08:00
|
|
|
public function fields()
|
2017-08-04 10:02:02 +08:00
|
|
|
{
|
2020-08-12 05:48:51 +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()
|
|
|
|
{
|
2018-02-11 03:25:40 +08:00
|
|
|
return $this->hasMany(Pirep::class, 'user_id')
|
2018-03-20 09:50:40 +08:00
|
|
|
->where('state', '!=', PirepState::CANCELLED);
|
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
|
|
|
}
|
2017-06-09 02:28:26 +08:00
|
|
|
}
|