Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

column filters to allow datetime (and potentially more) #37

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Block/Adminhtml/Report/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ public function _prepareLayout()
$customReport = $this->currentCustomReportRegistry->get();
$genericCollection = $this->customReportManagement->getGenericReportCollection($customReport);
$columnList = $this->customReportManagement->getColumnsList($customReport);
$this->addColumnSet($columnList);
$columnTypes = $this->customReportManagement->getColumnTypes($customReport);
$this->addColumnSet($columnList, $columnTypes);
$this->addGridExportBlock();
$this->setCollection($genericCollection);
parent::_prepareLayout();
}

/**
* @param $columnList
* @param $columnTypes
*
* @return void
*/
public function addColumnSet($columnList)
public function addColumnSet($columnList, $columnTypes)
{
/** @var $columnSet \Magento\Backend\Block\Widget\Grid\ColumnSet * */
$columnSet = $this->_layout->createBlock(
Expand All @@ -69,14 +71,15 @@ public function addColumnSet($columnList)
if ($this->_defaultSort === false) {
$this->_defaultSort = $columnName;
}
/** @var $column \Magento\Backend\Block\Widget\Grid\Column * */
$type = (isset($columnTypes[$columnName]))?$columnTypes[$columnName]:'text';
$data = [
'data' => [
'header' => $columnName,
'index' => $columnName,
'type' => 'text',
'type' => $type,
],
];
/** @var $column \Magento\Backend\Block\Widget\Grid\Column * */
$column = $this->_layout->createBlock(
Column::class,
'deg_customreports_grid.grid.column.'.$columnName,
Expand Down
29 changes: 29 additions & 0 deletions Model/CustomReportManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DEG\CustomReports\Api\Data\CustomReportInterface;
use DEG\CustomReports\Api\CustomReportManagementInterface;
use Magento\Setup\Exception;
use Zend_Db_Expr;

class CustomReportManagement implements CustomReportManagementInterface
Expand Down Expand Up @@ -59,4 +60,32 @@ protected function formatSql(string $rawSql): string
{
return trim($rawSql, ';');
}

/**
* @param \DEG\CustomReports\Api\Data\CustomReportInterface $customReport
*
* @return array
*/
public function getColumnTypes(CustomReportInterface $customReport): array
{
$rawSql = $customReport->getReportSql();
$commentMatches = [];
preg_match_all('~/\*(.*?)\*/~s',$rawSql, $commentMatches);
$commentMatched = array_pop($commentMatches);
$commentsCleaned = array_map('trim', $commentMatched);
$result = [];
foreach($commentsCleaned as $comment) {
try {
$columnFilterTypes = explode(',', $comment);
foreach ($columnFilterTypes as $type) {
[$column, $typeFilter] = explode(':', $type);
$result[trim($column)] = trim($typeFilter);
}
} catch (\Exception $e) {

}
}
return $result;
}

}
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ crontab/default/jobs/automated_export_1/name = automated_export_1

The popular third-party Magento tool, n98-magerun, can be used to run the automated exports manually from the command line using the above name, e.g. 'n98-magerun sys:cron:run automated_export_1'.

## Define column filters

Note: Only tested with datetime / date

Place a comment block after your SQL, with format: column_name:type

example:
```mysql
select entity_id, state, status, created_at, updated_at from sales_order order by entity_id desc limit 500

/* created_at:datetime, updated_at:datetime */
```