#90 - wip
This commit is contained in:
		| @@ -33,7 +33,8 @@ class YearPeriodRetriever | ||||
|         $selected = $this->selected(); | ||||
|         $current = $this->current(); | ||||
|  | ||||
|         $years = YearPeriod::query()->whereIn("year", $this->offset($selected->year))->get(); | ||||
|         $years = YearPeriod::all(); | ||||
|  | ||||
|         $navigation = $years->map(fn(YearPeriod $yearPeriod) => $this->toNavigation($yearPeriod)); | ||||
|  | ||||
|         return [ | ||||
| @@ -43,11 +44,6 @@ class YearPeriodRetriever | ||||
|         ]; | ||||
|     } | ||||
|  | ||||
|     protected function offset(int $year): array | ||||
|     { | ||||
|         return range($year - 2, $year + 2); | ||||
|     } | ||||
|  | ||||
|     protected function toNavigation(YearPeriod $yearPeriod): array | ||||
|     { | ||||
|         return [ | ||||
|   | ||||
							
								
								
									
										63
									
								
								app/Eloquent/Models/Profile.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								app/Eloquent/Models/Profile.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Eloquent\Models; | ||||
|  | ||||
| use Database\Factories\ProfileFactory; | ||||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Rackbeat\UIAvatars\HasAvatar; | ||||
| use Toby\Domain\Enums\EmploymentForm; | ||||
| use Toby\Eloquent\Helpers\ColorGenerator; | ||||
|  | ||||
| /** | ||||
|  * @property string $first_name | ||||
|  * @property string $last_name | ||||
|  * @property string $position | ||||
|  * @property EmploymentForm $employment_form | ||||
|  * @property Carbon $employment_date | ||||
|  */ | ||||
| class Profile extends Model | ||||
| { | ||||
|     use HasFactory; | ||||
|     use HasAvatar; | ||||
|  | ||||
|     protected $primaryKey = "user_id"; | ||||
|  | ||||
|     protected $guarded = []; | ||||
|  | ||||
|     protected $casts = [ | ||||
|         "employment_form" => EmploymentForm::class, | ||||
|         "employment_date" => "date", | ||||
|     ]; | ||||
|  | ||||
|     public function user(): BelongsTo | ||||
|     { | ||||
|         return $this->belongsTo(User::class); | ||||
|     } | ||||
|  | ||||
|     public function getAvatar(): string | ||||
|     { | ||||
|         return $this->getAvatarGenerator() | ||||
|             ->backgroundColor(ColorGenerator::generate($this->fullName)) | ||||
|             ->image(); | ||||
|     } | ||||
|  | ||||
|     public function getFullNameAttribute(): string | ||||
|     { | ||||
|         return "{$this->first_name} {$this->last_name}"; | ||||
|     } | ||||
|  | ||||
|     protected function getAvatarName(): string | ||||
|     { | ||||
|         return mb_substr($this->first_name, 0, 1) . mb_substr($this->last_name, 0, 1); | ||||
|     } | ||||
|  | ||||
|     protected static function newFactory(): ProfileFactory | ||||
|     { | ||||
|         return ProfileFactory::new(); | ||||
|     } | ||||
| } | ||||
| @@ -8,27 +8,20 @@ use Database\Factories\UserFactory; | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||||
| use Illuminate\Database\Eloquent\Relations\HasMany; | ||||
| use Illuminate\Database\Eloquent\Relations\HasOne; | ||||
| use Illuminate\Database\Eloquent\SoftDeletes; | ||||
| use Illuminate\Foundation\Auth\User as Authenticatable; | ||||
| use Illuminate\Notifications\Notifiable; | ||||
| use Illuminate\Support\Carbon; | ||||
| use Illuminate\Support\Collection; | ||||
| use Rackbeat\UIAvatars\HasAvatar; | ||||
| use Toby\Domain\Enums\EmploymentForm; | ||||
| use Toby\Domain\Enums\Role; | ||||
| use Toby\Eloquent\Helpers\ColorGenerator; | ||||
|  | ||||
| /** | ||||
|  * @property int $id | ||||
|  * @property string $first_name | ||||
|  * @property string $last_name | ||||
|  * @property string $email | ||||
|  * @property string $password | ||||
|  * @property string $avatar | ||||
|  * @property string $position | ||||
|  * @property Role $role | ||||
|  * @property EmploymentForm $employment_form | ||||
|  * @property Carbon $employment_date | ||||
|  * @property Profile $profile | ||||
|  * @property Collection $vacationLimits | ||||
|  * @property Collection $vacationRequests | ||||
|  * @property Collection $vacations | ||||
| @@ -38,12 +31,12 @@ class User extends Authenticatable | ||||
|     use HasFactory; | ||||
|     use Notifiable; | ||||
|     use SoftDeletes; | ||||
|     use HasAvatar; | ||||
|  | ||||
|     protected $guarded = []; | ||||
|  | ||||
|     protected $casts = [ | ||||
|         "role" => Role::class, | ||||
|         "last_active_at" => "datetime", | ||||
|         "employment_form" => EmploymentForm::class, | ||||
|         "employment_date" => "date", | ||||
|     ]; | ||||
| @@ -52,6 +45,15 @@ class User extends Authenticatable | ||||
|         "remember_token", | ||||
|     ]; | ||||
|  | ||||
|     protected $with = [ | ||||
|         "profile", | ||||
|     ]; | ||||
|  | ||||
|     public function profile(): HasOne | ||||
|     { | ||||
|         return $this->hasOne(Profile::class); | ||||
|     } | ||||
|  | ||||
|     public function vacationLimits(): HasMany | ||||
|     { | ||||
|         return $this->hasMany(VacationLimit::class); | ||||
| @@ -72,18 +74,6 @@ class User extends Authenticatable | ||||
|         return $this->hasMany(Vacation::class); | ||||
|     } | ||||
|  | ||||
|     public function getAvatar(): string | ||||
|     { | ||||
|         return $this->getAvatarGenerator() | ||||
|             ->backgroundColor(ColorGenerator::generate($this->fullName)) | ||||
|             ->image(); | ||||
|     } | ||||
|  | ||||
|     public function getFullNameAttribute(): string | ||||
|     { | ||||
|         return "{$this->first_name} {$this->last_name}"; | ||||
|     } | ||||
|  | ||||
|     public function hasRole(Role $role): bool | ||||
|     { | ||||
|         return $this->role === $role; | ||||
| @@ -104,9 +94,20 @@ class User extends Authenticatable | ||||
|         } | ||||
|  | ||||
|         return $query | ||||
|             ->where("first_name", "ILIKE", "%{$text}%") | ||||
|             ->orWhere("last_name", "ILIKE", "%{$text}%") | ||||
|             ->orWhere("email", "ILIKE", "%{$text}%"); | ||||
|             ->where("email", "ILIKE", "%{$text}%") | ||||
|             ->orWhereRelation( | ||||
|                 "profile", | ||||
|                 fn(Builder $query) => $query | ||||
|                     ->where("first_name", "ILIKE", "%{$text}%") | ||||
|                     ->orWhere("last_name", "ILIKE", "%{$text}%"), | ||||
|             ); | ||||
|     } | ||||
|  | ||||
|     public function scopeOrderByProfileField(Builder $query, string $field): Builder | ||||
|     { | ||||
|         $profileQuery = Profile::query()->select($field)->whereColumn("users.id", "profiles.user_id"); | ||||
|  | ||||
|         return $query->orderBy($profileQuery); | ||||
|     } | ||||
|  | ||||
|     public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder | ||||
| @@ -119,11 +120,6 @@ class User extends Authenticatable | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     protected function getAvatarName(): string | ||||
|     { | ||||
|         return mb_substr($this->first_name, 0, 1) . mb_substr($this->last_name, 0, 1); | ||||
|     } | ||||
|  | ||||
|     protected static function newFactory(): UserFactory | ||||
|     { | ||||
|         return UserFactory::new(); | ||||
|   | ||||
| @@ -5,7 +5,6 @@ declare(strict_types=1); | ||||
| namespace Toby\Eloquent\Models; | ||||
|  | ||||
| use Database\Factories\VacationLimitFactory; | ||||
| use Illuminate\Database\Eloquent\Builder; | ||||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||||
| @@ -37,13 +36,6 @@ class VacationLimit extends Model | ||||
|         return $this->belongsTo(YearPeriod::class); | ||||
|     } | ||||
|  | ||||
|     public function scopeOrderByUserField(Builder $query, string $field): Builder | ||||
|     { | ||||
|         $userQuery = User::query()->select($field)->whereColumn("vacation_limits.user_id", "users.id"); | ||||
|  | ||||
|         return $query->orderBy($userQuery); | ||||
|     } | ||||
|  | ||||
|     protected static function newFactory(): VacationLimitFactory | ||||
|     { | ||||
|         return VacationLimitFactory::new(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user