Created
April 29, 2019 20:12
-
-
Save donrorro/ce0b76b6e5bcdfebf0c55becb2d7cd3d to your computer and use it in GitHub Desktop.
articulo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ob_start(); | |
if (strlen(session_id()) < 1){ | |
session_start();//Validamos si existe o no la sesión | |
} | |
if (!isset($_SESSION["nombre"])) | |
{ | |
header("Location: ../vistas/login.html");//Validamos el acceso solo a los usuarios logueados al sistema. | |
} | |
else | |
{ | |
//Validamos el acceso solo al usuario logueado y autorizado. | |
if ($_SESSION['almacen']==1) | |
{ | |
require_once "../modelos/Articulo.php"; | |
$articulo=new Articulo(); | |
$idarticulo=isset($_POST["idarticulo"])? limpiarCadena($_POST["idarticulo"]):""; | |
$idcategoria=isset($_POST["idcategoria"])? limpiarCadena($_POST["idcategoria"]):""; | |
$codigo=isset($_POST["codigo"])? limpiarCadena($_POST["codigo"]):""; | |
$nombre=isset($_POST["nombre"])? limpiarCadena($_POST["nombre"]):""; | |
$stock=isset($_POST["stock"])? limpiarCadena($_POST["stock"]):""; | |
$descripcion=isset($_POST["descripcion"])? limpiarCadena($_POST["descripcion"]):""; | |
$imagen=isset($_POST["imagen"])? limpiarCadena($_POST["imagen"]):""; | |
switch ($_GET["op"]){ | |
case 'guardaryeditar': | |
if (!file_exists($_FILES['imagen']['tmp_name']) || !is_uploaded_file($_FILES['imagen']['tmp_name'])) | |
{ | |
$imagen=$_POST["imagenactual"]; | |
} | |
else | |
{ | |
$ext = explode(".", $_FILES["imagen"]["name"]); | |
if ($_FILES['imagen']['type'] == "image/jpg" || $_FILES['imagen']['type'] == "image/jpeg" || $_FILES['imagen']['type'] == "image/png") | |
{ | |
$imagen = round(microtime(true)) . '.' . end($ext); | |
move_uploaded_file($_FILES["imagen"]["tmp_name"], "../files/articulos/" . $imagen); | |
} | |
} | |
if (empty($idarticulo)){ | |
$rspta=$articulo->insertar($idcategoria,$codigo,$nombre,$stock,$descripcion,$imagen); | |
echo $rspta ? "Artículo registrado" : "Artículo no se pudo registrar"; | |
} | |
else { | |
$rspta=$articulo->editar($idarticulo,$idcategoria,$codigo,$nombre,$stock,$descripcion,$imagen); | |
echo $rspta ? "Artículo actualizado" : "Artículo no se pudo actualizar"; | |
} | |
break; | |
case 'desactivar': | |
$rspta=$articulo->desactivar($idarticulo); | |
echo $rspta ? "Artículo Desactivado" : "Artículo no se puede desactivar"; | |
break; | |
case 'activar': | |
$rspta=$articulo->activar($idarticulo); | |
echo $rspta ? "Artículo activado" : "Artículo no se puede activar"; | |
break; | |
case 'mostrar': | |
$rspta=$articulo->mostrar($idarticulo); | |
//Codificar el resultado utilizando json | |
echo json_encode($rspta); | |
break; | |
case 'listar': | |
$rspta=$articulo->listar(); | |
//Vamos a declarar un array | |
$data= Array(); | |
while ($reg=$rspta->fetch_object()){ | |
$data[]=array( | |
"0"=>($reg->condicion)?'<button class="btn btn-warning" onclick="mostrar('.$reg->idarticulo.')"> | |
<i class="fa fa-pencil"></i></button>'. | |
' <button class="btn btn-danger" onclick="desactivar('.$reg->idarticulo.')"> | |
<i class="fa fa-close"></i></button>'. | |
'<button class="btn btn-warning" onclick="mostrar('.$reg->idarticulo.')"> | |
<i class="fa fa-eye"></i></button>': | |
' <button class="btn btn-primary" onclick="activar('.$reg->idarticulo.')"> | |
<i class="fa fa-check"></i></button>', | |
"1"=>$reg->nombre, | |
"2"=>$reg->categoria, | |
"3"=>$reg->codigo, | |
"4"=>$reg->stock, | |
"5"=>"<img src='../files/articulos/".$reg->imagen."' height='50px' width='50px' >", | |
"6"=>($reg->condicion)?'<span class="label bg-green">Activado</span>': | |
'<span class="label bg-red">Desactivado</span>' | |
); | |
} | |
$results = array( | |
"sEcho"=>1, //Información para el datatables | |
"iTotalRecords"=>count($data), //enviamos el total registros al datatable | |
"iTotalDisplayRecords"=>count($data), //enviamos el total registros a visualizar | |
"aaData"=>$data); | |
echo json_encode($results); | |
break; | |
case "selectCategoria": | |
require_once "../modelos/Categoria.php"; | |
$categoria = new Categoria(); | |
$rspta = $categoria->select(); | |
while ($reg = $rspta->fetch_object()) | |
{ | |
echo '<option value=' . $reg->idcategoria . '>' . $reg->nombre . '</option>'; | |
} | |
break; | |
} | |
//Fin de las validaciones de acceso | |
} | |
else | |
{ | |
require 'noacceso.php'; | |
} | |
} | |
ob_end_flush(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment