Skip to content

Commit

Permalink
OWN-159 Add grid to view reports
Browse files Browse the repository at this point in the history
  • Loading branch information
flancer64 committed May 19, 2020
1 parent 6359f57 commit 841ae4d
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 19 deletions.
37 changes: 23 additions & 14 deletions App/Repo/Query/ClauseSet/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

namespace Flancer32\Base\App\Repo\Query\ClauseSet;

/**
* Apply set of clauses on query.
*/
Expand All @@ -25,14 +26,16 @@ public function __construct(
/**
* @param \Magento\Framework\DB\Select $query
* @param \Flancer32\Base\App\Repo\Data\ClauseSet $clauses
* @param array $aliasMap
* @param bool $filterOnly 'true' - apply only filter clauses (for totals)
*/
public function exec(
\Magento\Framework\DB\Select $query,
\Flancer32\Base\App\Repo\Data\ClauseSet $clauses,
$aliasMap = null,
$filterOnly = false
) {
$aliases = $this->mapAliases($query);
$aliases = is_null($aliasMap) ? $this->mapAliases($query) : $aliasMap;
$filter = $clauses->filter;
$order = $clauses->order;
$pagination = $clauses->pagination;
Expand All @@ -44,30 +47,36 @@ public function exec(
}

/**
* @param $query
* @param \Magento\Framework\DB\Select $query
* @return array
*/
private function mapAliases(\Magento\Framework\DB\Select $query)
{
$result = [];
$columns = $query->getPart(\Zend_Db_Select::COLUMNS);
foreach ($columns as $one) {
$table = $one[0];
$expression = $one[1];
$alias = $one[2];
$data = new \Flancer32\Base\App\Repo\Query\ClauseSet\Processor\AliasMapEntry();
$data->alias = $alias;
$data->expression = $expression;
$data->table = $table;
$result[$alias] = $data;
try {
$columns = $query->getPart(\Zend_Db_Select::COLUMNS);
foreach ($columns as $one) {
$table = $one[0];
$expression = $one[1];
$alias = $one[2];
$data = new \Flancer32\Base\App\Repo\Query\ClauseSet\Processor\AliasMapEntry();
$data->alias = $alias;
$data->expression = $expression;
$data->table = $table;
$result[$alias] = $data;
}
} catch (\Zend_Db_Select_Exception $e) {
// just stealth the exception
}
return $result;
}

private function processFilter($query, $filter, $aliases)
{
$where = $this->ownFilterParser->parse($filter, $aliases);
if ($where) $query->where($where);
if ($where) {
$query->where($where);
}
}

private function processOrder($query, $order, $aliases)
Expand Down Expand Up @@ -99,4 +108,4 @@ private function processPagination($query, $pagination)
}
}
}
}
}
9 changes: 8 additions & 1 deletion App/Repo/Query/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
interface Grid
{

/**
* Map SELECT aliases to "table.column" or "expression" to process UI clauses in .
*
* @return array
*/
public function getAliasMap();

/**
* Base query to select total count for the grid.
*
Expand All @@ -23,4 +30,4 @@ public function getCountQuery();
* @return \Magento\Framework\DB\Select
*/
public function getSelectQuery();
}
}
18 changes: 17 additions & 1 deletion App/Repo/Query/Grid/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ public function __construct(
$this->conn = $resource->getConnection();
}

}
/**
* Use in children to create mapping for aliases to table.column or expression.
*
* @param $alias
* @param $table
* @param $expression
* @return \Flancer32\Base\App\Repo\Query\ClauseSet\Processor\AliasMapEntry
*/
protected function createAliasMapEntry($alias, $table, $expression)
{
$result = new \Flancer32\Base\App\Repo\Query\ClauseSet\Processor\AliasMapEntry();
$result->alias = $alias;
$result->table = $table;
$result->expression = $expression;
return $result;
}
}
7 changes: 4 additions & 3 deletions App/Ui/DataProvider/Admin/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ public function getData()
$criteria = $this->getSearchCriteria();
$clauses = $this->adptClauseSet->getClauseSet($criteria);

$map = $this->qGrid->getAliasMap();
$qTotal = $this->qGrid->getCountQuery();
$this->procClauseSet->exec($qTotal, $clauses, true);
$this->procClauseSet->exec($qTotal, $clauses, $map, true);
$totals = $this->conn->fetchOne($qTotal);

$qItems = $this->qGrid->getSelectQuery();
$this->procClauseSet->exec($qItems, $clauses);
$this->procClauseSet->exec($qItems, $clauses, $map);
$items = $this->conn->fetchAll($qItems);
$result = [
'items' => $items,
'totalRecords' => $totals
];
return $result;
}
}
}
46 changes: 46 additions & 0 deletions view/adminhtml/web/js/grid/column/decimal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* UI component to render cell data as a decimal number.
*
* Based on "./magento/module-ui/view/base/web/js/grid/columns/column.js"
*/
define([
"Magento_Ui/js/grid/columns/column"
], function (Column) {
"use strict";
/**
* Formatting function.
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
*/
const language = 'en-US';
const fraction = 2;
const options = {
minimumFractionDigits: fraction,
maximumFractionDigits: fraction
};
const formatter = new Intl.NumberFormat(language, options);
const fnFormat = function (num) {
const value = Number(num);
const result = formatter.format(value);
return result;
}

/**
* Magento UI component based on 'Column':
*/
return Column.extend({
defaults: {
bodyTmpl: 'Flancer32_Base/grid/cells/number',
},

/**
* Override parent function to format numeric value.
*
* @param {Object} row
* @returns {String}
*/
getLabel: function (row) {
const num = Column.prototype.getLabel.apply(this, [row]);
return fnFormat(num);
}
});
});
18 changes: 18 additions & 0 deletions view/adminhtml/web/js/grid/column/integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* UI component to render cell data as an integer number.
*
* Based on "./magento/module-ui/view/base/web/js/grid/columns/column.js"
*/
define([
"Magento_Ui/js/grid/columns/column"
], function (Column) {
"use strict";
/**
* Magento UI component based on "Column":
*/
return Column.extend({
defaults: {
bodyTmpl: "Flancer32_Base/grid/cells/number",
}
});
});
59 changes: 59 additions & 0 deletions view/adminhtml/web/js/grid/column/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Base UI component to render row data as link to other adminhtml routes.
*/
define([
"Magento_Ui/js/grid/columns/column",
"mageUtils"
], function (Column, utils) {
"use strict";


/**
* Remove route part from BASE URL (http://.../admin/customer/ => http://.../admin).
*
* BASE_URL see at "./module-backend/view/adminhtml/templates/page/js/require_js.phtml"
*/
const ROOT_URL = function () {
const length = BASE_URL.length;
const lastCharOff = BASE_URL.substr(0, length - 1);
const lastSlashPos = lastCharOff.lastIndexOf('/');
const result = BASE_URL.substr(0, lastSlashPos);
return result;
}();

return Column.extend({
defaults: {
/**
* Replace idAttrName & route in children.
*/
/* name of the identification attribute */
idAttrName: "customerId",
/* route part to the page */
route: "/customer/index/edit/id/",
bodyTmpl: "ui/grid/cells/link"
},

/**
* Returns link to given record.
*
* @param {object} record - grid row data.
* @returns {string}
*/
getLink: function (record) {
const id = utils.nested(record, this.idAttrName);
const result = ROOT_URL + this.route + id;
return result;
},

/**
* Check if link attribute exists in record.
*
* @param {object} record - grid row data.
* @returns {boolean}
*/
isLink: function (record) {
const result = !!utils.nested(record, this.idAttrName);
return result;
}
});
});
4 changes: 4 additions & 0 deletions view/adminhtml/web/template/grid/cells/number.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- Template for grid cells that contain numeric info (right justified) -->
<div class="data-grid-cell-content">
<div style="float: right" text="$col.getLabel($row())"/>
</div>

0 comments on commit 841ae4d

Please sign in to comment.