Skip to content

Commit f3eac91

Browse files
committed
OWN-160 Add grid to view rules
1 parent 3975c5c commit f3eac91

File tree

9 files changed

+259
-4
lines changed

9 files changed

+259
-4
lines changed

Controller/Adminhtml/Report/Listing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function execute()
1616
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
1717
$resultPage = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_PAGE);
1818
$resultPage->setActiveMenu(Cfg::MENU_CSP);
19-
$resultPage->getConfig()->getTitle()->prepend(__('CSP Report'));
19+
$resultPage->getConfig()->getTitle()->prepend(__('CSP Reports'));
2020
return $resultPage;
2121
}
2222

Controller/Adminhtml/Rule/Listing.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Authors: Alex Gusev <alex@flancer64.com>
4+
* Since: 2020
5+
*/
6+
7+
namespace Flancer32\Csp\Controller\Adminhtml\Rule;
8+
9+
use Flancer32\Csp\Config as Cfg;
10+
11+
class Listing
12+
extends \Magento\Backend\App\Action
13+
{
14+
public function execute()
15+
{
16+
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
17+
$resultPage = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_PAGE);
18+
$resultPage->setActiveMenu(Cfg::MENU_CSP);
19+
$resultPage->getConfig()->getTitle()->prepend(__('CSP Rules'));
20+
return $resultPage;
21+
}
22+
23+
}

Model/Config/Source/Report/Area.php renamed to Model/Config/Source/Area.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Since: 2020
55
*/
66

7-
namespace Flancer32\Csp\Model\Config\Source\Report;
7+
namespace Flancer32\Csp\Model\Config\Source;
88

99
class Area
1010
implements \Magento\Framework\Data\OptionSourceInterface

Model/Config/Source/Policy.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Authors: Alex Gusev <alex@flancer64.com>
4+
* Since: 2020
5+
*/
6+
7+
namespace Flancer32\Csp\Model\Config\Source;
8+
9+
class Policy
10+
implements \Magento\Framework\Data\OptionSourceInterface
11+
{
12+
private $cache;
13+
/** @var \Flancer32\Csp\Api\Repo\Dao\Type\Policy */
14+
private $daoPolicy;
15+
16+
public function __construct(
17+
\Flancer32\Csp\Api\Repo\Dao\Type\Policy $daoPolicy
18+
) {
19+
$this->daoPolicy = $daoPolicy;
20+
}
21+
22+
public function toOptionArray()
23+
{
24+
if (is_null($this->cache)) {
25+
$all = $this->daoPolicy->getSet();
26+
$this->cache = [];
27+
foreach ($all as $one) {
28+
$this->cache[] = ['value' => $one->getId(), 'label' => $one->getKey()];
29+
}
30+
usort($this->cache, function ($a, $b) {
31+
return $a['label'] > $b['label'];
32+
});
33+
}
34+
return $this->cache;
35+
}
36+
}

Ui/DataProvider/Report/Grid.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Grid
2222

2323
/** Entities are used in the query (table names w/o prefix) */
2424
const E_REPORT = \Flancer32\Csp\Api\Repo\Dao\Report::ENTITY_NAME;
25+
2526
/** @var \Flancer32\Base\App\Repo\Query\ClauseSet\Processor\AliasMapEntry[] */
2627
private $mapAliases;
2728

Ui/DataProvider/Rule/Grid.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Authors: Alex Gusev <alex@flancer64.com>
4+
* Since: 2020
5+
*/
6+
7+
namespace Flancer32\Csp\Ui\DataProvider\Rule;
8+
9+
use Flancer32\Base\App\Repo\Query\Expression as Expression;
10+
use Flancer32\Csp\Api\Repo\Data\Rule as ERule;
11+
12+
class Grid
13+
extends \Flancer32\Base\App\Repo\Query\Grid\Builder
14+
{
15+
/** Tables aliases for external usage ('camelCase' naming) */
16+
const AS_RULE = 'r';
17+
18+
/** Columns/expressions aliases for external usage ('CamelCase' naming) */
19+
const A_AREA = 'Area';
20+
const A_ENABLED = 'Enabled';
21+
const A_ID = 'Id';
22+
const A_POLICY_ID = 'PolicyId';
23+
const A_SOURCE = 'Source';
24+
25+
/** Entities are used in the query (table names w/o prefix) */
26+
const E_RULE = \Flancer32\Csp\Api\Repo\Dao\Rule::ENTITY_NAME;
27+
28+
/** @var \Flancer32\Base\App\Repo\Query\ClauseSet\Processor\AliasMapEntry[] */
29+
private $mapAliases;
30+
31+
public function getAliasMap()
32+
{
33+
if (is_null($this->mapAliases)) {
34+
$this->mapAliases = [
35+
self::A_AREA => $this->createAliasMapEntry(self::A_AREA, self::AS_RULE, ERule::ADMIN_AREA),
36+
self::A_ENABLED => $this->createAliasMapEntry(self::A_ENABLED, self::AS_RULE, ERule::ENABLED),
37+
self::A_ID => $this->createAliasMapEntry(self::A_ID, self::AS_RULE, ERule::ID),
38+
self::A_POLICY_ID => $this->createAliasMapEntry(self::A_POLICY_ID, self::AS_RULE, ERule::TYPE_ID),
39+
self::A_SOURCE => $this->createAliasMapEntry(self::A_SOURCE, self::AS_RULE, ERule::SOURCE)
40+
];
41+
}
42+
return $this->mapAliases;
43+
}
44+
45+
public function getCountQuery()
46+
{
47+
/* get query to select items */
48+
/** @var \Magento\Framework\DB\Select $result */
49+
$result = $this->getSelectQuery();
50+
/* ... then replace "columns" part with own expression */
51+
$value = 'COUNT(' . self::AS_RULE . '.' . ERule::ID . ')';
52+
53+
/**
54+
* See method \Magento\Framework\DB\Select\ColumnsRenderer::render:
55+
*/
56+
/**
57+
* if ($column instanceof \Zend_Db_Expr) {...}
58+
*/
59+
$exp = new Expression($value);
60+
/**
61+
* list($correlationName, $column, $alias) = $columnEntry;
62+
*/
63+
$entry = [null, $exp, null];
64+
$cols = [$entry];
65+
$result->setPart('columns', $cols);
66+
return $result;
67+
}
68+
69+
public function getSelectQuery()
70+
{
71+
$result = $this->conn->select();
72+
73+
/* FROM fl32_csp_rule */
74+
$tbl = $this->resource->getTableName(self::E_RULE);
75+
$cols = [
76+
self::A_ID => ERule::ID,
77+
self::A_AREA => ERule::ADMIN_AREA,
78+
self::A_ENABLED => ERule::ENABLED,
79+
self::A_POLICY_ID => ERule::TYPE_ID,
80+
self::A_SOURCE => ERule::SOURCE
81+
];
82+
$result->from([self::AS_RULE => $tbl], $cols);
83+
84+
return $result;
85+
}
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
4+
<body>
5+
<referenceContainer name="content">
6+
<uiComponent name="fl32csp_rule_grid"/>
7+
</referenceContainer>
8+
</body>
9+
</page>

view/adminhtml/ui_component/fl32csp_report_grid.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@
6565
</settings>
6666
</column>
6767

68-
6968
<column name="Area"
7069
component="Magento_Ui/js/grid/columns/select">
7170
<settings>
7271
<dataType>select</dataType>
7372
<filter>select</filter>
74-
<options class="Flancer32\Csp\Model\Config\Source\Report\Area"/>
73+
<options class="Flancer32\Csp\Model\Config\Source\Area"/>
7574
<sortable>true</sortable>
7675
<label translate="true">Area</label>
7776
</settings>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<listing
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
5+
6+
<!-- **************************************************** -->
7+
<!-- Configuration of the component's data provider. -->
8+
<!-- **************************************************** -->
9+
<argument name="data" xsi:type="array">
10+
<item name="js_config" xsi:type="array">
11+
<!-- First part (fl32csp_rule_grid.) should be the same as name of this XML file -->
12+
<item name="provider" xsi:type="string">fl32csp_rule_grid.ds_for_grid</item>
13+
<!-- Prevent JS error: "this.source is not a function at UiClass.exportSorting (column.js:187)" -->
14+
<item name="deps" xsi:type="string">fl32csp_rule_grid.ds_for_grid</item>
15+
</item>
16+
<!-- data loading indicator is bound to grid columns area (spinner will not disappear w/o it) -->
17+
<item name="spinner" xsi:type="string">columns_for_the_grid</item>
18+
</argument>
19+
20+
<!-- **************************************************** -->
21+
<!-- Data Source (is used by data provider). -->
22+
<!-- **************************************************** -->
23+
<dataSource name="ds_for_grid"
24+
component="Magento_Ui/js/grid/provider">
25+
<argument name="dataProvider" xsi:type="configurableObject">
26+
<!--https://magento.stackexchange.com/questions/248611/magento-2-grid-data-source-definition-in-compact-form -->
27+
<argument name="class" xsi:type="string">Flancer32\Base\App\Ui\DataProvider\Admin\Grid</argument>
28+
<argument name="name" xsi:type="string">ds_for_grid</argument>
29+
<argument name="qGrid" xsi:type="object">Flancer32\Csp\Ui\DataProvider\Rule\Grid</argument>
30+
</argument>
31+
</dataSource>
32+
33+
<listingToolbar name="listing_top">
34+
<argument name="data" xsi:type="array">
35+
<item name="config" xsi:type="array">
36+
<item name="sticky" xsi:type="boolean">false</item>
37+
</item>
38+
</argument>
39+
<bookmark name="bookmarks"/>
40+
<columnsControls name="columns_controls"/>
41+
<filters name="listing_filters"/>
42+
<paging name="listing_paging"/>
43+
</listingToolbar>
44+
45+
<columns name="columns_for_the_grid">
46+
47+
<column name="Id"
48+
component="Flancer32_Base/js/grid/column/integer">
49+
<settings>
50+
<dataType>number</dataType>
51+
<filter>text</filter>
52+
<label translate="true">ID</label>
53+
<sortable>true</sortable>
54+
<sorting>desc</sorting>
55+
</settings>
56+
</column>
57+
58+
<column name="Enabled"
59+
component="Magento_Ui/js/grid/columns/select">
60+
<settings>
61+
<dataType>select</dataType>
62+
<filter>select</filter>
63+
<options class="Magento\Config\Model\Config\Source\Yesno"/>
64+
<sortable>true</sortable>
65+
<label translate="true">Enabled</label>
66+
</settings>
67+
</column>
68+
69+
<column name="Area"
70+
component="Magento_Ui/js/grid/columns/select">
71+
<settings>
72+
<dataType>select</dataType>
73+
<filter>select</filter>
74+
<options class="Flancer32\Csp\Model\Config\Source\Area"/>
75+
<sortable>true</sortable>
76+
<label translate="true">Area</label>
77+
</settings>
78+
</column>
79+
80+
<column name="PolicyId"
81+
component="Magento_Ui/js/grid/columns/select">
82+
<settings>
83+
<dataType>select</dataType>
84+
<filter>select</filter>
85+
<options class="Flancer32\Csp\Model\Config\Source\Policy"/>
86+
<sortable>true</sortable>
87+
<label translate="true">Policy</label>
88+
</settings>
89+
</column>
90+
91+
<column name="Source">
92+
<settings>
93+
<dataType>date</dataType>
94+
<filter>text</filter>
95+
<label translate="true">Source</label>
96+
</settings>
97+
</column>
98+
99+
</columns>
100+
101+
</listing>

0 commit comments

Comments
 (0)