PHP 7.4.33
Preview: User.php Size: 10.40 KB
/var/www/multi-event-cfp.bitkit.dk/httpdocs/app/Models/User.php
<?php

namespace App\Models;

use App\Repositories\UserRepository;
use App\Support\Entity;
use App\Traits\HasFiles;
use App\Traits\Repository;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

/**
 * @property mixed id
 * @property Entity|mixed $avatar
 * @property mixed $company_logo
 * @property mixed|string $password
 * @property boolean|mixed $valid
 * @property mixed $email
 * @property mixed $created_by
 * @property mixed $name
 */
class User extends Authenticatable
{
    use HasFactory, Notifiable, HasRoles, Repository, HasFiles, HasApiTokens;

    const FILE_SAVE_PATH = 'public/media/users/';
    const FILE_PUBLIC_PATH = 'storage/media/users/';
    const FILE_VERIFIED_PUBLIC_PATH = 'storage/media/verifiedUsers/';
    const PRIVATE_FILE_SAVE_PATH = 'private/media/users/';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'password',
        'cc_emails',
        'do_not_send_emails',
        'salutation',
        'job_title',
        'country',
        'phone',
        'mobile',
        'fax',
        'po_box',
        'street',
        'city',
        'state',
        'post_code',
        'linkedin_link',
        'industry_code',
        'company',
        'company_address',
        'company_country',
        'biography',
        'dietary_requirements',
        'dob',
        'nationality',
        'passport_number',
        'uae_resident',
        'emirates_id',
        'user_secondary_image',
        'emirates_id_images',
        'passport_images',
        'avatar',
        'company_logo',
        'login_email_count',
        'valid',
        'checkboxes',
        'created_by',
        'passport_first_last_name',
        'salesforce_opportunity_id',
        'profile_type',
        'vip_marked_by',
        'last_modified_by',
        'last_modified_at',
        'last_modified_event_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'forgot_password'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'last_modified_at' => 'datetime',
        'checkboxes' => 'array',
        'avatar' => 'array',
        'user_secondary_image' => 'array',
        'passport_images' => AsArrayObject::class,
        'emirates_id_images' => AsArrayObject::class,
        'company_logo' => 'array',
        'do_not_send_emails' => 'boolean',
        'uae_resident' => 'boolean',
        'event_profile_data' => 'array',
        'agreement_details' => 'array',
        'verified_data' => 'array',
        'previously_verified_data' => 'array',
    ];

    protected $appends = [
        'name',
        'user_roles'
    ];


    //===========================================//
    // Attributes

    public function getNameAttribute(): string
    {
        return "{$this->first_name} {$this->last_name}";
    }

    public function getUserRolesAttribute()
    {
        $roles = $this->roles()->pluck('name')->toArray();
        return implode(',', $roles);
    }

    public function getLastModifiedInfoAttribute(): ?string
    {
        if (!$this->last_modified_by || !$this->last_modified_at) {
            return null;
        }

        $modifier = $this->lastModifiedBy;
        $modifierName = $modifier ? $modifier->name : 'Unknown User';
        $dateTime = $this->last_modified_at->format('Y-m-d h:i A') . ' UTC';
        $event = $this->lastModifiedEvent;
        $eventName = $event ? $event->event_name : 'Unknown Event';

        return "Last modified by {$modifierName} at {$dateTime} on {$eventName}";
    }

    //===========================================//

    //===========================================//
    //Relationships
    public function events(): BelongsToMany
    {
        return $this->belongsToMany(Event::class , 'event_user', 'user_id', 'event_id')
            ->using(EventUser::class)
            ->withPivot(['id'])
            ->withTimestamps();
    }

    public function eventUsers(): hasMany
    {
        return $this->hasMany(EventUser::class , 'user_id');
    }

    public function sessions($eventId = null): BelongsToMany
    {
        $query = $this->belongsToMany(Session::class , 'session_users', 'user_id', 'session_id')
            ->using(SessionUser::class)
            ->withPivot(['id', 'role'])
            ->withTimestamps();
        if ($eventId)
            $query->where('session_users.event_id', '=', $eventId);

        return $query;
    }

    public function presentations($eventId = null): BelongsToMany
    {
        $query = $this->belongsToMany(Presentation::class , 'presenters', 'user_id', 'presentation_id')
            ->using(Presenter::class)
            ->withPivot(['id'])
            ->withTimestamps();

        if ($eventId)
            $query->where('presenters.event_id', '=', $eventId);

        return $query;
    }

    public function slots($eventId = null): BelongsToMany
    {
        $query = $this->belongsToMany(Slot::class , 'slot_users', 'user_id', 'slot_id')
            ->using(SlotUser::class)
            ->withPivot(['id', 'role'])
            ->withTimestamps();

        if ($eventId)
            $query->where('slot_users.event_id', '=', $eventId);

        return $query;
    }

    public function abstracts($eventId = null): HasMany
    {
        $query = $this->hasMany(Abstracts::class , 'user_id');
        if ($eventId)
            $query->where('abstracts.event_id', '=', $eventId);
        return $query;
    }

    public function vipMarkedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'vip_marked_by');
    }

    public function lastModifiedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'last_modified_by');
    }

    public function lastModifiedEvent(): BelongsTo
    {
        return $this->belongsTo(Event::class, 'last_modified_event_id');
    }

    //===========================================//

    //===========================================//
    //scope functions

    /**
     * Scope a query to join event
     *
     * @param Builder $query
     * @param $eventId
     * @return Builder
     */
    public function scopeEvent(Builder $query, $eventId = null): Builder
    {
        $query->join('event_user', 'users.id', '=', 'event_user.user_id')
            ->join('events', 'event_user.event_id', '=', 'events.id')
            ->select('users.*');
        if ($eventId)
            $query->where('events.id', '=', $eventId)
                ->addSelect('events.id as event_id');
        return $query;
    }

    public function scopeWithEvent(Builder $query, $eventId = null): Builder
    {
        $query = $this->scopeEvent($query, $eventId);
        return $query->addSelect([
            'events.event_name',
            'events.slug_name',
            'events.published',
            'events.publish_step'
        ]);
    }

    public function scopeWithEventRoles(Builder $query, $eventId = null): Builder
    {
        return $this->scopeWithEvent($query, $eventId);
    //todo join query for getting roles
    }

    //End of scope functions
    //===========================================//


    public function useRepository($event = null): UserRepository
    {
        return new UserRepository($event);
    }

    /**
     * @param $role
     * @return bool
     */
    public function hasEventRole($role)
    {
        $query = $this->withEvent()
            ->where('users.id', '=', $this->id)
            ->join('model_has_roles as mhs', 'mhs.model_id', '=', 'event_user.id')
            ->where('mhs.model_type', '=', EventUser::class)
            ->join('roles as r', 'r.id', '=', 'mhs.role_id')
            ->where('r.name', '=', $role);
        return $query->get();
    }

    public function getEventWithRoles($eventId = null)
    {
        $query = $this->newQuery();
        $query->from('events')
            ->setModel(new Event())
            ->join('event_user', 'events.id', '=', 'event_user.event_id')
            ->where('event_user.user_id', '=', $this->id)
            ->leftJoin('abstracts as a', function ($join) {
            $join->on('events.id', '=', 'a.event_id')
                ->where('a.user_id', '=', $this->id)
                ->where('a.selection_status', '=', 'Accepted')
                ->where('a.presentation_invite', '=', null);
        });
        if ($eventId)
            $query->where('events.id', '=', $eventId);
        $query
            ->join('model_has_roles as mhs', 'mhs.model_id', '=', 'event_user.id')
            ->where('mhs.model_type', '=', EventUser::class)
            ->join('roles as r', 'r.id', '=', 'mhs.role_id')
            ->groupBy('id')
            ->select([
            'events.id',
            'events.event_name',
            'events.status',
            'events.slug_name',
            'events.year',
            'events.publish_step',
            'events.published',
            'events.published_date',
            'event_user.event_profile_data',
            'event_user.developer_options',
            'event_user.all_user_api_access',
            'event_user.user_export_access',
            'event_user.abstract_export_access',
            'event_user.presentation_files_download_access',
            'event_user.type',
            'event_user.confirmed_speaker',
            'event_user.speaker_added_via',
            DB::raw("GROUP_CONCAT(distinct(r.name)) as roles"),
            DB::raw("count(distinct(a.id)) as presentation_invited")
        ]);
        return $query;
    }
}

Directory Contents

Dirs: 0 × Files: 26
Name Size Perms Modified Actions
2.71 KB lrw-r--r-- 2024-02-09 12:37:30
Edit Download
386 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
784 B lrw-rw-r-- 2025-04-21 06:11:52
Edit Download
737 B lrw-rw-r-- 2024-02-22 08:11:18
Edit Download
944 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
2.66 KB lrw-rw-r-- 2025-12-10 09:24:27
Edit Download
3.99 KB lrw-rw-r-- 2025-07-30 10:06:01
Edit Download
2.36 KB lrw-rw-r-- 2026-04-07 05:00:19
Edit Download
1.66 KB lrw-rw-r-- 2024-10-23 04:50:49
Edit Download
496 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
4.01 KB lrw-r--r-- 2024-02-09 12:37:30
Edit Download
881 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
1.13 KB lrw-r--r-- 2024-02-09 12:37:30
Edit Download
909 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
941 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
1.03 KB lrw-r--r-- 2025-07-28 09:14:47
Edit Download
835 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
889 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
2.78 KB lrw-rw-r-- 2026-04-22 04:31:30
Edit Download
945 B lrw-rw-r-- 2024-09-20 05:02:14
Edit Download
2.21 KB lrw-rw-r-- 2024-09-20 05:02:14
Edit Download
972 B lrw-rw-r-- 2024-09-20 05:02:14
Edit Download
893 B lrw-rw-r-- 2026-03-31 07:15:50
Edit Download
659 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
10.40 KB lrw-rw-r-- 2026-04-30 09:24:18
Edit Download
574 B lrw-rw-r-- 2024-12-03 04:36:57
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).