dbutils = new DbUtils(); } function handleCommand($command) { if (!$this->isUserAlreadyLoggedInForPhpAndMayReserve()) { echo json_encode(array("status" => "ERROR", "code" => ERROR_RES_NOT_AUTHOTRIZED, "msg" => ERROR_RES_NOT_AUTHOTRIZED_MSG)); } else { switch ($command) { case 'createReservation': $pdo = DbUtils::openDbAndReturnPdoStatic(); self::createReservation($pdo,$_POST['day'],$_POST['month'],$_POST['year'],$_POST['starthour'],$_POST['startmin'],$_POST['name'],$_POST['email'],$_POST['persons'],$_POST['durationhours'],$_POST['durationmins'],$_POST['phone'],$_POST['remark'],$_POST["tableid"],self::$RES_TYPE_NORMAL); break; case 'getReservations': $this->getReservations($_GET['day'],$_GET['month'],$_GET['year']); break; case 'changeReservation': $this->changeReservation($_POST['id'],$_POST['day'],$_POST['month'],$_POST['year'],$_POST['starthour'],$_POST['startmin'],$_POST['name'],$_POST['email'],$_POST['persons'],$_POST['durationhours'],$_POST['durationmins'],$_POST['phone'],$_POST['remark'],$_POST["tableid"]); break; case 'delReservation': $this->delReservation($_POST['id']); break; case 'emailConfirmReservation': $this->emailConfirmReservation($_POST['to'],$_POST['msg']); break; case 'reservationsAsHtml': $this->reservationsAsHtml($_GET['day'],$_GET['month'],$_GET['year']); break; case 'blockday': $pdo = DbUtils::openDbAndReturnPdoStatic(); self::blockday($pdo,$_POST["day"],$_POST["month"],$_POST["year"],$_POST["remark"]); break; case 'releaseday': $pdo = DbUtils::openDbAndReturnPdoStatic(); self::releaseday($pdo,$_POST["day"],$_POST["month"],$_POST["year"]); break; case 'getfutureblocks': $pdo = DbUtils::openDbAndReturnPdoStatic(); self::getFutureBlocks($pdo); break; default: echo json_encode(array("status" => "OK","msg" => "Kommando nicht unterstuetzt.")); } } } function isUserAlreadyLoggedInForPhpAndMayReserve() { if(session_id() == '') { session_start(); } if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) { return false; } else { return ($_SESSION['right_reservation']); } } private static function blockday($pdo,$day,$month,$year,$remark) { // REM* check, if there is already a block. In this case replace it $sql = "SELECT id FROM %reservations% WHERE DATE(scheduledate)=? AND restype=?"; $resdate = new DateTime($year . '-' . $month . '-' . $day); $resdateTxt = $resdate->format('Y-m-d'); $result = CommonUtils::fetchSqlAll($pdo, $sql, array($resdateTxt,self::$RES_TYPE_BLOCKDAY)); if (count($result) > 0) { // REM* block entries found! User must delete it first! echo json_encode(array("status" => "ERROR","msg" => "Der Tag wurde bereits geblockt")); return; } return self::createReservation($pdo, $day, $month, $year, 0, 0, "", "", 0, 23, 59, "", $remark, null, self::$RES_TYPE_BLOCKDAY); } private static function releaseday($pdo,$day,$month,$year) { try { $sql = "DELETE FROM %reservations% WHERE DATE(scheduledate)=? AND restype=?"; $resdate = new DateTime($year . '-' . $month . '-' . $day); $resdateTxt = $resdate->format('Y-m-d'); CommonUtils::execSql($pdo, $sql, array($resdateTxt,self::$RES_TYPE_BLOCKDAY)); echo json_encode(array("status" => "OK")); } catch (Exception $ex) { echo json_encode(array("status" => "ERROR","msg" => "Datenbank-Fehler: " . $ex->getMessage())); } } // REM* get all days that are full-day blocked private static function getFutureBlocks($pdo) { try { $sql = "SELECT id,DATE(scheduledate) as scheduledate,remark FROM %reservations% WHERE restype=? AND DATE(scheduledate) >= DATE_ADD(NOW(), INTERVAL -360 DAY) ORDER BY DATE(scheduledate)"; $result = CommonUtils::fetchSqlAll($pdo, $sql, array(self::$RES_TYPE_BLOCKDAY)); echo json_encode(array("status" => "OK","msg" => $result)); } catch (Exception $ex) { echo json_encode(array("status" => "ERROR","msg" => "Datenbank-Fehler: " . $ex->getMessage())); } } private static function isBookingAllowed($pdo,$day,$month,$year) { $scheduledDate = "$year-$month-$day"; $sql = "SELECT id FROM %reservations% WHERE restype=? AND DATE(scheduledate) = ?"; $result = CommonUtils::fetchSqlAll($pdo, $sql, array(self::$RES_TYPE_BLOCKDAY,$scheduledDate)); if (count($result) > 0) { return false; } else { return true; } } private static function createReservation($pdo,$day,$month,$year,$start,$startmin,$name,$email,$persons,$durationhours,$durationmins,$phone,$remark,$tableid,$restype) { // REM* check if booking is allowed if (!self::isBookingAllowed($pdo, $day, $month, $year)) { echo json_encode(array("status" => "ERROR","msg" => "Tag für weitere Buchungen gesperrt")); return; } $userid = $_SESSION['userid']; date_default_timezone_set(DbUtils::getTimeZone()); $currentTime = date('Y-m-d H:i:s'); $scheduledDate = "$year-$month-$day 00:00:00"; // REM* check if date is in the past // REM* $resdate = new DateTime($year . '-' . $month . '-' . $day); // REM* $curDate = new DateTime(date('Y-m-d')); // REM* $interval = $curDate->diff($resdate); // REM* $daysDiff = intval($interval->format('%R%a')); // REM* if ($daysDiff < 0) { // REM* echo json_encode(array("status" => "ERROR", "msg" => "Reservierungsdatum liegt in der Vergangenheit")); // REM* return; // REM* } if ($tableid <= 0) { $tableid = null; } try { $pdo->beginTransaction(); $sql = "INSERT INTO `%reservations%` ( `id` , `creator`,`creationdate`,`scheduledate`,`name`,`email`,`starttime`,`starttimemin`,`duration`,`durationmins`,`persons`,`phone`,`remark`,`tableid`,`restype`) VALUES ( NULL , ?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; CommonUtils::execSql($pdo, $sql, array($userid,$currentTime,$scheduledDate,$name,$email,$start,$startmin,$durationhours,$durationmins,$persons,$phone,$remark,$tableid,$restype)); $pdo->commit(); echo json_encode(array("status" => "OK")); } catch (PDOException $e) { $pdo->rollBack(); echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG)); } } private function changeReservation($id,$day,$month,$year,$startHour,$startMin,$name,$email,$persons,$durationHours,$durationMins,$phone,$remark,$tableid) { // REM* check if booking is allowed $pdo = DbUtils::openDbAndReturnPdoStatic(); if (!self::isBookingAllowed($pdo, $day, $month, $year)) { echo json_encode(array("status" => "ERROR","msg" => "Tag für weitere Buchungen gesperrt")); return; } $userid = $_SESSION['userid']; date_default_timezone_set(DbUtils::getTimeZone()); $currentTime = date('Y-m-d H:i:s'); $scheduledDate = "$year-$month-$day 00:00:00"; try { $pdo->beginTransaction(); $sql = "UPDATE `%reservations%` SET creator=?,creationdate=?,scheduledate=?,name=?,email=?,starttime=?,starttimemin=?,duration=?,durationmins=?,persons=?,phone=?,remark=?,tableid=? WHERE id=?"; CommonUtils::execSql($pdo, $sql, array($userid,$currentTime,$scheduledDate,$name,$email,$startHour,$startMin,$durationHours,$durationMins,$persons,$phone,$remark,$tableid,$id)); $pdo->commit(); echo json_encode(array("status" => "OK")); } catch (PDOException $e) { $pdo->rollBack(); echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG)); } } private function delReservation($id) { $pdo = DbUtils::openDbAndReturnPdoStatic(); try { $pdo->beginTransaction(); $sql = "DELETE FROM `%reservations%` WHERE id=?"; CommonUtils::execSql($pdo, $sql, array($id)); $pdo->commit(); echo json_encode(array("status" => "OK")); } catch (PDOException $e) { $pdo->rollBack(); echo json_encode(array("status" => "ERROR", "code" => ERROR_GENERAL_DB_NOT_READABLE, "msg" => ERROR_GENERAL_DB_NOT_READABLE_MSG)); } } private function emailConfirmReservation($toEmail,$msg) { // first find sender email $pdo = $this->dbutils->openDbAndReturnPdo(); $msg = str_replace("\n", "\r\n", $msg); $topictxt = "Reservierungsbestätigung\r\n"; if (Emailer::sendEmail($pdo, $msg, $toEmail, $topictxt)) { echo json_encode("OK"); } else { echo json_encode("ERROR"); } } private static function getNoOfActiveRooms($pdo) { $sql = "SELECT COUNT(id) as countid FROM %room% WHERE removed is null"; $result = CommonUtils::fetchSqlAll($pdo, $sql, null); if (count($result) > 0) { return $result[0]["countid"]; } return 0; } private function reservationsAsHtml($day,$month,$year) { $pdo = DbUtils::openDbAndReturnPdoStatic(); $sql = self::getSqlForResByTime(); $timeSortedReservations = $this->getReservationsCore($pdo,$day,$month,$year,$sql . " ORDER BY starttime,roomsorting,tablesorting"); $numberOfActiveRooms = self::getNoOfActiveRooms($pdo); header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" ); header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" ); header( "Cache-Control: no-cache, must-revalidate" ); header( "Pragma: no-cache" ); header( "Content-Type: text/html; charset=utf8" ); $txt = "
"; $txt .= ""; $txt .= "
Startuhrzeit | Dauer (Std.) | Gast | Personen | Platz | Zusatzinfo | |
---|---|---|---|---|---|---|
" . $row['start'] . ":00"; $txt .= " | " . $row['duration']; $txt .= " | " . htmlspecialchars($row['guest']); $txt .= " | " . htmlspecialchars($row['persons']); if ($numberOfActiveRooms > 1) { $txt .= " | " . htmlspecialchars($row['roomname']) . "/" . htmlspecialchars($row['tablename']); } else { $txt .= " | " . htmlspecialchars($row['tablename']); } $txt .= " | " . htmlspecialchars($row['remark']); $txt .= " |