File: /var/www/html/xfacil.desafio.com.py/app/Http/Livewire/Bibliotecas.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Boletin;
use App\Models\Biblioteca;
use App\Models\User;
use App\Notifications\NuevaNotificacion;
use Auth;
use Livewire\WithFileUploads;
use Image;
use App\File;
class Bibliotecas extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
protected $queryString = ['search' => ['except' => '']];
public $search='', $boletinid=null;
public $boletin_id,$titulo,$boletin,$categoria,$categoria_id,$logo,$logoupdate;
public $updateMode = 0;
public function render(){
if($this->boletinid){
$biblioteca = Biblioteca::where('categoria_id',$this->boletinid)
->orderBy('orden', 'asc')->paginate(20);
$total=Biblioteca::where('categoria_id',$this->boletinid)
->orderBy('orden', 'asc')->count();
}
else{
$biblioteca = Biblioteca::where('titulo', 'LIKE', '%'.$this->search.'%')
->whereNull('categoria_id')
->orderBy('orden', 'asc')
->paginate(20);
$total=Biblioteca::where('titulo', 'LIKE', '%'.$this->search.'%')
->whereNull('categoria_id')
->orderBy('orden', 'asc')
->count();
}
return view('livewire.bibliotecas.index',["boletines"=>$biblioteca,"total"=>$total]);
}
public function reordenar($ordenar, $item){
if($ordenar == 'aumentar'){
$itemafectado=$item+1;
}else{
$itemafectado=$item-1;
}
//dd($ordenar,$item,$itemafectado);
$boletinafectado=Biblioteca::where('categoria_id',$this->boletinid)
->where('orden', $itemafectado)->first();
$boletin=Biblioteca::where('categoria_id',$this->boletinid)
->where('orden', $item)->first();
$boletinId=$boletin->id;
$boletinafectado->orden=$item;
$boletinafectado->update();
$boletin=Biblioteca::find($boletinId);
$boletin->orden=$itemafectado;
$boletin->update();
//dd($boletinafectado,$boletin);
}
private function resetInputFields(){
$this->boletin_id = '';
$this->titulo = '';
$this->categoria = '';
$this->boletin = '';
$this->logo = '';
$this->logoupdate = '';
$this->emit('boletin', '');
}
public function crear(){
$this->updateMode = 1;
$this->resetInputFields();
}
public function cancel(){
$this->updateMode = 0;
$this->resetInputFields();
}
public function store()
{
// Validación
$validatedData = $this->validate([
'titulo' => 'required',
'categoria' => 'required',
'logo' => $this->categoria == 1 ? 'required' : 'nullable',
// Validar boletin solo si es tipo post (1)
'boletin' => $this->categoria == 1 ? 'required' : 'nullable',
]);
$nombre = null;
if($file = $this->logo) {
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (is_file( public_path() . '/images/biblioteca/' . $nombre )) {
$nombre = rand() . $nombre;
}else{
Image::make($this->logo)
->heighten(1000)
->save(public_path() . '/images/biblioteca/' . $nombre);
$control=1;
}
}
}
// Crear el registro
$biblioteca = Biblioteca::create([
'titulo' => $this->titulo,
'boletin' => $this->boletin,
'categoria_id' => $this->categoria_id,
'tipo' => $this->categoria,
'logo' => $nombre,
]);
$this->resetInputFields();
$this->emit('alert', ['type' => 'success', 'message' => 'Información agregada correctamente!']);
}
public function edit($id){
$this->updateMode = 2;
$boletin = Biblioteca::find($id);
$this->boletin_id = $id;
$this->titulo = $boletin->titulo;
$this->categoria = $boletin->tipo;
$this->logo = $boletin->logo;
$this->boletin = $boletin->boletin;
$this->emit('boletin', $boletin->boletin);
}
public function delete($id)
{
if ($id) {
// Encontrar el registro principal
$boletin = Biblioteca::find($id);
if ($boletin) {
// Primero borrar los nietos (registros de tercer nivel)
$hijos = Biblioteca::where('categoria_id', $id)->get();
foreach ($hijos as $hijo) {
// Borrar los nietos de cada hijo
Biblioteca::where('categoria_id', $hijo->id)->delete();
}
// Luego borrar los hijos (registros de segundo nivel)
Biblioteca::where('categoria_id', $id)->delete();
// Finalmente borrar el registro principal
$boletin->delete();
$this->emit('alert', ['type' => 'error', 'message' => 'Eliminado correctamente con todos sus registros relacionados!']);
}
}
}
public function view($id){
$this->boletinid=$id;
$this->categoria_id=$id;
}
public function notview(){
$this->boletinid=null;
$this->categoria_id='';
}
public function name($id)
{
$breadcrumb = [];
$current = Biblioteca::find($id);
while ($current) {
array_unshift($breadcrumb, [
'id' => $current->id,
'titulo' => $current->titulo ?: 'Sin título'
]);
if ($current->categoria_id) {
$current = Biblioteca::find($current->categoria_id);
} else {
break;
}
}
return collect($breadcrumb)->map(function($item) {
return '<span wire:click="view('.$item['id'].')" style="cursor: pointer; color: #2563eb;" onmouseover="this.style.textDecoration=\'underline\'" onmouseout="this.style.textDecoration=\'none\'">'. $item['titulo'] .'</span>';
})->implode('/');
}
public function update(){
$validatedDate = $this->validate([
'titulo' => 'required',
'boletin' => $this->categoria == 1 ? 'required' : 'nullable',
],
[
'titulo.required' => 'El campo Titulo es requerido',
'boletin.required' => 'El campo boletin es requerido',
]);
if ($this->boletin_id) {
$boletin = Biblioteca::find($this->boletin_id);
if($this->logoupdate) {
$file = $this->logoupdate;
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (is_file( public_path() . '/images/biblioteca/' . $nombre )) {
$nombre = rand() . $nombre;
}else{
Image::make($this->logoupdate)
->heighten(1000)
->save(public_path() . '/images/biblioteca/' . $nombre);
$control=1;
}
}
$boletin->update([
'titulo' => $this->titulo,
'boletin' => $this->boletin,
'logo' => $nombre,
'tipo' => $this->categoria,
]);
}
else{
$boletin->update([
'titulo' => $this->titulo,
'boletin' => $this->boletin,
'tipo' => $this->categoria,
]);
}
$this->updateMode = 0;
$this->emit('alert', ['type' => 'info', 'message' => 'Actualizado correctamente!']);
$this->resetInputFields();
}
}
}