Sindbad~EG File Manager
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Noticia;
use Livewire\WithFileUploads;
use Image;
use App\Models\Emoticonnoticia;
use App\Models\Comentarionoticia;
use App\File;
use App\Models\User;
use App\Notifications\NuevaNotificacion;
use Illuminate\Support\Facades\Storage;
use Auth;
class Noticias extends Component{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
protected $queryString = ['search' => ['except' => '']];
public $search='';
public $titulo,$video,$noticia_id,$tipo,$vista;
public function render(){
$noticias = Noticia::where('titulo','LIKE','%'.$this->search.'%')->where('vista',$this->vista)
->orderBy('id','desc')->paginate(20);
return view('livewire.noticias',["noticias"=>$noticias]);
}
private function resetInputFields(){
$this->video = '';
$this->titulo = '';
$this->tipo = '';
$this->noticia_id = '';
}
public function store(){
$validatedDate = $this->validate([
'titulo' => 'required',
'video' => 'required',
],
[
'titulo.required' => 'El campo titulo es requerido',
'video.required' => 'El campo noticia es requerido',
]);
if($file = $this->video) {
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (Storage::disk('subidavideos')->exists('videos/noticia/' . $nombre)) {
$nombre = rand() . $nombre;
}else{
$this->video->storeAs('/videos/noticia',$nombre,'subidavideos');
$control=1;
}
}
}
$post = Noticia::create([
'titulo' => $this->titulo,
'video' => $nombre,
'vista' => $this->vista
]);
if($this->vista == 1){
User::all()
->each(function(User $user) use ($post){
$user->notify(new NuevaNotificacion([
'url' => '/noticias?search='.$post->titulo,
'titulo' => $post->titulo,
'descripcion' => 'Nueva noticia agregada',
]));
});
}else{
User::all()
->each(function(User $user) use ($post){
$user->notify(new NuevaNotificacion([
'url' => '/tecontamosque?search='.$post->titulo,
'titulo' => $post->titulo,
'descripcion' => 'Nueva te contamos que agregada',
]));
});
}
session()->flash('success', 'Noticia agregada correctamente!');
$this->resetInputFields();
}
public function edit($id){
$noticia = Noticia::find($id);
$this->tipo = 'editar';
$this->noticia_id = $id;
$this->titulo = $noticia->titulo;
$this->video = $noticia->video;
}
public function update(){
$validatedDate = $this->validate([
'titulo' => 'required',
],
[
'titulo.required' => 'El campo titulo es requerido',
]);
$noticia = Noticia::find($this->noticia_id);
if($noticia->video != $this->video){
if($file = $this->video) {
$control=0;
$nombre = rand().".".$file->getClientOriginalExtension();
while ($control == 0) {
if (Storage::disk('subidavideos')->exists('videos/noticia/' . $nombre)) {
$nombre = rand() . $nombre;
}else{
$this->video->storeAs('/videos/noticia',$nombre,'subidavideos');
$control=1;
}
}
}
$noticia->update([
'titulo' => $this->titulo,
'video' => $nombre,
]);
}else{
$noticia->update([
'titulo' => $this->titulo,
]);
}
session()->flash('success', 'Noticia actualizada correctamente!');
$this->resetInputFields();
}
public function delete($id){
$emociones = Emoticonnoticia::where('noticia_id',$id)->get();
$comentarios = Comentarionoticia::where('noticia_id',$id)->get();
foreach($emociones as $emo){
$emo->delete();
}
foreach($comentarios as $com){
$com->delete();
}
$noticia = Noticia::find($id);
if ($noticia) {
$archivo = 'videos/noticia/' . $noticia->video;
if (Storage::disk('subidavideos')->exists($archivo)) {
// Elimina el archivo del sistema de archivos
Storage::disk('subidavideos')->delete($archivo);
}
// Elimina el registro del documento en la base de datos
$noticia->delete();
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists