-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducto-formulario.php
More file actions
138 lines (122 loc) · 5.77 KB
/
Copy pathproducto-formulario.php
File metadata and controls
138 lines (122 loc) · 5.77 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
include_once "config.php";
include_once "entidades/tipoproducto.php";
include_once "entidades/producto.php";
include_once "entidades/venta.php";
$pg = "Edición de producto";
$producto = new Producto();
$producto->cargarFormulario($_REQUEST);
if ($_POST) {
if (isset($_POST["btnGuardar"])) {
if (isset($_FILES["fileImagen"]) && $_FILES["fileImagen"]["error"] == UPLOAD_ERR_OK) {
$mime = mime_content_type($_FILES["fileImagen"]["tmp_name"]);
$ext = strtolower(pathinfo($_FILES["fileImagen"]["name"], PATHINFO_EXTENSION));
if (str_starts_with($mime, "image/") && in_array($ext, ["jpg", "jpeg", "png", "webp", "gif"])) {
$nombreImagen = date("Ymdhmsi") . rand(1000, 9999) . "." . $ext;
if (!move_uploaded_file($_FILES["fileImagen"]["tmp_name"], "img/productos/$nombreImagen")) {
unset($nombreImagen);
}
}
}
if (isset($_GET["id"]) && $_GET["id"] > 0) {
$productoOld = new Producto();
$productoOld->idproducto = $producto->idproducto;
$productoOld->obtenerImagen();
if (isset($nombreImagen)) {
$productoOld->eliminarImagen();
$producto->imagen = $nombreImagen;
} else {
$producto->imagen = $productoOld->imagen;
}
$producto->actualizar();
} else {
if (isset($nombreImagen)) {
$producto->imagen = $nombreImagen;
}
$producto->insertar();
}
$msg["texto"] = "Guardado correctamente";
$msg["codigo"] = "alert-success";
} else if (isset($_POST["btnBorrar"])) {
if (Venta::obtenerVentasPorProducto($producto->idproducto)) {
$msg["texto"] = "No se puede eliminar un producto con ventas asociadas.";
$msg["codigo"] = "alert-danger";
} else {
$producto->eliminar();
header("Location: producto-listado.php");
}
}
}
if (isset($_GET["id"]) && $_GET["id"] > 0) {
$producto->obtenerPorId();
}
$aTipoProductos = TipoProducto::obtenerTodos();
include_once "header.php";
?>
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Producto</h1>
<?php if (isset($msg)): ?>
<div class="row">
<div class="col-12">
<div class="alert <?php echo $msg["codigo"]; ?>" role="alert">
<?php echo $msg["texto"]; ?>
</div>
</div>
</div>
<?php endif;?>
<div class="row">
<div class="col-12 mb-3">
<a href="producto-listado.php" class="btn btn-primary mr-2">Listado</a>
<a href="producto-formulario.php" class="btn btn-primary mr-2">Nuevo</a>
<button type="submit" class="btn btn-success mr-2" id="btnGuardar" name="btnGuardar">Guardar</button>
<button type="submit" class="btn btn-danger" id="btnBorrar" name="btnBorrar">Borrar</button>
</div>
</div>
<div class="row">
<div class="col-6 form-group">
<label for="txtNombre">Nombre:</label>
<input type="text" required class="form-control" name="txtNombre" id="txtNombre" value="<?= $producto->nombre ?>">
</div>
<div class="col-6 form-group">
<label for="lstTipoProducto">Tipo de producto:</label>
<select class="form-control" name="lstTipoProducto" id="lstTipoProducto">
<option value="" selected disabled>Seleccionar</option>
<?php foreach($aTipoProductos as $tipo): ?>
<option value="<?= $tipo->idtipoproducto ?>"<?= $tipo->idtipoproducto == $producto->fk_idtipoproducto ? " selected" : "" ?>><?= $tipo->nombre ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-6 form-group">
<label for="txtCantidad">Cantidad:</label>
<input type="number" required class="form-control" name="txtCantidad" id="txtCantidad" value="<?= $producto->cantidad ?>">
</div>
<div class="col-6 form-group">
<label for="txtPrecio">Precio:</label>
<input type="number" required class="form-control" name="txtPrecio" id="txtPrecio" value="<?= $producto->precio ?>">
</div>
<div class="col-12 form-group">
<label for="txtDescripcion">Descripción:</label>
<textarea name="txtDescripcion" id="txtDescripcion" class="form-control"><?= $producto->descripcion ?></textarea>
<script>
ClassicEditor
.create( document.querySelector( '#txtDescripcion' ) )
.catch( error => {
console.error( error );
} );
</script>
</div>
<div class="col-6 form-group">
<label for="fileImagen">Imagen:</label>
<input class="form-control mb-2" type="file" name="fileImagen" id="fileImagen" accept=".jpg, .jpeg, .png, .webp, .gif">
<?php if($producto->imagen): ?>
<img class="img-thumbnail" src="img/productos/<?= $producto->imagen ?>">
<?php endif; ?>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<?php include_once "footer.php";?>