From b0238cc9b09ddbba7aa470db18f5543069bc0837 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 16 Jul 2020 03:19:08 +0200 Subject: [PATCH] Improve PHP formatting --- src/appointment.php | 2 +- src/appointment_feed.php | 15 +++++----- src/appointment_management.php | 52 ++++++++++++++++++++-------------- src/calendar_management.php | 23 ++++++++------- src/calendar_settings_feed.php | 12 ++++---- src/database.php | 30 +++++++++++++------- src/holiday_feed.php | 10 +++---- 7 files changed, 83 insertions(+), 61 deletions(-) diff --git a/src/appointment.php b/src/appointment.php index 94124fa..3c91793 100644 --- a/src/appointment.php +++ b/src/appointment.php @@ -25,7 +25,7 @@ $calendar_settings = fetchCalendarSettings($pdo, $doctor); $time_ranges = computeTimeRanges($pdo); ?> - +
diff --git a/src/appointment_feed.php b/src/appointment_feed.php index e8aceb5..fcc2283 100644 --- a/src/appointment_feed.php +++ b/src/appointment_feed.php @@ -1,8 +1,9 @@ modify("+{$row["duracion"]} minutes"); $time_string = $time->format('H:i:s'); @@ -11,10 +12,11 @@ function computeEndTime(&$data){ } -function formatArray($data){ +function formatArray($data) +{ computeEndTime($data); $events = array(); - foreach($data as $row){ + foreach ($data as $row) { $events[] = array( "id" => $row["id"], "title" => $row["apellido"] . ", " . $row["nombre"], @@ -28,7 +30,8 @@ function formatArray($data){ return $events; } -function fetchDatabase(){ +function fetchDatabase() +{ $pdo = connectDatabase(); $data = listEvents($pdo); closeDatabase($pdo); @@ -39,5 +42,3 @@ function fetchDatabase(){ $result = fetchDatabase(); $events = formatArray($result); echo json_encode($events); - -?> diff --git a/src/appointment_management.php b/src/appointment_management.php index 8d85813..cfbecb5 100644 --- a/src/appointment_management.php +++ b/src/appointment_management.php @@ -3,37 +3,42 @@ include'database.php'; session_start(); -function fetchDoctor($pdo){ - if($_SESSION["user_type"] == 2){ +function fetchDoctor($pdo) +{ + if ($_SESSION["user_type"] == 2) { $user = finduser($pdo, $_SESSION["user"]); return $user[0][4]; - } + } return $_GET["medico"]; } -function listEvents($pdo){ +function listEvents($pdo) +{ $doctor = fetchDoctor($pdo); $events = fetchCalendarEvents($pdo, $doctor); return $events; } -function listHolidays($pdo){ +function listHolidays($pdo) +{ $doctor = fetchDoctor($pdo); $holidays = fetchDoctorHolidays($pdo, $doctor); return $holidays; } -function listCalendarSettings($pdo){ +function listCalendarSettings($pdo) +{ $doctor = fetchDoctor($pdo); $calendar_settings = fetchCalendarSettings($pdo, $doctor); return $calendar_settings; } -function appointmentCreation(){ +function appointmentCreation() +{ $data = $_POST; $pdo = connectDatabase(); createAppointment($pdo, $data); @@ -42,7 +47,8 @@ function appointmentCreation(){ } -function appointmentModification(){ +function appointmentModification() +{ $data = $_POST; $pdo = connectDatabase(); editAppointment($pdo, $data); @@ -51,7 +57,8 @@ function appointmentModification(){ } -function appointmentDeletion(){ +function appointmentDeletion() +{ $id = $_POST["event_id"]; $pdo = connectDatabase(); deleteAppointment($pdo, $id); @@ -60,12 +67,13 @@ function appointmentDeletion(){ } -function getFloorCeiling($pdo){ +function getFloorCeiling($pdo) +{ $data = listCalendarSettings($pdo); - if($data["horario"] == "completo"){ + if ($data["horario"] == "completo") { $start = $data["hora_inicio_mañana"]; $end = $data["hora_fin_tarde"]; - } else if($data["horario"] == "mañana"){ + } elseif ($data["horario"] == "mañana") { $start = $data["hora_inicio_mañana"]; $end = $data["hora_fin_mañana"]; } else { @@ -80,21 +88,23 @@ function getFloorCeiling($pdo){ } -function getTimeDiff(){ - $current = time(); - $addTime = strtotime("+30 mins", $current); +function getTimeDiff() +{ + $current = time(); + $addTime = strtotime("+30 mins", $current); $diff = $addTime - $current; return $diff; } -function computeTimeRanges($pdo){ +function computeTimeRanges($pdo) +{ $range = getFloorCeiling($pdo); $diff = getTimeDiff(); $start = strtotime($range["start"]); $end = strtotime($range["end"]); $time_range = array(); - while($start < $end){ + while ($start < $end) { $time_range[] = date('G:i:s', $start); $start += $diff; } @@ -102,19 +112,19 @@ function computeTimeRanges($pdo){ } -if(isset($_POST["create"])){ +if (isset($_POST["create"])) { appointmentCreation(); } -if(isset($_POST["edit_form"])){ +if (isset($_POST["edit_form"])) { $event_id = $_POST["event_id"]; header("location: appointment_edit_form.php?event_id=$event_id"); } -if(isset($_POST["edit"])){ +if (isset($_POST["edit"])) { appointmentModification(); } -if(isset($_POST["delete"])){ +if (isset($_POST["delete"])) { appointmentDeletion(); } diff --git a/src/calendar_management.php b/src/calendar_management.php index baef20f..5621798 100644 --- a/src/calendar_management.php +++ b/src/calendar_management.php @@ -30,28 +30,31 @@ function holidayDeletion() } -function setWorkingHours(&$data){ - if(isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"], $data["hora_inicio_tarde"], $data["hora_fin_tarde"])){ +function setWorkingHours(&$data) +{ + if (isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"], $data["hora_inicio_tarde"], $data["hora_fin_tarde"])) { $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"; - } else if (isset($data["hora_inicio_tarde"], $data["hora_fin_tarde"])){ + } elseif (isset($data["hora_inicio_tarde"], $data["hora_fin_tarde"])) { $data["horario"] = "tarde"; } } -function setUncheckedBoxes(&$data){ - if (!isset($data["sabado_habil"])){ - $data["sabado_habil"] = 0; +function setUncheckedBoxes(&$data) +{ + if (!isset($data["sabado_habil"])) { + $data["sabado_habil"] = 0; } - if (!isset($data["domingo_habil"])){ - $data["domingo_habil"] = 0; + if (!isset($data["domingo_habil"])) { + $data["domingo_habil"] = 0; } } -function sanitizeInputs(&$data){ +function sanitizeInputs(&$data) +{ setWorkingHours($data); setUncheckedBoxes($data); } diff --git a/src/calendar_settings_feed.php b/src/calendar_settings_feed.php index 8e8d3bb..2270366 100644 --- a/src/calendar_settings_feed.php +++ b/src/calendar_settings_feed.php @@ -2,18 +2,20 @@ include 'appointment_management.php'; -function formatArray($data){ +function formatArray($data) +{ $settings = array(); - if(!$data["sabado_habil"]){ + if (!$data["sabado_habil"]) { array_push($settings, 6); } - if(!$data["domingo_habil"]){ + if (!$data["domingo_habil"]) { array_push($settings, 0); } return $settings; } -function fetchDatabase(){ +function fetchDatabase() +{ $pdo = connectDatabase(); $data = listCalendarSettings($pdo); closeDatabase($pdo); @@ -24,5 +26,3 @@ function fetchDatabase(){ $result = fetchDatabase(); $events = formatArray($result); echo json_encode($events); - -?> diff --git a/src/database.php b/src/database.php index 7a25403..8098aa6 100644 --- a/src/database.php +++ b/src/database.php @@ -23,7 +23,8 @@ function listRows($pdo, $table) return $result; } -function fetchLatestRegion($pdo){ +function fetchLatestRegion($pdo) +{ $query = "SELECT id_provincia FROM provincias ORDER BY id_provincia DESC LIMIT 1;"; $result = $pdo->prepare($query); $result->execute(); @@ -31,7 +32,8 @@ function fetchLatestRegion($pdo){ return $data; } -function fetchLatestCity($pdo){ +function fetchLatestCity($pdo) +{ $query = "SELECT id_municipio FROM municipios ORDER BY id_municipio DESC LIMIT 1;"; $result = $pdo->prepare($query); $result->execute(); @@ -39,7 +41,8 @@ function fetchLatestCity($pdo){ return $data; } -function createRegion($pdo, $data){ +function createRegion($pdo, $data) +{ $query = "INSERT INTO provincias (id_provincia, provincia) VALUES (?,?)"; $row = fetchLatestRegion($pdo); $last_region = $row["id_provincia"]; @@ -48,7 +51,8 @@ function createRegion($pdo, $data){ return $index; } -function createCity($pdo, $data, $region){ +function createCity($pdo, $data, $region) +{ $query = "INSERT INTO municipios (id_provincia, cod_municipio, DC, nombre) VALUES (?,?,?,?)"; $pdo->prepare($query)->execute([$region, 999, 999, $data["localidad2"]]); $city = fetchLatestCity($pdo); @@ -60,7 +64,7 @@ function createPatient($pdo, $data) $query = "INSERT INTO paciente (nombre, apellido, fecha_de_nacimiento, documento_identificativo, tipo_documento, direccion, localidad, provincia, pais) VALUES (?,?,?,?,?,?,?,?,?)"; - if($data["provincia"] == "empty" and $data["localidad"] == "empty"){ + if ($data["provincia"] == "empty" and $data["localidad"] == "empty") { $region = createRegion($pdo, $data); $city = createCity($pdo, $data, $region); $data["provincia"] = $region; @@ -76,7 +80,7 @@ function editPatient($pdo, $data, $id) { $query = "UPDATE paciente SET nombre=?, apellido=?, fecha_de_nacimiento=?, documento_identificativo=?, 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); $city = createCity($pdo, $data, $region); $data["provincia"] = $region; @@ -388,7 +392,7 @@ function fetchCalendarSettings($pdo, $doctor) $result = $pdo->prepare($query); $result->execute([$doctor]); $data = $result->fetch(PDO::FETCH_ASSOC); - if(empty($data[8])){ + if (empty($data[8])) { $data[8] = 60; } 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 (?,?,?,?,?,?)"; $pdo->prepare($query)->execute([$data["fecha"], $data["hora"], $data["duracion"], $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=? WHERE id=?"; $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=?"; $pdo->prepare($statement)->execute([$id]); return "Cita borrada con éxito"; } -function fetchAppointmentData($pdo, $id){ +function fetchAppointmentData($pdo, $id) +{ $query = "SELECT id, fecha, hora, duracion, observaciones FROM cita WHERE id=?"; $result = $pdo->prepare($query); $result->execute([$id]); diff --git a/src/holiday_feed.php b/src/holiday_feed.php index 75dbd5d..35847a2 100644 --- a/src/holiday_feed.php +++ b/src/holiday_feed.php @@ -2,9 +2,10 @@ include 'appointment_management.php'; -function formatArray($data){ +function formatArray($data) +{ $events = array(); - foreach($data as $row){ + foreach ($data as $row) { $events[] = array( "id" => $row["id"], "start" => $row["fecha_festivo"], @@ -17,7 +18,8 @@ function formatArray($data){ return $events; } -function fetchDatabase(){ +function fetchDatabase() +{ $pdo = connectDatabase(); $data = listHolidays($pdo); closeDatabase($pdo); @@ -28,5 +30,3 @@ function fetchDatabase(){ $result = fetchDatabase(); $events = formatArray($result); echo json_encode($events); - -?>