'datetime', ]; protected $appends = [ 'used_capacity', 'storage' ]; /** * Sortable columns * * @var string[] */ public $sortable = [ 'id', 'name', 'role', 'created_at', 'storage_capacity', ]; /** * Get tax rate id for user * * @return array */ public function taxRates() { $stripe = resolve('App\Services\StripeService'); // Get tax rates $rates = collect($stripe->getTaxRates()); // Find tax rate $user_tax_rate = $rates->first(function ($item) { return $item['jurisdiction'] === $this->settings->billing_country && $item['active']; }); return $user_tax_rate ? [$user_tax_rate['id']] : []; } /** * Get user used storage details * * @return mixed */ public function getStorageAttribute() { // Get storage limitation setup $storage_limitation = get_setting('storage_limitation'); $is_storage_limit = $storage_limitation ? $storage_limitation : 1; // Get user storage usage if (! $is_storage_limit) { return [ 'used' => $this->used_capacity, 'used_formatted' => Metric::bytes($this->used_capacity)->format(), ]; } return [ 'used' => (float)get_storage_fill_percentage($this->used_capacity, $this->settings->storage_capacity), 'used_formatted' => get_storage_fill_percentage($this->used_capacity, $this->settings->storage_capacity) . '%', 'capacity' => $this->settings->storage_capacity, 'capacity_formatted' => format_gigabytes($this->settings->storage_capacity), ]; } /** * Get user used storage capacity in bytes * * @return mixed */ public function getUsedCapacityAttribute() { $user_capacity = $this->files_with_trashed->map(function ($item) { return $item->getRawOriginal(); })->sum('filesize'); return $user_capacity; } /** * Get user full folder tree * * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection */ public function getFolderTreeAttribute() { return FileManagerFolder::with(['folders.shared', 'shared:token,id,item_id,permission,protected,expire_in']) ->where('parent_id', 0) ->where('user_id', $this->id) ->get(); } /** * Format avatar to full url * * @return \Illuminate\Contracts\Routing\UrlGenerator|string */ public function getAvatarAttribute() { // Get avatar from external storage if ($this->attributes['avatar'] && is_storage_driver(['s3', 'spaces', 'wasabi', 'backblaze'])) { return Storage::temporaryUrl($this->attributes['avatar'], now()->addDay()); } // Get avatar from local storage if ($this->attributes['avatar']) { return url('/' . $this->attributes['avatar']); } return url('/assets/images/' . 'default-avatar.png'); } /** * Set user billing info * * @param $billing * @return UserSettings */ public function setBilling($billing) { $this->settings()->update([ 'billing_address' => $billing['billing_address'], 'billing_city' => $billing['billing_city'], 'billing_country' => $billing['billing_country'], 'billing_name' => $billing['billing_name'], 'billing_phone_number' => $billing['billing_phone_number'], 'billing_postal_code' => $billing['billing_postal_code'], 'billing_state' => $billing['billing_state'], ]); return $this->settings; } /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new ResetPassword($token)); } /** * Get user favourites folder * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function favourite_folders() { return $this->belongsToMany(FileManagerFolder::class, 'favourite_folder', 'user_id', 'folder_unique_id', 'id', 'unique_id')->with('shared:token,id,item_id,permission,protected,expire_in'); } /** * Get 5 latest uploads * * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Query\Builder */ public function latest_uploads() { return $this->hasMany(FileManagerFile::class)->with(['parent'])->orderBy('created_at', 'DESC')->take(40); } /** * Get all user files * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function files() { return $this->hasMany(FileManagerFile::class); } /** * Get all user files * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function files_with_trashed() { return $this->hasMany(FileManagerFile::class)->withTrashed(); } /** * Get user attributes * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function settings() { return $this->hasOne(UserSettings::class); } }