Improve PHP formatting

This commit is contained in:
coolneng 2020-07-16 03:19:08 +02:00
parent 6bc9db9117
commit b0238cc9b0
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
7 changed files with 83 additions and 61 deletions

View File

@ -25,7 +25,7 @@
$calendar_settings = fetchCalendarSettings($pdo, $doctor); $calendar_settings = fetchCalendarSettings($pdo, $doctor);
$time_ranges = computeTimeRanges($pdo); $time_ranges = computeTimeRanges($pdo);
?> ?>
<?php if($_SESSION["user_type"] == 1 and !isset($_GET["medico"])): ?> <?php if ($_SESSION["user_type"] == 1 and !isset($_GET["medico"])): ?>
<form name="select_doctor" method="get" action="appointment.php"> <form name="select_doctor" method="get" action="appointment.php">
<?php $doctors = listDoctors($pdo); ?> <?php $doctors = listDoctors($pdo); ?>
<div class="input-group"> <div class="input-group">

View File

@ -1,8 +1,9 @@
<?php <?php
include 'appointment_management.php'; include 'appointment_management.php';
function computeEndTime(&$data){ function computeEndTime(&$data)
foreach($data as &$row){ {
foreach ($data as &$row) {
$time = new DateTime($row["hora"]); $time = new DateTime($row["hora"]);
$time->modify("+{$row["duracion"]} minutes"); $time->modify("+{$row["duracion"]} minutes");
$time_string = $time->format('H:i:s'); $time_string = $time->format('H:i:s');
@ -11,10 +12,11 @@ function computeEndTime(&$data){
} }
function formatArray($data){ function formatArray($data)
{
computeEndTime($data); computeEndTime($data);
$events = array(); $events = array();
foreach($data as $row){ foreach ($data as $row) {
$events[] = array( $events[] = array(
"id" => $row["id"], "id" => $row["id"],
"title" => $row["apellido"] . ", " . $row["nombre"], "title" => $row["apellido"] . ", " . $row["nombre"],
@ -28,7 +30,8 @@ function formatArray($data){
return $events; return $events;
} }
function fetchDatabase(){ function fetchDatabase()
{
$pdo = connectDatabase(); $pdo = connectDatabase();
$data = listEvents($pdo); $data = listEvents($pdo);
closeDatabase($pdo); closeDatabase($pdo);
@ -39,5 +42,3 @@ function fetchDatabase(){
$result = fetchDatabase(); $result = fetchDatabase();
$events = formatArray($result); $events = formatArray($result);
echo json_encode($events); echo json_encode($events);
?>

View File

@ -3,8 +3,9 @@ include'database.php';
session_start(); session_start();
function fetchDoctor($pdo){ function fetchDoctor($pdo)
if($_SESSION["user_type"] == 2){ {
if ($_SESSION["user_type"] == 2) {
$user = finduser($pdo, $_SESSION["user"]); $user = finduser($pdo, $_SESSION["user"]);
return $user[0][4]; return $user[0][4];
} }
@ -12,28 +13,32 @@ function fetchDoctor($pdo){
} }
function listEvents($pdo){ function listEvents($pdo)
{
$doctor = fetchDoctor($pdo); $doctor = fetchDoctor($pdo);
$events = fetchCalendarEvents($pdo, $doctor); $events = fetchCalendarEvents($pdo, $doctor);
return $events; return $events;
} }
function listHolidays($pdo){ function listHolidays($pdo)
{
$doctor = fetchDoctor($pdo); $doctor = fetchDoctor($pdo);
$holidays = fetchDoctorHolidays($pdo, $doctor); $holidays = fetchDoctorHolidays($pdo, $doctor);
return $holidays; return $holidays;
} }
function listCalendarSettings($pdo){ function listCalendarSettings($pdo)
{
$doctor = fetchDoctor($pdo); $doctor = fetchDoctor($pdo);
$calendar_settings = fetchCalendarSettings($pdo, $doctor); $calendar_settings = fetchCalendarSettings($pdo, $doctor);
return $calendar_settings; return $calendar_settings;
} }
function appointmentCreation(){ function appointmentCreation()
{
$data = $_POST; $data = $_POST;
$pdo = connectDatabase(); $pdo = connectDatabase();
createAppointment($pdo, $data); createAppointment($pdo, $data);
@ -42,7 +47,8 @@ function appointmentCreation(){
} }
function appointmentModification(){ function appointmentModification()
{
$data = $_POST; $data = $_POST;
$pdo = connectDatabase(); $pdo = connectDatabase();
editAppointment($pdo, $data); editAppointment($pdo, $data);
@ -51,7 +57,8 @@ function appointmentModification(){
} }
function appointmentDeletion(){ function appointmentDeletion()
{
$id = $_POST["event_id"]; $id = $_POST["event_id"];
$pdo = connectDatabase(); $pdo = connectDatabase();
deleteAppointment($pdo, $id); deleteAppointment($pdo, $id);
@ -60,12 +67,13 @@ function appointmentDeletion(){
} }
function getFloorCeiling($pdo){ function getFloorCeiling($pdo)
{
$data = listCalendarSettings($pdo); $data = listCalendarSettings($pdo);
if($data["horario"] == "completo"){ if ($data["horario"] == "completo") {
$start = $data["hora_inicio_mañana"]; $start = $data["hora_inicio_mañana"];
$end = $data["hora_fin_tarde"]; $end = $data["hora_fin_tarde"];
} else if($data["horario"] == "mañana"){ } elseif ($data["horario"] == "mañana") {
$start = $data["hora_inicio_mañana"]; $start = $data["hora_inicio_mañana"];
$end = $data["hora_fin_mañana"]; $end = $data["hora_fin_mañana"];
} else { } else {
@ -80,7 +88,8 @@ function getFloorCeiling($pdo){
} }
function getTimeDiff(){ function getTimeDiff()
{
$current = time(); $current = time();
$addTime = strtotime("+30 mins", $current); $addTime = strtotime("+30 mins", $current);
$diff = $addTime - $current; $diff = $addTime - $current;
@ -88,13 +97,14 @@ function getTimeDiff(){
} }
function computeTimeRanges($pdo){ function computeTimeRanges($pdo)
{
$range = getFloorCeiling($pdo); $range = getFloorCeiling($pdo);
$diff = getTimeDiff(); $diff = getTimeDiff();
$start = strtotime($range["start"]); $start = strtotime($range["start"]);
$end = strtotime($range["end"]); $end = strtotime($range["end"]);
$time_range = array(); $time_range = array();
while($start < $end){ while ($start < $end) {
$time_range[] = date('G:i:s', $start); $time_range[] = date('G:i:s', $start);
$start += $diff; $start += $diff;
} }
@ -102,19 +112,19 @@ function computeTimeRanges($pdo){
} }
if(isset($_POST["create"])){ if (isset($_POST["create"])) {
appointmentCreation(); appointmentCreation();
} }
if(isset($_POST["edit_form"])){ if (isset($_POST["edit_form"])) {
$event_id = $_POST["event_id"]; $event_id = $_POST["event_id"];
header("location: appointment_edit_form.php?event_id=$event_id"); header("location: appointment_edit_form.php?event_id=$event_id");
} }
if(isset($_POST["edit"])){ if (isset($_POST["edit"])) {
appointmentModification(); appointmentModification();
} }
if(isset($_POST["delete"])){ if (isset($_POST["delete"])) {
appointmentDeletion(); appointmentDeletion();
} }

View File

@ -30,28 +30,31 @@ function holidayDeletion()
} }
function setWorkingHours(&$data){ function setWorkingHours(&$data)
if(isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"], $data["hora_inicio_tarde"], $data["hora_fin_tarde"])){ {
if (isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"], $data["hora_inicio_tarde"], $data["hora_fin_tarde"])) {
$data["horario"] = "completo"; $data["horario"] = "completo";
} else if (isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"])){ } elseif (isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"])) {
$data["horario"] = "mañana"; $data["horario"] = "mañana";
} else if (isset($data["hora_inicio_tarde"], $data["hora_fin_tarde"])){ } elseif (isset($data["hora_inicio_tarde"], $data["hora_fin_tarde"])) {
$data["horario"] = "tarde"; $data["horario"] = "tarde";
} }
} }
function setUncheckedBoxes(&$data){ function setUncheckedBoxes(&$data)
if (!isset($data["sabado_habil"])){ {
$data["sabado_habil"] = 0; if (!isset($data["sabado_habil"])) {
$data["sabado_habil"] = 0;
} }
if (!isset($data["domingo_habil"])){ if (!isset($data["domingo_habil"])) {
$data["domingo_habil"] = 0; $data["domingo_habil"] = 0;
} }
} }
function sanitizeInputs(&$data){ function sanitizeInputs(&$data)
{
setWorkingHours($data); setWorkingHours($data);
setUncheckedBoxes($data); setUncheckedBoxes($data);
} }

View File

@ -2,18 +2,20 @@
include 'appointment_management.php'; include 'appointment_management.php';
function formatArray($data){ function formatArray($data)
{
$settings = array(); $settings = array();
if(!$data["sabado_habil"]){ if (!$data["sabado_habil"]) {
array_push($settings, 6); array_push($settings, 6);
} }
if(!$data["domingo_habil"]){ if (!$data["domingo_habil"]) {
array_push($settings, 0); array_push($settings, 0);
} }
return $settings; return $settings;
} }
function fetchDatabase(){ function fetchDatabase()
{
$pdo = connectDatabase(); $pdo = connectDatabase();
$data = listCalendarSettings($pdo); $data = listCalendarSettings($pdo);
closeDatabase($pdo); closeDatabase($pdo);
@ -24,5 +26,3 @@ function fetchDatabase(){
$result = fetchDatabase(); $result = fetchDatabase();
$events = formatArray($result); $events = formatArray($result);
echo json_encode($events); echo json_encode($events);
?>

View File

@ -23,7 +23,8 @@ function listRows($pdo, $table)
return $result; return $result;
} }
function fetchLatestRegion($pdo){ function fetchLatestRegion($pdo)
{
$query = "SELECT id_provincia FROM provincias ORDER BY id_provincia DESC LIMIT 1;"; $query = "SELECT id_provincia FROM provincias ORDER BY id_provincia DESC LIMIT 1;";
$result = $pdo->prepare($query); $result = $pdo->prepare($query);
$result->execute(); $result->execute();
@ -31,7 +32,8 @@ function fetchLatestRegion($pdo){
return $data; return $data;
} }
function fetchLatestCity($pdo){ function fetchLatestCity($pdo)
{
$query = "SELECT id_municipio FROM municipios ORDER BY id_municipio DESC LIMIT 1;"; $query = "SELECT id_municipio FROM municipios ORDER BY id_municipio DESC LIMIT 1;";
$result = $pdo->prepare($query); $result = $pdo->prepare($query);
$result->execute(); $result->execute();
@ -39,7 +41,8 @@ function fetchLatestCity($pdo){
return $data; return $data;
} }
function createRegion($pdo, $data){ function createRegion($pdo, $data)
{
$query = "INSERT INTO provincias (id_provincia, provincia) VALUES (?,?)"; $query = "INSERT INTO provincias (id_provincia, provincia) VALUES (?,?)";
$row = fetchLatestRegion($pdo); $row = fetchLatestRegion($pdo);
$last_region = $row["id_provincia"]; $last_region = $row["id_provincia"];
@ -48,7 +51,8 @@ function createRegion($pdo, $data){
return $index; return $index;
} }
function createCity($pdo, $data, $region){ function createCity($pdo, $data, $region)
{
$query = "INSERT INTO municipios (id_provincia, cod_municipio, DC, nombre) VALUES (?,?,?,?)"; $query = "INSERT INTO municipios (id_provincia, cod_municipio, DC, nombre) VALUES (?,?,?,?)";
$pdo->prepare($query)->execute([$region, 999, 999, $data["localidad2"]]); $pdo->prepare($query)->execute([$region, 999, 999, $data["localidad2"]]);
$city = fetchLatestCity($pdo); $city = fetchLatestCity($pdo);
@ -60,7 +64,7 @@ function createPatient($pdo, $data)
$query = "INSERT INTO paciente $query = "INSERT INTO paciente
(nombre, apellido, fecha_de_nacimiento, documento_identificativo, tipo_documento, direccion, localidad, provincia, pais) (nombre, apellido, fecha_de_nacimiento, documento_identificativo, tipo_documento, direccion, localidad, provincia, pais)
VALUES (?,?,?,?,?,?,?,?,?)"; VALUES (?,?,?,?,?,?,?,?,?)";
if($data["provincia"] == "empty" and $data["localidad"] == "empty"){ if ($data["provincia"] == "empty" and $data["localidad"] == "empty") {
$region = createRegion($pdo, $data); $region = createRegion($pdo, $data);
$city = createCity($pdo, $data, $region); $city = createCity($pdo, $data, $region);
$data["provincia"] = $region; $data["provincia"] = $region;
@ -76,7 +80,7 @@ function editPatient($pdo, $data, $id)
{ {
$query = "UPDATE paciente SET nombre=?, apellido=?, fecha_de_nacimiento=?, documento_identificativo=?, $query = "UPDATE paciente SET nombre=?, apellido=?, fecha_de_nacimiento=?, documento_identificativo=?,
tipo_documento=?, direccion=?, localidad=?, provincia=?, pais=? WHERE id=?"; tipo_documento=?, direccion=?, localidad=?, provincia=?, pais=? WHERE id=?";
if($data["provincia"] == "empty" and $data["localidad"] == "empty"){ if ($data["provincia"] == "empty" and $data["localidad"] == "empty") {
$region = createRegion($pdo, $data); $region = createRegion($pdo, $data);
$city = createCity($pdo, $data, $region); $city = createCity($pdo, $data, $region);
$data["provincia"] = $region; $data["provincia"] = $region;
@ -388,7 +392,7 @@ function fetchCalendarSettings($pdo, $doctor)
$result = $pdo->prepare($query); $result = $pdo->prepare($query);
$result->execute([$doctor]); $result->execute([$doctor]);
$data = $result->fetch(PDO::FETCH_ASSOC); $data = $result->fetch(PDO::FETCH_ASSOC);
if(empty($data[8])){ if (empty($data[8])) {
$data[8] = 60; $data[8] = 60;
} }
return $data; return $data;
@ -407,7 +411,8 @@ function fetchDoctorHolidays($pdo, $doctor)
} }
function createAppointment($pdo, $data){ function createAppointment($pdo, $data)
{
$query = "INSERT INTO cita (fecha, hora, duracion, medico, observaciones, paciente) VALUES (?,?,?,?,?,?)"; $query = "INSERT INTO cita (fecha, hora, duracion, medico, observaciones, paciente) VALUES (?,?,?,?,?,?)";
$pdo->prepare($query)->execute([$data["fecha"], $data["hora"], $data["duracion"], $pdo->prepare($query)->execute([$data["fecha"], $data["hora"], $data["duracion"],
$data["doctor"], $data["observaciones"], $data["paciente"]]); $data["doctor"], $data["observaciones"], $data["paciente"]]);
@ -415,7 +420,8 @@ function createAppointment($pdo, $data){
} }
function editAppointment($pdo, $data){ function editAppointment($pdo, $data)
{
$query = "UPDATE cita SET fecha=?, hora=?, duracion=?, observaciones=?, paciente=? $query = "UPDATE cita SET fecha=?, hora=?, duracion=?, observaciones=?, paciente=?
WHERE id=?"; WHERE id=?";
$pdo->prepare($query)->execute([$data["fecha"], $data["hora"], $data["duracion"], $pdo->prepare($query)->execute([$data["fecha"], $data["hora"], $data["duracion"],
@ -424,14 +430,16 @@ function editAppointment($pdo, $data){
} }
function deleteAppointment($pdo, $id){ function deleteAppointment($pdo, $id)
{
$statement = "DELETE FROM cita WHERE id=?"; $statement = "DELETE FROM cita WHERE id=?";
$pdo->prepare($statement)->execute([$id]); $pdo->prepare($statement)->execute([$id]);
return "Cita borrada con éxito"; return "Cita borrada con éxito";
} }
function fetchAppointmentData($pdo, $id){ function fetchAppointmentData($pdo, $id)
{
$query = "SELECT id, fecha, hora, duracion, observaciones FROM cita WHERE id=?"; $query = "SELECT id, fecha, hora, duracion, observaciones FROM cita WHERE id=?";
$result = $pdo->prepare($query); $result = $pdo->prepare($query);
$result->execute([$id]); $result->execute([$id]);

View File

@ -2,9 +2,10 @@
include 'appointment_management.php'; include 'appointment_management.php';
function formatArray($data){ function formatArray($data)
{
$events = array(); $events = array();
foreach($data as $row){ foreach ($data as $row) {
$events[] = array( $events[] = array(
"id" => $row["id"], "id" => $row["id"],
"start" => $row["fecha_festivo"], "start" => $row["fecha_festivo"],
@ -17,7 +18,8 @@ function formatArray($data){
return $events; return $events;
} }
function fetchDatabase(){ function fetchDatabase()
{
$pdo = connectDatabase(); $pdo = connectDatabase();
$data = listHolidays($pdo); $data = listHolidays($pdo);
closeDatabase($pdo); closeDatabase($pdo);
@ -28,5 +30,3 @@ function fetchDatabase(){
$result = fetchDatabase(); $result = fetchDatabase();
$events = formatArray($result); $events = formatArray($result);
echo json_encode($events); echo json_encode($events);
?>