PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/utilidades/GestionBusqueda.php

https://bitbucket.org/veroreinah/bookworm
PHP | 346 lines | 273 code | 62 blank | 11 comment | 85 complexity | ac545b7723e2f99b9fbf7da927b8a4cc MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'conexion.php';
  3. require_once 'GestionLibros.php';
  4. require_once 'clases/Usuario.php';
  5. require_once 'clases/TipoUsuario.php';
  6. require_once 'clases/Libro.php';
  7. require_once 'clases/Cita.php';
  8. require_once 'clases/Autor.php';
  9. /*
  10. * To change this template, choose Tools | Templates
  11. * and open the template in the editor.
  12. */
  13. /**
  14. * Description of gestionBusqueda
  15. *
  16. * @author Vero
  17. */
  18. class GestionBusqueda {
  19. public static function buscarUsuario($begin, $limit, $nombre) {
  20. global $conexion;
  21. $usuarios = array();
  22. if ($begin != "" && $limit != "") {
  23. $l = " limit $begin, $limit ";
  24. }
  25. try {
  26. $query = "SELECT id_usuario, nombre_usuario, imagen, fecha_nacimiento, facebook, t_usuarios.id_tipo_usuario, descripcion_tipo_usuario
  27. FROM t_usuarios inner join t_tipo_usuario
  28. on t_usuarios.id_tipo_usuario = t_tipo_usuario.id_tipo_usuario
  29. where nombre_usuario like '%" . trim($nombre) . "%' $l";
  30. $result = mysql_query($query, $conexion);
  31. while ($row = mysql_fetch_array($result)) {
  32. $usuario = new Usuario();
  33. $usuario->setId($row["id_usuario"]);
  34. $usuario->setNombre($row["nombre_usuario"]);
  35. $usuarios[] = $usuario;
  36. }
  37. $query2 = "SELECT count(id_usuario) as total
  38. FROM t_usuarios inner join t_tipo_usuario
  39. on t_usuarios.id_tipo_usuario = t_tipo_usuario.id_tipo_usuario
  40. where nombre_usuario like '%" . trim($nombre) . "%'";
  41. $result2 = mysql_query($query2, $conexion);
  42. while ($row2 = mysql_fetch_array($result2)) {
  43. $usuarios["total"] = $row2["total"];
  44. }
  45. return $usuarios;
  46. } catch (Exception $e) {
  47. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  48. }
  49. }
  50. public static function buscarUsuarioPorEmail($begin, $limit, $email) {
  51. global $conexion;
  52. $usuarios = array();
  53. if ($begin != "" && $limit != "") {
  54. $l = " limit $begin, $limit ";
  55. }
  56. try {
  57. $query = "SELECT id_usuario, nombre_usuario, email, imagen, fecha_nacimiento, facebook, t_usuarios.id_tipo_usuario, descripcion_tipo_usuario
  58. FROM t_usuarios inner join t_tipo_usuario
  59. on t_usuarios.id_tipo_usuario = t_tipo_usuario.id_tipo_usuario
  60. where email like '" . trim($email) . "' $l";
  61. $result = mysql_query($query, $conexion);
  62. while ($row = mysql_fetch_array($result)) {
  63. $usuario = new Usuario();
  64. $usuario->setId($row["id_usuario"]);
  65. $usuario->setNombre($row["nombre_usuario"]);
  66. $usuarios[] = $usuario;
  67. }
  68. $query2 = "SELECT count(id_usuario) as total
  69. FROM t_usuarios inner join t_tipo_usuario
  70. on t_usuarios.id_tipo_usuario = t_tipo_usuario.id_tipo_usuario
  71. where email like '" . trim($email) . "'";
  72. $result2 = mysql_query($query2, $conexion);
  73. while ($row2 = mysql_fetch_array($result2)) {
  74. $usuarios["total"] = $row2["total"];
  75. }
  76. return $usuarios;
  77. } catch (Exception $e) {
  78. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  79. }
  80. }
  81. public static function buscarLibro($begin, $limit, $titulo) {
  82. global $conexion;
  83. $libros = array();
  84. if ($begin != "" && $limit != "") {
  85. $l = " limit $begin, $limit ";
  86. }
  87. try {
  88. $query = "SELECT isbn
  89. FROM t_libros
  90. where titulo like '%" . trim($titulo) . "%' $l";
  91. $result = mysql_query($query, $conexion);
  92. while ($row = mysql_fetch_array($result)) {
  93. $libro = new Libro();
  94. $libro = GestionLibros::recuperarLibro($row["isbn"]);
  95. $libros[] = $libro;
  96. }
  97. $query2 = "SELECT count(isbn) as total
  98. FROM t_libros
  99. where titulo like '%" . trim($titulo) . "%'";
  100. $result2 = mysql_query($query2, $conexion);
  101. while ($row2 = mysql_fetch_array($result2)) {
  102. $libros["total"] = $row2["total"];
  103. }
  104. return $libros;
  105. } catch (Exception $e) {
  106. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  107. }
  108. }
  109. public static function buscarLibroPorIsbn($begin, $limit, $isbn) {
  110. global $conexion;
  111. $libros = array();
  112. if ($begin != "" && $limit != "") {
  113. $l = " limit $begin, $limit ";
  114. }
  115. try {
  116. $query = "SELECT isbn
  117. FROM t_libros
  118. where isbn = '" . $isbn . "' $l";
  119. $result = mysql_query($query, $conexion);
  120. while ($row = mysql_fetch_array($result)) {
  121. $libro = new Libro();
  122. $libro = GestionLibros::recuperarLibro($row["isbn"]);
  123. $libros[] = $libro;
  124. }
  125. $query2 = "SELECT count(isbn) as total
  126. FROM t_libros
  127. where isbn = '" . $isbn . "'";
  128. $result2 = mysql_query($query2, $conexion);
  129. while ($row2 = mysql_fetch_array($result2)) {
  130. $libros["total"] = $row2["total"];
  131. }
  132. return $libros;
  133. } catch (Exception $e) {
  134. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  135. }
  136. }
  137. public static function buscarLibrosPorAutor($begin, $limit, $autor) {
  138. global $conexion;
  139. $libros = array();
  140. if ($begin != "" && $limit != "") {
  141. $l = " limit $begin, $limit ";
  142. }
  143. try {
  144. $query = "SELECT t_libros.isbn
  145. FROM t_libros inner join t_libros_autores
  146. on t_libros.isbn = t_libros_autores.isbn
  147. where id_autor = $autor $l";
  148. $result = mysql_query($query, $conexion);
  149. while ($row = mysql_fetch_array($result)) {
  150. $libro = new Libro();
  151. $libro = GestionLibros::recuperarLibro($row["isbn"]);
  152. $libros[] = $libro;
  153. }
  154. $query2 = "SELECT count(t_libros.isbn) as total
  155. FROM t_libros inner join t_libros_autores
  156. on t_libros.isbn = t_libros_autores.isbn
  157. where id_autor = $autor";
  158. $result2 = mysql_query($query2, $conexion);
  159. while ($row2 = mysql_fetch_array($result2)) {
  160. $libros["total"] = $row2["total"];
  161. }
  162. return $libros;
  163. } catch (Exception $e) {
  164. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  165. }
  166. }
  167. public static function buscarLibrosPorTematica($begin, $limit, $tematica) {
  168. global $conexion;
  169. $libros = array();
  170. if ($begin != "" && $limit != "") {
  171. $l = " limit $begin, $limit ";
  172. }
  173. try {
  174. $query = "SELECT t_libros.isbn
  175. FROM t_libros inner join t_libros_tematica
  176. on t_libros.isbn = t_libros_tematica.isbn
  177. where id_tematica = $tematica $l";
  178. $result = mysql_query($query, $conexion);
  179. while ($row = mysql_fetch_array($result)) {
  180. $libro = new Libro();
  181. $libro = GestionLibros::recuperarLibro($row["isbn"]);
  182. $libros[] = $libro;
  183. }
  184. $query2 = "SELECT count(t_libros.isbn) as total
  185. FROM t_libros inner join t_libros_tematica
  186. on t_libros.isbn = t_libros_tematica.isbn
  187. where id_tematica = $tematica";
  188. $result2 = mysql_query($query2, $conexion);
  189. while ($row2 = mysql_fetch_array($result2)) {
  190. $libros["total"] = $row2["total"];
  191. }
  192. return $libros;
  193. } catch (Exception $e) {
  194. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  195. }
  196. }
  197. public static function buscarLibrosAvanzada($begin, $limit, $titulo, $subtitulo, $anyo, $editorial, $tematicas, $autores) {
  198. global $conexion;
  199. $libros = array();
  200. if ($begin != "" && $limit != "") {
  201. $l = " limit $begin, $limit ";
  202. }
  203. try {
  204. //Primero "construimos" el where, comprobando los parámetros que nos han pasado.
  205. $where = "";
  206. if (isset($titulo) && $titulo != "") {
  207. $where .= "titulo like '%" . trim($titulo) . "%'";
  208. }
  209. if (isset($subtitulo) && $subtitulo != "") {
  210. if ($where == "") {
  211. $where .= "subtitulo like '%" . trim($subtitulo) . "%'";
  212. } else {
  213. $where .= " or subtitulo like '%" . trim($subtitulo) . "%'";
  214. }
  215. }
  216. if (isset($anyo) && $anyo != "") {
  217. if ($where == "") {
  218. $where .= "anyo_publicacion = '" . $anyo . "'";
  219. } else {
  220. $where .= " or anyo_publicacion = '" . $anyo . "'";
  221. }
  222. }
  223. if (isset($editorial) && intval($editorial) != 0) {
  224. if ($where == "") {
  225. $where .= "id_editorial = $editorial";
  226. } else {
  227. $where .= " or id_editorial = $editorial";
  228. }
  229. }
  230. if (isset($tematicas) && intval($tematicas) != 0) {
  231. $whereTematica = " where id_tematica = $tematicas ";
  232. }
  233. if (isset($autores) && intval($autores) != 0) {
  234. $whereAutor = " where id_autor = $autores ";
  235. }
  236. //Comenzamos a construir la consulta.
  237. if (isset($tematicas) && intval($tematicas) != 0) {
  238. $query1 = "(select distinct t_libros.isbn
  239. from t_libros inner join t_libros_tematica
  240. on t_libros.isbn = t_libros_tematica.isbn $whereTematica)";
  241. }
  242. if (isset($autores) && intval($autores) != 0) {
  243. $query2 = "(select distinct t_libros.isbn
  244. from t_libros inner join t_libros_autores
  245. on t_libros.isbn = t_libros_autores.isbn $whereAutor)";
  246. }
  247. if ($where != "") {
  248. $query3 = "(select distinct t_libros.isbn
  249. from t_libros where $where)";
  250. }
  251. $query = "";
  252. if (isset($query1)) {
  253. $query .= $query1;
  254. }
  255. if (isset($query2)) {
  256. if ($query == "") {
  257. $query .= $query2;
  258. } else {
  259. $query .= " union " . $query2;
  260. }
  261. }
  262. if (isset($query3)) {
  263. if ($query == "") {
  264. $query .= $query3;
  265. } else {
  266. $query .= " union " . $query3;
  267. }
  268. }
  269. $query .= $l;
  270. $result = mysql_query($query, $conexion);
  271. $libros["total"] = 0;
  272. while ($row = mysql_fetch_array($result)) {
  273. $libro = new Libro();
  274. $libro = GestionLibros::recuperarLibro($row["isbn"]);
  275. $libros["total"] = $libros["total"] + 1;
  276. $libros[] = $libro;
  277. }
  278. return $libros;
  279. } catch (Exception $e) {
  280. $_SESSION["error"] = "Se ha producido un error al realizar la búsqueda. Inténtelo de nuevo más tarde.";
  281. }
  282. }
  283. }
  284. ?>