EVOLUTION-MANAGER
Edit File: AdminInventarioAtractivosController.php
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Storage; use App\ParroquiaModel; use App\AtractivoModel; use App\AtractivoOfreceModel; use App\AtractivoImagenModel; use DB; use Log; class AdminInventarioAtractivosController extends Controller { public function atractivos_index(){ try { $parroquias = ParroquiaModel::all(); return view("admin.atractivos.atractivos_index", compact("parroquias")); } catch (\Throwable $th) { // dd($th->getMessage()); Log::error(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); return abort(500); } } public function atractivos_listado(){ try { $listado = AtractivoModel::with("parroquia")->get(); return response()->json([ "status"=> 200, "response" => $listado, "msg_text" => "Información", "msg_color" => "success" ]); } catch (\Throwable $th) { // dd($th->getMessage()); Log::error(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); return response()->json([ "status"=> 500, "msg_text" => "Error interdo del servidor", "msg_color" => "error" ]); } } public function atractivos_guardar(Request $request){ try { DB::beginTransaction(); // dd($request); #creamos la url de la pagina $codigo_url = $request->nombre; $codigo_url = str_replace(" ", "-", $codigo_url); $codigo_url = preg_replace("/[^A-Za-z\-]/", "", $codigo_url); $codigo_url = strtolower($codigo_url); for ($i=0; $i < 5; $i++) { $codigo_url = str_replace("--", "-", $codigo_url); } $codigo_url = substr($codigo_url, 0, 80); //maximo 80 de largo #si se usa el celuar para whatsapp $celular_what = 0; if(isset($request->celular_what)){ $celular_what = 1; } if(empty($request->atractivo_id_edit)){ //se esta creando uno nuevo $atractivo = new AtractivoModel(); }else{ //se esta editando $atractivo_id = decrypt($request->atractivo_id_edit); $atractivo = AtractivoModel::where("id", $atractivo_id)->first(); } $atractivo->parroquia_id = $request->parroquia; $atractivo->nombre = $request->nombre; $atractivo->direccion = $request->direccion; $atractivo->correo = $request->correo; $atractivo->telefono = $request->telefono; $atractivo->celular = $request->celular; $atractivo->celular_what = $celular_what; $atractivo->costo = $request->costo; $atractivo->responsable = $request->responsable; $atractivo->horario = $request->horario; $atractivo->latitud = $request->latitud; $atractivo->longitud = $request->longitud; $atractivo->url_web = $request->url_web; $atractivo->url_fb = $request->url_fb; $atractivo->url_in = $request->url_in; $atractivo->url_x = $request->url_x; $atractivo->url_yt = $request->url_yt; $atractivo->codigo_url = $codigo_url; $atractivo->save(); #GUARDAMOS LO QUE OFRECE if(isset($request->ofrece_nombre)){ foreach ($request->ofrece_nombre as $on => $ofrece_nombre) { $atractivo_ofrece = new AtractivoOfreceModel(); $atractivo_ofrece->icono = $request->ofrece_icono[$on]; $atractivo_ofrece->nombre = $ofrece_nombre; $atractivo_ofrece->atractivo_id = $atractivo->id; $atractivo_ofrece->save(); } } #SI SE ESTA EDITANDO - VERIFICAMOS SI EXISTEN OFRECE POR ELIMINAR if(isset($request->ofrece_del)){ foreach ($request->ofrece_del as $od => $ofrece_del) { $atractivo_ofrece_id_del = decrypt($ofrece_del); $atractivo_ofrece = AtractivoOfreceModel::where("id", $atractivo_ofrece_id_del)->first(); $atractivo_ofrece->delete(); } } #GUARDAMOS LA FICHA TECNICA EN EL CASO QUE EXISTA $ficha_tecnica = $request->file("ficha_tecnica"); if(!empty($ficha_tecnica)){ $extension = pathinfo($ficha_tecnica->getClientOriginalName(), PATHINFO_EXTENSION); $nombre_ficha = $atractivo->id."-ficha-atractivo-".date('Y-m-d').time()."-".text_random(8).".$extension"; Storage::disk("google")->put($nombre_ficha, file_get_contents($ficha_tecnica)); $url_ficha = Storage::disk("google")->url($nombre_ficha); // Obtenemos el ID del archivo $dir = "/"; // directorio raíz $recursive = false; // Indica si la búsqueda es recursiva $ficha_tecnica_cod = null; // Para guardar el ID del documento $contents = collect(Storage::disk("google")->listContents($dir, $recursive)); $file = $contents->where("type", "file") ->where("filename", pathinfo($nombre_ficha, PATHINFO_FILENAME)) ->where("extension", pathinfo($nombre_ficha, PATHINFO_EXTENSION)) ->first(); if($file){ $ficha_tecnica_cod = $file["path"];} $ficha_tecnica_cod_del = $atractivo->ficha_tecnica_cod; //Guardamos la informacion en base de datos $atractivo->ficha_tecnica = $url_ficha; $atractivo->ficha_tecnica_cod = $ficha_tecnica_cod; $atractivo->save(); //Si hay un documentos previso lo eliminamos if(!empty($ficha_tecnica_cod_del)){ if(Storage::disk("google")->exists($ficha_tecnica_cod_del)){ Storage::disk("google")->delete($ficha_tecnica_cod_del); } } } #GUARDAMOS LAS IMAGENES NUEVAS INGRESADAS $lista_imagen = $request->file("atractivo_imagen"); if(!empty($lista_imagen)){ foreach ($lista_imagen as $key => $imagen){ $extension = pathinfo($imagen->getClientOriginalName(), PATHINFO_EXTENSION); $ruta_imagen = "imagenes/atractivos/".$atractivo->id."/atractivo-".date('Y-m-d').time()."-".text_random(8).".$extension"; Storage::disk("diskPublic")->put($ruta_imagen, file_get_contents($imagen)); $existe = Storage::disk("diskPublic")->exists($ruta_imagen); if($existe){ $imagen_detalle = ""; if(isset($request->imagen_detalle)){ $imagen_detalle = $request->imagen_detalle[$key]; } $atractivo_imagen = new AtractivoImagenModel(); $atractivo_imagen->ruta = $ruta_imagen; $atractivo_imagen->detalle = $imagen_detalle; $atractivo_imagen->nombre = $atractivo->codigo_url; $atractivo_imagen->atractivo_id = $atractivo->id; $atractivo_imagen->save(); } } } #SI SE ESTA EDITANDO -- VERIFICANOS SI EXISTEN IMAGENES POR ELIMINAR if(isset($request->atractivo_imagen_del)){ foreach ($request->atractivo_imagen_del as $key => $imagen_del) { $imagen_del_id = decrypt($imagen_del); $atractivo_imagen = AtractivoImagenModel::where("id", $imagen_del_id)->first(); $existe = Storage::disk("diskPublic")->exists($atractivo_imagen->ruta); if($existe){ Storage::disk("diskPublic")->delete($atractivo_imagen->ruta); } $atractivo_imagen->delete(); } } DB::commit(); return response()->json([ "status"=> 200, "msg_text" => "Registro guardado correctamente", "msg_color" => "success" ]); } catch (\Throwable $th) { // dd(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); DB::rollback(); Log::error(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); return response()->json([ "status"=> 500, "msg_text" => "Error interdo del servidor", "msg_color" => "error" ]); } } public function atractivos_editar($id){ try { $id = decrypt($id); $atractivo = AtractivoModel::with("parroquia", "atractivo_imagen", "atractivo_ofrece")->where("id",$id)->first(); return response()->json([ "status"=> 200, "response" => $atractivo, "msg_text" => "Información", "msg_color" => "success" ]); } catch (\Throwable $th) { // dd($th->getMessage()); Log::error(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); return response()->json([ "status"=> 500, "msg_text" => "Error interdo del servidor", "msg_color" => "danger" ]); } } public function atractivos_eliminar($id){ try { DB::beginTransaction(); $id = decrypt($id); $atractivo = AtractivoModel::with("parroquia", "atractivo_imagen", "atractivo_ofrece")->where("id",$id)->first(); //ELIMINAMOS TODOS LOS OFRECE AtractivoOfreceModel::where("atractivo_id", $atractivo->id)->delete(); //ELIMINAMOS LAS IMAGENES foreach ($atractivo->atractivo_imagen as $key => $atractivo_imagen) { //eliminamos las imagenes locales $existe = Storage::disk("diskPublic")->exists($atractivo_imagen->ruta); if($existe){ Storage::disk("diskPublic")->delete($atractivo_imagen->ruta); } $atractivo_imagen->delete(); } //ELIMINAMOS EL DIRECTORIO DEL SERVICIO $existe = Storage::disk("diskPublic")->exists("imagenes/atractivos/".$atractivo->id); if($existe){ Storage::disk("diskPublic")->deleteDirectory("imagenes/atractivos/".$atractivo->id); } $atractivo->delete(); DB::commit(); return response()->json([ "status"=> 200, "response" => [], "msg_text" => "Registro eliminado correctamente", "msg_color" => "success" ]); } catch (\Throwable $th) { DB::rollback(); // dd(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); Log::error(__CLASS__." => ".__FUNCTION__." => Mensaje => ".$th->getMessage()." => en la linea:".$th->getLine()); return response()->json([ "status"=> 500, "msg_text" => "Error interdo del servidor", "msg_color" => "error" ]); } } }