Skip to content

Commit

Permalink
Merge pull request #20 from CottaCush/features/report-pagination
Browse files Browse the repository at this point in the history
(Reports): Implement pagination on reports
  • Loading branch information
otaruMendez authored May 7, 2019
2 parents 4951484 + 57499ed commit da7de34
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -67,10 +68,13 @@ public function actionIndex()

/**
* @author Olawale Lawal <[email protected]>
* @author Taiwo Ladipo <[email protected]>
* @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);

Expand All @@ -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()]);
Expand All @@ -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
]);
}

Expand Down
3 changes: 3 additions & 0 deletions src/models/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,6 +21,7 @@
class Report extends BaseReportsModel implements CricketQueryableInterface
{

const PAGE_LIMIT = SQLGenerator::QUERY_LIMIT;
/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/views/default/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
38 changes: 33 additions & 5 deletions src/widgets/ReportTableWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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();
}
Expand All @@ -61,6 +64,7 @@ public function run()
}

$this->renderTable();
$this->renderPagination();
}

private function renderTable()
Expand Down Expand Up @@ -121,12 +125,36 @@ private function renderResultCount()

echo Html::tag(
'b',
$this->noOfRecords . ' record' . ($this->noOfRecords == 1 ? '' : 's') . ' returned'
$this->getDataSummary()
);

echo $this->endDiv();
}

private function getDataSummary()
{
$limit = Report::PAGE_LIMIT;
$msgTemplate = 'Showing %s &ndash; %s of %s items';
$msg = sprintf($msgTemplate, 1, $this->count, $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;
}

private function renderPagination()
{
echo LinkPager::widget([
'pagination' => new Pagination([
'totalCount' => $this->count,
'pageSize' => Report::PAGE_LIMIT
])
]);
}

/**
* @author Olawale Lawal <[email protected]>
*/
Expand Down Expand Up @@ -167,7 +195,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
]);
}
Expand Down

0 comments on commit da7de34

Please sign in to comment.