File: /var/www/html/xfacil.desafio.com.py/app/Http/Livewire/Reglamentos.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Categoriareglamento;
use App\Models\Reglamento;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use App\File;
class Reglamentos extends Component{
use WithFileUploads;
protected $queryString = ['buscar' => ['except' => '']];
protected $paginationTheme = 'bootstrap';
public $buscar='';
public $categoria_id,$reglamento_id,$nombre,$enlace,$tipo,$crear=0;
public function render(){
$categoria=Categoriareglamento::find($this->categoria_id);
$reglamentos=Reglamento::where('categoria_id',$this->categoria_id)->where('nombre','LIKE',"%{$this->buscar}%")->paginate(20);
return view('livewire.reglamentos',["categoria"=>$categoria,"reglamentos"=>$reglamentos]);
}
private function resetInputFields(){
$this->reglamento_id = '';
$this->nombre = '';
$this->enlace = '';
$this->tipo = '';
$this->crear = 0;
}
public function cancel(){
$this->resetInputFields();
}
public function crear(){
$this->crear = 1;
}
public function store() {
$validatedDate = $this->validate(
[
'nombre' => 'required',
'tipo' => 'required',
'enlace' => 'required',
],
[
'nombre.required' => 'El campo Titulo categoria es requerido',
'tipo.required' => 'El campo Tipo de archivo es requerido',
'enlace.required' => 'El archivo es requerido',
]
);
$reglamento=new Reglamento;
$reglamento->nombre=$this->nombre;
$reglamento->tipo=$this->tipo;
$reglamento->categoria_id=$this->categoria_id;
if($file = $this->enlace) {
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (is_file( public_path() . '/pdf/' . $nombre )) {
$nombre = rand() . $nombre;
}else{
$this->enlace->storeAs('/pdf',$nombre,'subidaarchivos');
$reglamento->enlace=$nombre;
$control=1;
}
}
}
if ($reglamento->save()){
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Reglamento creado correctamente.']);
}
}
public function edit($id){
$this->resetInputFields();
$reglamento=Reglamento::find($id);
$this->reglamento_id = $id;
$this->nombre = $reglamento->nombre;
$this->tipo = $reglamento->tipo;
$this->crear = 2;
}
public function update() {
$validatedDate = $this->validate(
[
'nombre' => 'required',
'tipo' => 'required',
],
[
'nombre.required' => 'El campo Titulo categoria es requerido',
'tipo.required' => 'El campo Tipo de archivo es requerido',
]
);
$reglamento=Reglamento::find($this->reglamento_id);
$reglamento->nombre=$this->nombre;
$reglamento->tipo=$this->tipo;
$archivo="";
if($file = $this->enlace) {
$archivo=$reglamento->enlace;
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (is_file( public_path() . '/pdf/' . $nombre )) {
$nombre = rand() . $nombre;
}else{
$this->enlace->storeAs('/pdf',$nombre,'subidaarchivos');
$reglamento->enlace=$nombre;
$control=1;
}
}
}
if ($reglamento->save()){
if($archivo){
unlink(public_path().'/pdf/'.$archivo);
}
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Reglamento editado correctamente.']);
}
}
public function delete($id){
if($id){
$reglamento = Reglamento::find($id);
if(unlink(public_path().'/pdf/'.$reglamento->enlace)) {
$reglamento->delete();
}
$this->emit('alert', ['type' => 'error', 'message' => '¡El reglamento se elimino correctamente.!']);
}
}
}