File "SigasModelo.php"

Full Path: C:/wamp64/www/Formaciones/Modelos/SigasModelo.php
File size: 3.27 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Modelos/SigasModelo.php

class SigasModelo {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function getDB() { return $this->db; }

    public function guardar($area, $cedula, $responsable, $cargo, $f_ini, $f_fin, $temas, $file_data, $quien_registra) {
        // Asegúrate de que tu tabla tenga la columna 'temas' o cámbiala por namePro si prefieres
        $sql = "INSERT INTO Sigas (
                    nombre_sigas, cedula_usuario, capacitador, cargoCap, 
                    fecha_Inicial, fecha_Final, temas, evidencia_datos, namePro, created_at
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
        
        $stmt = $this->db->prepare($sql);
        if (!$stmt) return false;

        $null = NULL; 
        // sssssssbs -> 7 strings, 1 blob (b), 1 string
        $stmt->bind_param("sssssssbs", 
            $area, 
            $cedula, 
            $responsable, 
            $cargo, 
            $f_ini, 
            $f_fin, 
            $temas,
            $null, 
            $quien_registra
        );

        if ($file_data !== null) {
            $stmt->send_long_data(7, $file_data); // El PDF es el parámetro 8 (índice 7)
        }

        return $stmt->execute();
    }

    public function listarPaginadas($busqueda = '', $limit = 10, $offset = 0) {
        $sql = "SELECT i.*, u.name as nombre_usuario 
                FROM Sigas i
                LEFT JOIN users u ON i.cedula_usuario = u.cedula
                WHERE 1=1";
        if (!empty($busqueda)) {
            $sql .= " AND (i.nombre_sigas LIKE ? OR i.cedula_usuario LIKE ? OR i.capacitador LIKE ?)";
        }
        $sql .= " ORDER BY i.created_at DESC LIMIT ? OFFSET ?";
        
        $stmt = $this->db->prepare($sql);
        if (!empty($busqueda)) {
            $term = "%$busqueda%";
            $stmt->bind_param("sssii", $term, $term, $term, $limit, $offset);
        } else {
            $stmt->bind_param("ii", $limit, $offset);
        }
        $stmt->execute();
        return $stmt->get_result();
    }

    public function contarTodas($busqueda = '') {
        $sql = "SELECT COUNT(*) as total FROM Sigas i WHERE 1=1";
        if (!empty($busqueda)) {
            $sql .= " AND (nombre_sigas LIKE ? OR cedula_usuario LIKE ? OR capacitador LIKE ?)";
            $stmt = $this->db->prepare($sql);
            $term = "%$busqueda%";
            $stmt->bind_param("sss", $term, $term, $term);
        } else {
            $stmt = $this->db->prepare($sql);
        }
        $stmt->execute();
        return $stmt->get_result()->fetch_assoc()['total'];
    }

    public function obtenerPorId($id) {
        $stmt = $this->db->prepare("SELECT * FROM Sigas WHERE id = ?");
        $stmt->bind_param("i", $id);
        $stmt->execute();
        return $stmt->get_result()->fetch_assoc();
    }
	
	//Obtener todas las areas para el select
     
    public function obtenerAreas() {
        $query = "SELECT area FROM areas ORDER BY area ASC";
        return $this->db->query($query);
    }

    public function borrar($id) {
        $stmt = $this->db->prepare("DELETE FROM Sigas WHERE id = ?");
        $stmt->bind_param("i", $id);
        return $stmt->execute();
    }
}