Skip to content

Commit

Permalink
Manage/DVD list and search and paging
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsDyze committed Aug 17, 2024
1 parent d26d512 commit b18e4b2
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 38 deletions.
1 change: 0 additions & 1 deletion github-action-trigger

This file was deleted.

23 changes: 18 additions & 5 deletions src/Controllers/Manage/ManageDVDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,34 @@
use Models\Exceptions\BadRouteException;
use Models\Exceptions\RouteNotFoundException;
use Models\QueryModel\DVDQueryModel;
use Models\ViewModels\DVDViewModel;
use Models\ViewModels\ManageDVDViewModel;
use Services\DVDService;
use Views\Manage\DVD\DVDView;
use Views\Manage\DVD\ManageDVDView;

class ManageDVDController
{
public function index()
{
$viewModel = new DVDViewModel();
$viewModel = new ManageDVDViewModel();
$service = DVDService::getInstance();

$queryModel = new DVDQueryModel();
$queryModel->IsOffered=true;
if(!empty($_GET))
{
$queryModel->setFromQueryString($_GET);
}
else
{
header('Location: ' . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)."?".$queryModel->getQueryString(), true, 303);
die();
}

$viewModel->Query = $queryModel;
$viewModel->FilteredCount = $service->getDVDCount($queryModel);
new DVDView($viewModel);
$viewModel->DVDs = $service->getDVDs($queryModel);
$viewModel->TotalPages = ceil($viewModel->FilteredCount / $queryModel->Limit);
$viewModel->CurrentPage = ($queryModel->Offset / $queryModel->Limit) + 1;
new ManageDVDView($viewModel);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Models/QueryModel/BaseQueryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{
class BaseQueryModel
{
public int $limit = 10;
public int $offset = 0;
public int $Limit = 10;
public int $Offset = 0;
}
}

28 changes: 27 additions & 1 deletion src/Models/QueryModel/DVDQueryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,33 @@
{
class DVDQueryModel extends BaseQueryModel
{
public bool|null $IsOffered;
public bool|null $IsOffered = null;
public string|null $OrderBy = null;
public string|null $OrderDesc = null;
public string|null $Search = null;

public function getQueryString(): string
{
return "IsOffered=$this->IsOffered&".
"OrderBy=$this->OrderBy&".
"OrderDesc=$this->OrderDesc&".
"Search=$this->Search&".
"Limit=$this->Limit&".
"Offset=$this->Offset";
}

public function setFromQueryString(array $param): void
{

$this->IsOffered = array_key_exists('IsOffered', $param) && $param['IsOffered'] !== ''
? filter_var($param['IsOffered'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
: null;
$this->OrderBy = $param['OrderBy'] ?? '';
$this->OrderDesc = $param['OrderDesc'] ?? '';
$this->Search = $param['Search'] ?? '';
$this->Limit = isset($param['Limit']) ? (int)$param['Limit'] : 10;
$this->Offset = isset($param['Offset']) ? (int)$param['Offset'] : 0;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use Interfaces\IViewModel;
use Models\QueryModel\DVDQueryModel;

class DVDViewModel implements IViewModel
class ManageDVDViewModel implements IViewModel
{
public array $DVDs;
public int $FilteredCount;
public DVDQueryModel $Query;
public int $TotalPages;
public int $CurrentPage;
}
}

50 changes: 40 additions & 10 deletions src/Services/DVDService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ public function getDVDCount(DVDQueryModel $queryModel = null):int
->from("dvds");
if($queryModel)
{
if($queryModel->IsOffered != null)
if($queryModel->IsOffered !== null)
{
$queryBuilder->where("IsOffered", "=", $queryModel->IsOffered);
}

if($queryModel->Search !== null)
{
$queryBuilder->where("Title", "LIKE", "%" . $queryModel->Search. "%");
$queryBuilder->where("LocalTitle", "LIKE", "%" . $queryModel->Search. "%");
}
}


Expand All @@ -40,28 +46,52 @@ public function getDVDCount(DVDQueryModel $queryModel = null):int
return 0;
}

public function getDVDs(DVDQueryModel $query): array
public function getDVDs(DVDQueryModel $queryModel): array
{
$result = array();
$query = "SELECT Id, Title, LocalTitle, Synopsis, Notation, Note, Certification, IsOffered, Quantity, Price, Year FROM dvds LIMIT ?, ?;";
$queryBuilder = (new QueryBuilder())
->select(["Id", "Title", "LocalTitle", "Synopsis", "Notation", "Note", "Certification", "IsOffered", "Quantity", "Price", "Year"])
->from("dvds")
->limit($queryModel->Offset, $queryModel->Limit);

$values = array(
$offset = $query->offset,
$limit = $query->limit
);
if($queryModel->IsOffered !== null)
{
$queryBuilder->where("IsOffered", "=", $queryModel->IsOffered);
}

$queryResult = $this->fetchAllStatement($query, $values);
if($queryModel->Search !== null)
{
$queryBuilder->where("Title", "LIKE", "%" . $queryModel->Search. "%");
$queryBuilder->where("LocalTitle", "LIKE", "%" . $queryModel->Search. "%");
}

if($queryResult && array_count_values($queryResult) > 0)
if($queryModel->OrderBy !== null &&
$queryModel->OrderBy !== "" &&
$this->isAllowedOrderColumn($queryModel->OrderBy))
{
$queryBuilder->orderBy($queryModel->OrderBy, $queryModel->OrderDesc);
}

$query = $queryBuilder->getQuery();

$queryResult = $this->fetchAllStatement($query->sql, $query->params);

if($queryResult && count($queryResult) > 0)
{
foreach($queryResult as $row)
{
array_push($result, $row);
$result[] = $row;
}
}

return $result;
}

function isAllowedOrderColumn(string $column): bool
{
$allowedOrderColumns = array("Quantity", "Year", "Title", "IsOffered", "Price");
return in_array($column, $allowedOrderColumns);
}
}
}

20 changes: 16 additions & 4 deletions src/Utils/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ class QueryBuilder
protected array $where = [];
protected int|null $limit;
protected int|null $offset;
protected string|null $orderBy;
protected bool|null $orderDesc;
protected array $params;

function __construct()
{
$this->limit = null;
$this->offset = null;
$this->orderDesc = null;
$this->orderBy = null;
$this->params = [];
}

Expand All @@ -35,11 +39,15 @@ public function where($column, $operator, $value) {
return $this;
}

public function limit($offset, $limit) {
public function limit(int $offset, int $limit) {
$this->limit = $limit;
$this->offset = $offset;
$this->params[] = $offset;
$this->params[] = $limit;
return $this;
}

public function orderBy(string $column, bool $descending = true) {
$this->orderDesc = $descending;
$this->orderBy = $column;
return $this;
}

Expand All @@ -48,9 +56,13 @@ public function getQuery():QueryModel {
if (!empty($this->where)) {
$sql .= " WHERE " . implode(' AND ', $this->where);
}
if ($this->orderBy !== "" && $this->orderDesc !== null) {
$sql .= " ORDER BY $this->orderBy " . ($this->orderDesc ? 'DESC' : 'ASC') . " ";
}
if ($this->limit) {
$sql .= " LIMIT ?, ?";
$sql .= " LIMIT $this->offset, $this->limit";
}
$sql .= ";";
$result = new QueryModel();
$result->sql = $sql;
$result->params = $this->params;
Expand Down
18 changes: 17 additions & 1 deletion src/Views/BaseView.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ abstract class BaseView
protected string $viewName;
protected string $subTitle;
protected string $subContent;
protected string $subScripts;
public LayoutViewModel $layoutData;
protected string $cssInclude;

protected abstract function render();

protected function renderLayout(LayoutViewModel $viewModel, IVIewModel|null $contentData): void
protected function renderLayout(LayoutViewModel $viewModel, IVIewModel|null $contentData, bool $hasJS = false): void
{
if(!isset($this->viewName))
{
Expand All @@ -38,11 +39,26 @@ protected function renderLayout(LayoutViewModel $viewModel, IVIewModel|null $con
$this->layoutData->isLoggedIn = false;
}

if($hasJS)
{
$this->subScripts = $this->loadJS($this->viewName);
}
else
{
$this->subScripts = "";
}
$this->subContent = $this->loadView($this->viewName, $contentData);
$this->cssInclude = $this->loadCSS($this->viewName, $contentData);
require "Layout/LayoutView.template.php";
}

protected function loadJS(string $viewFiles): string
{
ob_start();
include $viewFiles . ".js";
return ob_get_clean();
}

protected function loadView(string $viewFiles, IViewModel|null $pData): string
{
$data = $pData;
Expand Down
5 changes: 5 additions & 0 deletions src/Views/Layout/LayoutView.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>C218 - <?php echo $this->layoutData->pageSubTitle ?? "Home" ?></title>
<script type="text/javascript">
<?php
echo $this->subScripts;
?>
</script>
</head>
<body>
<style>
Expand Down
4 changes: 0 additions & 4 deletions src/Views/Manage/DVD/DVDView.style.css

This file was deleted.

3 changes: 0 additions & 3 deletions src/Views/Manage/DVD/DVDView.template.php

This file was deleted.

30 changes: 30 additions & 0 deletions src/Views/Manage/DVD/ManageDVDView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function orderBy(column) {
let url = new URL(window.location.href);
let orderDesc = url.searchParams.get('OrderDesc');
url.searchParams.set("OrderBy", column);
if (!orderDesc || orderDesc === "") {
url.searchParams.set("OrderDesc", "True");
} else if (orderDesc === "True") {
url.searchParams.set("OrderDesc", "False");
} else if (orderDesc === "False") {
url.searchParams.set("OrderDesc", "");
url.searchParams.set("OrderBy", "");
}

window.location.href = url.toString();
}

function search() {
let url = new URL(window.location.href);
const query = document.querySelector('#dvd-search-input').value.trim();
if (query) {
url.searchParams.set("Search", query);
}
window.location.href = url.toString();
}

function changePage(offset) {
let url = new URL(window.location.href);
url.searchParams.set("Offset", offset);
window.location.href = url.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Views\Manage\DVD
{

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

class DVDView extends BaseView
class ManageDVDView extends BaseView
{
private DVDViewModel $data;
private ManageDVDViewModel $data;

function __construct(DVDViewModel $viewModel)
function __construct(ManageDVDViewModel $viewModel)
{
$this->viewName="Manage/DVD/DVDView";
$this->viewName="Manage/DVD/ManageDVDView";
$this->subTitle="DVD";

$this->data = $viewModel;
Expand All @@ -24,7 +24,7 @@ protected function render(): void
{
$layoutData = new LayoutViewModel();
$layoutData -> pageSubTitle = $this->subTitle;
parent::renderLayout($layoutData, $this->data);
parent::renderLayout($layoutData, $this->data, true);
}
}
}
Expand Down
Loading

0 comments on commit b18e4b2

Please sign in to comment.