File "SigasControlador.php"

Full Path: C:/wamp64/www/Formaciones/Controladores/SigasControlador.php
File size: 3.56 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Controladores/SigasControlador.php
require_once 'Modelos/SigasModelo.php';

class SigasControlador {
    private $modelo;

    public function __construct($db) {
        $this->modelo = new SigasModelo($db);
        if (!esta_autenticado()) {
            header("Location: index.php?r=auth/login");
            exit;
        }
    }

    public function index() {
		$busqueda = $_GET['busqueda'] ?? '';
		$por_pagina = 10;
		$pagina_actual = isset($_GET['p']) ? (int)$_GET['p'] : 1;
		$inicio = ($pagina_actual - 1) * $por_pagina;

		$total_registros = $this->modelo->contarTodas($busqueda);
		$total_paginas = ceil($total_registros / $por_pagina);
		
		// AQUÍ: La variable se llama $Sigases (con S al final)
		$Sigases = $this->modelo->listarPaginadas($busqueda, $por_pagina, $inicio);
		$areas = $this->modelo->obtenerAreas();

		require_once 'Vistas/admin/sigas.php';
	}

    public function guardar() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // CAPTURA CON LOS NOMBRES EXACTOS DE TU FORMULARIO
            $area        = $_POST['area'] ?? '';
            $f_ini       = $_POST['fecha_Inicial'] ?? '';
            $f_fin       = $_POST['fecha_Final'] ?? '';
            $responsable = $_POST['Responsable'] ?? ''; // Mayúscula como en tu HTML
            $cedula      = $_POST['Cedula'] ?? '';      // Mayúscula como en tu HTML
            $cargo       = $_POST['Cargo'] ?? '';       // Mayúscula como en tu HTML
            $temas       = $_POST['Temas'] ?? '';       // Mayúscula como en tu HTML
            
            // Quien está logueado actualmente
            $quien_registra = usuario_actual()['nombre']; 

            // Manejo del archivo
            $file_data = null;
            if (isset($_FILES['evidencia']) && $_FILES['evidencia']['error'] === UPLOAD_ERR_OK) {
                $file_data = file_get_contents($_FILES['evidencia']['tmp_name']);
            }

            // VALIDACIÓN
            if (empty($area) || empty($cedula) || empty($f_ini) || empty($f_fin)) {
                $_SESSION['error'] = "Los campos Área, Cédula y Fechas son obligatorios.";
            } else {
                $exito = $this->modelo->guardar($area, $cedula, $responsable, $cargo, $f_ini, $f_fin, $temas, $file_data, $quien_registra);
                
                if ($exito) {
                    $_SESSION['exito'] = "Registro guardado exitosamente.";
                } else {
                    $_SESSION['error'] = "Error técnico al guardar en la base de datos.";
                }
            }
        }
        header("Location: index.php?r=admin/Sigas");
        exit;
    }

    public function verPdf() {
        $id = intval($_GET['id'] ?? 0);
        $res = $this->modelo->obtenerPorId($id);
        if ($res && $res['evidencia_datos']) {
            header("Content-type: application/pdf");
            echo $res['evidencia_datos'];
            exit;
        }
        echo "PDF no encontrado.";
    }

    public function descargar() {
        $id = intval($_GET['id'] ?? 0);
        $res = $this->modelo->obtenerPorId($id);
        if ($res && $res['evidencia_datos']) {
            header("Content-type: application/pdf");
            header("Content-Disposition: attachment; filename=\"evidencia_{$id}.pdf\"");
            echo $res['evidencia_datos'];
            exit;
        }
    }

    public function borrar() {
        $id = intval($_GET['id'] ?? 0);
        $this->modelo->borrar($id);
        header("Location: index.php?r=admin/Sigas");
        exit;
    }
}