/ app / View / Components / Editor.php
Editor.php
 1  <?php
 2  
 3  namespace App\View\Components;
 4  
 5  use App\Models\Evidence;
 6  use App\Traits\ComponentHandlesValidationErrors;
 7  use App\Traits\ComponentHasModels;
 8  use Illuminate\Support\Facades\Log;
 9  use Illuminate\Support\Js;
10  use Illuminate\View\Component;
11  
12  class Editor extends Component
13  {
14      use ComponentHandlesValidationErrors, ComponentHasModels;
15  
16      public function __construct(
17          public null|string $name = null,
18          public null|string $id = null,
19          public null|string $value = null,
20          public null|string $model = null,
21          bool $showErrors = true,
22          public bool $autofocus = false,
23          public bool $readonly = false,
24          public null|string $placeholder = null,
25  
26          public             $versions = null,
27  
28          public array $atValues = [],
29          public array $hashValues = [],
30  
31          public bool $isNotTable = false,
32          public bool $simplified = false,
33          public null|int $dossierId = null,
34      ) {
35          $this->id = $this->id ?? $this->name;
36          $this->value = $this->name ? old($this->name, $this->value) : $this->value;
37          $this->showErrors = $showErrors;
38          $this->dossierId = $dossierId;
39          $this->generateFeeds();
40      }
41  
42      private function generateFeeds()
43      {
44          if (!is_null($this->dossierId)) {
45              $this->atValues = Evidence::whereHas('document', function ($query) {
46                  $query->where('dossier_id', $this->dossierId);
47              })->get(['id as ref', 'code AS value'])->toArray();
48  
49              $this->atValues = array_map(function ($item) {
50                  $item['id'] = '@' . $item['value'];
51                  return $item;
52              }, $this->atValues);
53  
54              $this->hashValues = Evidence::whereHas('document', function ($query) {
55                  $query->where('dossier_id', $this->dossierId);
56              })->get(['id as ref', 'code AS value'])->toArray();
57  
58              $this->hashValues = array_map(function ($item) {
59                  $item['id'] = '#' . $item['value'];
60                  return $item;
61              }, $this->hashValues);
62          }
63      }
64  
65      public function ats(): Js
66      {
67          return Js::from($this->atValues);
68      }
69  
70      public function hashes(): Js
71      {
72          return Js::from($this->hashValues);
73      }
74  
75      public function render()
76      {
77          return view('livewire.editor')->extends('layouts.vertical');
78      }
79  }