-
Notifications
You must be signed in to change notification settings - Fork 0
/
funciones.php
112 lines (103 loc) · 3.63 KB
/
funciones.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
function obtenerProductosEnCarrito()
{
$bd = obtenerConexion();
iniciarSesionSiNoEstaIniciada();
$sentencia = $bd->prepare("SELECT producto.id_producto, producto.nombre, producto.modelo, producto.precio, producto.cantidad_existente,
producto.imagen, producto.id_categoria, producto.id_tallas
FROM producto
INNER JOIN carrito_usuarios
ON producto.id = carrito_usuarios.id_producto
WHERE carrito_usuarios.id_sesion = ?");
$idSesion = session_id();
$sentencia->execute([$idSesion]);
return $sentencia->fetchAll();
}
function quitarProductoDelCarrito($idProducto)
{
$bd = obtenerConexion();
iniciarSesionSiNoEstaIniciada();
$idSesion = session_id();
$sentencia = $bd->prepare("DELETE FROM carrito_usuarios WHERE id_sesion = ? AND id_producto = ?");
return $sentencia->execute([$idSesion, $idProducto]);
}
function obtenerProductos()
{
$bd = obtenerConexion();
$sentencia = $bd->query("SELECT id_producto, nombre, modelo, precio, cantidad_existente, imagen, id_categoria, id_tallas FROM producto");
return $sentencia->fetchAll();
}
function productoYaEstaEnCarrito($idProducto)
{
$ids = obtenerIdsDeProductosEnCarrito();
foreach ($ids as $id) {
if ($id == $idProducto) return true;
}
return false;
}
function obtenerIdsDeProductosEnCarrito()
{
$bd = obtenerConexion();
iniciarSesionSiNoEstaIniciada();
$sentencia = $bd->prepare("SELECT id_producto FROM carrito_usuarios WHERE id_sesion = ?");
$idSesion = session_id();
$sentencia->execute([$idSesion]);
return $sentencia->fetchAll(PDO::FETCH_COLUMN);
}
function agregarProductoAlCarrito($idProducto)
{
// Ligar el id del producto con el usuario a través de la sesión
$bd = obtenerConexion();
iniciarSesionSiNoEstaIniciada();
$idSesion = session_id();
$sentencia = $bd->prepare("INSERT INTO carrito_usuarios(id_sesion, id_producto) VALUES (?, ?)");
return $sentencia->execute([$idSesion, $idProducto]);
}
function iniciarSesionSiNoEstaIniciada()
{
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
}
function eliminarProducto($id)
{
$bd = obtenerConexion();
$sentencia = $bd->prepare("DELETE FROM producto WHERE id = ?");
return $sentencia->execute([$id]);
}
function guardarProducto($nombre, $precio, $descripcion)
{
$bd = obtenerConexion();
$sentencia = $bd->prepare("INSERT INTO producto(nombre, precio, descripcion) VALUES(?, ?, ?)");
return $sentencia->execute([$nombre, $precio, $descripcion]);
}
function obtenerVariableDelEntorno($key)
{
if (defined("_ENV_CACHE")) {
$vars = _ENV_CACHE;
} else {
$file = "env.php";
if (!file_exists($file)) {
throw new Exception("El archivo de las variables de entorno ($file) no existe. Favor de crearlo");
}
$vars = parse_ini_file($file);
define("_ENV_CACHE", $vars);
}
if (isset($vars[$key])) {
return $vars[$key];
} else {
throw new Exception("La clave especificada (" . $key . ") no existe en el archivo de las variables de entorno");
}
}
function obtenerConexion()
{
$password = obtenerVariableDelEntorno("MYSQL_PASSWORD");
$user = obtenerVariableDelEntorno("MYSQL_USER");
$dbName = obtenerVariableDelEntorno("MYSQL_DATABASE_NAME");
$database = new PDO('mysql:host=localhost;dbname=' . $dbName, $user, $password);
$database->query("set names utf8;");
$database->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
return $database;
}