tieneAcceso()){ $return["error"] = "No tienes permisos para realizar esta acción."; }else if(!isset($_POST["autorizacion"]) || !isset($_POST["fecha"]) || $_POST["fecha"] == ""){ $return["error"] = "No se recibió la información del horario."; }else if(!isset($_SESSION["periodo_id"]) || $_SESSION["periodo_id"] ==""){ $return["error"] = "Necesitas seleccionar un periodo."; }else{ $autorizacion = filter_input(INPUT_POST, "autorizacion", FILTER_SANITIZE_NUMBER_INT);//limpia texto $filter_fecha = fechaGuion(trim(filter_input(INPUT_POST, "fecha", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto $stmt = $pdo->prepare('Select * from fs_mihorariofull(:usr, :fecha, :tipoauto)');//Obtiene todo el calendario $stmt->bindParam(":fecha", $filter_fecha); $stmt->bindParam(":usr", $_SESSION["usuario_id"]); $stmt->bindParam(":tipoauto", $autorizacion); if(!$stmt->execute()){ $t = $stmt->errorInfo(); $return["error"] = "Ocurrió un error al obtener los horarios ".$t[2]; $error = true; }else{ $horario_rs = $stmt->fetchAll(); $stmt->closeCursor(); $json = array(); $erroresArr = validaHorarios($horario_rs); if(count($erroresArr) > 0){ $return["errorArr"] = $erroresArr; } foreach($horario_rs as $horario){//crea objeto json $horario_tmp =array(); $horario_tmp["id"] = $horario["Horario_id"]; $horario_tmp["dia"] = $horario["Dia_id"]; $horario_tmp["hora"] = substr($horario["Hora_inicio"], 0, 5);//hh:mm $horario_tmp["duracion"] = $horario["Duracion"]; $horario_tmp["tipo"] = $horario["TipoHorario_id"]; $horario_tmp["tipo_nombre"] = $horario["TipoHorario_desc"]; if($autorizacion == 1)//Si es crear, depende del tipo de horario $horario_tmp["editable"] = $horario["TipoHorario_isAsignable"]; else//es en espera de aprobacoin, no se edita $horario_tmp["editable"] = false; $horario_tmp["color"] = $horario["TipoHorario_color"]; if(!horarioRepetido($json, $horario_tmp["dia"], $horario_tmp["hora"])) $json[] = $horario_tmp; }//fin foreach }//fin tiene datos $return["horario"] = $json; $return["ok"] = "El horario se cargó correctamente"; } $return["json"] = json_encode($return); echo json_encode($return); function validaHorarios(& $arr){ $erroresArr = array(); for($i = 0; $i < count($arr)-1; $i++){ for($j = $i+1; $j < count($arr); $j++){ if($arr[$i]["Dia_id"] == $arr[$j]["Dia_id"] && !($arr[$i]["TipoHorario_id"] == 3 && $arr[$i]["TipoHorario_id"] == $arr[$j]["TipoHorario_id"])){ if( (date('H:i', strtotime($arr[$i]["Hora_inicio"])) <= date('H:i', strtotime($arr[$j]["Hora_inicio"])) && date('H:i', strtotime($arr[$i]["Hora_final"])) > date('H:i', strtotime($arr[$j]["Hora_inicio"])) ) || (date('H:i', strtotime($arr[$j]["Hora_inicio"])) <= date('H:i', strtotime($arr[$i]["Hora_inicio"])) && date('H:i', strtotime($arr[$j]["Hora_final"])) > date('H:i', strtotime($arr[$i]["Hora_inicio"])) ) ){ if($arr[$i]["TipoHorario_id"] < $arr[$j]["TipoHorario_id"]){ $erroresArr[] = array('nombre'=>$arr[$i]["TipoHorario_desc"], 'dia'=> diaNombre($arr[$i]["Dia_id"]), 'hora_i'=>date('H:i', strtotime($arr[$i]["Hora_inicio"])), 'hora_f'=> date('H:i', strtotime($arr[$i]["Hora_final"]))); $arr[$i]["Duracion"] = (strtotime('2019-01-01 '.$arr[$j]["Hora_inicio"]) - strtotime('2019-01-01 '.$arr[$i]["Hora_inicio"]))/60; if($arr[$i]["Duracion"] <= 0) array_splice($arr, $i, 1); }else{ //if($arr[$i]["TipoHorario_id"] < $arr[$j]["TipoHorario_id"]) $erroresArr[] = array('nombre'=>$arr[$j]["TipoHorario_desc"], 'dia'=> diaNombre($arr[$j]["Dia_id"]), 'hora_i'=>date('H:i', strtotime($arr[$j]["Hora_inicio"])), 'hora_f'=> date('H:i', strtotime($arr[$j]["Hora_final"]))); $arr[$j]["Duracion"] = (strtotime('2019-01-01 '.$arr[$i]["Hora_inicio"]) - strtotime('2019-01-01 '.$arr[$j]["Hora_inicio"]))/60; if($arr[$j]["Duracion"] <= 0) array_splice($arr, $j, 1); } } } } } return $erroresArr; } function horarioRepetido($arreglo, $dia, $hora){ foreach($arreglo as $horario){ if($dia == $horario["dia"] && $hora == $horario["hora"]) return true; } return false; } ?>