Sixtens hemsida Uppgifter Blogg Om

VED House bokningssystem

Gå till sida

Källkod

api/

database.php

api.php

ved/

monthBookingData.php

openingHoursException.php

bookingData.php

dayTimeframes.php

bookings.php

admin/

revokeToken.php

authorization.php

login.php

modules/

vedDatabase.php

utility.php

gyar/

log.php

auth/

revokeToken.php

authorization.php

login.php

station/

settings.php

log.php

modules/

gyarDatabase.php

databaseConnection.php

database.php

utility.php

credentials.json

weather/

latest.php

old.php

admin/

index.html

changeTimetable.html

login.html

js/

login.js

modules.js

admin.js

header.js

changeTimetable.js

cron/

vedClearExpiredTokens.php

api/ved/openingHoursException.php

1 lines
<?php
# openingHoursException.php
# API endpoint
# Sköter hanteringen av undantag i vanliga öppettider.

require_once "ved/modules/utility.php";

# Hämtar alla undantagstider.
$functionGET = function () {
    require 
"ved/modules/vedDatabase.php";
    
$data $db->queryAll("SELECT exceptionId, date1, CASE WHEN date1=date2 THEN NULL ELSE date2 END AS date2, DATE_FORMAT(openingTime, '%H:%i') openingTime,  DATE_FORMAT(closingTime, '%H:%i') closingTime FROM openHoursException WHERE date2>=CURRENT_DATE ORDER BY date1 ASC;");
    return 
$data;
};

# Lägger till en ny undantagstid och eventuellt förflyttar eller tar bort existerande.
$functionPOST = function () {
    require 
"ved/modules/vedDatabase.php";
    
$settings $db->query("SELECT bookingInterval, bookingLength FROM restaurantSettings");
    
# Kontroll av parametrar.
    
$isTimeSet = isset($openingHours["openingTime"]) && isset($openingHours["closingTime"]);
    if (
$isTimeSet) {
        
$opening time_to_min($_POST["openingTime"]); # Öppettid i minuter efter 00:00
        
$closing time_to_min($_POST["closingTime"]); # Stängtid i minuter efter 00:00

        
$dayLength $closing $opening;
        if (
$dayLength $settings["bookingLength"])
            throw new 
APIParameterError(400"openingHours""Invalid times.");
        if (
$dayLength $settings["bookingInterval"] != 0)
            throw new 
APIParameterError(400"openingHours""Booking interval of {$settings['bookingInterval']} minutes does not fit.");
    }

    
# Kontrollerar ifall date2 är efter date1(i fallet av flerdagarsundantag).
    # Annars sätts både date1 och date2 kolumnerna i databasen till samma dag(enkeldagsundantag).
    
if (isset($_POST["date2"])) {
        
$date1 = new DateTime($_POST["date"]);
        
$date2 = new DateTime($_POST["date2"]);
        if (
$date1->getTimestamp() >= $date2->getTimestamp())
            throw new 
APIParameterError(400, ["date""date2"], "Invalid dates.");
    }

    
# Eftersom många ändrande SQL-satser körs på rad används transaktioner för att se till att allt går bra.
    
$db->pdo->beginTransaction();
    try {
        
$date1 $_POST["date"];
        
$date2 $_POST["date2"] ?? $_POST["date"];
        
$opening $_POST["openingTime"];
        
$closing $_POST["closingTime"];

        
# Hämtar alla undantag som delvis överlappar med det nya undantaget.
        
$partialOverlaps $db->queryAll("SELECT *, CASE
WHEN date1 < :date1 THEN -1
WHEN date2 > :date2 THEN 1
ELSE 0 END position,
(CASE WHEN (:openingTime IS NULL) THEN (openingTime IS NULL) ELSE (:openingTime=openingTime AND openingTime IS NOT NULL) END) AND 
(CASE WHEN (:closingTime IS NULL) THEN (closingTime IS NULL) ELSE (:closingTime=closingTime AND closingTime IS NOT NULL) END) sameTime
FROM openHoursException WHERE
date1 BETWEEN :date1 AND :date2 XOR
date2 BETWEEN :date1 AND :date2;"
, array(
                
"date1" => $date1,
                
"date2" => $date2,
                
"openingTime" => $opening,
                
"closingTime" => $closing
            
));

        
# Går igenom de delvis överlappande och antingen förflyttar dem eller tar bort dem.
        
foreach ($partialOverlaps as $overlap) {
            if (
$overlap["sameTime"]) {
                
# Tas bort ifall de har samma öppet- och stängtider som det nya undantaget.
                
$db->query("DELETE FROM openHoursException WHERE exceptionId=:exceptionId", array("exceptionId" => $overlap["exceptionId"]));
                switch (
$overlap["position"]) {
                    case -
1:
                        
$date1 $overlap["date1"];
                        break;
                    case 
1:
                        
$date2 $overlap["date2"];
                        break;
                }
            } else {
                
# Förflyttas annars ifall de inte har samma öppet- och stängtider.
                
$queryDate;
                
$paramDate;
                switch (
$overlap["position"]) {
                    case -
1:
                        
$queryDate "date2";
                        
$paramDate $date1;
                        break;
                    case 
1:
                        
$queryDate "date1";
                        
$paramDate $date2;
                        break;
                }
                
$db->query("UPDATE openHoursException SET {$queryDate}=ADDDATE(:date, :offset) WHERE exceptionId=:exceptionId", array(
                    
"exceptionId" => $overlap["exceptionId"],
                    
"date" => $paramDate,
                    
"offset" => $overlap["position"]
                ));
            }
        }

        
# Tar bort alla existerande undantag som helt och hållet befinner sig i nya undantaget.
        
$db->query("DELETE FROM openHoursException WHERE date1 >= :date1 AND date2 <= :date2", array(
            
"date1" => $date1,
            
"date2" => $date2
        
));

        
$params = array(
            
"date1" => $date1,
            
"date2" => $date2,
            
"openingTime" => $opening,
            
"closingTime" => $closing
        
);
        
# Hämtar alla undantag som helt och hållet omger det nya.
        
$fullIntersect $db->query("SELECT *, (CASE WHEN (:openingTime IS NULL) THEN (openingTime IS NULL) ELSE (:openingTime=openingTime AND openingTime IS NOT NULL) END) AND 
        (CASE WHEN (:closingTime IS NULL) THEN (closingTime IS NULL) ELSE (:closingTime=closingTime AND closingTime IS NOT NULL) END) sameTimes
        FROM openHoursException WHERE date1 < :date1 AND date2 > :date2"
$params);

        
# Ifall det finns ett undantag som omger det nya delas det gamla upp.
        
if ($fullIntersect && !$fullIntersect["sameTimes"]) {
            
$db->query("UPDATE openHoursException SET date2=ADDDATE(:date1, -1) WHERE exceptionId=:exceptionId", array("exceptionId" => $fullIntersect["exceptionId"], "date1" => $date1));
            
$db->query("INSERT INTO openHoursException(date1, date2, openingTime, closingTime) VALUES (ADDDATE(:date1, 1), :date2, :openingTime, :closingTime)", array(
                
"date1" => $date2,
                
"date2" => $fullIntersect["date2"],
                
"openingTime" => $fullIntersect["openingTime"],
                
"closingTime" => $fullIntersect["closingTime"]
            ));
        }

        
# Ifall det finns ett gammalt som omger och som dessutom har samma öppet- och stängtider behöver man inte lägga till det nya.
        
if (!($fullIntersect && $fullIntersect["sameTimes"]))
            
$db->query("INSERT INTO openHoursException(date1, date2, openingTime, closingTime) VALUES (:date1, :date2, :openingTime, :closingTime)"$params);
    } catch (
Exception $ex) {
        
# Om någonting misslyckas backas allting tillbaka..
        
$db->pdo->rollBack();
        throw 
$ex;
    }
    
$db->pdo->commit();
    
http_response_code(201);
    
$data = array("message" => "Exception was added.");
    return 
$data;
};

# Tar bort en specifik undantagstid.
$functionDELETE = function () {
    require 
"ved/modules/vedDatabase.php";
    
$data $db->queryAll("DELETE FROM openHoursException WHERE exceptionId=:exceptionId", array("exceptionId" => $_POST["exceptionId"]));
    
$data = array("message" => "Exception was removed.");
    return 
$data;
};

$api = new APIEndpoint();
$auth = require_once "ved/admin/authorization.php";

$httpGET = new APIMethod("GET"$functionGET);
$httpGET->setAuthorization($auth);
$api->addMethod($httpGET);

$httpPOST = new APIMethod("POST"$functionPOST);
$httpPOST->setAuthorization($auth);
$httpPOST->setRequiredBodyParameters(array("date"));
$api->addMethod($httpPOST);

$httpDELETE = new APIMethod("DELETE"$functionDELETE);
$httpDELETE->setAuthorization($auth);
$httpDELETE->setRequiredBodyParameters(array("exceptionId"));
$api->addMethod($httpDELETE);

return 
$api;
?>