calendariodia_update.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /* AJAX
  3. * Inserta datos del objeto para el calendario
  4. * Recibe:
  5. * json
  6. * Return:
  7. * id insertado o cadena de error
  8. */
  9. require_once("../../include/constantes.php");
  10. require_once("../../include/nocache.php");
  11. require_once("../../include/bd_pdo.php");
  12. require_once("../../include/util.php");
  13. require_once("../../classes/ValidaSesion.php");
  14. require_once("../classes/LogActividad.php");//die on error
  15. require_once("../classes/Evento.php");
  16. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  17. $objSesion = new ValidaSesion($pdo, 57, APSA);
  18. if(!$objSesion->tieneAcceso() || !$objSesion->puedeEditar()){
  19. $return["error"] = "Error! No tienes permisos para realizar esta acción.";
  20. }else if(!isset($_POST["json"])){
  21. $return["error"] = "Error! No se recibió la información del evento.";
  22. }else{
  23. $clase = json_decode($_POST["json"], true);
  24. $return["error"] = "";
  25. try {
  26. $pdo->beginTransaction();
  27. $error = false;
  28. //Revisa cambio de día
  29. if(true){
  30. //--Obtiene todos los eventos dentro del rango del periodo para los perfiles indicados
  31. $stmt = $pdo->prepare('Select * from fs_calendarioevento_reglas(:id)');
  32. $stmt->bindParam(":id", $clase["id_db"]);
  33. if(!$stmt->execute()){
  34. $t = $stmt->errorInfo();
  35. $return["error"] .= "Ocurrió un error al obtener los eventos.".$t[2];
  36. $error = true;
  37. }
  38. $unico = false;
  39. if(!$error){
  40. $evento = $stmt->fetch();
  41. $stmt->closeCursor();
  42. //Recorre eventos
  43. $eventoObj = new Evento($evento["CalendarioEvento_id"], $evento["CalendarioEvento_titulo"],$evento["CalendarioEvento_desc"],$evento["CalendarioCategoria_id"], $evento["CalendarioCategoria_desc"], $evento["CalendarioCategoria_color"], $evento["Periodo_id"] ,$evento["CalendarioEvento_fecha"], $evento["CalendarioEvento_todo_dia"], $evento["CalendarioEvento_hora_inicial"],$evento["CalendarioEvento_hora_final"]);
  44. //-- Calcular posibles fechas e insertar fechas en objeto --
  45. $fecha = fechaGuion($evento["CalendarioEvento_fecha"]);
  46. switch($evento["CalendarioRepeticion_id"]){
  47. case 1: //diario
  48. while (strtotime($fecha) <= strtotime($evento["CalendarioReglas_fecha_final"])) {
  49. $eventoObj->addFecha($fecha);
  50. $fecha = date ("Y-m-d", strtotime("+1 day", strtotime($fecha)));
  51. }
  52. break;
  53. case 2: //semanal
  54. $diasArr = explode(",", $evento["CalendarioReglas_dias_str"]);
  55. while (strtotime($fecha) <= strtotime($evento["CalendarioReglas_fecha_final"])) {
  56. if(in_array(date("w", strtotime($fecha)), $diasArr) ){//si es el día que quiero
  57. $eventoObj->addFecha($fecha);
  58. }
  59. $fecha = date ("Y-m-d", strtotime("+1 day", strtotime($fecha)));
  60. }
  61. break;
  62. case 3://mensual
  63. $diasArr = explode(",", $evento["CalendarioReglas_dias_str"]);
  64. //reglas
  65. $weekTxt = array(1=>"first", 2=>"second", 3=>"third", 4=>"fourth", -1=>"last");
  66. $dayname = array("sun", "mon", "tue", "wed", "thu", "fri", "sat");
  67. $fecha = date ("Y-m-01", strtotime($fecha));//empieza a revisar en el primer día del mes
  68. $semana = $evento["CalendarioReglas_semana"];
  69. while (strtotime($fecha) <= strtotime($evento["CalendarioReglas_fecha_final"])) {
  70. foreach($diasArr as $d){
  71. //echo "-->". intval(date("w", strtotime($fecha)))." == ". intval($d)."[".$fecha."]";
  72. if($semana == 1 && intval(date("w", strtotime($fecha))) == intval($d) ){//si el día actual es el que quiero lo guarda
  73. $eventoObj->addFecha($fecha);
  74. }else{//si no calcula siguiente día
  75. if(intval(date("w", strtotime($fecha))) == intval($d)){
  76. $fechaTmp = date ("Y-m-d", strtotime($weekTxt[$semana-1]." ".$dayname[$d], strtotime($fecha)));
  77. }else{
  78. $fechaTmp = date ("Y-m-d", strtotime($weekTxt[$semana]." ".$dayname[$d], strtotime($fecha)));
  79. }
  80. $eventoObj->addFecha($fechaTmp);
  81. }
  82. }
  83. $fecha = date ("Y-m-01", strtotime("+1 month", strtotime($fecha)));//siguiente mes primer día
  84. }
  85. break;
  86. default: //no se repite
  87. $unico = true;
  88. $eventoObj->addFecha($fecha);
  89. }
  90. if($unico){
  91. if($clase["todo_dia"]){
  92. $stmt = $pdo->prepare('Select * from fu_calendarioevento_fecha(:id, :fecha, NULL, NULL)');
  93. }else{
  94. $stmt = $pdo->prepare('Select * from fu_calendarioevento_fecha(:id, :fecha, :hora_i, :hora_f)');
  95. $stmt->bindParam(":hora_i", $clase["hora_ini"]);
  96. $stmt->bindParam(":hora_f", $clase["hora_fin"]);
  97. }
  98. $stmt->bindParam(":id", $eventoObj->id_db);
  99. $stmt->bindParam(":fecha", $clase["fecha_nueva"]);
  100. if(!$stmt->execute()){
  101. $t = $stmt->errorInfo();
  102. $return["error"] .= "Ocurrió un error al editar la fecha.".$t[2];
  103. $error = true;
  104. }
  105. }else{
  106. //Obtiene eventos modificados (fechas que cambiaron)
  107. $stmt = $pdo->prepare('Select * from fs_calendarioeventoeditado(:id)');
  108. $stmt->bindParam(":id", $eventoObj->id_db);
  109. if(!$stmt->execute()){
  110. $t = $stmt->errorInfo();
  111. $return["error"] .= "Ocurrió un error al obtener las fechas editadas.".$t[2];
  112. $error = true;
  113. }
  114. if(!$error){
  115. //modifica fechas
  116. $eventosEditados_rs = $stmt->fetchAll();
  117. $stmt->closeCursor();
  118. foreach($eventosEditados_rs as $modif){
  119. $eventoObj->replaceFecha($modif["CalendarioEventoEditado_fecha_origen"], $modif["CalendarioEventoEditado_fecha_nueva"], $modif["CalendarioEventoEditado_hora_inicial"], $modif["CalendarioEventoEditado_hora_final"]);
  120. }
  121. //Quitar fechas de excepción
  122. foreach(explode(",", $evento["CalendarioEvento_excepcion_str"]) as $ex){
  123. $eventoObj->removeFecha($ex);
  124. }
  125. //revisa que la fecha del evento nueva no se repita con las fechas que ya tenía el evento
  126. //modifica la fecha
  127. if($clase["todo_dia"]){
  128. $stmt = $pdo->prepare('Select * from fi_calendarioeventoeditado(:id, :fecha_o, :fecha_n, NULL, NULL)');
  129. }else{
  130. $stmt = $pdo->prepare('Select * from fi_calendarioeventoeditado(:id, :fecha_o, :fecha_n, :hora_i, :hora_f)');
  131. $stmt->bindParam(":hora_i", $clase["hora_ini"]);
  132. $stmt->bindParam(":hora_f", $clase["hora_fin"]);
  133. }
  134. $stmt->bindParam(":id", $eventoObj->id_db);
  135. $stmt->bindParam(":fecha_o", $clase["fecha_orig"]);
  136. $stmt->bindParam(":fecha_n", $clase["fecha_nueva"]);
  137. if(!$stmt->execute()){
  138. $t = $stmt->errorInfo();
  139. $return["error"] .= "Ocurrió un error al editar la fecha.".$t[2];
  140. $error = true;
  141. }
  142. }
  143. }//fin no unico
  144. }
  145. }//fin hay cambio de fecha
  146. if(!$error){
  147. $pdo->commit();
  148. unset($return["error"]);
  149. $return["ok"] = "El evento se editó correctamente";
  150. //Inserta Log
  151. $log = new LogActividad();
  152. $desc_log = "Actualiza día de evento EventoID[".$clase["id_db"]."] Original[".$clase["fecha_orig"]."] Día[".$clase["fecha_nueva"]."] HoraIni[".$clase["hora_ini"]."] HoraFin[".$clase["hora_fin"]."]";
  153. $log->appendLog($_SESSION["usuario_id"], $_SESSION["usuario_nombre"]." ".$_SESSION["usuario_apellidos"], $desc_log);
  154. }else
  155. $pdo->rollBack();
  156. $stmt = null; // cierra conexion
  157. } catch(PDOException $e) {
  158. $pdo->rollBack();
  159. $return["error"] = "Ocurrió un error al insertar los datos del evento.";
  160. }
  161. }
  162. $return["json"] = json_encode($return);
  163. echo json_encode($return);
  164. ?>