Created
October 14, 2020 11:00
-
-
Save jongravois/07dbb87591b02afdacd892c5172baf79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Carbon\Carbon; | |
use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Support\Facades\Config; | |
use Illuminate\Support\Facades\Storage; | |
use Laravel\Fortify\TwoFactorAuthenticatable; | |
use Laravel\Jetstream\HasProfilePhoto; | |
use Laravel\Sanctum\HasApiTokens; | |
/** | |
* @mixin IdeHelperUser | |
*/ | |
class User extends Authenticatable | |
{ | |
use HasApiTokens; | |
use HasFactory; | |
use HasProfilePhoto; | |
use Notifiable; | |
use SoftDeletes; | |
use TwoFactorAuthenticatable; | |
protected $table = 'users'; | |
protected $guarded = []; | |
protected $hidden = [ | |
'password', | |
'remember_token', | |
'two_factor_recovery_codes', | |
'two_factor_secret', | |
]; | |
protected $dates = []; | |
protected $appends = [ | |
'avatar_path', | |
'b_date', | |
'default_password', | |
'initials', | |
'locale', | |
'profile_photo_url', | |
]; | |
protected $casts = [ | |
'anniversary' => 'date', | |
'dob' => 'date', | |
'email_verified_at' => 'datetime', | |
'new_user' => 'boolean', | |
'can_drive' => 'boolean', | |
'can_manage' => 'boolean', | |
'can_sponsor' => 'boolean', | |
'is_developer' => 'boolean', | |
'is_leadership' => 'boolean', | |
'is_management' => 'boolean', | |
'is_online' => 'boolean', | |
'is_ontask' => 'boolean', | |
'is_paused' => 'boolean', | |
'is_staff' => 'boolean', | |
'is_bd' => 'boolean', | |
'display' => 'boolean', | |
'show_messages' => 'boolean', | |
]; | |
/* MODEL EVENTS */ | |
protected static function boot() | |
{ | |
parent::boot(); | |
User::saving(function ($model) { | |
$model->name = $model->first_name . ' ' . $model->last_name; | |
}); | |
} // end function | |
/* MODEL EVENTS */ | |
/* RELATIONSHIPS */ | |
public function customers() | |
{ | |
return $this->hasMany(CompanyCustomerAgreement::class, 'requester_id', 'id'); | |
} // end function | |
public function departments() | |
{ | |
return $this->belongsToMany(Department::class); | |
} // end function | |
public function manager() | |
{ | |
return $this->hasOne(User::class, 'id', 'manager_id'); | |
} // end function | |
public function office() | |
{ | |
return $this->belongsTo(Office::class); | |
} // end function | |
public function tech_tickets() | |
{ | |
return $this->hasMany(Ticket::class, 'tech_id') | |
->whereIsActive(true); | |
} // end function | |
public function tickets() | |
{ | |
return $this->hasMany(Ticket::class) | |
->orderBy('status_id')->latest(); | |
} // end function | |
/* RELATIONSHIPS */ | |
/* MODEL SCOPES */ | |
public function scopeActive($query) | |
{ | |
return $query->whereNull('deleted_at'); | |
} // end function | |
public function scopeManagers($query) | |
{ | |
return $query->where('can_manage', true); | |
} // end function | |
public function scopeOrderByAnniversary($query) | |
{ | |
$query->orderByRaw('date_format(anniversary, "%m-%d")'); | |
} // end function | |
public function scopeOrderByBirthday($query) | |
{ | |
$query->orderByRaw('date_format(dob, "%m-%d")'); | |
} // end function | |
public function scopeSales($query) | |
{ | |
return $query->whereNotNull('salesperson'); | |
} // end function | |
public function scopeSearch($query, $terms=null) | |
{ | |
collect(str_getcsv($terms, ' ', '"')) | |
->filter()->each(function($term) use ($query) { | |
$term = preg_replace('/[^A-za-z0-9]/', '', $term) . '%'; | |
$query->whereIn('id', function($query) use ($term) { | |
$query->select('id') | |
->from(function($query) use($term) { | |
$query->select('id') | |
->from('users') | |
->whereRaw("regexp_replace(first_name), '[^A-za-z0-9]', '') like ?", [$term]) | |
->orWhereRaw("regexp_replace(last_name), '[^A-za-z0-9]', '') like ?", [$term]); | |
}, 'matches'); | |
}); | |
}); | |
} // end function | |
public function scopeWhereAnniversaryThisWeek($query) | |
{ | |
$dates = Carbon::tomorrow('America/Chicago')->startOfWeek() | |
->daysUntil(Carbon::tomorrow('America/Chicago')->endOfWeek()) | |
->map(function ($date) { | |
return $date->format('m-d'); | |
}); | |
$query->whereRaw('date_format(anniversary, "%m-%d") in (?,?,?,?,?,?,?)', iterator_to_array($dates)); | |
} // end function | |
public function scopeWhereBirthdayThisWeek($query) | |
{ | |
$dates = Carbon::tomorrow('America/Chicago')->startOfWeek() | |
->daysUntil(Carbon::tomorrow('America/Chicago')->endOfWeek()) | |
->map(function ($date) { | |
return $date->format('m-d'); | |
}); | |
$query->whereRaw('date_format(dob, "%m-%d") in (?,?,?,?,?,?,?)', iterator_to_array($dates)); | |
} // end function | |
/* MODEL SCOPES */ | |
/* STATIC METHODS */ | |
public static function lookFor($query) | |
{ | |
return empty($query) ? static::query() | |
: static::where('first_name', 'like', '%'.$query.'%') | |
->orWhere('last_name', 'like', '%'.$query.'%'); | |
} // end function | |
public static function fullSearch($query) | |
{ | |
return empty($query) ? static::query() | |
: static::where('first_name', 'like', '%'.$query.'%') | |
->orWhere('last_name', 'like', '%'.$query.'%') | |
->orWhere('email', 'like', '%'.$query.'%') | |
->orWhere('badge', 'like', '%'.$query.'%'); | |
} // end function | |
public static function seek($field, $q) | |
{ | |
return $q | |
? static::where($field, 'like', "%{$q}%")->get() | |
: []; | |
} // end function | |
/* STATIC METHODS */ | |
/* METHODS */ | |
public function avatarUrl() | |
{ | |
return $this->avatar | |
? Storage::disk('avatars')->url($this->avatar) | |
: 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))); | |
} | |
public function getAvatarPathAttribute() | |
{ | |
return $this->avatar | |
? Config::get('paths.spaces_uri') . 'avatars/' . $this->avatar | |
: $this->defaultProfilePhotoUrl(); | |
} // end function | |
public function getBDateAttribute() | |
{ | |
return optional($this->dob)->format('Y-m-d'); | |
} // end function | |
public function getDefaultPasswordAttribute() | |
{ | |
$cleanLastName = ucfirst(preg_replace("/[^A-Za-z]/", '', strtolower($this->last_name))); | |
return '!' . $cleanLastName . '@uam'; | |
} // end function | |
public function getInitialsAttribute() | |
{ | |
return $this->first_name[0] . $this->last_name[0]; | |
} // end function | |
public function getLocaleAttribute() | |
{ | |
if(!$this->office) { return 'UAM'; } // end if | |
return $this->office->display; | |
} // end function | |
public function isAnniversary() | |
{ | |
if(!$this->anniversary) { return false; } | |
return $this->anniversary->isBirthday(); | |
} // end function | |
public function isBirthday() | |
{ | |
if(!$this->dob) { return false; } | |
return $this->dob->isBirthday(); | |
} // end function | |
/* METHODS */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment