Dr-Panel/app/Models/User.php

72 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use App\Casts\JalaliDatetime;
use BezhanSalleh\FilamentShield\Traits\HasPanelShield;
use Database\Factories\UserFactory;
use Filament\Models\Contracts\HasAvatar;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements HasAvatar
{
use HasFactory, Notifiable, HasRoles, HasPanelShield;
protected $fillable = [
'name',
'username',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'created_at' => JalaliDatetime::class,
'updated_at' => JalaliDatetime::class,
];
}
public function getFilamentAvatarUrl(): ?string
{
$name = $this->name ?? $this->username ?? '?';
$initials = $this->getAvatarInitials($name);
$color = $this->getAvatarColor($name);
$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">'
. '<circle cx="50" cy="50" r="50" fill="' . $color . '"/>'
. '<text x="50" y="50" font-size="38" text-anchor="middle" dominant-baseline="central" fill="white" font-family="Vazirmatn,Tahoma,sans-serif">' . $initials . '</text>'
. '</svg>';
return 'data:image/svg+xml;base64,' . base64_encode($svg);
}
private function getAvatarInitials(string $name): string
{
$words = preg_split('/\s+/', trim($name));
if (count($words) >= 2) {
return mb_substr($words[0], 0, 1) . mb_substr($words[1], 0, 1);
}
return mb_substr($name, 0, 2);
}
private function getAvatarColor(string $name): string
{
$colors = ['#4F46E5', '#7C3AED', '#2563EB', '#0891B2', '#059669', '#D97706', '#DC2626', '#0F766E'];
$index = abs(crc32($name) % count($colors));
return $colors[$index];
}
}