PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/utilidades/GestionCitas.php

https://bitbucket.org/veroreinah/bookworm
PHP | 244 lines | 198 code | 37 blank | 9 comment | 31 complexity | 0cb0ae18b8a4876a8ab828a9d48a733a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'conexion.php';
  3. require_once 'clases/Cita.php';
  4. require_once 'clases/Autor.php';
  5. /*
  6. * To change this template, choose Tools | Templates
  7. * and open the template in the editor.
  8. */
  9. /**
  10. * Description of GestionCitas
  11. *
  12. * @author Vero
  13. */
  14. class GestionCitas {
  15. public static function crearCita($cita) {
  16. global $conexion;
  17. $c = new Cita();
  18. $c = $cita;
  19. $autor = new Autor();
  20. $autor = $c->getAutor();
  21. try {
  22. $query = "insert into t_citas
  23. (texto, id_autor)
  24. values ('" . $c->getTexto() . "',
  25. '" . $autor->getId() . "')";
  26. $result = mysql_query($query, $conexion);
  27. $_SESSION["insertada"] = "La cita se ha insertado con éxito.";
  28. } catch (Exception $e) {
  29. $_SESSION["error"] = "No se ha podido crear la nueva cita. Inténtelo de nuevo más tarde.";
  30. }
  31. }
  32. public static function modificarCita($cita) {
  33. global $conexion;
  34. $c = new Cita();
  35. $c = $cita;
  36. $autor = new Autor();
  37. $autor = $c->getAutor();
  38. try {
  39. $query = "update t_citas
  40. set texto = '" . $c->getTexto() . "',
  41. id_autor = '" . $autor->getId() . "'
  42. where id_cita = '" . $c->getId() . "'";
  43. $result = mysql_query($query, $conexion);
  44. return mysql_affected_rows();
  45. } catch (Exception $e) {
  46. $_SESSION["error"] = "No se ha podido modificar la cita. Inténtelo de nuevo más tarde.";
  47. }
  48. }
  49. public static function eliminarCita($id) {
  50. global $conexion;
  51. try {
  52. $query = "delete from t_citas
  53. where id_cita = '" . $id . "'";
  54. $result = mysql_query($query, $conexion);
  55. return mysql_affected_rows();
  56. } catch (Exception $e) {
  57. $_SESSION["error"] = "No se ha podido eliminar la cita. Inténtelo de nuevo más tarde.";
  58. }
  59. }
  60. public static function totalCitas() {
  61. global $conexion;
  62. try {
  63. $query = "select count(*) as total
  64. from t_citas";
  65. $result = mysql_query($query, $conexion);
  66. $row = mysql_fetch_array($result);
  67. $total = $row["total"];
  68. return $total;
  69. } catch (Exception $e) {
  70. $_SESSION["error"] = "No se han podido recuperar las citas. Inténtelo de nuevo más tarde.";
  71. }
  72. }
  73. public static function totalFiltered($where) {
  74. global $conexion;
  75. if ($where != "") {
  76. $w = " where texto like '%$where%' or nombre_autor like '%$where%' ";
  77. }
  78. try {
  79. $query = "select count(id_cita) as total
  80. from t_citas inner join t_autores
  81. on t_citas.id_autor = t_autores.id_autor $w";
  82. $result = mysql_query($query, $conexion);
  83. $row = mysql_fetch_array($result);
  84. $total = $row["total"];
  85. return $total;
  86. } catch (Exception $e) {
  87. $_SESSION["error"] = "No se han podido recuperar las citas. Inténtelo de nuevo más tarde.";
  88. }
  89. }
  90. public static function recuperarCitasL($begin, $limit, $field="", $order="", $where="") {
  91. global $conexion;
  92. $citas = array();
  93. if ($begin != "" && $limit != "") {
  94. $l = " limit $begin, $limit ";
  95. }
  96. if ($field != "" && $order != "") {
  97. $orderBy = " order by $field $order ";
  98. }
  99. if ($where != "") {
  100. $w = " where texto like '%$where%' or nombre_autor like '%$where%' ";
  101. }
  102. try {
  103. $query = "select id_cita, texto, t_citas.id_autor, nombre_autor
  104. from t_citas inner join t_autores
  105. on t_citas.id_autor = t_autores.id_autor";
  106. if (isset($w)) {
  107. $query = $query . $w;
  108. }
  109. if (isset($orderBy)) {
  110. $query = $query . $orderBy;
  111. }
  112. if (isset($l)) {
  113. $query = $query . $l;
  114. }
  115. $result = mysql_query($query, $conexion);
  116. while($row = mysql_fetch_array($result)) {
  117. $cita = new Cita();
  118. $cita->setId($row["id_cita"]);
  119. $cita->setTexto($row["texto"]);
  120. $autor = new Autor();
  121. $autor->setId($row["id_autor"]);
  122. $autor->setNombre($row["nombre_autor"]);
  123. $cita->setAutor($autor);
  124. $citas[] = $cita;
  125. }
  126. return $citas;
  127. } catch (Exception $e) {
  128. $_SESSION["error"] = "No se han podido recuperar las citas. Inténtelo de nuevo más tarde.";
  129. }
  130. }
  131. public static function recuperarCitas() {
  132. global $conexion;
  133. $citas = array();
  134. try {
  135. $query = "select id_cita, texto, t_citas.id_autor, nombre_autor
  136. from t_citas inner join t_autores
  137. on t_citas.id_autor = t_autores.id_autor
  138. order by id_cita";
  139. $result = mysql_query($query, $conexion);
  140. while($row = mysql_fetch_array($result)) {
  141. $cita = new Cita();
  142. $cita->setId($row["id_cita"]);
  143. $cita->setTexto($row["texto"]);
  144. $autor = new Autor();
  145. $autor->setId($row["id_autor"]);
  146. $autor->setNombre($row["nombre_autor"]);
  147. $cita->setAutor($autor);
  148. $citas[] = $cita;
  149. }
  150. if (count($citas) > 0) {
  151. return $citas;
  152. } else {
  153. $_SESSION["noHay"] = "No existe ninguna cita en BookWorm.";
  154. return 0;
  155. }
  156. } catch (Exception $e) {
  157. $_SESSION["error"] = "No se han podido recuperar las citas. Inténtelo de nuevo más tarde.";
  158. }
  159. }
  160. public static function generarAleatorios($cuantos) {
  161. $total = GestionCitas::totalCitas();
  162. $aleatorios = array();
  163. if ($cuantos >= $total) {
  164. for ($i = 0; $i < $cuantos; $i++) {
  165. $aleatorios[] = $i+1;
  166. }
  167. } else {
  168. for ($i = 0; $i < $cuantos; $i++) {
  169. do {
  170. $existe = false;
  171. $numero = mt_rand(1, $total);
  172. foreach ($aleatorios as $n) {
  173. if ($n == $numero) {
  174. $existe = true;
  175. }
  176. }
  177. } while ($existe);
  178. $aleatorios[] = $numero;
  179. }
  180. }
  181. return $aleatorios;
  182. }
  183. public static function generarImagenes($cuantos) {
  184. $total = 12;
  185. $imagenes = array();
  186. if ($cuantos >= $total) {
  187. for ($i = 0; $i < $cuantos; $i++) {
  188. $imagenes[] = $i+1;
  189. }
  190. } else {
  191. for ($i = 0; $i < $cuantos; $i++) {
  192. do {
  193. $existe = false;
  194. $numero = mt_rand(1, $total);
  195. foreach ($imagenes as $n) {
  196. if ($n == $numero) {
  197. $existe = true;
  198. }
  199. }
  200. } while ($existe);
  201. $imagenes[] = $numero;
  202. }
  203. }
  204. return $imagenes;
  205. }
  206. }
  207. ?>