PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/utilidades/GestionAutores.php

https://bitbucket.org/veroreinah/bookworm
PHP | 198 lines | 153 code | 36 blank | 9 comment | 17 complexity | d25db2edb3383d69d8cc1aa7da97f5ef MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'conexion.php';
  3. require_once 'clases/Autor.php';
  4. /*
  5. * To change this template, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. /**
  9. * Description of GestionCitas
  10. *
  11. * @author Vero
  12. */
  13. class GestionAutores {
  14. public static function crearAutor($autor) {
  15. global $conexion;
  16. $a = new Autor();
  17. $a = $autor;
  18. try {
  19. $query = "insert into t_autores
  20. (nombre_autor)
  21. values ('" . $a->getNombre() . "')";
  22. $result = mysql_query($query, $conexion);
  23. $_SESSION["insertada"] = "El autor se ha insertado con éxito.";
  24. return mysql_insert_id($conexion);
  25. } catch (Exception $e) {
  26. $_SESSION["error"] = "No se ha podido crear el nuevo autor. Inténtelo de nuevo más tarde.";
  27. }
  28. }
  29. public static function modificarAutor($autor) {
  30. global $conexion;
  31. $a = new Autor();
  32. $a = $autor;
  33. try {
  34. $query = "update t_autores
  35. set nombre_autor = '" . $a->getNombre() . "'
  36. where id_autor = '" . $a->getId() . "'";
  37. $result = mysql_query($query, $conexion);
  38. return mysql_affected_rows();
  39. } catch (Exception $e) {
  40. $_SESSION["error"] = "No se ha podido modificar el autor. Inténtelo de nuevo más tarde.";
  41. }
  42. }
  43. public static function eliminarAutor($id) {
  44. global $conexion;
  45. try {
  46. $query = "delete from t_autores
  47. where id_autor = '" . $id . "'";
  48. $result = mysql_query($query, $conexion);
  49. return mysql_affected_rows();
  50. } catch (Exception $e) {
  51. $_SESSION["error"] = "No se ha podido eliminar el autor. Inténtelo de nuevo más tarde.";
  52. }
  53. }
  54. public static function totalAutores() {
  55. global $conexion;
  56. try {
  57. $query = "select count(*) as total
  58. from t_autores";
  59. $result = mysql_query($query, $conexion);
  60. $row = mysql_fetch_array($result);
  61. $total = $row["total"];
  62. return $total;
  63. } catch (Exception $e) {
  64. $_SESSION["error"] = "No se han podido recuperar los autores. Inténtelo de nuevo más tarde.";
  65. }
  66. }
  67. public static function totalFiltered($where="") {
  68. global $conexion;
  69. if ($where != "") {
  70. $w = " where nombre_autor like '%$where%' ";
  71. }
  72. try {
  73. $query = "select count(*) as total
  74. from t_autores
  75. $w";
  76. $result = mysql_query($query, $conexion);
  77. $row = mysql_fetch_array($result);
  78. $total = $row["total"];
  79. return $total;
  80. } catch (Exception $e) {
  81. $_SESSION["error"] = "No se han podido recuperar los autores. Inténtelo de nuevo más tarde.";
  82. }
  83. }
  84. public static function recuperarAutoresL($begin, $limit, $field="", $order="", $where="") {
  85. global $conexion;
  86. $autores = array();
  87. if ($begin != "" && $limit != "") {
  88. $l = " limit $begin, $limit ";
  89. }
  90. if ($field != "" && $order != "") {
  91. $orderBy = " order by $field $order ";
  92. }
  93. if ($where != "") {
  94. $w = " where nombre_autor like '%$where%' ";
  95. }
  96. try {
  97. $query = "select *
  98. from t_autores";
  99. if (isset($w)) {
  100. $query = $query . $w;
  101. }
  102. if (isset($orderBy)) {
  103. $query = $query . $orderBy;
  104. }
  105. if (isset($l)) {
  106. $query = $query . $l;
  107. }
  108. $result = mysql_query($query, $conexion);
  109. while($row = mysql_fetch_array($result)) {
  110. $autor = new Autor();
  111. $autor->setId($row["id_autor"]);
  112. $autor->setNombre($row["nombre_autor"]);
  113. $autores[] = $autor;
  114. }
  115. return $autores;
  116. } catch (Exception $e) {
  117. $_SESSION["error"] = "No se han podido recuperar los autores. Inténtelo de nuevo más tarde.";
  118. }
  119. }
  120. public static function recuperarAutores() {
  121. global $conexion;
  122. $autores = array();
  123. try {
  124. $query = "select *
  125. from t_autores
  126. order by nombre_autor";
  127. $result = mysql_query($query, $conexion);
  128. while($row = mysql_fetch_array($result)) {
  129. $autor = new Autor();
  130. $autor->setId($row["id_autor"]);
  131. $autor->setNombre($row["nombre_autor"]);
  132. $autores[] = $autor;
  133. }
  134. if (count($autores) > 0) {
  135. return $autores;
  136. } else {
  137. $_SESSION["noHay"] = "No existe ningún autor en BookWorm.";
  138. return 0;
  139. }
  140. } catch (Exception $e) {
  141. $_SESSION["error"] = "No se han podido recuperar los autores. Inténtelo de nuevo más tarde.";
  142. }
  143. }
  144. public static function recuperarAutor($id) {
  145. global $conexion;
  146. try {
  147. $query = "select *
  148. from t_autores
  149. where id_autor = $id";
  150. $result = mysql_query($query, $conexion);
  151. while($row = mysql_fetch_array($result)) {
  152. $autor = new Autor();
  153. $autor->setId($row["id_autor"]);
  154. $autor->setNombre($row["nombre_autor"]);
  155. return $autor;
  156. }
  157. } catch (Exception $e) {
  158. $_SESSION["error"] = "No se han podido recupera el autor. Inténtelo de nuevo más tarde.";
  159. }
  160. }
  161. }
  162. ?>