PageRenderTime 82ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/utilidades/GestionLibros.php

https://bitbucket.org/veroreinah/bookworm
PHP | 857 lines | 707 code | 140 blank | 10 comment | 100 complexity | 5083f9c47dd147470e27db7b631943d1 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'conexion.php';
  3. require_once 'GestionUsuarios.php';
  4. require_once 'clases/Usuario.php';
  5. require_once 'clases/Libro.php';
  6. require_once 'GestionEditoriales.php';
  7. require_once 'clases/Editorial.php';
  8. require_once 'GestionTipoEdicion.php';
  9. require_once 'clases/TipoEdicion.php';
  10. require_once 'clases/Tematica.php';
  11. require_once 'clases/Comentario.php';
  12. require_once 'clases/Autor.php';
  13. /*
  14. * To change this template, choose Tools | Templates
  15. * and open the template in the editor.
  16. */
  17. /**
  18. * Description of GestionLibros
  19. *
  20. * @author Vero
  21. */
  22. class GestionLibros {
  23. public static function crearLibro($libro) {
  24. global $conexion;
  25. $l = new Libro();
  26. $l = $libro;
  27. $insert = "(isbn, titulo, subtitulo, paginas, portada, sinopsis, anyo_publicacion, edicion, ";
  28. $e = new Editorial();
  29. $e = $l->getEditorial();
  30. if ($e != null) {
  31. $editorial = "'" . $e->getId() . "',";
  32. $insert .= " id_editorial, ";
  33. } else {
  34. $editorial = "";
  35. }
  36. $t = new TipoEdicion();
  37. $t = $l->getTipoEdicion();
  38. if ($t != null) {
  39. $tipo = "'" . $t->getId() . "',";
  40. $insert .= " id_tipo_edicion, ";
  41. } else {
  42. $tipo = "";
  43. }
  44. $u = new Usuario();
  45. $u = $l->getUsuario();
  46. $tematicas = array();
  47. $tematicas = $l->getTematicas();
  48. $autores = array();
  49. $autores = $l->getAutores();
  50. $insert .= " id_usuario, fecha)";
  51. try {
  52. $query = "insert into t_libros
  53. $insert
  54. values ('" . $l->getIsbn() . "',
  55. '" . $l->getTitulo() . "',
  56. '" . $l->getSubtitulo() . "',
  57. '" . $l->getPaginas() . "',
  58. '" . $l->getPortada() . "',
  59. \"" . mysql_escape_string($l->getSinopsis()) . "\",
  60. '" . $l->getAnyoPublicacion() . "',
  61. '" . $l->getEdicion() . "',
  62. $editorial
  63. $tipo
  64. '" . $u->getId() . "',
  65. '" . $l->getFecha() . "')";
  66. $result = mysql_query($query, $conexion);
  67. if ($tematicas != null && count($tematicas) > 0) {
  68. foreach ($tematicas as $tem) {
  69. $tematica = new Tematica();
  70. $tematica = $tem;
  71. $query2 = "insert into t_libros_tematica (isbn, id_tematica)
  72. values ('" . $l->getIsbn() . "', '" . $tematica->getId() . "')";
  73. $result2 = mysql_query($query2, $conexion);
  74. }
  75. }
  76. foreach ($autores as $aut) {
  77. $autor = new Autor();
  78. $autor = $aut;
  79. $query3 = "insert into t_libros_autores (isbn, id_autor)
  80. values ('" . $l->getIsbn() . "', '" . $autor->getId() . "')";
  81. $result3 = mysql_query($query3, $conexion);
  82. }
  83. $_SESSION["insertada"] = "El libro se ha insertado con éxito.";
  84. } catch (Exception $e) {
  85. $_SESSION["error"] = "No se ha podido crear el libro. Inténtelo de nuevo más tarde.";
  86. }
  87. }
  88. public static function agregarLibro($idUsuario, $isbn) {
  89. global $conexion;
  90. try {
  91. $query = "insert into t_usuario_libros
  92. (id_usuario, isbn)
  93. values ('" . $idUsuario . "',
  94. '" . $isbn . "')";
  95. $result = mysql_query($query, $conexion);
  96. } catch (Exception $e) {
  97. $_SESSION["error"] = "No se ha podido agregar el libro a su colección. Inténtelo de nuevo más tarde.";
  98. }
  99. }
  100. public static function modificarLibro($libro) {
  101. //TODO terminar!!
  102. global $conexion;
  103. $l = new Libro();
  104. $l = $libro;
  105. $e = new Editorial();
  106. $e = $l->getEditorial();
  107. $t = new TipoEdicion();
  108. $t = $l->getTipoEdicion();
  109. $u = new Usuario();
  110. $u = $l->getUsuario();
  111. $tematicas = array();
  112. $tematicas = $l->getTematicas();
  113. $autores = array();
  114. $autores = $l->getAutores();
  115. $portada = "";
  116. if ($l->getPortada() == "") {
  117. } else {
  118. $portada = " portada = '" . $l->getPortada() . "',";
  119. }
  120. $subtitulo = "";
  121. if (trim($l->getSubtitulo()) == "") {
  122. } else {
  123. $subtitulo = " subtitulo = '" . $l->getSubtitulo() . "',";
  124. }
  125. $paginas = "";
  126. if (trim($l->getPaginas()) == "") {
  127. } else {
  128. $paginas = " paginas = '" . $l->getPaginas() . "',";
  129. }
  130. $sinopsis = "";
  131. if (trim($l->getSinopsis()) == "" || $l->getSinopsis() == '<p><br></p>') {
  132. } else {
  133. $sinopsis = " sinopsis = '" . mysql_escape_string($l->getSinopsis()) . "',";
  134. }
  135. $anyo = "";
  136. if (trim($l->getAnyoPublicacion()) == "") {
  137. } else {
  138. $anyo = " anyo_publicacion = '" . $l->getAnyoPublicacion() . "',";
  139. }
  140. $edicion = "";
  141. if (trim($l->getEdicion()) == "") {
  142. } else {
  143. $edicion = " edicion = '" . $l->getEdicion() . "',";
  144. }
  145. $editorial = "";
  146. if ($l->getEditorial() == null) {
  147. } else {
  148. $editorial = " id_editorial = '" . $e->getId() . "',";
  149. }
  150. $tipoEditorial = "";
  151. if ($l->getTipoEdicion() == null) {
  152. } else {
  153. $tipoEditorial = " id_tipo_edicion = '" . $t->getId() . "',";
  154. }
  155. try {
  156. $query = "update t_libros
  157. set titulo = '" . $l->getTitulo() . "',
  158. $subtitulo
  159. $paginas
  160. $portada
  161. $sinopsis
  162. $anyo
  163. $edicion
  164. $editorial
  165. $tipoEditorial
  166. id_usuario = '" . $u->getId() . "'
  167. where isbn like '" . $l->getIsbn() . "'";
  168. $result = mysql_query($query, $conexion);
  169. if (!empty($tematicas)) {
  170. $query2 = "delete from t_libros_tematica where isbn like '" . $l->getIsbn() . "'";
  171. $result2 = mysql_query($query2, $conexion);
  172. foreach ($tematicas as $tem) {
  173. $tematica = new Tematica();
  174. $tematica = $tem;
  175. $query3 = "insert into t_libros_tematica (isbn, id_tematica)
  176. values ('" . $l->getIsbn() . "', '" . $tematica->getId() . "')";
  177. $result3 = mysql_query($query3, $conexion);
  178. }
  179. }
  180. $query4 = "delete from t_libros_autores where isbn like '" . $l->getIsbn() . "'";
  181. $result4 = mysql_query($query4, $conexion);
  182. foreach ($autores as $aut) {
  183. $autor = new Autor();
  184. $autor = $aut;
  185. $query5 = "insert into t_libros_autores (isbn, id_autor)
  186. values ('" . $l->getIsbn() . "', '" . $autor->getId() . "')";
  187. $result5 = mysql_query($query5, $conexion);
  188. }
  189. return mysql_affected_rows();
  190. } catch (Exception $e) {
  191. $_SESSION["error"] = "No se ha podido modificar el libro. Inténtelo de nuevo más tarde.";
  192. }
  193. }
  194. public static function quitarLibro($idUsuario, $isbn) {
  195. global $conexion;
  196. try {
  197. $query = "delete from t_usuario_libros
  198. where id_usuario = '" . $idUsuario . "'
  199. and isbn = '" . $isbn . "'";
  200. $result = mysql_query($query, $conexion);
  201. } catch (Exception $e) {
  202. $_SESSION["error"] = "No se ha podido eliminar el libro de su colección. Inténtelo de nuevo más tarde.";
  203. }
  204. }
  205. public static function eliminarLibro($isbn) {
  206. global $conexion;
  207. try {
  208. $query0 = "update t_libros set id_editorial = null, id_tipo_edicion = null, id_usuario = null where isbn = '" . $isbn . "'";
  209. $query1 = "delete from t_comentarios where isbn = '" . $isbn . "'";
  210. $query2 = "delete from t_denuncias where isbn = '" . $isbn . "'";
  211. $query3 = "delete from t_libros_autores where isbn = '" . $isbn . "'";
  212. $query4 = "delete from t_libros_tematica where isbn = '" . $isbn . "'";
  213. $query5 = "delete from t_usuario_libros where isbn = '" . $isbn . "'";
  214. $query6 = "delete from t_libros where isbn = '" . $isbn . "'";
  215. $result = mysql_query($query0, $conexion);
  216. $result = mysql_query($query1, $conexion);
  217. $result = mysql_query($query2, $conexion);
  218. $result = mysql_query($query3, $conexion);
  219. $result = mysql_query($query4, $conexion);
  220. $result = mysql_query($query5, $conexion);
  221. $result = mysql_query($query6, $conexion);
  222. return mysql_affected_rows();
  223. } catch (Exception $e) {
  224. $_SESSION["error"] = "No se ha podido eliminar el libro. Inténtelo de nuevo más tarde.";
  225. }
  226. }
  227. public static function recuperarLibro($isbn, $id_usuario = 0) {
  228. global $conexion;
  229. $libro = new Libro();
  230. try {
  231. $query = "select * from t_libros where isbn like '$isbn'";
  232. $result = mysql_query($query, $conexion);
  233. $row = mysql_fetch_array($result);
  234. if (!$row) {
  235. $_SESSION["noHay"] = "No existe ningún libro con ese ISBN en BookWorm.";
  236. return 0;
  237. } else {
  238. $libro->setIsbn($isbn);
  239. $libro->setTitulo($row['titulo']);
  240. $libro->setSubtitulo($row['subtitulo']);
  241. $libro->setPaginas($row['paginas']);
  242. $libro->setPortada($row['portada']);
  243. $libro->setSinopsis($row['sinopsis']);
  244. $libro->setAnyoPublicacion($row['anyo_publicacion']);
  245. $libro->setEdicion($row['edicion']);
  246. $libro->setFecha($row['fecha']);
  247. if ($row['id_editorial'] != null) {
  248. $editorial = new Editorial();
  249. $editorial = GestionEditoriales::recuperarEditorial($row['id_editorial']);
  250. $libro->setEditorial($editorial);
  251. }
  252. if ($row['id_tipo_edicion'] != null) {
  253. $tipo_edicion = new TipoEdicion();
  254. $tipo_edicion = GestionTipoEdicion::recuperarTipoEdicion($row['id_tipo_edicion']);
  255. $libro->setTipoEdicion($tipo_edicion);
  256. }
  257. $usuario = new Usuario();
  258. $usuario = GestionUsuarios::recuperarUsuario($row['id_usuario']);
  259. $libro->setUsuario($usuario);
  260. if ($id_usuario != 0) {
  261. $query2 = "select * from t_comentarios where isbn like '$isbn' and id_usuario = $id_usuario";
  262. $result2 = mysql_query($query2, $conexion);
  263. $row2 = mysql_fetch_array($result2);
  264. if ($row2) {
  265. $libro->setValoracionUsuario($row2['valoracion']);
  266. }
  267. }
  268. $query3 = "select avg(valoracion) as media from t_comentarios where isbn like '$isbn'";
  269. $result3 = mysql_query($query3, $conexion);
  270. $row3 = mysql_fetch_array($result3);
  271. if ($row3) {
  272. $libro->setValoracionMedia($row3['media']);
  273. }
  274. $tematicas = array();
  275. $query4 = "select t.*
  276. from t_tematica as t inner join t_libros_tematica as lt
  277. on t.id_tematica = lt.id_tematica
  278. where isbn like '$isbn'";
  279. $result4 = mysql_query($query4, $conexion);
  280. while($row4 = mysql_fetch_array($result4)) {
  281. $tematica = new Tematica();
  282. $tematica->setId($row4["id_tematica"]);
  283. $tematica->setDescripcion($row4["descripcion_tematica"]);
  284. $tematicas[] = $tematica;
  285. }
  286. $libro->setTematicas($tematicas);
  287. $autores = array();
  288. $query5 = "select a.*
  289. from t_autores as a inner join t_libros_autores as la
  290. on a.id_autor = la.id_autor
  291. where isbn like '$isbn'";
  292. $result5 = mysql_query($query5, $conexion);
  293. while($row5 = mysql_fetch_array($result5)) {
  294. $autor = new Autor();
  295. $autor->setId($row5["id_autor"]);
  296. $autor->setNombre($row5["nombre_autor"]);
  297. $autores[] = $autor;
  298. }
  299. $libro->setAutores($autores);
  300. return $libro;
  301. }
  302. } catch (Exception $e) {
  303. $_SESSION["error"] = "No se ha podido recuperar el libro. Inténtelo de nuevo más tarde.";
  304. }
  305. }
  306. public static function recuperarLibros() {
  307. global $conexion;
  308. try {
  309. $query = "select * from t_libros";
  310. $result = mysql_query($query, $conexion);
  311. $libros = array();
  312. while($row = mysql_fetch_array($result)) {
  313. $libro = new Libro();
  314. $libro = GestionLibros::recuperarLibro($row['isbn']);
  315. $libros[] = $libro;
  316. }
  317. if ($libros != null && count($libros) > 0) {
  318. } else {
  319. $_SESSION["noHayL"] = "No existe ningún libro en BookWorm.";
  320. }
  321. return $libros;
  322. } catch (Exception $e) {
  323. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  324. }
  325. }
  326. public static function recuperarLibrosL($begin, $limit, $field="", $order="", $where="") {
  327. global $conexion;
  328. if ($begin != "" && $limit != "") {
  329. $l = " limit $begin, $limit ";
  330. }
  331. if ($field != "" && $order != "") {
  332. $orderBy = " order by $field $order ";
  333. }
  334. if ($where != "") {
  335. $w = " where l.isbn like '%$where%'
  336. or l.titulo like '%$where%'
  337. or a.nombre_autor like '%$where%'
  338. or u.nombre_usuario like '%$where%' ";
  339. }
  340. try {
  341. $query = "select distinct l.isbn
  342. from t_libros as l inner join t_usuarios as u
  343. on l.id_usuario = u.id_usuario
  344. inner join t_libros_autores as la
  345. on l.isbn = la.isbn
  346. inner join t_autores as a
  347. on la.id_autor = a.id_autor ";
  348. if (isset($w)) {
  349. $query = $query . $w;
  350. }
  351. if (isset($orderBy)) {
  352. $query = $query . $orderBy;
  353. }
  354. if (isset($l)) {
  355. $query = $query . $l;
  356. }
  357. $result = mysql_query($query, $conexion);
  358. $libros = array();
  359. while($row = mysql_fetch_array($result)) {
  360. $libro = new Libro();
  361. $libro = GestionLibros::recuperarLibro($row['isbn']);
  362. $libros[] = $libro;
  363. }
  364. return $libros;
  365. } catch (Exception $e) {
  366. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  367. }
  368. }
  369. public static function recuperarTop($cuantos) {
  370. global $conexion;
  371. try {
  372. $query = "select l.*, avg(valoracion) as media
  373. from t_libros as l inner join t_comentarios as c
  374. on l.isbn = c.isbn
  375. where valoracion is not null
  376. group by l.isbn
  377. order by media desc
  378. limit 0, $cuantos";
  379. $result = mysql_query($query, $conexion);
  380. $libros = array();
  381. while($row = mysql_fetch_array($result)) {
  382. $libro = new Libro();
  383. $libro = GestionLibros::recuperarLibro($row['isbn']);
  384. $libros[] = $libro;
  385. }
  386. return $libros;
  387. } catch (Exception $e) {
  388. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  389. }
  390. }
  391. public static function recuperarUltimos($cuantos) {
  392. global $conexion;
  393. try {
  394. $query = "select *
  395. from t_libros
  396. order by fecha desc
  397. limit 0, $cuantos";
  398. $result = mysql_query($query, $conexion);
  399. $libros = array();
  400. while($row = mysql_fetch_array($result)) {
  401. $libro = new Libro();
  402. $libro = GestionLibros::recuperarLibro($row['isbn']);
  403. $libros[] = $libro;
  404. }
  405. return $libros;
  406. } catch (Exception $e) {
  407. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  408. }
  409. }
  410. public static function recuperarLibrosPorUsuario($begin, $limit, $field="", $order="", $where="", $id = 0) {
  411. global $conexion;
  412. if (intval($id) === 0) {
  413. $u = new Usuario();
  414. $u = unserialize($_SESSION["usuario"]);
  415. $id = $u->getId();
  416. }
  417. if ($begin != "" && $limit != "") {
  418. $l = " limit $begin, $limit ";
  419. }
  420. if ($field != "" && $order != "") {
  421. $orderBy = " order by $field $order ";
  422. }
  423. if ($where != "") {
  424. $w = " and (l.isbn like '%$where%'
  425. or l.titulo like '%$where%'
  426. or a.nombre_autor like '%$where%') ";
  427. }
  428. try {
  429. $query = "select distinct l.isbn
  430. from t_libros as l inner join t_usuario_libros as ul
  431. on l.isbn = ul.isbn
  432. inner join t_libros_autores as la
  433. on l.isbn = la.isbn
  434. inner join t_autores as a
  435. on la.id_autor = a.id_autor
  436. where ul.id_usuario = $id";
  437. if (isset($w)) {
  438. $query = $query . $w;
  439. }
  440. if (isset($orderBy)) {
  441. $query = $query . $orderBy;
  442. }
  443. if (isset($l)) {
  444. $query = $query . $l;
  445. }
  446. $result = mysql_query($query, $conexion);
  447. $libros = array();
  448. while($row = mysql_fetch_array($result)) {
  449. $libro = new Libro();
  450. $libro = GestionLibros::recuperarLibro($row['isbn'], $id);
  451. $libros[] = $libro;
  452. }
  453. return $libros;
  454. } catch (Exception $e) {
  455. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  456. }
  457. }
  458. public static function recuperarIsbnPorUsuario($id = 0) {
  459. global $conexion;
  460. if (intval($id) === 0) {
  461. $u = new Usuario();
  462. $u = unserialize($_SESSION["usuario"]);
  463. $id = $u->getId();
  464. }
  465. try {
  466. $query = "select distinct l.isbn
  467. from t_libros as l inner join t_usuario_libros as ul
  468. on l.isbn = ul.isbn
  469. inner join t_libros_autores as la
  470. on l.isbn = la.isbn
  471. inner join t_autores as a
  472. on la.id_autor = a.id_autor
  473. where ul.id_usuario = $id";
  474. $result = mysql_query($query, $conexion);
  475. $isbn = array();
  476. while($row = mysql_fetch_array($result)) {
  477. $isbn[] = $row['isbn'];
  478. }
  479. if (count($isbn) == 0) {
  480. $_SESSION["noHayE"] = "Todavía no has añadido ningún libro a tu estantería.";
  481. }
  482. return $isbn;
  483. } catch (Exception $e) {
  484. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  485. }
  486. }
  487. public static function agregarValoracion($idUsuario, $isbn, $valoracion) {
  488. global $conexion;
  489. try {
  490. $query = "select *
  491. from t_comentarios
  492. where id_usuario = $idUsuario and isbn like '$isbn'";
  493. $result = mysql_query($query, $conexion);
  494. $row = mysql_fetch_array($result);
  495. if(!$row) {
  496. $query2 = "insert into t_comentarios
  497. (id_usuario, isbn, valoracion)
  498. values
  499. ($idUsuario, '$isbn', $valoracion)";
  500. } else {
  501. $query2 = "update t_comentarios
  502. set valoracion = $valoracion
  503. where id_usuario = $idUsuario and isbn like '$isbn'";
  504. }
  505. $result2 = mysql_query($query2, $conexion);
  506. $_SESSION["ok"] = "La valoración se ha añadido correctamente.";
  507. } catch (Exception $e) {
  508. $_SESSION["error"] = "No se ha podido valorar el libro. Inténtelo de nuevo más tarde.";
  509. }
  510. }
  511. public static function agregarComentario($comentario) {
  512. global $conexion;
  513. $c = new Comentario();
  514. $c = $comentario;
  515. $u = new Usuario();
  516. $u = $c->getUsuario();
  517. $l = new Libro();
  518. $l = $c->getLibro();
  519. try {
  520. $query = "select *
  521. from t_comentarios
  522. where id_usuario = " . $u->getId() . " and isbn like '" . $l->getIsbn() . "'";
  523. $result = mysql_query($query, $conexion);
  524. $row = mysql_fetch_array($result);
  525. if(!$row) {
  526. $query2 = "insert into t_comentarios
  527. (id_usuario, isbn, fecha, titulo, comentario, spoiler)
  528. values
  529. ('" . $u->getId() . "',
  530. '" . $l->getIsbn() . "',
  531. '" . $c->getFecha() . "',
  532. '" . $c->getTitulo() . "',
  533. '" . mysql_escape_string($c->getComentario()) . "',
  534. '" . $c->getSpoiler() . "')";
  535. } else {
  536. $query2 = "update t_comentarios
  537. set fecha = '" . $c->getFecha() . "',
  538. titulo = '" . $c->getTitulo() . "',
  539. comentario = '" . mysql_escape_string($c->getComentario()) . "',
  540. spoiler = '" . $c->getSpoiler() . "'
  541. where id_usuario = '" . $u->getId() . "' and isbn like '" . $l->getIsbn() . "'";
  542. }
  543. $result2 = mysql_query($query2, $conexion);
  544. $_SESSION["ok"] = "El comentario se ha añadido correctamente.";
  545. } catch (Exception $e) {
  546. $_SESSION["error"] = "No se ha podido añadir el comentario el libro. Inténtelo de nuevo más tarde.";
  547. }
  548. }
  549. public static function recuperarComentarios($isbn) {
  550. global $conexion;
  551. $comentarios = array();
  552. try {
  553. $query = "select * from t_comentarios
  554. where isbn like '$isbn' and (comentario is not null or comentario != '')
  555. order by fecha desc";
  556. $result = mysql_query($query, $conexion);
  557. while($row = mysql_fetch_array($result)) {
  558. $c = new Comentario();
  559. $c->setComentario($row["comentario"]);
  560. $c->setFecha($row["fecha"]);
  561. $c->setSpoiler($row["spoiler"]);
  562. $c->setTitulo($row["titulo"]);
  563. $u = new Usuario();
  564. $u = GestionUsuarios::recuperarUsuario($row["id_usuario"]);
  565. $l = new Libro();
  566. $l = GestionLibros::recuperarLibro($row["isbn"]);
  567. $c->setUsuario($u);
  568. $c->setLibro($l);
  569. $comentarios[] = $c;
  570. }
  571. if (count($comentarios) == 0) {
  572. $_SESSION["noHayC"] = "Todavía no se han añadido comentarios para este libro.";
  573. }
  574. return $comentarios;
  575. } catch (Exception $e) {
  576. $_SESSION["error"] = "No se ha podido añadir el comentario el libro. Inténtelo de nuevo más tarde.";
  577. }
  578. }
  579. public static function tieneComentario($isbn) {
  580. global $conexion;
  581. $u = new Usuario();
  582. $u = unserialize($_SESSION["usuario"]);
  583. try {
  584. $query = "SELECT *
  585. FROM t_comentarios
  586. where id_usuario = '" . $u->getId() . "' and isbn like '$isbn'
  587. and (comentario is not null or comentario != '')";
  588. $result = mysql_query($query, $conexion);
  589. $row = mysql_fetch_array($result);
  590. if (!$row) {
  591. return 0;
  592. } else {
  593. return 1;
  594. }
  595. } catch (Exception $e) {
  596. $_SESSION["error"] = "No se ha podido recuperar el comentario. Inténtelo de nuevo más tarde.";
  597. }
  598. }
  599. public static function totalLibros() {
  600. global $conexion;
  601. try {
  602. $query = "select count(*) as total
  603. from t_libros";
  604. $result = mysql_query($query, $conexion);
  605. $row = mysql_fetch_array($result);
  606. $total = $row["total"];
  607. return $total;
  608. } catch (Exception $e) {
  609. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  610. }
  611. }
  612. public static function totalLibrosPorUsuario($id = 0) {
  613. global $conexion;
  614. if (intval($id) === 0) {
  615. $u = new Usuario();
  616. $u = unserialize($_SESSION["usuario"]);
  617. $id = $u->getId();
  618. }
  619. try {
  620. $query = "select count(*) as total
  621. from t_usuario_libros where id_usuario = $id";
  622. $result = mysql_query($query, $conexion);
  623. $row = mysql_fetch_array($result);
  624. $total = $row["total"];
  625. return $total;
  626. } catch (Exception $e) {
  627. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  628. }
  629. }
  630. public static function totalFiltered($where) {
  631. global $conexion;
  632. if ($where != "") {
  633. $w = " where l.isbn like '%$where%'
  634. or l.titulo like '%$where%'
  635. or a.nombre_autor like '%$where%'
  636. or u.nombre_usuario like '%$where%' ";
  637. }
  638. try {
  639. $query = "select count(distinct l.isbn) as total
  640. from t_libros as l inner join t_usuarios as u
  641. on l.id_usuario = u.id_usuario
  642. inner join t_libros_autores as la
  643. on l.isbn = la.isbn
  644. inner join t_autores as a
  645. on la.id_autor = a.id_autor
  646. $w";
  647. $result = mysql_query($query, $conexion);
  648. $row = mysql_fetch_array($result);
  649. $total = $row["total"];
  650. return $total;
  651. } catch (Exception $e) {
  652. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  653. }
  654. }
  655. public static function totalFilteredPorUsuario($where, $id = 0) {
  656. global $conexion;
  657. if (intval($id) === 0) {
  658. $u = new Usuario();
  659. $u = unserialize($_SESSION["usuario"]);
  660. $id = $u->getId();
  661. }
  662. if ($where != "") {
  663. $w = " and (l.isbn like '%$where%'
  664. or l.titulo like '%$where%'
  665. or a.nombre_autor like '%$where%') ";
  666. }
  667. try {
  668. $query = "select count(distinct l.isbn) as total
  669. from t_libros as l inner join t_usuario_libros as ul
  670. on l.isbn = ul.isbn
  671. inner join t_libros_autores as la
  672. on l.isbn = la.isbn
  673. inner join t_autores as a
  674. on la.id_autor = a.id_autor
  675. where ul.id_usuario = $id $w";
  676. $result = mysql_query($query, $conexion);
  677. $row = mysql_fetch_array($result);
  678. $total = $row["total"];
  679. return $total;
  680. } catch (Exception $e) {
  681. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  682. }
  683. }
  684. public static function existeLibro($isbn) {
  685. global $conexion;
  686. try {
  687. $query = "SELECT *
  688. FROM t_libros
  689. where isbn like '$isbn'";
  690. $result = mysql_query($query, $conexion);
  691. $row = mysql_fetch_array($result);
  692. if (!$row) {
  693. return false;
  694. } else {
  695. return true;
  696. }
  697. } catch (Exception $e) {
  698. $_SESSION["error"] = "No se ha podido recuperar el libro. Inténtelo de nuevo más tarde.";
  699. }
  700. }
  701. public static function existe($campo, $tabla, $where) {
  702. global $conexion;
  703. try {
  704. $query = "SELECT $campo FROM $tabla $where";
  705. $result = mysql_query($query, $conexion);
  706. $row = mysql_fetch_array($result);
  707. if (!$row) {
  708. return 0;
  709. } else {
  710. return $row["$campo"];
  711. }
  712. } catch (Exception $e) {
  713. $_SESSION["error"] = "No se ha podido recuperar el libro. Inténtelo de nuevo más tarde.";
  714. }
  715. }
  716. }
  717. ?>