/ app / Http / Livewire / Documents / Document / Index.php
Index.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\Documents\Document;
  4  
  5  use App\Http\Services\DocumentService;
  6  use App\Models\Document;
  7  use App\Models\DocumentType;
  8  use App\Traits\WithPerPagePagination;
  9  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 10  use Illuminate\Support\Str;
 11  use Livewire\Component;
 12  use Livewire\Redirector;
 13  use Livewire\WithPagination;
 14  
 15  class Index extends Component
 16  {
 17      use WithPagination, AuthorizesRequests, WithPerPagePagination;
 18  
 19      public bool $readyToLoad = false;
 20  
 21      public array $filters = [
 22          'created_at_since' => null,
 23          'created_at_to' => null,
 24          'withTrashed' => false,
 25          'types' => [],
 26          /**created_by, reviewed_by, approved_by */
 27          'user_by' => '',
 28          'reviewed_at_since' => null,
 29          'reviewed_at_to' => null,
 30          'approved_at_since' => null,
 31          'approved_at_to' => null,
 32      ];
 33  
 34      public string $search = '';
 35  
 36      public string $sortField = 'name';
 37  
 38      public string $sortDirection = 'asc';
 39  
 40      protected $rules = [
 41          'filters.created_at_since' => 'nullable|date',
 42          'filters.created_at_to' => 'nullable|date|after:filters.created_at_since',
 43  
 44      ];
 45  
 46      public function mount()
 47      {
 48          $this->authorize('viewAny', Document::class);
 49      }
 50  
 51      public function getTypeEvidenceIdProperty()
 52      {
 53          return DocumentType::where('code', 'EVI')->pluck('id')->first();
 54      }
 55  
 56      public function fetchData()
 57      {
 58          $this->readyToLoad = true;
 59      }
 60  
 61      public function updated($propertyName)
 62      {
 63          $this->validateOnly($propertyName);
 64      }
 65  
 66      protected function getListeners()
 67      {
 68          return [
 69              'refresh' => '$refresh',
 70              'documentConvertedToPdf' => '$refresh',
 71              'documentUpdated' => '$refresh',
 72          ];
 73      }
 74  
 75      public function updatingSearch(): void
 76      {
 77          $this->resetPage();
 78      }
 79  
 80      public function updatedFilters(): void
 81      {
 82          $this->getDocumentsProperty();
 83      }
 84  
 85      public function sortBy(string $field): void
 86      {
 87          $this->sortField === $field
 88              ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'
 89              : $this->sortDirection = 'asc';
 90  
 91          $this->sortField = $field;
 92      }
 93  
 94      public function getColSpanCountProperty()
 95      {
 96          return app()->environment() === 'local' ? 10 : 9;
 97      }
 98  
 99      protected function getDocumentByType(bool $isInternal)
100      {
101          return DocumentService::search($isInternal, $this->search, $this->sortField, $this->sortDirection, $this->filters, auth()->user())
102              ->with('head', 'lastReview', 'lastApproval');
103      }
104  
105      public function getDocumentsProperty()
106      {
107          return $this->readyToLoad ? $this->applyPagination($this->getDocumentByType(true), 'page') : [];
108      }
109  
110      public function getExternalDocumentsProperty()
111      {
112          return $this->readyToLoad ? $this->applyPagination($this->getDocumentByType(false), 'external_page') : [];
113      }
114  
115      public function getTypesProperty()
116      {
117          return DocumentType::get(['id', 'code as name', 'description'])->map(function ($documentType) {
118              return [
119                  'id' => $documentType->id,
120                  'name' => $documentType->name,
121                  'description' => __(Str::snake(Str::lower('documents.templates.types.' . $documentType->description))),
122              ];
123          })->toArray();
124      }
125  
126      public function viewRow(int $rowId)
127      {
128          if (Document::withTrashed()->find($rowId)->trashed()) {
129              return false;
130          }
131  
132          $this->emit('openPanel', __('documents.panel.title.details'), 'documents.document.view', $rowId);
133      }
134  
135      public function render()
136      {
137          return view('livewire.documents.document.index')
138              ->layoutData([
139                  'title' => __('documents.title'),
140                  'entity' => null,
141                  'breadcrumb' => 'documents',
142              ]);
143      }
144  }