Users.php
1 <?php 2 3 namespace App\Http\Livewire\Entities\Relations; 4 5 use App\Http\Services\UserService; 6 use App\Models\Entity; 7 use App\Traits\WithPerPagePagination; 8 use App\Http\Livewire\Users\Create as UsersCreate; 9 use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 10 use Livewire\Component; 11 use Livewire\Redirector; 12 use Livewire\WithPagination; 13 14 class Users extends Component 15 { 16 use WithPagination, WithPerPagePagination, AuthorizesRequests; 17 18 public $entityId; 19 20 public array $filters = [ 21 'entity_id' => null, 22 'created_at_since' => null, 23 'created_at_to' => null, 24 'withTrashed' => false, 25 ]; 26 27 public string $search = ''; 28 29 public string $sortField = 'name'; 30 31 public string $sortDirection = 'asc'; 32 33 protected $rules = [ 34 'filters.created_at_since' => 'nullable|date', 35 'filters.created_at_to' => 'nullable|date|after:filters.created_at_since', 36 37 ]; 38 39 public function mount() 40 { 41 $this->authorize('can_read_users'); 42 $this->filters['entity_id'] = $this->entityId; 43 } 44 45 public function updated($propertyName) 46 { 47 $this->validateOnly($propertyName); 48 } 49 50 protected function getListeners() 51 { 52 return ['refresh' => '$refresh']; 53 } 54 55 public function updatingSearch(): void 56 { 57 $this->resetPage(); 58 } 59 60 public function updatedFilters(): void 61 { 62 $this->getUsersProperty(); 63 } 64 65 public function sortBy(string $field): void 66 { 67 $this->sortField === $field 68 ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc' 69 : $this->sortDirection = 'asc'; 70 71 $this->sortField = $field; 72 } 73 74 public function getEntityProperty() 75 { 76 return Entity::find($this->entityId); 77 } 78 79 public function getRowsQueryProperty() 80 { 81 return UserService::searchUsers(false, $this->search, $this->sortField, $this->sortDirection, $this->filters); 82 } 83 84 public function getUsersProperty() 85 { 86 return $this->applyPagination($this->rowsQuery); 87 } 88 89 public function viewRow(int $rowId): Redirector 90 { 91 return redirect()->to(route('users.view', ['user' => $rowId])); 92 } 93 94 public function openCreatePanel(): void 95 { 96 $this->emit('openPanel', __('users.panel.title.create'), UsersCreate::class, $this->entityId, '', '', ['entityId' => $this->entityId]); 97 } 98 99 public function render() 100 { 101 return view('livewire.entities.relations.users'); 102 } 103 }