45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
/**
|
|
* POST /api/lab/save_formulario.php
|
|
* Solo admins pueden crear/editar plantillas.
|
|
*
|
|
* Body JSON para crear: { nombre, descripcion?, categoria?, esquema, permite_firma?, requiere_firma? }
|
|
* Body JSON para editar: { id, ...mismos campos... }
|
|
* Body JSON para borrar: { id, borrar: true }
|
|
*/
|
|
require_once __DIR__ . '/_helpers.php';
|
|
requireMethod('POST');
|
|
requireAdmin(); // enfermeros NO pueden diseñar formularios
|
|
|
|
try {
|
|
$datos = inputJson();
|
|
$form = new Formulario();
|
|
$admin = adminId();
|
|
|
|
// Borrar (soft delete)
|
|
if (!empty($datos['id']) && !empty($datos['borrar'])) {
|
|
$form->actualizar((int)$datos['id'], ['is_active' => 0], $admin);
|
|
jsonOk(['id' => (int)$datos['id']], 'Formulario eliminado');
|
|
}
|
|
|
|
// Editar
|
|
if (!empty($datos['id'])) {
|
|
$id = (int)$datos['id'];
|
|
unset($datos['id']);
|
|
$form->actualizar($id, $datos, $admin);
|
|
jsonOk(['id' => $id], 'Formulario actualizado');
|
|
}
|
|
|
|
// Crear
|
|
if (empty($datos['nombre'])) jsonError('El nombre es obligatorio');
|
|
if (empty($datos['esquema'])) jsonError('El esquema es obligatorio');
|
|
|
|
$id = $form->crear($datos, $admin);
|
|
jsonOk(['id' => $id], 'Formulario creado correctamente');
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
jsonError($e->getMessage());
|
|
} catch (Exception $e) {
|
|
jsonError($e->getMessage(), 500);
|
|
}
|