You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.1 KiB
PHTML

2 months ago
<?php
namespace App\Http\Controllers\Administracion;
use App\Http\Controllers\Controller;
use App\Models\ProcesoAdmision;
use App\Models\ProcesoAdmisionDetalle;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
class ProcesoAdmisionDetalleController extends Controller
{
2 months ago
2 months ago
public function index(Request $request, $procesoId)
{
ProcesoAdmision::findOrFail($procesoId);
$q = ProcesoAdmisionDetalle::query()->where('proceso_admision_id', $procesoId);
if ($request->filled('tipo')) {
$q->where('tipo', $request->string('tipo'));
}
return response()->json($q->orderByDesc('id')->get());
}
2 months ago
2 months ago
public function store(Request $request, $procesoId)
{
ProcesoAdmision::findOrFail($procesoId);
2 months ago
// ✅ Convertir JSON string -> array antes de validar (cuando llega desde FormData)
if ($request->has('listas') && is_string($request->input('listas'))) {
$decoded = json_decode($request->input('listas'), true);
if (json_last_error() === JSON_ERROR_NONE) {
$request->merge(['listas' => $decoded]);
}
}
if ($request->has('meta') && is_string($request->input('meta'))) {
$decoded = json_decode($request->input('meta'), true);
if (json_last_error() === JSON_ERROR_NONE) {
$request->merge(['meta' => $decoded]);
}
}
2 months ago
$data = $request->validate([
'tipo' => ['required', Rule::in(['requisitos','pagos','vacantes','cronograma'])],
'titulo_detalle' => ['required','string','max:255'],
'descripcion' => ['nullable','string'],
'listas' => ['nullable','array'],
'meta' => ['nullable','array'],
'url' => ['nullable','string','max:500'],
'imagen' => ['nullable','image','mimes:jpg,jpeg,png,webp','max:10240'],
'imagen_2' => ['nullable','image','mimes:jpg,jpeg,png,webp','max:10240'],
]);
$data['proceso_admision_id'] = (int) $procesoId;
if ($request->hasFile('imagen')) {
$data['imagen_path'] = $request->file('imagen')->store('admisiones/detalles', 'public');
}
if ($request->hasFile('imagen_2')) {
$data['imagen_path_2'] = $request->file('imagen_2')->store('admisiones/detalles', 'public');
}
$detalle = ProcesoAdmisionDetalle::create($data);
return response()->json($detalle, 201);
}
2 months ago
2 months ago
public function show($id)
{
return response()->json(ProcesoAdmisionDetalle::findOrFail($id));
}
2 months ago
2 months ago
public function update(Request $request, $id)
{
$detalle = ProcesoAdmisionDetalle::findOrFail($id);
2 months ago
// ✅ Convertir JSON string -> array antes de validar (cuando llega desde FormData)
if ($request->has('listas') && is_string($request->input('listas'))) {
$decoded = json_decode($request->input('listas'), true);
if (json_last_error() === JSON_ERROR_NONE) {
$request->merge(['listas' => $decoded]);
}
}
if ($request->has('meta') && is_string($request->input('meta'))) {
$decoded = json_decode($request->input('meta'), true);
if (json_last_error() === JSON_ERROR_NONE) {
$request->merge(['meta' => $decoded]);
}
}
2 months ago
$data = $request->validate([
'tipo' => ['sometimes', Rule::in(['requisitos','pagos','vacantes','cronograma'])],
'titulo_detalle' => ['sometimes','string','max:255'],
'descripcion' => ['sometimes','nullable','string'],
'listas' => ['sometimes','nullable','array'],
'meta' => ['sometimes','nullable','array'],
'url' => ['sometimes','nullable','string','max:500'],
'imagen' => ['sometimes','nullable','image','mimes:jpg,jpeg,png,webp','max:10240'],
'imagen_2' => ['sometimes','nullable','image','mimes:jpg,jpeg,png,webp','max:10240'],
]);
if ($request->hasFile('imagen')) {
if ($detalle->imagen_path) Storage::disk('public')->delete($detalle->imagen_path);
$data['imagen_path'] = $request->file('imagen')->store('admisiones/detalles', 'public');
}
if ($request->hasFile('imagen_2')) {
if ($detalle->imagen_path_2) Storage::disk('public')->delete($detalle->imagen_path_2);
$data['imagen_path_2'] = $request->file('imagen_2')->store('admisiones/detalles', 'public');
}
$detalle->update($data);
return response()->json($detalle->fresh());
}
public function destroy($id)
{
$detalle = ProcesoAdmisionDetalle::findOrFail($id);
if ($detalle->imagen_path) Storage::disk('public')->delete($detalle->imagen_path);
if ($detalle->imagen_path_2) Storage::disk('public')->delete($detalle->imagen_path_2);
$detalle->delete();
return response()->json(['message' => 'Detalle eliminado']);
}
}