phpvms/app/Models/User.php

181 lines
3.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;
2018-02-11 03:25:40 +08:00
use App\Models\Enums\PirepState;
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
/**
* @property integer $id
* @property string $name
* @property string $email
* @property string $password
* @property string $api_key
2018-03-03 07:29:11 +08:00
* @property Flight[] $flights
* @property string $flight_time
* @property string $remember_token
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
2018-03-03 07:29:11 +08:00
* @property Rank rank
* @property Journal journal
* @property string pilot_id
2018-03-17 00:38:06 +08:00
* @property int state
* @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 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 = [
'name',
'email',
'password',
'airline_id',
'rank_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',
'transferred_time',
'balance',
'timezone',
'state',
'status',
'created_at',
'updated_at',
];
2017-06-09 02:28:26 +08:00
/**
* The attributes that should be hidden for arrays.
*/
protected $hidden = [
'api_key',
'password',
'remember_token',
];
protected $casts = [
'flights' => 'integer',
'flight_time' => 'integer',
'transferred_time' => 'integer',
'balance' => 'double',
'state' => 'integer',
'status' => 'integer',
];
2017-06-29 08:56:10 +08:00
public static $rules = [
2018-01-01 03:08:41 +08:00
'name' => 'required',
2018-01-03 13:15:48 +08:00
'email' => 'required|email',
];
2018-01-01 03:08:41 +08:00
/**
* @return string
*/
public function getPilotIdAttribute()
2017-07-03 10:25:48 +08:00
{
$length = setting('pilots.id_length');
return $this->airline->icao . str_pad($this->id, $length, '0', STR_PAD_LEFT);
2017-07-03 10:25:48 +08:00
}
2018-01-01 03:08:41 +08:00
/**
* @return string
*/
public function getIdentAttribute()
{
return $this->getPilotIdAttribute();
}
2018-02-07 00:33:39 +08:00
/**
2018-02-07 01:02:40 +08:00
* Shorthand for getting the timezone
* @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
* @param $value
*/
public function setTzAttribute($value)
{
$this->attributes['timezone'] = $value;
}
2018-01-01 03:08:41 +08:00
/**
* @param mixed $size Size of the gravatar, in pixels
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;
}
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
}
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 bids()
2017-08-04 10:02:02 +08:00
{
2018-02-28 03:25:32 +08:00
return $this->hasMany(Bid::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')
->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
}