Skip to content

Commit

Permalink
last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
6-ON committed Mar 8, 2023
1 parent 4843085 commit bc08579
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 27 deletions.
18 changes: 12 additions & 6 deletions controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ public function createCruise(Request $request, Response $response)

$cruise = new Cruise();
$data = array_merge($request->getBody(), $request->getNamesOfFiles());
$data['nights'] = date_diff(new \DateTime($data['endDate']), new \DateTime($data['startDate']))->d;
$nights = date_diff(new \DateTime($data['endDate']), new \DateTime($data['startDate']))->d;
if ($nights)
$data['nights'] = $nights;
else
$data['nights'] = 1;

$data['startDate'] = date('Y-m-d', strtotime($data['startDate']));
$cruise->loadData($data);


try {
$this->uploadImage();
Application::$app->db->exec('SET FOREIGN_KEY_CHECKS=0;');
Expand All @@ -115,8 +119,10 @@ public function createCruise(Request $request, Response $response)
}

Application::$app->db->pdo->commit();
$response->redirect('/cruise');
} else {
Application::$app->session->setFlash('errors', $cruise->errors);
}
$response->redirect('/cruise');
} catch (\PDOException $e) {
Application::$app->db->pdo->rollBack();
throw $e;
Expand Down Expand Up @@ -220,12 +226,12 @@ public function reserveCancel(Request $request, Response $response)
$reservation = Reservation::findOne($conditions, true);
$current_date = date('Y-m-d', time());
if ($reservation) {
if (strtotime($reservation->startDate) > strtotime($current_date)) {
if (date_diff(new \DateTime($current_date), new \DateTime($reservation->startDate))->d > 2) {
Reservation::delete($conditions);
Application::$app->session->setFlash('success-r','Reservation cancelled');
Application::$app->session->setFlash('success-r', 'Reservation cancelled');
} else {
$response->redirect('/reservation');
Application::$app->session->setFlash('error-r','You can\'t cancel this reservation');
Application::$app->session->setFlash('error-r', 'You can\'t cancel this reservation');
}

}
Expand Down
24 changes: 23 additions & 1 deletion controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,37 @@ public function ship(Request $request)
);
}


public function cruise(Request $request)
{
$filters = [];
$idShip = $request->getBody()['ship'] ?? false;
$idPort = $request->getBody()['port'] ?? false;
$month = $request->getBody()['month'] ?? false;
$page = $request->getBody()['page'] ?? false;
$pagination = [];
if ($page) {
$pagination = [
$page - 1,
5
];
}
if ($month)
$filters['startDate'] = [
['>', "$month-01"]
, ['<', "$month-31"]
];
else
date('Y-m-d', time());


if ($idShip)
$filters['shipId'] = $idShip;
if ($idPort)
$filters['startPort'] = $idPort;

$cruises = Cruise::getAll(true, $filters);
$cruises = Cruise::getAll(true, $filters,$pagination);

$ships = Ship::getAll();
$ports = Port::getAll();

Expand Down Expand Up @@ -87,7 +107,9 @@ public function room(Request $request)

public function reservation(Request $request, Response $response)
{

$uid = Application::$app->session->get('user');

if ($uid === false) {
$response->redirect('/login');
return null;
Expand Down
2 changes: 1 addition & 1 deletion core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function __construct($rootPath, array $config)

public function run()
{
echo $this->router->resolve();
try {
echo $this->router->resolve();
} catch (\Exception $exception) {
$code = is_int($exception->getCode()) ? $exception->getCode() : 500;
self::$app->response->setStatusCode($code);
Expand Down
70 changes: 59 additions & 11 deletions core/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function save()
return true;
}

public static function findOne($where,bool $useView = false)
public static function findOne($where, bool $useView = false)
{
$tableName = static::tableName();
if ($useView){
if ($useView) {
$tableName = static::ViewName();
}
$attributes = array_keys($where);
Expand All @@ -55,24 +55,72 @@ public static function findOne($where,bool $useView = false)
return $stmt->fetchObject(static::class);
}

public static function getAll(bool $useView = false, array $where = [])
// public function scopeWhere(array $where = [])
// {
// if (!$where) return '';
// $sql = 'WHERE ';
// array_map(function ($param,$value) {
// if(is_string($value)){
// $sql = '$'
// }
// }, $where);
//
// }

/**
* @throws \Exception
*/
public static function getAll(bool $useView = false, array $where = [], array $paginate = [])
{
if ($useView) {
$tableName = static::ViewName();
} else {
$tableName = static::tableName();
}

$tableName = $useView ? static::ViewName() : static::tableName();
$sql = "SELECT * FROM $tableName";

if (!empty($where)) {
$params = array_map(fn($prm) => "$prm = :$prm", array_keys($where));
$sql .= (' WHERE ' . implode(' AND ', $params));
$sql .= ' WHERE ';
$index = 0;
foreach ($where as $param => $value) {
if (is_string($value) || is_int($value) ) {
if ($index) $sql .= 'AND ';
$sql .= "$param = :$param";
$index++;
} elseif (is_array($value)) {
foreach ($value as $rule) {
if (is_array($rule)) {

if ($index) $sql .= 'AND ';
$sql .= "$param " . $rule[0] . " :param_$index ";
} else
throw new \Exception();
$index++;
}
} else
throw new \Exception();
}

}

if ($paginate)
$sql .= sprintf(' LIMIT %u,%u',
$paginate[0] * $paginate[1] + 1,
$paginate[1]);
$stmt = self::prepare($sql);

if (!empty($where)) {
$index = 0;
foreach ($where as $param => $value) {
$stmt->bindValue(":$param",$value);
if (is_array($value))
foreach ($value as $rule) {
$stmt->bindValue(":param_$index", $rule[1]);
$index++;
}
else {
$stmt->bindValue(":$param", $value);
$index++;
}
}
}

$stmt->execute();

return $stmt->fetchAll(PDO::FETCH_CLASS, static::class);
Expand Down
4 changes: 4 additions & 0 deletions public/scripts/CRUD.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
$(function () {
const inputs = $('#Modal input,#Modal select');

inputs.on('invalid',function (e) {
e.preventDefault();
})

let modalHeader = $('#Modal h3')
$('#createButton').on('click', function () {
Expand Down
4 changes: 4 additions & 0 deletions public/styles/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@tailwind components;
@tailwind utilities;

* {
transition: background-color ease-in 200ms;
}


.form-field {
@apply max-md:mb-4 rounded-3xl border-none bg-yellow-400 bg-opacity-50 px-6 py-2 text-center text-inherit placeholder-slate-200 shadow-lg outline-none backdrop-blur-md;
Expand Down
68 changes: 60 additions & 8 deletions views/cruise.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@
<h1 class="text-3xl font-extrabold text-center md:text-5xl lg:text-6xl bg-clip-text text-transparent bg-gradient-to-r from-blue-700 via-purple-400 to-sky-500 capitalize">
Choose a cruise</h1>
</div>

<div class=" w-full">
<form class="flex gap-16 p-8" action="" method="get">
<div class="relative max-w-sm">
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor"
viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
clip-rule="evenodd"></path>
</svg>
</div>
<input name="month" datepicker datepicker-format="yyyy-mm" type="text"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
datepicker-buttons
placeholder="Select Month">
</div>
<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 mr-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">filter</button>
</form>
</div>
<div class="p-5 grid dark:bg-gray-900 gap-3 sm:grid-cols-2 grid-cols-1 justify-items-center ">

<?php
Expand Down Expand Up @@ -94,14 +112,14 @@ class="bg-[conic-gradient(at_top,_var(--tw-gradient-stops))] from-gray-900 via-g
<div>
<label for="label" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">label</label>
<input type="text" name="label" id="label"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Type Cruise name" required="">
class="bg-gray-50 invalid:bg-red-400 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-600 focus:border-blue-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Type Cruise name" required>
</div>
<div>
<label for="ship"
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Ship</label>
<select id="ship" name="shipId"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" required>

<option selected value="">Choose a Ship</option>
<?php foreach ($ships as $ship): ?>
Expand All @@ -114,7 +132,7 @@ class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:
class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Starting
Port</label>
<select id="start_port" name="startPort"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" required>
<option selected value="">Choose a port</option>
<?php foreach ($ports as $port): ?>
<option value="<?= $port->id ?>"><?= $port->label ?></option>
Expand Down Expand Up @@ -169,7 +187,7 @@ class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Period </la
</div>
<input name="startDate" type="text"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Select date start">
placeholder="Select date start" required>
</div>
<span class="mx-4 text-gray-500">to</span>
<div class="relative">
Expand All @@ -183,15 +201,15 @@ class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:
</div>
<input name="endDate" type="text"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
placeholder="Select date end">
placeholder="Select date end" required>
</div>
</div>

</div>
<div>
<label for="image" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Image</label>
<input class="block w-full text-sm text-gray-900 border border-gray-300 rounded-lg cursor-pointer bg-gray-50 dark:text-gray-400 focus:outline-none dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400"
aria-describedby="file_input_help" name="image" accept="image/jpeg" type="file">
aria-describedby="file_input_help" name="image" accept="image/jpeg" type="file" required>
</div>
</div>
<button type="submit"
Expand All @@ -204,6 +222,7 @@ class="text-white inline-flex items-center bg-blue-700 hover:bg-blue-800 focus:r
</div>



<!-- create button-->
<div class="fixed bottom-2 right-5">
<button type="button" id="createButton" data-modal-toggle="Modal"
Expand All @@ -218,6 +237,39 @@ class="flex justify-center items-center w-14 h-14 text-white bg-blue-700 rounded
</div>

<?php endif; ?>
<nav class="flex justify-center">
<ul class="inline-flex items-center -space-x-px">
<li>
<a href="#" class="block px-3 py-2 ml-0 leading-tight text-gray-500 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">
<span class="sr-only">Previous</span>
<svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd"></path></svg>
</a>
</li>
<li>
<a href="?page=1" class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">1</a>
</li>
<li>
<a href="?page=2" class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">2</a>
</li>
<li>
<a href="?page=3"
class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">3</a>
</li>

<li>
<a href="?page=4" class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">4</a>
</li>
<li>
<a href="?page=5" class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">5</a>
</li>
<li>
<a href="#" class="block px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white">
<span class="sr-only">Next</span>
<svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg>
</a>
</li>
</ul>
</nav>


<script src="https://unpkg.com/flowbite@1.5.5/dist/datepicker.js"></script>
Expand Down
Loading

0 comments on commit bc08579

Please sign in to comment.