From f70f41c787e64d57860686a013dafaa54c456203 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sat, 17 Jun 2017 16:51:00 +0100 Subject: [PATCH] added pagination to claims explorer --- src/Controller/MainController.php | 26 +++++++++++++++++++++++++- src/Template/Main/claims.ctp | 15 +++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index 1e3938c..c7d5ffc 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -118,7 +118,26 @@ class MainController extends AppController { } if (!$id) { - $claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->order(['Claims.Created' => 'DESC'])->limit(96)->toArray(); + // paginate claims + $offset = 0; + $pageLimit = 96; + $page = intval($this->request->query('page')); + + $conn = ConnectionManager::get('default'); + $stmt = $conn->execute('SELECT COUNT(Id) AS Total FROM Claims'); + $count = $stmt->fetch(\PDO::FETCH_OBJ); + $numClaims = $count->Total; + + $numPages = ceil($numClaims / $pageLimit); + if ($page < 1) { + $page = 1; + } + if ($page > $numPages) { + $page = $numPages; + } + + $offset = ($page - 1) * $pageLimit; + $claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->order(['Claims.Created' => 'DESC'])->offset($offset)->limit($pageLimit)->toArray(); for ($i = 0; $i < count($claims); $i++) { if ($canConvert && $claims[$i]->Fee > 0 && $claims[$i]->FeeCurrency == 'USD') { $claims[$i]->Price = $claims[$i]->Fee / $priceInfo->price; @@ -134,6 +153,11 @@ class MainController extends AppController { } } } + + $this->set('pageLimit', $pageLimit); + $this->set('numPages', $numPages); + $this->set('numRecords', $numClaims); + $this->set('currentPage', $page); $this->set('claims', $claims); } else { $claim = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['ClaimId', 'Name']]])->where(['Claims.ClaimId' => $id])->first(); diff --git a/src/Template/Main/claims.ctp b/src/Template/Main/claims.ctp index 878d040..17f2e67 100644 --- a/src/Template/Main/claims.ctp +++ b/src/Template/Main/claims.ctp @@ -273,8 +273,17 @@ if (strlen(trim($desc)) == 0) {
- Name; if (isset($claim->Publisher->Name)) { @@ -373,4 +382,6 @@ if (strlen(trim($desc)) == 0) {
+element('pagination') ?> + \ No newline at end of file