Skip to content

Commit

Permalink
Image resizing done, proper access management and DVD detail
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsDyze committed Sep 1, 2024
1 parent af3767a commit c850bc0
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 56 deletions.
46 changes: 28 additions & 18 deletions public/assets/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)
}
}
22 changes: 22 additions & 0 deletions src/Controllers/DVD/DVDController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Controllers\DVD;

use Models\DVDLightModel;
use Models\ViewModels\DVDViewModel;
use Services\DVDService;
use Views\DVD\DVDView;

class DVDController
{
public function get($id)
{
$data = new DVDViewModel();
$service = DVDService::getInstance();

$data->dvd = $service->getLightModelById($id);

$view = new DVDView($data);
$view->render();
}
}
11 changes: 6 additions & 5 deletions src/Controllers/Manage/ManageDVDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/Controllers/Manage/ManageDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion src/Controllers/Manage/ManageOrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace Controllers\Manage
{

use Utils\JWTUtils;

class ManageOrderController
{

function __construct()
{
JWTUtils::isAuthorized(true);
}
}
}

22 changes: 0 additions & 22 deletions src/Middlewares/Authentication.php

This file was deleted.

4 changes: 4 additions & 0 deletions src/Middlewares/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,9 @@ public function route(): void
},
"/manage/dvd" => function() {
return new ManageDVDController();
},
"/dvd" => function() {
return new DVDController();
}
);

Expand Down
2 changes: 2 additions & 0 deletions src/Models/DVDLightModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Models/ViewModels/DVDViewModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Models\ViewModels;

use Interfaces\IViewModel;
use Models\DVDLightModel;

class DVDViewModel implements IViewModel{
public DVDLightModel $dvd;
}
23 changes: 22 additions & 1 deletion src/Services/DVDService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{

use Exception;
use Models\DVDLightModel;
use Models\DVDModel;
use Models\QueryModel\DVDQueryModel;
use Utils\Query\InsertQueryBuilder;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function getAll(DVDQueryModel $queryModel): array
return $result;
}

public function getById($id):?DVDModel
public function getById($id)
{
$result = array();
$queryBuilder = (new QueryBuilder())
Expand All @@ -109,6 +110,26 @@ public function getById($id):?DVDModel
return null;
}

public function getLightModelById($id)
{
$result = array();
$queryBuilder = (new QueryBuilder())
->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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
id="file-<?php echo $this->name; ?>"
<?php echo $this->required?"required":""; ?>
/>
<img src="<?php echo $this->base64Value; ?>" alt="image preview"/>
</label>
<img id="preview-<?php echo $this->name; ?>" src="<?php echo $this->base64Value; ?>" alt="preview"/>
</label>
21 changes: 21 additions & 0 deletions src/Utils/JWTUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Empty file added src/Views/DVD/DVDView.js
Empty file.
27 changes: 27 additions & 0 deletions src/Views/DVD/DVDView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Views\DVD
{

use Models\ViewModels\DVDViewModel;
use Models\ViewModels\LayoutViewModel;
use Views\BaseView;

class DVDView extends BaseView
{
private DVDViewModel $data;

function __construct(DVDViewModel $viewModel)
{
$this->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);
}
}
}
Empty file added src/Views/DVD/DVDView.style.css
Empty file.
3 changes: 3 additions & 0 deletions src/Views/DVD/DVDView.template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>
DVD Detail!
</h2>
15 changes: 13 additions & 2 deletions src/Views/Home/HomeView.style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions src/Views/Home/HomeView.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

<div class="card-container">
<?php foreach ($data->DVDs as $row): ?>
<div class="card clickable" onclick="document.location.href='/manage/dvd/<?php echo $row->Id; ?>'">
<div class="card clickable" onclick="document.location.href='/dvd/<?php echo $row->Id; ?>'">
<div class="card-content">
<h3><?php echo $row->LocalTitle; ?></h3>
<div class="no-preview">
<span>No Preview Available</span>
</div>
<?php if($row->Image): ?>
<img src="<?php echo $row->Image; ?>" alt="preview" />
<?php else: ?>
<div class="no-preview">
<span>No Preview Available</span>
</div>
<?php endif; ?>


</div>
</div>
<?php endforeach; ?>
Expand Down
Loading

0 comments on commit c850bc0

Please sign in to comment.