Prechádzať zdrojové kódy

Add pagination to all pages

Alan Lam 3 rokov pred
rodič
commit
b63184a418

+ 13 - 3
controllers/arus.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
@@ -16,11 +16,21 @@ $nvp = [
 	"dts_deleted" => null
 ];
 
-$documents = Document::query($nvp, ["sort" => "id", "order" => "desc"]);
+$count = Document::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s ARU"), __("%s ARUs"));
+
+$documents = Document::query($nvp, [
+	"sort" => "id",
+	"order" => "desc",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 $core->addScript("/js/new-document-modal.js");
 
 View::include("header");
-View::include("arus", compact("documents"));
+View::include("arus", compact("documents", "pagination"));
 View::include("modals/new_document");
 View::include("footer");

+ 12 - 2
controllers/companies.php

@@ -5,8 +5,18 @@ namespace App;
 $core = Core::getInstance();
 $core->addBreadcrumb("/companies", __("Companies"));
 
-$companies = Company::getAll();
+$nvp = [];
+
+$count = Company::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s company"), __("%s companies"));
+
+$companies = Company::query($nvp, [
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("companies", compact("companies"));
+View::include("companies", compact("companies", "pagination"));
 View::include("footer");

+ 11 - 2
controllers/customers.php

@@ -5,8 +5,17 @@ namespace App;
 $core = Core::getInstance();
 $core->addBreadcrumb("/customers", __("Customers"));
 
-$customers = Customer::query([], ["sort" => "name"]);
+$count = Customer::count();
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s customer"), __("%s customers"));
+
+$customers = Customer::query([], [
+	"sort" => "name",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("customers", compact("customers"));
+View::include("customers", compact("customers", "pagination"));
 View::include("footer");

+ 13 - 3
controllers/goods_in.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
@@ -16,11 +16,21 @@ $nvp = [
 	"dts_deleted" => null
 ];
 
-$documents = Document::query($nvp, ["sort" => "id", "order" => "desc"]);
+$count = Document::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s Goods-In"), __("%s Goods-In"));
+
+$documents = Document::query($nvp, [
+	"sort" => "id",
+	"order" => "desc",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 $core->addScript("/js/new-document-modal.js");
 
 View::include("header");
-View::include("goods_in", compact("documents"));
+View::include("goods_in", compact("documents", "pagination"));
 View::include("modals/new_document");
 View::include("footer");

+ 10 - 3
controllers/locations.php

@@ -7,10 +7,17 @@ $core->addBreadcrumb("/locations", __("Locations"));
 
 $user = $core->getUser();
 
-$location_count = Location::count(["dts_deleted" => null]);
+$count = Location::count(["dts_deleted" => null]);
 
-$locations = Location::query([], ["sort" => "name"]);
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s location"), __("%s locations"));
+
+$locations = Location::query([], [
+	"sort" => "name",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("locations", compact("locations"));
+View::include("locations", compact("locations", "pagination"));
 View::include("footer");

+ 14 - 3
controllers/products.php

@@ -1,12 +1,23 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
 $core = Core::getInstance();
 $core->addBreadcrumb("/products", __("Products"));
 
-$products = Product::getAll();
+$nvp = [];
+
+$count = Product::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s product"), __("%s products"));
+
+$products = Product::query($nvp, [
+	"sort" => "id",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("products", compact("products"));
+View::include("products", compact("products", "pagination"));
 View::include("footer");

+ 13 - 3
controllers/sales.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
@@ -11,9 +11,19 @@ $core->addBreadcrumb("/sales", __("Sales"));
 
 $nvp = ["dts_deleted" => null];
 
-$sales = Sale::query($nvp, ["sort" => "dts_updated", "order" => "desc"]);
+$sale_count = Sale::count($nvp);
+
+$pagination = new Pagination($sale_count);
+$pagination->setTerms(__("%s sale"), __("%s sales"));
+
+$sales = Sale::query($nvp, [
+	"sort" => "dts_updated",
+	"order" => "desc",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("sales", compact("sales"));
+View::include("sales", compact("sales", "pagination"));
 View::include("modals/new_sale");
 View::include("footer");

+ 13 - 2
controllers/suppliers.php

@@ -5,8 +5,19 @@ namespace App;
 $core = Core::getInstance();
 $core->addBreadcrumb("/suppliers", __("Suppliers"));
 
-$suppliers = Supplier::query([], ["sort" => "name"]);
+$nvp = [];
+
+$count = Supplier::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s supplier"), __("%s suppliers"));
+
+$suppliers = Supplier::query([], [
+	"sort" => "name",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("suppliers", compact("suppliers"));
+View::include("suppliers", compact("suppliers", "pagination"));
 View::include("footer");

+ 12 - 2
controllers/tickets.php

@@ -54,9 +54,19 @@ $nvp = [
 	"dts_deleted" => null
 ];
 
-$tickets = Ticket::query($nvp, ["sort" => "dts_updated", "order" => "desc"]);
+$count = Ticket::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s ticket"), __("%s tickets"));
+
+$tickets = Ticket::query($nvp, [
+	"sort" => "dts_updated",
+	"order" => "desc",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("tickets", compact("tickets"));
+View::include("tickets", compact("tickets", "pagination"));
 View::include("modals/ticket");
 View::include("footer");

+ 13 - 2
controllers/users.php

@@ -6,8 +6,19 @@ namespace App;
 $core = Core::getInstance();
 $core->addBreadcrumb("/users", __("Users"));
 
-$users = User::getAll();
+$nvp = [];
+
+$count = User::count($nvp);
+
+$pagination = new Pagination($count);
+$pagination->setTerms(__("%s user"), __("%s users"));
+
+$users = User::query($nvp, [
+	"sort" => "id",
+	"offset" => $pagination->getOffset(),
+	"limit" => $pagination->getPageSize()
+]);
 
 View::include("header");
-View::include("users", compact("users"));
+View::include("users", compact("users", "pagination"));
 View::include("footer");

+ 21 - 2
engine/Pagination.php

@@ -24,6 +24,12 @@ class Pagination {
 		$this->itemCount = $item_count;
 
 		$this->setTerms(__("%s result"), __("%s results"));
+
+		$page = $_GET["page"] ?? null;
+		$page = (int) $page;
+		if($page > 0) {
+			$this->setPage($page);
+		}
 	}
 
 	public function setItemCount(int $count): bool {
@@ -128,7 +134,9 @@ class Pagination {
 		$output = "";
 
 		// Page count is 1 or less
-		if(!$this->requiresPagination()) return "";
+		if(!$this->requiresPagination()) {
+			//return "";
+		}
 
 		// Start building output
 
@@ -148,7 +156,7 @@ class Pagination {
 			return "<li class=\"{$item_class} active\"><span class=\"page-link\">{$this->getCurrentPage()}</span></li>";
 		};
 
-		$output .= "<ul class=\"{$container_class}\">";
+		$output .= "<ul class=\"{$container_class} mb-3\">";
 
 		if($this->hasPreviousPage()) {
 			$output .= $get_page_link_html($first_text, 1);
@@ -181,9 +189,16 @@ class Pagination {
 
 		$output .= "</ul>";
 
+
+		$output .= '<div class="mb-3 text-center">' . $this->getText() . ' / ' . $this->getPageText() . '</div>';
+
 		return $output;
 	}
 
+	public function render(): void {
+		echo $this->build();
+	}
+
 	public function addParameter(string $name, ?string $value) {
 		if(!$value) return;
 		$this->parameters[$name] = $value;
@@ -204,4 +219,8 @@ class Pagination {
 	public function setIsJsHandled(bool $js_handled): void {
 		$this->isJsHandled = $js_handled;
 	}
+
+	public function getPageSize(): int {
+		return $this->pageSize;
+	}
 }

+ 3 - 1
views/arus.php

@@ -6,6 +6,7 @@ use App\Enums\DocumentTypes;
 
 /**
  * @var Document[] $documents
+ * @var Pagination $pagination
  */
 
 ?>
@@ -33,7 +34,7 @@ if(empty($documents)) {
 }
 else {
 	?>
-	<table class="table table-bordered table-hover">
+	<table class="table table-bordered table-hover mb-3">
 		<thead>
 		<tr>
 			<th><?=htmlspecialchars(__("Number"))?></th>
@@ -94,4 +95,5 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }

+ 3 - 1
views/companies.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var Company[] $companies
+ * @var Pagination $pagination
  */
 
 ?>
@@ -30,7 +31,7 @@ if(empty($companies)) {
 }
 else {
 	?>
-	<table class="table table-bordered">
+	<table class="table table-bordered table-hover mb-3">
 		<thead>
 		<tr>
 			<th><?=htmlspecialchars(__("ID"))?></th>
@@ -59,5 +60,6 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }
 

+ 3 - 1
views/customers.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var Customer[] $customers
+ * @var Pagination $pagination
  */
 
 ?>
@@ -30,7 +31,7 @@ if(empty($customers)) {
 }
 else {
 	?>
-	<table class="table table-bordered">
+	<table class="table table-bordered table-hover mb-3">
 		<colgroup>
 			<col style="width: 20%"/>
 			<col style="width: 60%"/>
@@ -67,5 +68,6 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }
 

+ 3 - 1
views/goods_in.php

@@ -6,6 +6,7 @@ use App\Enums\DocumentTypes;
 
 /**
  * @var Document[] $documents
+ * @var Pagination $pagination
  */
 
 ?>
@@ -33,7 +34,7 @@ if(empty($documents)) {
 }
 else {
 	?>
-	<table class="table table-bordered table-hover">
+	<table class="table table-bordered table-hover mb-3">
 		<thead>
 		<tr>
 			<th><?=htmlspecialchars(__("Number"))?></th>
@@ -102,4 +103,5 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }

+ 3 - 1
views/locations.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var Location[] $locations
+ * @var Pagination $pagination
  */
 
 ?>
@@ -21,5 +22,6 @@ if(empty($locations)) {
 	<?php
 }
 else {
-	View::include("locations_table", compact("locations"));
+	View::include("locations_table", compact("locations", "pagination"));
+	$pagination->render();
 }

+ 1 - 1
views/locations_table.php

@@ -7,7 +7,7 @@ namespace App;
  */
 
 ?>
-<table class="table table-bordered">
+<table class="table table-bordered table-hover mb-3">
 	<colgroup>
 		<col style="width: 20%"/>
 		<col style="width: 30%"/>

+ 2 - 0
views/products.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var Product[] $products
+ * @var Pagination $pagination
  */
 
 ?>
@@ -30,4 +31,5 @@ if(empty($products)) {
 }
 else {
 	View::include("products_table", compact("products"));
+	$pagination->render();
 }

+ 1 - 1
views/products_table.php

@@ -7,7 +7,7 @@ namespace App;
  */
 
 ?>
-<table class="table table-bordered">
+<table class="table table-bordered table-hover mb-3">
 	<thead>
 	<tr>
 		<th><?=htmlspecialchars(__("ID"))?></th>

+ 3 - 1
views/sales.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var Sale[] $sales
+ * @var Pagination $pagination
  */
 
 ?>
@@ -31,7 +32,7 @@ if(empty($sales)) {
 }
 else {
 	?>
-	<table class="table table-bordered table-hover">
+	<table class="table table-bordered table-hover mb-3">
 		<thead>
 		<tr>
 			<th><?=htmlspecialchars(__("Number"))?></th>
@@ -69,4 +70,5 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }

+ 3 - 1
views/suppliers.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var Supplier[] $suppliers
+ * @var Pagination $pagination
  */
 
 ?>
@@ -30,7 +31,7 @@ if(empty($suppliers)) {
 }
 else {
 	?>
-	<table class="table table-bordered">
+	<table class="table table-bordered table-hover mb-3">
 		<colgroup>
 			<col style="width: 20%"/>
 			<col style="width: 60%"/>
@@ -67,5 +68,6 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }
 

+ 6 - 1
views/tickets.php

@@ -7,6 +7,7 @@ use App\Helpers\HtmlHelper;
 
 /**
  * @var Ticket[] $tickets
+ * @var Pagination $pagination
  */
 
 $core = Core::getInstance();
@@ -38,7 +39,7 @@ if(!$tickets) {
 else {
 	?>
 	<form method="post">
-		<table class="table table-bordered table-hover">
+		<table class="table table-bordered table-hover mb-3">
 			<colgroup>
 				<col style="width: 1%;" />
 				<col style="width: 1%;" />
@@ -89,6 +90,10 @@ else {
 			</tbody>
 		</table>
 
+		<?php
+		$pagination->render();
+		?>
+
 		<div class="card">
 			<div class="card-body">
 				<h5><?=htmlspecialchars(__("Batch Actions"))?></h5>

+ 3 - 1
views/users.php

@@ -4,6 +4,7 @@ namespace App;
 
 /**
  * @var User[] $users
+ * @var Pagination $pagination
  */
 
 ?>
@@ -30,7 +31,7 @@ if(empty($users)) {
 }
 else {
 	?>
-	<table class="table table-bordered">
+	<table class="table table-bordered table-hover mb-3">
 		<thead>
 		<tr>
 			<th><?=htmlspecialchars(__("ID"))?></th>
@@ -61,4 +62,5 @@ else {
 		</tbody>
 	</table>
 	<?php
+	$pagination->render();
 }