From c850bc030d5f91b3946bcc250e614b5ca2d7a7b3 Mon Sep 17 00:00:00 2001 From: Dyze Date: Sun, 1 Sep 2024 15:20:34 +0200 Subject: [PATCH] Image resizing done, proper access management and DVD detail --- public/assets/scripts.js | 46 +++++++++++-------- src/Controllers/DVD/DVDController.php | 22 +++++++++ .../Manage/ManageDVDController.php | 11 +++-- .../Manage/ManageDashboardController.php | 6 +++ .../Manage/ManageOrderController.php | 8 +++- src/Middlewares/Authentication.php | 22 --------- src/Middlewares/Routing.php | 4 ++ src/Models/DVDLightModel.php | 2 + src/Models/ViewModels/DVDViewModel.php | 10 ++++ src/Services/DVDService.php | 23 +++++++++- .../FormImageComponent.template.php | 4 +- src/Utils/JWTUtils.php | 21 +++++++++ src/Views/DVD/DVDView.js | 0 src/Views/DVD/DVDView.php | 27 +++++++++++ src/Views/DVD/DVDView.style.css | 0 src/Views/DVD/DVDView.template.php | 3 ++ src/Views/Home/HomeView.style.css | 15 +++++- src/Views/Home/HomeView.template.php | 14 ++++-- .../Detail/ManageDVDDetailView.template.php | 2 +- 19 files changed, 184 insertions(+), 56 deletions(-) create mode 100644 src/Controllers/DVD/DVDController.php delete mode 100644 src/Middlewares/Authentication.php create mode 100644 src/Models/ViewModels/DVDViewModel.php create mode 100644 src/Views/DVD/DVDView.js create mode 100644 src/Views/DVD/DVDView.php create mode 100644 src/Views/DVD/DVDView.style.css create mode 100644 src/Views/DVD/DVDView.template.php diff --git a/public/assets/scripts.js b/public/assets/scripts.js index 013aa7d..5216633 100644 --- a/public/assets/scripts.js +++ b/public/assets/scripts.js @@ -11,8 +11,8 @@ function fileHelper(inputId, e) const reader = new FileReader(); reader.onload = function(event) { const base64String = event.target.result; - //resizeImage(base64String, 512, 768, img => document.getElementById(inputId).value = img); document.getElementById(inputId).value = base64String; + resizeAndPreview(inputId, base64String, 256, 384); }; reader.readAsDataURL(file); } @@ -23,35 +23,45 @@ function fileHelper(inputId, e) } } -function resizeImage(base64, maxWidth, maxHeight, callback) -{ +function resizeAndPreview(input, base64, maxWidth, maxHeight, cb) { const img = new Image(); img.src = base64; img.onload = () => { let width = img.width; let height = img.height; - if (width > height) { - if (width > maxWidth) { - height *= maxWidth / width; - width = maxWidth; - } + const imgAspectRatio = width / height; + const maxAspectRatio = maxWidth / maxHeight; + + let newWidth, newHeight; + + if (imgAspectRatio > maxAspectRatio) { + newWidth = maxWidth; + newHeight = maxWidth / imgAspectRatio; } else { - if (height > maxHeight) { - width *= maxHeight / height; - height = maxHeight; - } + newHeight = maxHeight; + newWidth = maxHeight * imgAspectRatio; } const canvas = document.createElement('canvas'); - canvas.width = width; - canvas.height = height; + canvas.width = maxWidth; + canvas.height = maxHeight; const ctx = canvas.getContext('2d'); - ctx.drawImage(img, 0, 0, width, height); + + ctx.fillStyle = 'black'; + ctx.fillRect(0, 0, maxWidth, maxHeight); + + const offsetX = (maxWidth - newWidth) / 2; + const offsetY = (maxHeight - newHeight) / 2; + + ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight); + + const resizedBase64 = canvas.toDataURL(); - console.log(resizedBase64) - callback(resizedBase64); - }; + document.getElementById("preview-" + input).src=resizedBase64 + document.getElementById(input).value = resizedBase64 + cb(resizedBase64) + } } \ No newline at end of file diff --git a/src/Controllers/DVD/DVDController.php b/src/Controllers/DVD/DVDController.php new file mode 100644 index 0000000..3efcc07 --- /dev/null +++ b/src/Controllers/DVD/DVDController.php @@ -0,0 +1,22 @@ +dvd = $service->getLightModelById($id); + + $view = new DVDView($data); + $view->render(); + } +} \ No newline at end of file diff --git a/src/Controllers/Manage/ManageDVDController.php b/src/Controllers/Manage/ManageDVDController.php index 6e2c3c6..91a6857 100644 --- a/src/Controllers/Manage/ManageDVDController.php +++ b/src/Controllers/Manage/ManageDVDController.php @@ -4,23 +4,24 @@ { use Controllers\BaseController; - use Exception; use Models\DVDModel; - use Models\Exceptions\BadRouteException; - use Models\Exceptions\RouteNotFoundException; use Models\QueryModel\DVDQueryModel; use Models\ViewModels\ManageDVDDetailViewModel; use Models\ViewModels\ManageDVDDetailViewStateEnum; use Models\ViewModels\ManageDVDListViewModel; use Services\DVDService; - use Utils\ImageUtils; - use Utils\PHPUtils; + use Utils\JWTUtils; use Views\Manage\DVD\Detail\ManageDVDDetailView; use Views\Manage\DVD\List\ManageDVDListView; class ManageDVDController extends BaseController { + function __construct() + { + JWTUtils::isAuthorized(true); + } + public function get(int $id = null): void { if($id === null) diff --git a/src/Controllers/Manage/ManageDashboardController.php b/src/Controllers/Manage/ManageDashboardController.php index e6dc919..5133ddf 100644 --- a/src/Controllers/Manage/ManageDashboardController.php +++ b/src/Controllers/Manage/ManageDashboardController.php @@ -7,10 +7,16 @@ use Models\QueryModel\DVDQueryModel; use Models\ViewModels\DashboardViewModel; use Services\DVDService; + use Utils\JWTUtils; use Views\Manage\Dashboard\DashboardView; class ManageDashboardController extends BaseController { + function __construct() + { + JWTUtils::isAuthorized(true); + } + public function get(): void { $viewModel = new DashboardViewModel(); diff --git a/src/Controllers/Manage/ManageOrderController.php b/src/Controllers/Manage/ManageOrderController.php index 9e87746..0da7d0b 100644 --- a/src/Controllers/Manage/ManageOrderController.php +++ b/src/Controllers/Manage/ManageOrderController.php @@ -2,9 +2,15 @@ namespace Controllers\Manage { + + use Utils\JWTUtils; + class ManageOrderController { - + function __construct() + { + JWTUtils::isAuthorized(true); + } } } diff --git a/src/Middlewares/Authentication.php b/src/Middlewares/Authentication.php deleted file mode 100644 index 1bc0953..0000000 --- a/src/Middlewares/Authentication.php +++ /dev/null @@ -1,22 +0,0 @@ -isAdmin) && $jwt->isAdmin)) { - // Redirect to an unauthorized page or show an error - header('Location: /auth/error'); - exit; - } -} \ No newline at end of file diff --git a/src/Middlewares/Routing.php b/src/Middlewares/Routing.php index b5110ba..8117425 100644 --- a/src/Middlewares/Routing.php +++ b/src/Middlewares/Routing.php @@ -7,6 +7,7 @@ use Controllers\Auth\LoginController; use Controllers\Auth\LogoutController; use Controllers\Auth\RegisterController; + use Controllers\DVD\DVDController; use Controllers\Error\ErrorController; use Controllers\Error\ErrorRouter; use Controllers\Home\HomeController; @@ -66,6 +67,9 @@ public function route(): void }, "/manage/dvd" => function() { return new ManageDVDController(); + }, + "/dvd" => function() { + return new DVDController(); } ); diff --git a/src/Models/DVDLightModel.php b/src/Models/DVDLightModel.php index 4825c0a..b76db4f 100644 --- a/src/Models/DVDLightModel.php +++ b/src/Models/DVDLightModel.php @@ -6,7 +6,9 @@ class DVDLightModel { public int $Id; public ?string $LocalTitle; + public ?string $Synopsis; public ?int $Notation; + public ?string $Note; public ?string $Certification; public ?bool $IsOffered; public ?int $Quantity; diff --git a/src/Models/ViewModels/DVDViewModel.php b/src/Models/ViewModels/DVDViewModel.php new file mode 100644 index 0000000..53b05e6 --- /dev/null +++ b/src/Models/ViewModels/DVDViewModel.php @@ -0,0 +1,10 @@ +select(["Id", "LocalTitle", "Synopsis", "Notation", "Note", "Certification", "Quantity", "Price", "Year", "Image", "TypeId"]) + ->from("dvds") + ->where("Id", "=", $id); + + $query = $queryBuilder->getQuery(); + + $queryResult = $this->fetchStatement($query->sql, $query->params, DVDLightModel::class); + + if($queryResult ) + { + return $queryResult; + } + + return null; + } + public function update(DVDModel $dvd) { $queryBuilder = (new UpdateQueryBuilder()) diff --git a/src/Utils/Components/FormImageComponent/FormImageComponent.template.php b/src/Utils/Components/FormImageComponent/FormImageComponent.template.php index fcda37b..7ca9493 100644 --- a/src/Utils/Components/FormImageComponent/FormImageComponent.template.php +++ b/src/Utils/Components/FormImageComponent/FormImageComponent.template.php @@ -7,5 +7,5 @@ id="file-name; ?>" required?"required":""; ?> /> - image preview - \ No newline at end of file + preview + diff --git a/src/Utils/JWTUtils.php b/src/Utils/JWTUtils.php index 0065855..4faf3a3 100644 --- a/src/Utils/JWTUtils.php +++ b/src/Utils/JWTUtils.php @@ -119,6 +119,27 @@ public static function isValid(string $token): string } return true; } + + public static function isAuthorized($admin = false) + { + if (empty($_COOKIE['jwt'])) { + // Redirect to the login page if the user is not authenticated + http_response_code(401); + exit; + } + + // Decode the JWT token to get user information + $jwt = JWTUtils::decode($_COOKIE['jwt']); + + // If a specific role is required, check if the user has that role + if (!(isset($jwt) || !$jwt->isAdmin)) { + // Redirect to an unauthorized page or show an error + http_response_code(403); + exit; + } + + return true; + } } } diff --git a/src/Views/DVD/DVDView.js b/src/Views/DVD/DVDView.js new file mode 100644 index 0000000..e69de29 diff --git a/src/Views/DVD/DVDView.php b/src/Views/DVD/DVDView.php new file mode 100644 index 0000000..5b8550b --- /dev/null +++ b/src/Views/DVD/DVDView.php @@ -0,0 +1,27 @@ +viewName="DVD/DVDView"; + $this->subTitle="DVD"; + $this->data = $viewModel; + } + + public function render(): void + { + $layoutData = new LayoutViewModel(); + $layoutData -> pageSubTitle = $this->subTitle; + parent::renderLayout($layoutData, $this->data); + } + } +} \ No newline at end of file diff --git a/src/Views/DVD/DVDView.style.css b/src/Views/DVD/DVDView.style.css new file mode 100644 index 0000000..e69de29 diff --git a/src/Views/DVD/DVDView.template.php b/src/Views/DVD/DVDView.template.php new file mode 100644 index 0000000..8acd8be --- /dev/null +++ b/src/Views/DVD/DVDView.template.php @@ -0,0 +1,3 @@ +

+ DVD Detail! +

\ No newline at end of file diff --git a/src/Views/Home/HomeView.style.css b/src/Views/Home/HomeView.style.css index 6cd0346..139a74c 100644 --- a/src/Views/Home/HomeView.style.css +++ b/src/Views/Home/HomeView.style.css @@ -59,6 +59,16 @@ padding: 20px; } +.card-content h3 { + height: 64px; +} + +.card-content img { + height: 384px; + width: 254px; + margin: auto; +} + .no-preview { text-align: center; color: #D8DEE9; @@ -67,11 +77,12 @@ display: flex; align-items: center; justify-content: center; - width: 100%; aspect-ratio: 1 / 1.5; position: relative; background-color: #3B4252; - border-radius: 4px; + height: 384px; + width: 254px; + margin: auto; } .no-preview span { diff --git a/src/Views/Home/HomeView.template.php b/src/Views/Home/HomeView.template.php index 5c4cd23..cee70f6 100644 --- a/src/Views/Home/HomeView.template.php +++ b/src/Views/Home/HomeView.template.php @@ -5,12 +5,18 @@
DVDs as $row): ?> -
+

LocalTitle; ?>

-
- No Preview Available -
+ Image): ?> + preview + +
+ No Preview Available +
+ + +
diff --git a/src/Views/Manage/DVD/Detail/ManageDVDDetailView.template.php b/src/Views/Manage/DVD/Detail/ManageDVDDetailView.template.php index 461dbfc..f6b18ce 100644 --- a/src/Views/Manage/DVD/Detail/ManageDVDDetailView.template.php +++ b/src/Views/Manage/DVD/Detail/ManageDVDDetailView.template.php @@ -17,7 +17,7 @@

🎬 DVD->LocalTitle; ?> 🎬

-Back to the list +Back to the list