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);
$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">
<?php $doctors = listDoctors($pdo); ?>
<div class="input-group">

View File

@ -1,8 +1,9 @@
<?php
include 'appointment_management.php';
function computeEndTime(&$data){
foreach($data as &$row){
function computeEndTime(&$data)
{
foreach ($data as &$row) {
$time = new DateTime($row["hora"]);
$time->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);
?>

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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);
?>

View File

@ -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]);

View File

@ -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);
?>