Skip to content

Commit

Permalink
- Cliente, Slider, Imagen, Nav, Botones
Browse files Browse the repository at this point in the history
- Cliente: se añade la funcionalidad de agregar y eliminar clientes por
medio de peticiones ajax.

- Slider: se elimina código innecesario relacionado con agregar imagen
slider, se evita ataques XSS en el titulo de la imagen del slider.

- Nav: se elimina código innecesario.

- Botones: se añaden acciones para el botón de agregar cliente, agregar
slider.
  • Loading branch information
admont28 committed Feb 4, 2015
1 parent f7eb27b commit 9d18a39
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 28 deletions.
17 changes: 17 additions & 0 deletions app/controller/clientModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,22 @@ function view_db_client($idCliente){
return $cliente = $response[0];
return null;
}

/*
* Función para obtener el ultimo cliente insertado
*/
function view_db_last_client(){

$sentencia = $this->_db->prepare("SELECT * FROM Cliente ORDER BY idCliente DESC LIMIT 1");
$sentencia->execute();

$response = array();
while ($fila = $sentencia->fetch()) {
$response[] = $fila;
}
if(sizeof($response) != 0)
return $cliente = $response[0];
return null;
}
}
?>
2 changes: 1 addition & 1 deletion app/controller/eliminarClienteAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
$resultado = $clientModel->delete_db_client($idCliente);
if($resultado){
$mensaje = get_success_delete_client($idCliente); // envio el mensaje alojado en notificaciones
$mensaje = get_success_delete_client($existente['nombreCliente']); // envio el mensaje alojado en notificaciones
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions app/controller/imagenModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function insert_images_project($ruta, $titulo, $idProyecto){
}

/*
* Función para insertar imagenes de un proyecto
* Función para insertar imagenes de un servicio
*/
function insert_image_service($ruta, $titulo, $idServicio){

Expand All @@ -58,21 +58,16 @@ function insert_image_service($ruta, $titulo, $idServicio){
}

/*
* Función para insertar imagenes de un proyecto
* Función para insertar imagenes de un cliente
*/
function insert_images($ruta, $titulo, $idCliente){
function insert_image_client($ruta, $titulo, $idCliente){

$sentencia = $this->_db->prepare("INSERT INTO Imagen (rutaImagen, tituloImagen, sliderImagen, Cliente_idCliente) VALUES (:ruta, :titulo, false, :idCliente)");
$sentencia->bindParam(':ruta', $ruta);
$sentencia->bindParam(':tituloImagen', $titulo);
$sentencia->bindParam(':idProyecto', $idCliente);
$sentencia->bindParam(':titulo', $titulo);
$sentencia->bindParam(':idCliente', $idCliente);
//Ejecución de la consulta
$sentencia->execute();
$response = array();
while ($fila = $sentencia->fetch()) {
$response[] = $fila;
}
return $response;
return $sentencia->execute();
}

function delete_db_images_service($idServicio){
Expand Down
42 changes: 42 additions & 0 deletions app/controller/insertarClienteAjax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
session_start();
if(!isset($_SESSION['idUsuario'],$_SESSION['nombreUsuario'], $_SESSION['apellidoUsuario'], $_SESSION['superAdminUsuario'])){
header('location: error');
}
require_once ("notificaciones.php");
$mensaje = get_error_insert_client();
print_r($_POST);
print_r($_FILES);
if(sizeof($_POST) == 1 && isset($_POST['nombreCliente'], $_FILES['myfile'])){

$directorioDeGuardado = "../../images/clientes/";
$nombreArchivo = $_FILES['myfile']['name'];
$fullPath = $directorioDeGuardado.$nombreArchivo; // construyo la ruta completa donde se guardará el archivo.
if (! file_exists($fullPath)){ //verifico que no exista la imagen anterior
require_once ("clientModel.php");
require_once ("userModel.php");
$clientModel = new ClientModel();
$userModel = new UserModel();
$usuario = $userModel->view_db_user($_SESSION['idUsuario']);
$nombreCliente = htmlspecialchars($_POST['nombreCliente'], ENT_NOQUOTES); //evito ataques XSS
$resultado = $clientModel->insert_db_client($nombreCliente, $usuario['idUsuario']);
if($resultado){
$ultimoCliente = $clientModel->view_db_last_client();
print_r($ultimoCliente);
if($ultimoCliente != null){

require_once ("imagenModel.php");
$imagenModel = new ImagenModel();
$resultado = $imagenModel->insert_image_client($fullPath, $nombreCliente, $ultimoCliente['idCliente']);
if($resultado){
move_uploaded_file($_FILES["myfile"]["tmp_name"], $fullPath); //muevo el archivo cargado
$mensaje = get_success_insert_client($nombreCliente); // obtengo el mensaje de que todo ha salido bien.
}
}
else
print("ultimoCliente null");
}
}
}
echo $mensaje;
?>
1 change: 1 addition & 0 deletions app/controller/insertarSliderAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
if (! file_exists($fullPath)){ //verifico que no exista la imagen anterior
$sliderModel = new SliderModel();
$tituloImagen = $_POST['tituloImagen'];
$tituloImagen = htmlspecialchars($tituloImagen, ENT_NOQUOTES); //evito ataques XSS
move_uploaded_file($_FILES["myfile"]["tmp_name"], $fullPath); //muevo el archivo cargado
$resultado = $sliderModel->insert_db_image_slider($tituloImagen, $fullPath);
if($resultado){
Expand Down
47 changes: 47 additions & 0 deletions app/controller/notificaciones.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,51 @@ function get_error_delete_image_slider(){
return $mensaje;
}

function get_success_insert_client($nombreCliente){
$mensaje = "<script type='text/javascript'>
$(function(){
new PNotify({
title: 'Acción Exitosa',
text: 'El cliente: ".$nombreCliente." ha sido agregado con éxito.<br>Si desea agregar un nuev cliente presione el botón Agregar.',
type: 'success',
animation: 'show',
confirm: {
confirm: true,
buttons: [{
text: 'Agregar',
addClass: 'btn btn-success',
click: function(notice) {
notice.remove();
document.location='agregarCliente';
}
}, {
text: 'Volver',
click: function(notice) {
notice.remove();
document.location='perfil';
}
}]
}
});
});
</script>";
return $mensaje;
}

function get_error_insert_client(){
$mensaje = "<script type='text/javascript'>
$(function(){
new PNotify({
title: 'Acción No Exitosa :(',
text: 'No se ha podido agregar el cliente con éxito, por favor revisa todos los datos e inténtelo de nuevo.',
type: 'error',
delay: 6000,
animation: 'show',
});
});
</script>";
return $mensaje;
}

?>
65 changes: 65 additions & 0 deletions app/view/agregarCliente.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
session_start();
if(!isset($_SESSION['idUsuario'],$_SESSION['nombreUsuario'], $_SESSION['apellidoUsuario'], $_SESSION['superAdminUsuario'])){
header('location: error');
}
include_once ("imports.php");
include_once ("header.php");
include_once ("nav.php");
include_once ("footer.php");
getImportsUp();
?>
<body id="body">
<div class="con" id="con">
<div class="container" id="main">
<?php
getHeader();
if($_SESSION['superAdminUsuario'] == 1){
getNavSuperAdmin();
}
else
getNavAdmin();
?>
</div>
<div class="contenido">
<div class="row">
<br>
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-8 col-md-offset-2">
<div class="panel panel-default empresa">
<div class="panel-heading">
Crear cliente
</div>
<div class="panel-body">
<div id="mensaje"></div>
<h2 style="color: #019831; text-align: center;">Formulario para agregar un nuevo cliente de Ingnovarq S.A.S</h2>
<br>
<div class="form-horizontal">
<div class="form-group">
<label for="name" class="control-label col-xs-2">Nombre:</label>
<div class="col-xs-10">
<input type="name" id="nombreCliente" name="nombreCliente" class="form-control" placeholder="Nombre">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Imagen:</label>
<div class="col-xs-10" id="cargador">
<div id="fileuploader">Cargar imagen</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-8">
<div id="btn-agregar-cliente-ajax" class="btn btn-success">
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Crear nuevo cliente</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
getFooter();
getImportsDown();
?>
9 changes: 1 addition & 8 deletions app/view/agregarImagenSlider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
include_once ("header.php");
include_once ("nav.php");
include_once ("footer.php");
require_once "../controller/sliderModel.php";

getImportsUp();
$sliderModel = new sliderModel();
?>

<body id="body">
Expand All @@ -22,8 +19,7 @@
getNavSuperAdmin();
}
else
getNavAdmin();
//$direccion = "../controlador/cargarImagenesProductos?idproducto=".$_GET['idproducto'];
getNavAdmin();
?>
</div>
<div class="contenido">
Expand All @@ -39,7 +35,6 @@
<h2 style="color: #019831; text-align: center;">Formulario para agregar una imagen al slider</h2>
<br>
<div class="form-horizontal">
<input type="hidden" name="idImagen" id="idImagen" readonly="readonly">
<div class="form-group">
<label for="inputName" class="control-label col-xs-2">Titulo:</label>
<div class="col-xs-10">
Expand All @@ -50,8 +45,6 @@
<label class="control-label col-xs-2">Imagen:</label>
<div class="col-xs-10" id="cargador">
<div id="fileuploader">Cargar imagen</div>
<p id="nota" class="col-xs-12 text-info">Nota: Si no selecciona ninguna imagen y guarda los cambios, se conservará la imagen actual.
</p>
</div>
</div>
<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion app/view/listarClientes.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</a>
</td>
<td>
<a href="#sinAccion" onclick="eliminarCliente(<?php echo $fila['idCliente'] ?>, '<?php echo $fila['nombreCliente'] ?>');">
<a href="#sinAccion" onclick="eliminarCliente(<?php echo $fila['idCliente'] ?>, '<?php echo htmlspecialchars($fila['nombreCliente'], ENT_NOQUOTES); ?>');">
<div><img style="width: 50px;" class="img-responsive" src="../../images/administrador/delete.png" title="Eliminar cliente"/></div>
</a>
</td>
Expand Down
8 changes: 2 additions & 6 deletions app/view/nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ function getNavSuperAdmin(){
}?>">Crear imagen del slider</a>
</li>
<li class="cuadrado" >
<a href="../controller/logout" id="<?php if(strstr($_SERVER['REQUEST_URI'], 'agregarCliente')){
echo 'seleccionado';
}?>">Cerrar Sesión</a>
<a href="../controller/logout">Cerrar Sesión</a>
</li>
</ul>
<a href="#" id="pull">Menú</a>
Expand Down Expand Up @@ -114,9 +112,7 @@ function getNavAdmin(){
}?>">Crear imagen del slider</a>
</li>
<li class="cuadrado" >
<a href="../controller/logout" id="<?php if(strstr($_SERVER['REQUEST_URI'], 'agregarCliente')){
echo 'seleccionado';
}?>">Cerrar Sesión</a>
<a href="../controller/logout">Cerrar Sesión</a>
</li>
</ul>
<a href="#" id="pull">Menú</a>
Expand Down
2 changes: 1 addition & 1 deletion app/view/perfil.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</div>
<div class="panel-body col-xs-12 col-sm-12 col-md-12 color-content-admin">
<div class="col-md-6 col-xs-6 col-sm-6">
<a href="inicio">
<a href="agregarCliente">
<img src="../../images/administrador/add.png" class="img-responsive">
</a>
</div>
Expand Down
26 changes: 26 additions & 0 deletions js/botones.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,38 @@ $(document).ready(function() {
return data; //debo retornar data para poder que se envien junto con las imagenes.
}
});
alert("Actualicé plugin slider");
} else if($('#btn-agregar-cliente-ajax').length){
// Si existe este botón, el usuario estará agregando un cliente
// Ejecutar si existe el elemento
uploadObj.update({
url: "../controller/insertarClienteAjax",
maxFileSize: 2097152, // 2MB escritos en bytes
dynamicFormData:function()
{
var nombre = $("#nombreCliente").val(); //capturo el nombre del cliente cargado en el input.
// los datos que se van a enviar
var data = {
nombreCliente: nombre //nombre del cliente
};
return data;
}
});
alert("Actualicé plugin cliente");
}


// al dar clic en crear imagen slider cambios al momento de crear una imagen del slider
// ejecuto el plugin uploadFile.
$('#btn-agregar-slider-ajax').click(function(){
uploadObj.startUpload();
});



// al dar clic en agregar cliente ajax ejecuto el plugin uploadFile.
$('#btn-agregar-cliente-ajax').click(function(){
uploadObj.startUpload();
});

});

0 comments on commit 9d18a39

Please sign in to comment.