Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.75 KB | None | 0 0
  1. <!-- Cabecera modal -->
  2. <div class="modal-header">
  3. <h5 class="modal-title h4 text-center text-uppercase">Insertar un nuevo usuario</h5>
  4. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  5. <span aria-hidden="true">&times;</span>
  6. </button>
  7. </div>
  8. <!-- Fin cabecera modal -->
  9.  
  10.  
  11. <!-- Cuerpo modal -->
  12. <div class="modal-body">
  13. <!-- Gif "Cargando" -->
  14. <div class="form-group d-none" id="gif">
  15. <label><img src="images/ajax-loader.gif"> Procesando...</label>
  16. </div>
  17. <!-- Campos ocultos -->
  18. <div class="form-group">
  19. <input type="hidden" id="opcion">
  20. <input type="hidden" id="id">
  21. </div>
  22.  
  23.  
  24. <!-- Campo nacionalidad -->
  25. <div class="form-group">
  26. <label for="">Nacionalidad: </label>
  27. <input type="text" id="txt_nacionalidad" class="form-control" placeholder="Ingrese su nombre">
  28. </div>
  29.  
  30. <!-- Campo cedula -->
  31. <div class="form-group">
  32. <label for="">Cédula: </label>
  33. <input type="text" id="txt_cedula" class="form-control" placeholder="Ingrese su nombre">
  34. </div>
  35.  
  36. <!-- Campo nombre -->
  37. <div class="form-group">
  38. <label for="">Nombres: </label>
  39. <input type="text" id="txt_nombres" class="form-control" placeholder="Ingrese su nombre">
  40. </div>
  41. <!-- Campo país -->
  42. <div class="form-group">
  43. <label for="">apellidos: </label>
  44. <input type="text" id="txt_apellidos" class="form-control" placeholder="Ingrese su país">
  45. </div>
  46. <!-- Campo profesion -->
  47. <div class="form-group">
  48. <label for="">profesion: </label>
  49. <input type="text" id="txt_id_profesion" class="form-control" placeholder="Ingrese su edad">
  50. </div>
  51. </div>
  52. <!-- Fin cuerpo modal -->
  53.  
  54. <!-- Pie del modal -->
  55. <div class="modal-footer">
  56. <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
  57. <button type="button" class="btn btn-primary" id="btn_guardar_cambios">Guardar cambios</button>
  58. </div>
  59. <!-- Fin pie modal -->
  60.  
  61. (function() {
  62. var input_busqueda = $('#txt_busqueda');
  63. listar('');
  64. tipoListado(input_busqueda);
  65. crearPaginacion();
  66. ejecutarAccion();
  67. });
  68.  
  69. // Quitar la alerta del Modal
  70. var quitarAlerta = () => {
  71. $('#alerta').html('');
  72. }
  73.  
  74. // Limpiar el cuadro de búsqueda
  75. var limpiarBusqueda = () => {
  76. $('#txt_busqueda').val('');
  77. }
  78.  
  79. // Desbloquear el botón 'Guardar Cambios'
  80. var desbloquearBoton = () => {
  81. $('#btn_guardar_cambios').removeAttr('disabled');
  82. }
  83.  
  84. // Mostrar una alerta de acuerdo a la respuesta del servidor
  85. var alerta = (opcion, respuesta) => {
  86. let mensaje = '';
  87. switch (opcion) {
  88. case 'insertar':
  89. mensaje = 'Usuario insertado correctamente.';
  90. break;
  91. case 'editar':
  92. mensaje = 'Información de usuario modificada con exito.';
  93. break;
  94. case 'eliminar':
  95. mensaje = 'Usuario eliminado exitosamente.';
  96. break;
  97. }
  98. switch (respuesta) {
  99. case 'BIEN':
  100. $('#alerta').html('<div class="alert alert-success text-center">
  101. <strong>¡BIEN! </strong>' + mensaje + '</div>');
  102. break;
  103. case 'ERROR':
  104. $('#alerta').html('<div class="alert alert-danger text-center">
  105. <strong>¡ERROR! </strong>Solicitud no procesada.</div>');
  106. break;
  107. case 'IGUAL':
  108. $('#alerta').html('<div class="alert alert-info text-center">
  109. <strong>¡ADVERTENCIA! </strong>Ha enviado los mismos datos.</div>');
  110. break;
  111. case 'VACIO':
  112. $('#alerta').html('<div class="alert alert-danger text-center">
  113. <strong>¡ERROR! </strong>No puede enviar datos vacíos.</div>');
  114. break;
  115. }
  116. }
  117.  
  118.  
  119.  
  120. // ----------------------------------------------------Ejecutar la acción seleccionada por el usuario----------------------------------------------------
  121. var ejecutarAccion = () => {
  122. $('#btn_guardar_cambios').on('click', function() {
  123. let opcion = $('#opcion').val();
  124. let id = $('#id').val();
  125. let nacionalidad = $('#txt_nacionalidad').val();
  126. let cedula = $('#txt_cedula').val();
  127. let nombres = $('#txt_nombres').val();
  128. let apellidos = $('#txt_apellidos').val();
  129. let id_profesion = $('#txt_id_profesion').val();
  130. $.ajax({
  131. beforeSend: function() {
  132. $('#gif').toggleClass('d-none');
  133. },
  134. url: 'controllers/controllerActions.php',
  135. method: 'POST',
  136. data: {
  137. opcion: opcion,
  138. id: id,
  139. nacionalidad: nacionalidad,
  140. cedula: cedula,
  141. nombres: nombres,
  142. apellidos: apellidos,
  143. id_profesion: id_profesion
  144. },
  145. }).done(function(data) {
  146. $('#gif').toggleClass('d-none');
  147. alerta(opcion, data);
  148. listar('');
  149. crearPaginacion();
  150. if (opcion == 'eliminar' && data == 'BIEN') {
  151. $('#btn_guardar_cambios').attr('disabled', true);
  152. }
  153. if (opcion == 'insertar' && data == 'BIEN') {
  154. $('#id').val('');
  155. $('#txt_nacionalidad').val('');
  156. $('#txt_cedula').val('');
  157. $('#txt_nombres').val('');
  158. $('#txt_apellidos').val('');
  159. $('#txt_id_profesion').val('');
  160. }
  161. });
  162. });
  163. }
  164.  
  165.  
  166.  
  167. // -------------------------------------------------------------------Preparar datos-------------------------------------------------------------------
  168. var prepararDatos = () => {
  169. let values = [];
  170. // Evento botón editar
  171. $('#table .editar').on('click', function() {
  172. values = ciclo($(this));
  173. $('#opcion').val('editar');
  174. $('#id').val(values[0]);
  175. $('#txt_nacionalidad').val(values[1]).removeAttr('disabled');
  176. $('#txt_cedula').val(values[2]).removeAttr('disabled');
  177. $('#txt_nombres').val(values[3]).removeAttr('disabled');
  178. $('#txt_apellidos').val(values[4]).removeAttr('disabled');
  179. $('#txt_id_profesion').val(values[5]).removeAttr('disabled');
  180. cambiarTitulo('Editar información');
  181. quitarAlerta();
  182. limpiarBusqueda();
  183. desbloquearBoton();
  184. });
  185. // Evento botón eliminar
  186. $('#table .eliminar').on('click', function() {
  187. values = ciclo($(this));
  188. $('#opcion').val('eliminar');
  189. $('#id').val(values[0]);
  190. $('#txt_nacionalidad').val(values[1]).attr('disabled', true);
  191. $('#txt_cedula').val(values[2]).attr('disabled', true);
  192. $('#txt_nombres').val(values[3]).attr('disabled', true);
  193. $('#txt_apellidos').val(values[4]).attr('disabled', true);
  194. $('#txt_id_profesion').val(values[5]).attr('disabled', true);
  195. cambiarTitulo('Eliminar usuario');
  196. quitarAlerta();
  197. limpiarBusqueda();
  198. desbloquearBoton();
  199. });
  200. // Evento btotón insertar
  201. $('#btn_insertar').on('click', function() {
  202. $('#opcion').val('insertar');
  203. $('#id').val('');
  204. $('#txt_nacionalidad').val('').removeAttr('disabled');
  205. $('#txt_cedula').val('').removeAttr('disabled');
  206. $('#txt_nombres').val('').removeAttr('disabled');
  207. $('#txt_apellidos').val('').removeAttr('disabled');
  208. $('#txt_id_profesion').val('').removeAttr('disabled');
  209. cambiarTitulo('Insertar usuario');
  210. quitarAlerta();
  211. limpiarBusqueda();
  212. desbloquearBoton();
  213. });
  214. }
  215. var ciclo = (selector) => {
  216. let datos = [];
  217. $(selector).parents('tr').find('td').each(function(i) {
  218. if (i < 6) {
  219. datos[i] = $(this).text();
  220. } else {
  221. return false;
  222. }
  223. });
  224. return datos;
  225. }
  226. var cambiarTitulo = (titulo) => {
  227. $('.modal-header .modal-title').text(titulo);
  228. }
  229. // --------------------------------------------------------------------------Paginación--------------------------------------------------------------------------
  230. var cambiarPagina = () => {
  231. $('.page-item>.page-link').on('click', function() {
  232. $.ajax({
  233. url: 'controllers/controllerList.php',
  234. method: 'POST',
  235. data: {
  236. pagina: $(this).text()
  237. },
  238. }).done(function(data) {
  239. $('#div_tabla').html(data);
  240. prepararDatos();
  241. });
  242. });
  243. }
  244. var crearPaginacion = () => {
  245. $.ajax({
  246. url: 'controllers/controllerPagination.php',
  247. method: 'POST'
  248. }).done(function(data) {
  249. $('#pagination li').remove();
  250. for (var i = 1; i <= data; i++) {
  251. $('#pagination').append('<li class="page-item"><a class="page-link
  252. text-muted" href="#">' + i + '</a></li>');
  253. }
  254. cambiarPagina();
  255. });
  256. }
  257. // ---------------------------------------------------Listar personas---------------------------------------------------
  258. var listar = (param) => {
  259. $.ajax({
  260. url: 'controllers/controllerList.php',
  261. method: 'POST',
  262. data: {
  263. termino: param
  264. }
  265. }).done(function(data) {
  266. $('#div_tabla').html(data);
  267. prepararDatos();
  268. });
  269. }
  270. var tipoListado = (input) => {
  271. $(input).on('keyup', function() {
  272. let termino = '';
  273. if ($(this).val() != '') {
  274. termino = $(this).val();
  275. }
  276. listar(termino);
  277. });
  278. }
  279.  
  280. class Personas extends Conexion
  281. {
  282. public function __construct()
  283. {
  284. parent::__construct();
  285. }
  286. public function insert(
  287. string $nacionalidad,
  288. string $cedula,
  289. string $nombres,
  290. string $apellidos,
  291. string $id_profesion)
  292. {
  293.  
  294. error_reporting(0);
  295.  
  296. try {
  297. $query = "INSERT INTO clientes VALUES (
  298. null,
  299. :nacionalidad,
  300. :cedula,
  301. :nombres,
  302. :apellidos,
  303. :id_profesion);";
  304. $result = $this->db->prepare($query);
  305. $result->execute(array(
  306. ':nacionalidad' => $nacionalidad,
  307. ':cedula' => $cedula,
  308. ':nombres' => $nombres,
  309. ':apellidos' => $apellidos,
  310. ':id_profesion' => $id_profesion));
  311.  
  312. echo 'BIEN';
  313.  
  314. } catch (PDOException $e) {
  315.  
  316. echo 'ERROR';
  317. }
  318. }
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325. public function delete(int $id_clientes)
  326. {
  327. error_reporting(0);
  328. try {
  329. $query = "DELETE FROM clientes WHERE id_clientes=:id_clientes;";
  330. $result = $this->db->prepare($query);
  331. $result->execute(array(':id_clientes' => $id_clientes));
  332.  
  333.  
  334. echo 'BIEN';
  335.  
  336.  
  337. } catch (PDOException $e) {
  338.  
  339. echo 'ERROR';
  340. }
  341. }
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351. public function edit(string $nacionalidad, string $cedula, string $nombres, string $apellidos, string $id_profesion)
  352. {
  353. error_reporting(0);
  354. try {
  355. $query = "UPDATE clientes SET nacionalidad=:nacionalidad, cedula=:cedula, nombres=:nombres, apellidos=:apellidos, id_profesion=:id_profesion, WHERE id_clientes=:id_clientes;";
  356. $result = $this->db->prepare($query);
  357. $result->execute(array(
  358. ':id_clientes' => $id_clientes,
  359. ':nacionalidad' => $nacionalidad,
  360. ':cedula' => $cedula,
  361. ':nombres' => $nombres,
  362. ':apellidos' => $apellidos,
  363. ':id_profesion' => $id_profesion));
  364. if ($result->rowCount()) {
  365. echo 'BIEN';
  366. } else {
  367. echo 'IGUAL';
  368. }
  369. } catch (PDOException $e) {
  370. echo 'ERROR';
  371. }
  372.  
  373. }
  374.  
  375. public function getAll(int $desde, int $filas): array
  376. {
  377. $query = "SELECT * FROM clientes ORDER BY cedula LIMIT {$desde},
  378. {$filas}";
  379. return $this->ConsultaSimple($query);
  380. }
  381.  
  382. public function getSearch(string $termino): array
  383. {
  384. $where = "WHERE cedula LIKE :cedula";
  385. $array = array(':cedula' => '%' . $termino . '%');
  386. return $this->ConsultaCompleja($where, $array);
  387. }
  388.  
  389. public function getPagination(): array
  390. {
  391. $query = "SELECT COUNT(*) FROM clientes;";
  392. return array(
  393. 'filasTotal' => intval($this->db->query($query)->fetch(PDO::FETCH_BOTH)[0]),
  394. 'filasPagina' => 3,
  395. );
  396. }
  397.  
  398. public function showTable(array $array): string
  399. {
  400. $html = '';
  401. if (count($array)) {
  402. $html = '
  403.  
  404.  
  405.  
  406.  
  407.  
  408. <table class="striped responsive-table">
  409. <thead>
  410. <tr>
  411. <th data-field="n°" class="black-text">N°</th>
  412. <th data-field="n°" class="black-text">Nac.</th>
  413. <th data-field="n°" class="black-text">Cédula</th>
  414. <th data-field="n°" class="black-text">Nombres y Apellidos</th>
  415. <th data-field="cedula" class="black-text">Profesión</th>
  416. <th data-field="operaciones" class="black-text center-align"
  417. colspan="2">Operaciones</th>
  418. </tr>
  419. </thead>
  420.  
  421.  
  422. <tbody>
  423. ';
  424. foreach ($array as $value) {
  425. $html .= ' <tr>
  426. <td class="d-none">' . $value['id_clientes'] . '</td>
  427. <td>' . $value['nacionalidad'] . '</td>
  428. <td>' . $value['cedula'] . '</td>
  429. <td>' . $value['nombres'] .' '. $value['apellidos'] . '</td>
  430. <td>' . $value['id_profesion'] . '</td>
  431.  
  432.  
  433.  
  434. <td class="text-center">
  435.  
  436.  
  437. <button title="Editar este usuario" class="editar btn btn-
  438. secondary" data-toggle="modal" data-target="#ventanaModal">
  439. <i class="fa fa-pencil-square-o"></i>
  440. </button>
  441.  
  442.  
  443. <button title="Eliminar este usuario" type="button"
  444. class="eliminar btn btn-danger" data-toggle="modal" data-
  445. target="#ventanaModal">
  446. <i class="fa fa-trash-o"></i>
  447. </button>
  448.  
  449.  
  450. </td>
  451. </tr>
  452. ';
  453. }
  454. $html .= ' </tbody>
  455. </table>';
  456. } else {
  457.  
  458.  
  459. $html = '
  460. <h6 class="black-text text-darken-2 center CONDENSED LIGHT5">¡
  461. Advertencia: No se ha encontrado ningún registro, debe registrar el
  462. nuevo cliente ! </h6>';
  463. }
  464. return $html;
  465. }
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement