You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
5.0 KiB
185 lines
5.0 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\File;
|
|
use App\Models\Model;
|
|
use App\Models\SoftDeletes;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Illuminate\Auth\Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
use Illuminate\Foundation\Auth\Access\Authorizable;
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract, HasMedia
|
|
{
|
|
use Notifiable,
|
|
SoftDeletes,
|
|
Authorizable,
|
|
Authenticatable,
|
|
InteractsWithMedia;
|
|
|
|
const CONVERSION_NAME = 'avatar';
|
|
|
|
const COLLECTION_NAME = 'avatars';
|
|
|
|
public $casts = [
|
|
'has_avatar' => 'boolean',
|
|
];
|
|
|
|
protected $fillable = ['name', 'email','mobile', 'username','password','active','has_avatar'];
|
|
|
|
protected $fillable_relations = ['projects'];
|
|
|
|
protected $reportable = [
|
|
'name', 'username', 'mobile', 'email', // fields
|
|
['projects' => 'project_user']
|
|
];
|
|
|
|
public $detach_relation = false;
|
|
|
|
public function updateRelations()
|
|
{
|
|
// projects relations
|
|
if (!empty($this->filled_relations['projects']) || $this->detach_relation) {
|
|
$this->dirties['projects'] = $this->projects()->sync($this->filled_relations['projects'], $this->detach_relation);
|
|
}
|
|
}
|
|
|
|
/** =============================== Validations ======================== */
|
|
public function rules()
|
|
{
|
|
return [
|
|
'name' => 'required|string|max:225|min:2',
|
|
'username' => ['required', Rule::unique('users', 'username')->ignore($this->id)],
|
|
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($this->id)],
|
|
'password' => ['required','string','min:8']
|
|
];
|
|
}
|
|
/** =============================== End Validations ==================== */
|
|
|
|
|
|
/** =============================== Relations ========================== */
|
|
public function fingerprints()
|
|
{
|
|
return $this->hasMany(Fingerprint::class, 'user_id', 'id');
|
|
}
|
|
|
|
public function businesses()
|
|
{
|
|
return $this->belongsToMany(
|
|
Business::class,
|
|
'business_user',
|
|
'user_id',
|
|
'business_id',
|
|
'id',
|
|
'id',
|
|
__FUNCTION__
|
|
);
|
|
}
|
|
|
|
|
|
public function tasks()
|
|
{
|
|
return $this->hasMany(Task::class, 'user_id', 'id');
|
|
}
|
|
|
|
public function projects()
|
|
{
|
|
return $this->belongsToMany(
|
|
Project::class,
|
|
'project_user',
|
|
'user_id',
|
|
'project_id',
|
|
'id',
|
|
'id',
|
|
__FUNCTION__
|
|
)->using(ReportableRelation::class);
|
|
}
|
|
|
|
public function files()
|
|
{
|
|
return $this->hasMany(File::class, 'user_id','id');
|
|
}
|
|
|
|
|
|
/** =============================== End Relations ====================== */
|
|
|
|
public function getValueOf(?string $key)
|
|
{
|
|
$values = [
|
|
'business_id' => request('_business_info')['id'] ?? null,
|
|
'user_id' => $this->id,
|
|
'workflow_id' => null,
|
|
'project_id' => null,
|
|
'sprint_id' => null,
|
|
'system_id' => null,
|
|
'status_id' => null,
|
|
'task_id' => null,
|
|
'subject_id' => $this->id,
|
|
];
|
|
|
|
if ($key && isset($values, $key)) {
|
|
return $values[$key];
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection(static::COLLECTION_NAME)
|
|
->acceptsMimeTypes([
|
|
'image/jpeg',
|
|
'image/png',
|
|
'image/tiff',
|
|
'image/gif',
|
|
])
|
|
->useDisk('public')
|
|
->singleFile();
|
|
}
|
|
|
|
public function registerMediaConversions(Media $media = null): void
|
|
{
|
|
$this->addMediaConversion(static::CONVERSION_NAME)
|
|
->width(200)
|
|
->height(200)
|
|
->queued()
|
|
->nonOptimized()
|
|
->performOnCollections(static::COLLECTION_NAME);
|
|
}
|
|
|
|
|
|
public function saveAsAvatar(UploadedFile $avatar): void
|
|
{
|
|
$this->addMedia($avatar)->toMediaCollection(static::COLLECTION_NAME);
|
|
$this->update([
|
|
'has_avatar' => true,
|
|
]);
|
|
@unlink($this->getFirstMedia(static::COLLECTION_NAME)->getPath());
|
|
}
|
|
|
|
public function deleteAvatar(): void
|
|
{
|
|
$path = $this->getFirstMedia(static::COLLECTION_NAME)->getPath();
|
|
$this->getFirstMedia(static::COLLECTION_NAME)->delete();
|
|
$this->update([
|
|
'has_avatar' => false,
|
|
]);
|
|
@unlink($path);
|
|
}
|
|
|
|
public function getAvatarUrl(): ?string
|
|
{
|
|
if ($url = $this->getFirstMediaUrl(static::COLLECTION_NAME, static::CONVERSION_NAME)) {
|
|
return $url;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|