102 lines
2.3 KiB
PHP
102 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class Documentos extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public $archivos = [];
|
|
public $alumno;
|
|
public $documentosPorTipo = [];
|
|
public $tiposDocumentos = [];
|
|
|
|
#[On('documentos')]
|
|
public function documentos()
|
|
{
|
|
$this->dispatch('open-documentos-modal');
|
|
}
|
|
|
|
public function cargarDocumentos()
|
|
{
|
|
$this->alumno = Auth::user()
|
|
->alumnos()
|
|
->with('nivelRel.documentosRequeridos')
|
|
->first();
|
|
|
|
if ($this->alumno) {
|
|
|
|
$this->documentosPorTipo = $this->alumno
|
|
->documentos()
|
|
->get()
|
|
->keyBy('tipo');
|
|
|
|
$this->tiposDocumentos = $this->alumno->nivelRel
|
|
? $this->alumno->nivelRel->documentosRequeridos
|
|
: collect();
|
|
}
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
if (!$this->alumno) {
|
|
abort(404, 'Alumno no encontrado');
|
|
}
|
|
|
|
$rules = [];
|
|
|
|
foreach ($this->tiposDocumentos as $tipo) {
|
|
$rules["archivos.{$tipo->slug}"] =
|
|
'nullable|file|mimes:' . implode(',', $tipo->extensiones);
|
|
}
|
|
|
|
$this->validate($rules);
|
|
|
|
foreach ($this->tiposDocumentos as $tipo) {
|
|
|
|
if (isset($this->archivos[$tipo->slug])) {
|
|
|
|
$ruta = $this->archivos[$tipo->slug]->store('documentos', 'public');
|
|
|
|
$this->alumno->documentos()->updateOrCreate(
|
|
['tipo' => $tipo->slug],
|
|
[
|
|
'archivo' => $ruta,
|
|
'status' => 0
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
$this->cargarDocumentos();
|
|
|
|
$this->reset('archivos');
|
|
|
|
$this->dispatch('close-documentos-modal');
|
|
|
|
$this->dispatch('swal',
|
|
icon: 'success',
|
|
title: 'Éxito!',
|
|
text: 'Documentos cargados',
|
|
timer: 2000,
|
|
confirm: false,
|
|
closeModal: true
|
|
);
|
|
}
|
|
|
|
public function mount()
|
|
{
|
|
$this->cargarDocumentos();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.documentos');
|
|
}
|
|
}
|