diff --git a/app/Http/Controllers/InstitucionesController.php b/app/Http/Controllers/InstitucionesController.php index 851ff02edc140fca5f98461dfa14f9a9035103db..ee38459f9506920bea6a6635f7b9530f4fc6fe77 100644 --- a/app/Http/Controllers/InstitucionesController.php +++ b/app/Http/Controllers/InstitucionesController.php @@ -40,16 +40,29 @@ public function store(Request $request) 'codigo_postal' => 'nullable|integer', 'localidad' => 'nullable|string', 'imagen' => 'nullable|file|image|max:2048', + 'red_social_inst' => 'nullable|array', + 'link_inst' => 'nullable|array', ]); $telefonosData = $request->only(['prefijo', 'lada', 'numero', 'tipo', 'estatus', 'ext', 'id_radio', 'observaciones']); $telefonosData = array_map(function ($item) { - return array_map(function ($value) { + return array_map(function ($value, $key) { + if ($key === 'numero' && !is_null($value)) { + // Elimina espacios y caracteres no numéricos + $value = preg_replace('/[^0-9]/', '', $value); + } return ($value === "null") ? null : $value; - }, $item); + }, $item, array_keys($item)); }, $telefonosData); - + + $redesSocialesData = $request->only(['red_social_inst', 'link_inst', 'tipo_RS_inst']); + + $redesSocialesData = array_filter($redesSocialesData, function ($item) { + // Solo incluir si no es nulo o vacío + return !is_null($item) && !empty($item); + }); + $redesSocialesData = array_map(function ($item) { return array_map(function ($value) { return ($value === "null") ? null : $value; @@ -195,13 +208,13 @@ public function update(Request $request, string $id) // Guardar auditoría de redes sociales $redesAnteriores = $institucion->redes->toArray(); - $redes = collect($request->input('red_social_inst'))->map(function($url, $index) use ($request) { + $redes = collect($request->input('red_social_inst'))->map(function($redSocial, $index) use ($request) { return [ - 'link_inst' => $url, - 'red_social_inst' => $request->input('red_social_inst')[$index], + 'link_inst' => $request->input('link_inst')[$index], // Asignar correctamente el link + 'red_social_inst' => $redSocial, 'tipo_RS_inst' => $request->input('tipo_RS_inst')[$index], ]; - })->toArray(); + })->toArray(); foreach ($redesAnteriores as $index => $redAnterior) { foreach ($redAnterior as $key => $value) { @@ -342,47 +355,46 @@ private function telefonoDataPresent($telefonosData) private function storeRedesSociales($institucionesId, $redesSocialesData) { $redesSociales = []; - - foreach ($redesSocialesData['red_social_inst'] as $index => $redSocial) { - // Verificar si todos los campos de la red social están vacíos (null o "null") - $isEmpty = true; - foreach ($redesSocialesData as $key => $values) { - if (!is_null($values[$index]) && $values[$index] !== "null") { - $isEmpty = false; - break; + + if (isset($redesSocialesData['red_social_inst'])) { + foreach ($redesSocialesData['red_social_inst'] as $index => $redSocial) { + // Verificar si el campo de la red social está vacío (null o "null") + $isEmpty = empty($redSocial) && empty($redesSocialesData['link_inst'][$index]) && empty($redesSocialesData['tipo_RS_inst'][$index]); + + if (!$isEmpty) { + $redesSociales[] = [ + 'instituciones_id' => $institucionesId, + 'red_social_inst' => $redSocial, + 'link_inst' => $redesSocialesData['link_inst'][$index] ?? null, + 'tipo_RS_inst' => $redesSocialesData['tipo_RS_inst'][$index] ?? null, + ]; } } - - if (!$isEmpty) { - $redesSociales[] = [ - 'instituciones_id' => $institucionesId, - 'red_social_inst' =>$redesSocialesData['red_social_inst'][$index], - 'link_inst' => $redesSocialesData['link_inst'][$index], - 'tipo_RS_inst' => $redesSocialesData['tipo_RS_inst'][$index], - ]; - } } - + if (!empty($redesSociales)) { RedesSocialesInst::insert($redesSociales); return true; } - + return false; } + private function redSocialDataPresent($redesSocialesData) { - foreach ($redesSocialesData['red_social'] as $index => $redSocial) { - foreach ($redesSocialesData as $key => $values) { - if (!is_null($values[$index]) && $values[$index] !== "null") { - return true; + if (isset($redesSocialesData['red_social_inst'])) { + foreach ($redesSocialesData['red_social_inst'] as $index => $redSocial) { + foreach ($redesSocialesData as $key => $values) { + if (isset($values[$index]) && !is_null($values[$index]) && $values[$index] !== "null") { + return true; + } } } } - + return false; - } + } private function storeProfilePicture(Request $request) diff --git a/resources/views/adminGen/instituciones/create.blade.php b/resources/views/adminGen/instituciones/create.blade.php index 6a6af7350fa923d9b9fa9a539eb2ae059e096773..5aef2bd27191ff490580740c27176d4e85312b29 100644 --- a/resources/views/adminGen/instituciones/create.blade.php +++ b/resources/views/adminGen/instituciones/create.blade.php @@ -367,47 +367,47 @@ }); }); - agregarBtnRed.addEventListener('click', function() { + agregarBtnRed.addEventListener('click', function() { const newFormGroup = document.createElement('div'); - newFormGroup.classList.add('grid', 'grid-cols-1', 'md:grid-cols-4', 'gap-5', 'form-group', 'relative'); + newFormGroup.classList.add('grid', 'grid-cols-1', 'md:grid-cols-4', 'gap-5', 'form-group', 'relative', 'mb-5'); newFormGroup.innerHTML = ` -