From f154c4848a7d713222e38163c4876d88274bd698 Mon Sep 17 00:00:00 2001 From: Taiwo Ladipo Date: Tue, 7 May 2019 13:35:13 +0100 Subject: [PATCH 1/2] (Reports): Implement pagination on reports --- composer.lock | 8 +++--- src/controllers/DefaultController.php | 21 ++++++++++++-- src/models/Report.php | 3 ++ src/views/default/view.php | 2 +- src/widgets/ReportTableWidget.php | 40 +++++++++++++++++++++++---- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/composer.lock b/composer.lock index 91e40e6..48fc30a 100644 --- a/composer.lock +++ b/composer.lock @@ -370,16 +370,16 @@ }, { "name": "cottacush/cricket-base", - "version": "v0.9.2", + "version": "v0.10.0", "source": { "type": "git", "url": "https://github.com/CottaCush/cricket-base.git", - "reference": "cdb3488d357b494a48a06c0b8912415679b782eb" + "reference": "391e041042980ed9b7bf1b9f1ba23af3348d2843" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/CottaCush/cricket-base/zipball/cdb3488d357b494a48a06c0b8912415679b782eb", - "reference": "cdb3488d357b494a48a06c0b8912415679b782eb", + "url": "https://api.github.com/repos/CottaCush/cricket-base/zipball/391e041042980ed9b7bf1b9f1ba23af3348d2843", + "reference": "391e041042980ed9b7bf1b9f1ba23af3348d2843", "shasum": "" }, "require": { diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index 15e99ba..698f8eb 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -19,6 +19,7 @@ class DefaultController extends BaseReportsController { const SQL_QUERY_KEY = '_sql_query_'; + const SQL_RESULT_COUNT_KEY = '_sql_result_count_'; /** * @author Taiwo Ladipo @@ -67,10 +68,13 @@ public function actionIndex() /** * @author Olawale Lawal + * @author Taiwo Ladipo * @param null $id + * @param null $page * @return string|\yii\web\Response + * @throws \CottaCush\Cricket\Exceptions\SQLQueryGenerationException */ - public function actionView($id = null) + public function actionView($id = null, $page = null) { $reportId = Utils::decodeId($id); @@ -85,10 +89,21 @@ public function actionView($id = null) } $data = []; + $paginationExtras = ['paginate' => true]; $placeholderValues = $this->getRequest()->post(); try { $parser = new SQLQueryBuilderParser(); + if ($page) { + $paginationExtras['page'] = $page; + $paginationExtras['query_string'] = $this->getSession()->get(self::SQL_QUERY_KEY . $id); + $count = $this->getSession()->get(self::SQL_RESULT_COUNT_KEY . $id); + } + $data = $parser->parse($report, $placeholderValues, null, null, $paginationExtras); + if (!$page) { + $count = ArrayHelper::getValue($data, 'count'); + $this->getSession()->set(self::SQL_RESULT_COUNT_KEY . $id, $count); + } $data = $parser->parse($report, $placeholderValues); } catch (SQLReportGenerationException $ex) { return $this->render('error', ['report' => $report, 'details' => $ex->getMessage()]); @@ -98,11 +113,13 @@ public function actionView($id = null) return $this->render('view', [ 'report' => $report, - 'data' => $data, + 'data' => ArrayHelper::getValue($data, 'data'), 'hasPlaceholders' => $parser->hasInputPlaceholders(), 'encodedId' => $id, 'hasPlaceholdersReplaced' => $parser->arePlaceholdersReplaced(), 'values' => $placeholderValues, + 'count' => $count, + 'page' => $page ]); } diff --git a/src/models/Report.php b/src/models/Report.php index 1167475..3570f56 100644 --- a/src/models/Report.php +++ b/src/models/Report.php @@ -2,7 +2,9 @@ namespace CottaCush\Cricket\Report\Models; +use CottaCush\Cricket\Generators\SQL\SQLGenerator; use CottaCush\Cricket\Interfaces\CricketQueryableInterface; +use CottaCush\Cricket\Models\Query; use CottaCush\Cricket\Report\Libs\Utils; /** @@ -19,6 +21,7 @@ class Report extends BaseReportsModel implements CricketQueryableInterface { + const PAGE_LIMIT = SQLGenerator::QUERY_LIMIT; /** * {@inheritdoc} */ diff --git a/src/views/default/view.php b/src/views/default/view.php index 0b7e890..238564f 100644 --- a/src/views/default/view.php +++ b/src/views/default/view.php @@ -31,7 +31,7 @@ } else { echo ReportTableWidget::widget([ 'data' => $data, 'report' => $report, 'hasPlaceholders' => $hasPlaceholders, 'placeholderValues' => $values, - 'excludeBootstrapAssets' => $excludeBootstrapAssets + 'excludeBootstrapAssets' => $excludeBootstrapAssets, 'count' => $count, 'page' => $page ]); } echo Html::endTag('div'); diff --git a/src/widgets/ReportTableWidget.php b/src/widgets/ReportTableWidget.php index 8a1f18b..9d383b5 100644 --- a/src/widgets/ReportTableWidget.php +++ b/src/widgets/ReportTableWidget.php @@ -4,10 +4,13 @@ use CottaCush\Cricket\Interfaces\CricketQueryableInterface; use CottaCush\Cricket\Report\Libs\Utils; +use CottaCush\Cricket\Report\Models\Report; use CottaCush\Yii2\Helpers\Html; use CottaCush\Yii2\Widgets\EmptyStateWidget; +use yii\data\Pagination; use yii\helpers\ArrayHelper; use yii\helpers\Url; +use yii\widgets\LinkPager; /** * Class ReportTableWidget @@ -24,7 +27,8 @@ class ReportTableWidget extends BaseReportsWidget public $tableClasses = 'table table-striped table-bordered'; public $emptyResultMsg = 'The query returned an empty data set'; - private $noOfRecords; + public $count; + public $page = null; public $hasPlaceholders = false; private $hasResults; @@ -37,8 +41,7 @@ class ReportTableWidget extends BaseReportsWidget public function init() { - $this->noOfRecords = count($this->data); - $this->hasResults = (bool)$this->noOfRecords; + $this->hasResults = (bool)$this->count; parent::init(); } @@ -61,6 +64,7 @@ public function run() } $this->renderTable(); + $this->renderPagination(); } private function renderTable() @@ -121,12 +125,38 @@ private function renderResultCount() echo Html::tag( 'b', - $this->noOfRecords . ' record' . ($this->noOfRecords == 1 ? '' : 's') . ' returned' + $this->getDataCountInfo() ); echo $this->endDiv(); } + private function getDataCountInfo() + { + $limit = Report::PAGE_LIMIT; + $msgTemplate = 'Showing %s – %s of %s items'; + $msg = sprintf($msgTemplate, 1, $this->count, $this->count); + if ($this->count > $limit) { + if ($this->page) { + $start = ($this->page - 1) * $limit + 1; + $end = ($this->page * $limit); + $pageTotal = ($end <= $this->count) ? $end : $this->count; + $msg = sprintf($msgTemplate, $start, $pageTotal, $this->count); + } + } + return $msg; + } + + private function renderPagination() + { + echo LinkPager::widget([ + 'pagination' => new Pagination([ + 'totalCount' => $this->count, + 'pageSize' => Report::PAGE_LIMIT + ]) + ]); + } + /** * @author Olawale Lawal */ @@ -167,7 +197,7 @@ public function renderButtons() if ($this->hasPlaceholders) { echo SQLReportFilterModalWidget::widget([ 'id' => $this->editFilterModalId, 'model' => $this->report, 'data' => $this->placeholderValues, - 'route' => Url::current(), 'database' => $this->database, + 'route' => Url::current(['page' => null]), 'database' => $this->database, 'excludeBootstrapAssets' => $this->excludeBootstrapAssets ]); } From 57499ed5db773fb906850e4f723f97ab5bedd19e Mon Sep 17 00:00:00 2001 From: Taiwo Ladipo Date: Tue, 7 May 2019 14:25:18 +0100 Subject: [PATCH 2/2] Update data summary condition --- src/widgets/ReportTableWidget.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/widgets/ReportTableWidget.php b/src/widgets/ReportTableWidget.php index 9d383b5..a518168 100644 --- a/src/widgets/ReportTableWidget.php +++ b/src/widgets/ReportTableWidget.php @@ -125,24 +125,22 @@ private function renderResultCount() echo Html::tag( 'b', - $this->getDataCountInfo() + $this->getDataSummary() ); echo $this->endDiv(); } - private function getDataCountInfo() + private function getDataSummary() { $limit = Report::PAGE_LIMIT; $msgTemplate = 'Showing %s – %s of %s items'; $msg = sprintf($msgTemplate, 1, $this->count, $this->count); - if ($this->count > $limit) { - if ($this->page) { - $start = ($this->page - 1) * $limit + 1; - $end = ($this->page * $limit); - $pageTotal = ($end <= $this->count) ? $end : $this->count; - $msg = sprintf($msgTemplate, $start, $pageTotal, $this->count); - } + if ($this->count > $limit && $this->page) { + $start = ($this->page - 1) * $limit + 1; + $end = ($this->page * $limit); + $pageTotal = ($end <= $this->count) ? $end : $this->count; + $msg = sprintf($msgTemplate, $start, $pageTotal, $this->count); } return $msg; }