File "User.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Models/User.php
File size: 4.46 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Models;

use \DateTimeInterface;
use Carbon\Carbon;
use Hash;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements HasMedia, MustVerifyEmail
{
    use Notifiable;
    use InteractsWithMedia;
    use HasFactory;
    use HasApiTokens;

    public const STATUS_RADIO = [
        '1' => 'Enable',
        '0' => 'Disable',
    ];

    public const REMINDER_DURATION = [
        '10',
        '30',
    ];

    public const USER_TYPE_RADIO = [
        '1' => 'Admin',
        '2' => 'Employee',
        '3' => 'Customer',
    ];

    public $table = 'users';

    protected $appends = [
        'profile_image',
    ];

    protected $hidden = [
        'remember_token',
        'password',
    ];

    protected $dates = [
        'email_verified_at',
        'birth_date',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $casts = [
        'email_verified_at' => 'date:m-d-Y',
        'birth_date' => 'date:m-d-Y',
        'created_at' => 'date:m-d-Y',
        'updated_at' => 'date:m-d-Y',
        'deleted_at' => 'date:m-d-Y',
    ];

    protected $fillable = [
        'name',
        'email',
        'pj_customer_id',
        'email_verified_at',
        'password',
        'phone_number',
        'company',
        'fax_number',
        'remember_token',
        'password_reset_code',
        'password_last_changed',
        'user_type',
        'birth_date',
        'secondary_name',
        'secondary_email',
        'secondary_phone',
        'email_token',
        'reminder_duration',
        'status',
        'sms_notification',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function getIsAdminAttribute()
    {
        return $this->roles()->where('id', 1)->exists();
    }

    public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('thumb')->fit('crop', 50, 50);
        $this->addMediaConversion('preview')->fit('crop', 120, 120);
    }

    public function userAddressBooks()
    {
        return $this->hasMany(AddressBook::class, 'user_id', 'id');
    }

    public function userOrders()
    {
        return $this->hasMany(Order::class, 'user_id', 'id');
    }

    public function assignedToOrders()
    {
        return $this->hasMany(Order::class, 'assigned_to_id', 'id');
    }

    public function getEmailVerifiedAtAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('panel.date_format') . ' ' . config('panel.time_format')) : null;
    }

    public function setEmailVerifiedAtAttribute($value)
    {
        $this->attributes['email_verified_at'] = $value ? Carbon::createFromFormat(config('panel.date_format') . ' ' . config('panel.time_format'), $value)->format('Y-m-d H:i:s') : null;
    }

    public function setPasswordAttribute($input)
    {
        if ($input) {
            $this->attributes['password'] = app('hash')->needsRehash($input) ? Hash::make($input) : $input;
        }
    }

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    public function getProfileImageAttribute()
    {
        $file = $this->getMedia('profile_image')->last();

        if ($file) {
            $file->url       = $file->getUrl();
            $file->thumbnail = $file->getUrl('thumb');
            $file->preview   = $file->getUrl('preview');
        }
        return $file;
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected function getStatusNameAttribute()
    {
        return self::STATUS_RADIO[$this->status];
    }

    protected function getUserTypeNameAttribute()
    {
        return self::USER_TYPE_RADIO[$this->user_type];
    }

    public function getPJIdAttribute()
    {
        return $this->pj_customer_id;
    }

    public function routeNotificationForSms()
    {
        return $this->phone_number;
        //return "+18328476000";
    }

}