/ app / Http / Livewire / Documents / Templates / Create.php
Create.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\Documents\Templates;
  4  
  5  use App\Http\Services\DocumentService;
  6  use App\Models\DocumentType;
  7  use App\Models\MeetType;
  8  use App\Models\Role;
  9  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 10  use Livewire\Component;
 11  use Livewire\WithFileUploads;
 12  use Log;
 13  use Str;
 14  use WireUi\Traits\Actions;
 15  
 16  class Create extends Component
 17  {
 18      use AuthorizesRequests, Actions, WithFileUploads;
 19  
 20      public bool $btnIsDisabled = false;
 21  
 22      public array $templateFile = [
 23          'approver_role_id' => null,
 24          'reviewer_role_id' => null,
 25          'name' => '',
 26          'path' => '',
 27          'filename' => '',
 28          'document_type_id' => null,
 29          'meet_type_id' => null,
 30          'custom_path' => null,
 31          'date_custom_path' => null,
 32          'is_certification' => false,
 33          'can_draft' => true,
 34          'class_doc_gen' => null,
 35          'is_word' => false,
 36      ];
 37  
 38      public $file;
 39  
 40      public $code;
 41  
 42      protected function rules()
 43      {
 44          return [
 45              'templateFile.approver_role_id' => 'nullable',
 46              'templateFile.reviewer_role_id' => 'nullable',
 47              'templateFile.document_type_id' => 'required',
 48              'templateFile.meet_type_id' => 'required_if:code,ACT',
 49              'file' => 'required|file|mimes:docx',
 50              'templateFile.name' => 'unique:templates,name',
 51          ];
 52      }
 53  
 54      public function getTypesProperty()
 55      {
 56          $documentTypes = DocumentType::where('internal', true)->where('code', '<>', 'EML')->get();
 57  
 58          return $documentTypes->map(function ($documentType) {
 59              return [
 60                  'id' => $documentType->id,
 61                  'description' => __(Str::snake(Str::lower('documents.templates.types.' . $documentType->description))),
 62              ];
 63          });
 64      }
 65  
 66      public function getMeetTypesProperty()
 67      {
 68          return MeetType::all(['id', 'name'])->map(function ($meetType) {
 69              return [
 70                  'id' => $meetType->id,
 71                  'name' => __('meets.meet.panel.form.type.' . $meetType->name),
 72              ];
 73          });
 74      }
 75  
 76      public function roles()
 77      {
 78          return Role::whereIn('id', config('app.usableRoles'))->get()->map(function (Role $role) {
 79              $role->name = __('roles.' . $role->name);
 80              return $role;
 81          });
 82      }
 83  
 84      public function updatedTemplateFile($value, $key)
 85      {
 86  
 87          $this->templateFile['meet_type_id'] = null;
 88          $this->templateFile['is_certification'] = false;
 89  
 90          if ($key === 'document_type_id') {
 91              $this->code = DocumentType::find($value)?->code;
 92  
 93              $this->templateFile['can_draft'] = match ($this->code) {
 94                  'CER', 'MAN', 'POL' => false,
 95                  default => true,
 96              };
 97  
 98              $this->templateFile['class_doc_gen'] = $this->getClassDocGen();
 99          }
100  
101          if ($key === 'meet_type_id' || $key === 'is_certification') {
102              $this->templateFile[$key] = $value;
103          }
104      }
105  
106      private function getClassDocGen()
107      {
108          return match ($this->code) {
109              'ACT' => 'ACTTemplateDocGen',
110              'CER' => 'CERTemplateDocGen',
111              'INF' => 'INFTemplateDocGen',
112              'NOT' => 'NOTTemplateDocGen',
113              'RES' => 'RESTemplateDocGen',
114              default => null,
115          };
116      }
117  
118      public function updatedFile()
119      {
120          $fileName = $this->file->getClientOriginalName();
121          $this->templateFile['name'] = basename($fileName, '.docx');
122          $this->templateFile['filename'] = $fileName;
123          $this->templateFile['path'] = 'templates/custom/' . $this->code . '_templates/' . $fileName;
124      }
125  
126      public function updated($propertyName)
127      {
128          $this->validateOnly($propertyName);
129      }
130  
131      public function create(): void
132      {
133          $this->authorize('can_write_templates');
134  
135          $this->validate();
136  
137          try {
138              DocumentService::createTemplate($this->templateFile);
139  
140              $this->file->storeAs('templates/custom/' . $this->code . '_templates/', $this->templateFile['filename']);
141  
142              // UI Trick for delaying spinner
143              if (config('app.env') !== 'testing') {
144                  sleep(1);
145              }
146  
147              $this->emitUp('closePanel');
148              $this->emitTo(Index::class, 'refresh');
149  
150              $this->reset();
151  
152              $this->notification()->success(
153                  __('documents.templates.notifications.create.success.title'),
154                  __('documents.templates.notifications.create.success.message')
155              );
156          } catch (\Exception $e) {
157              Log::error($e);
158              $this->notification()->error(
159                  'Error',
160                  __('documents.templates.notifications.create.error.message')
161              );
162          }
163      }
164  
165      public function render()
166      {
167          return view('livewire.documents.templates.create');
168      }
169  }