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

@ -1,7 +1,8 @@
<?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");
@ -11,7 +12,8 @@ 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) {
@ -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,7 +3,8 @@ 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,7 +67,8 @@ 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"];
@ -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,7 +97,8 @@ 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"]);

View File

@ -30,7 +30,8 @@ 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";
} elseif (isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"])) { } elseif (isset($data["hora_inicio_mañana"], $data["hora_fin_mañana"])) {
@ -41,7 +42,8 @@ function setWorkingHours(&$data){
} }
function setUncheckedBoxes(&$data){ function setUncheckedBoxes(&$data)
{
if (!isset($data["sabado_habil"])) { if (!isset($data["sabado_habil"])) {
$data["sabado_habil"] = 0; $data["sabado_habil"] = 0;
} }
@ -51,7 +53,8 @@ function setUncheckedBoxes(&$data){
} }
function sanitizeInputs(&$data){ function sanitizeInputs(&$data)
{
setWorkingHours($data); setWorkingHours($data);
setUncheckedBoxes($data); setUncheckedBoxes($data);
} }

View File

@ -2,7 +2,8 @@
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);
@ -13,7 +14,8 @@ function formatArray($data){
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);
@ -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,7 +2,8 @@
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(
@ -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);
?>