Skip to content

Commit cbadb92

Browse files
committed
Added admin grid & cronjob for category url key checker.
1 parent f3760ab commit cbadb92

File tree

10 files changed

+345
-1
lines changed

10 files changed

+345
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baldwin\UrlDataIntegrityChecker\Controller\Adminhtml\Catalog\Category\UrlKey;
6+
7+
use Magento\Backend\App\Action as BackendAction;
8+
use Magento\Backend\App\Action\Context as BackendContext;
9+
use Magento\Backend\Model\View\Result\Page as BackendResultPage;
10+
use Magento\Framework\View\Result\PageFactory as ResultPageFactory;
11+
12+
class Index extends BackendAction
13+
{
14+
const ADMIN_RESOURCE = 'Baldwin_UrlDataIntegrityChecker::catalog_data_integrity';
15+
16+
private $resultPageFactory;
17+
18+
public function __construct(
19+
BackendContext $context,
20+
ResultPageFactory $resultPageFactory
21+
) {
22+
parent::__construct($context);
23+
24+
$this->resultPageFactory = $resultPageFactory;
25+
}
26+
27+
public function execute()
28+
{
29+
/** @var BackendResultPage */
30+
$resultPage = $this->resultPageFactory->create();
31+
$resultPage->setActiveMenu('Baldwin_UrlDataIntegrityChecker::catalog_category_urlkey');
32+
$resultPage->getConfig()->getTitle()->prepend('Data Integrity - Category Url Key');
33+
34+
return $resultPage;
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baldwin\UrlDataIntegrityChecker\Controller\Adminhtml\Catalog\Category\UrlKey;
6+
7+
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlKey as UrlKeyChecker;
8+
use Baldwin\UrlDataIntegrityChecker\Cron\CheckCategoryUrlKey as CheckCategoryUrlKeyCron;
9+
use Baldwin\UrlDataIntegrityChecker\Cron\ScheduleJob;
10+
use Baldwin\UrlDataIntegrityChecker\Exception\AlreadyRefreshingException;
11+
use Baldwin\UrlDataIntegrityChecker\Storage\Meta as MetaStorage;
12+
use Magento\Backend\App\Action as BackendAction;
13+
use Magento\Backend\App\Action\Context as BackendContext;
14+
15+
class Refresh extends BackendAction
16+
{
17+
const ADMIN_RESOURCE = 'Baldwin_UrlDataIntegrityChecker::catalog_data_integrity';
18+
19+
private $scheduleJob;
20+
private $metaStorage;
21+
22+
public function __construct(
23+
BackendContext $context,
24+
ScheduleJob $scheduleJob,
25+
MetaStorage $metaStorage
26+
) {
27+
parent::__construct($context);
28+
29+
$this->scheduleJob = $scheduleJob;
30+
$this->metaStorage = $metaStorage;
31+
}
32+
33+
public function execute()
34+
{
35+
$scheduled = $this->scheduleJob->schedule(CheckCategoryUrlKeyCron::JOB_NAME);
36+
37+
if ($scheduled) {
38+
$this->getMessageManager()->addSuccess(
39+
(string) __(
40+
'The refresh job was scheduled, please check back in a few moments to see the updated results'
41+
)
42+
);
43+
44+
try {
45+
$storageIdentifier = UrlKeyChecker::STORAGE_IDENTIFIER;
46+
$this->metaStorage->setPending($storageIdentifier, MetaStorage::INITIATOR_CRON);
47+
} catch (AlreadyRefreshingException $ex) {
48+
$this->getMessageManager()->addError($ex->getMessage());
49+
}
50+
} else {
51+
$this->getMessageManager()->addError(
52+
(string) __('Couldn\'t schedule refreshing due to some unknown error')
53+
);
54+
}
55+
56+
$redirect = $this->resultRedirectFactory->create();
57+
$redirect->setRefererUrl();
58+
59+
return $redirect;
60+
}
61+
}

Cron/CheckCategoryUrlKey.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baldwin\UrlDataIntegrityChecker\Cron;
6+
7+
use Baldwin\UrlDataIntegrityChecker\Storage\Meta as MetaStorage;
8+
use Baldwin\UrlDataIntegrityChecker\Updater\Catalog\Category\UrlKey as UrlKeyUpdater;
9+
10+
class CheckCategoryUrlKey
11+
{
12+
const JOB_NAME = 'baldwin_urldataintegritychecker_cron_checkcategoryurlkey';
13+
14+
private $urlKeyUpdater;
15+
16+
public function __construct(
17+
UrlKeyUpdater $urlKeyUpdater
18+
) {
19+
$this->urlKeyUpdater = $urlKeyUpdater;
20+
}
21+
22+
public function execute()
23+
{
24+
$this->urlKeyUpdater->refresh(MetaStorage::INITIATOR_CRON);
25+
}
26+
}

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ checkquality:
2525
xmllint --noout etc/di.xml # schema validation doesn't work here since the xsd includes another xsd ..
2626
xmllint --noout --schema vendor/magento/framework/Module/etc/module.xsd etc/module.xml
2727

28+
xmllint --noout view/adminhtml/layout/baldwin_urldataintegritychecker_catalog_category_urlkey_index.xml # schema validation doesn't work here since the xsd includes another xsd ..
2829
xmllint --noout view/adminhtml/layout/baldwin_urldataintegritychecker_catalog_category_urlpath_index.xml # schema validation doesn't work here since the xsd includes another xsd ..
2930
xmllint --noout view/adminhtml/layout/baldwin_urldataintegritychecker_catalog_product_urlkey_index.xml # schema validation doesn't work here since the xsd includes another xsd ..
3031
xmllint --noout view/adminhtml/layout/baldwin_urldataintegritychecker_catalog_product_urlpath_index.xml # schema validation doesn't work here since the xsd includes another xsd ..
32+
xmllint --noout view/adminhtml/ui_component/baldwin_urldataintegritychecker_grid_catalog_category_urlkey.xml # schema validation doesn't work here since the xsd includes another xsd ..
3133
xmllint --noout view/adminhtml/ui_component/baldwin_urldataintegritychecker_grid_catalog_category_urlpath.xml # schema validation doesn't work here since the xsd includes another xsd ..
3234
xmllint --noout view/adminhtml/ui_component/baldwin_urldataintegritychecker_grid_catalog_product_urlkey.xml # schema validation doesn't work here since the xsd includes another xsd ..
3335
xmllint --noout view/adminhtml/ui_component/baldwin_urldataintegritychecker_grid_catalog_product_urlpath.xml # schema validation doesn't work here since the xsd includes another xsd ..
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Baldwin\UrlDataIntegrityChecker\Model\ResourceModel\Catalog\Category;
6+
7+
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlKey as UrlKeyChecker;
8+
use Baldwin\UrlDataIntegrityChecker\Storage\StorageInterface;
9+
use Magento\Framework\Api\AttributeInterface;
10+
use Magento\Framework\Api\AttributeValue;
11+
use Magento\Framework\Api\Search\SearchResultInterface;
12+
use Magento\Framework\Api\SearchCriteriaInterface;
13+
use Magento\Framework\Data\Collection as DataCollection;
14+
use Magento\Framework\Data\Collection\EntityFactoryInterface;
15+
use Magento\Framework\DataObject;
16+
use Magento\Framework\Exception\LocalizedException;
17+
18+
class UrlKeyCollection extends DataCollection implements SearchResultInterface
19+
{
20+
private $storage;
21+
22+
public function __construct(
23+
EntityFactoryInterface $entityFactory,
24+
StorageInterface $storage
25+
) {
26+
parent::__construct($entityFactory);
27+
28+
$this->storage = $storage;
29+
}
30+
31+
/**
32+
* @param bool $printQuery
33+
* @param bool $logQuery
34+
*
35+
* @return UrlKeyCollection<DataObject>
36+
*/
37+
public function loadData($printQuery = false, $logQuery = false)
38+
{
39+
if (!$this->isLoaded()) {
40+
$urlKeys = $this->storage->read(UrlKeyChecker::STORAGE_IDENTIFIER);
41+
foreach ($urlKeys as $urlKey) {
42+
$this->addItem($this->createDataObject($urlKey));
43+
}
44+
45+
foreach ($this->_orders as $field => $direction) {
46+
usort($this->_items, function ($itemA, $itemB) use ($field, $direction) {
47+
$comparisonFieldA = $itemA->getData($field);
48+
$comparisonFieldB = $itemB->getData($field);
49+
50+
if ($direction === DataCollection::SORT_ORDER_ASC) {
51+
return $comparisonFieldA <=> $comparisonFieldB;
52+
} else {
53+
return $comparisonFieldB <=> $comparisonFieldA;
54+
}
55+
});
56+
57+
break; // breaking after using one entry of $this->_orders
58+
}
59+
60+
$this->_setIsLoaded();
61+
62+
// page the data, need to do this after setting the data as loaded,
63+
// otherwise the getCurPage would create a recursive problem
64+
$startIndex = ($this->getCurPage() - 1) * $this->getPageSize();
65+
$this->_items = array_slice($this->_items, $startIndex, $this->getPageSize());
66+
}
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* @param array<string, mixed> $arguments
73+
*/
74+
public function createDataObject(array $arguments = []): DataObject
75+
{
76+
$obj = $this->_entityFactory->create($this->_itemObjectClass, ['data' => $arguments]);
77+
78+
$attributes = [];
79+
foreach ($arguments as $key => $value) {
80+
$attribute = new AttributeValue([
81+
AttributeInterface::ATTRIBUTE_CODE => $key,
82+
AttributeInterface::VALUE => $value,
83+
]);
84+
85+
$attributes[] = $attribute;
86+
}
87+
$obj->setCustomAttributes($attributes);
88+
89+
return $obj;
90+
}
91+
92+
public function setItems(array $items = null)
93+
{
94+
throw new LocalizedException(__('Not implemented: setItems!'));
95+
}
96+
97+
public function getAggregations()
98+
{
99+
throw new LocalizedException(__('Not implemented: getAggregations!'));
100+
}
101+
102+
public function setAggregations($aggregations)
103+
{
104+
throw new LocalizedException(__('Not implemented: setAggregations!'));
105+
}
106+
107+
public function getSearchCriteria()
108+
{
109+
throw new LocalizedException(__('Not implemented: getSearchCriteria!'));
110+
}
111+
112+
public function setSearchCriteria(SearchCriteriaInterface $searchCriteria)
113+
{
114+
throw new LocalizedException(__('Not implemented: setSearchCriteria!'));
115+
}
116+
117+
public function getTotalCount()
118+
{
119+
return $this->getSize();
120+
}
121+
122+
public function setTotalCount($totalCount)
123+
{
124+
throw new LocalizedException(__('Not implemented: setTotalCount!'));
125+
}
126+
}

etc/adminhtml/menu.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
33
<menu>
4-
<add id="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity" title="Data Integrity Checker" translate="title" module="Baldwin_UrlDataIntegrityChecker" parent="Magento_Catalog::catalog" sortOrder="100" dependsOnModule="Magento_Catalog" resource="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity"/>
4+
<add id="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity" title="Data Integrity Checker" translate="title" module="Baldwin_UrlDataIntegrityChecker" parent="Magento_Catalog::catalog" sortOrder="100" dependsOnModule="Magento_Catalog" resource="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity"/>
55
<add id="Baldwin_UrlDataIntegrityChecker::catalog_category_urlpath" title="Category Url Path" translate="title" module="Baldwin_UrlDataIntegrityChecker" parent="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity" sortOrder="100" action="baldwin_urldataintegritychecker/catalog_category_urlPath" resource="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity"/>
6+
<add id="Baldwin_UrlDataIntegrityChecker::catalog_category_urlkey" title="Category Url Key" translate="title" module="Baldwin_UrlDataIntegrityChecker" parent="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity" sortOrder="110" action="baldwin_urldataintegritychecker/catalog_category_urlKey" resource="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity"/>
67
<add id="Baldwin_UrlDataIntegrityChecker::catalog_product_urlpath" title="Product Url Path" translate="title" module="Baldwin_UrlDataIntegrityChecker" parent="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity" sortOrder="200" action="baldwin_urldataintegritychecker/catalog_product_urlPath" resource="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity"/>
78
<add id="Baldwin_UrlDataIntegrityChecker::catalog_product_urlkey" title="Product Url Key" translate="title" module="Baldwin_UrlDataIntegrityChecker" parent="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity" sortOrder="210" action="baldwin_urldataintegritychecker/catalog_product_urlKey" resource="Baldwin_UrlDataIntegrityChecker::catalog_data_integrity"/>
89
</menu>

etc/crontab.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<job name="baldwin_urldataintegritychecker_cron_checkcategoryurlpath" instance="Baldwin\UrlDataIntegrityChecker\Cron\CheckCategoryUrlPath" method="execute">
55
<schedule>10 2 * * *</schedule>
66
</job>
7+
<job name="baldwin_urldataintegritychecker_cron_checkcategoryurlkey" instance="Baldwin\UrlDataIntegrityChecker\Cron\CheckCategoryUrlKey" method="execute">
8+
<schedule>15 2 * * *</schedule>
9+
</job>
710
<job name="baldwin_urldataintegritychecker_cron_checkproducturlpath" instance="Baldwin\UrlDataIntegrityChecker\Cron\CheckProductUrlPath" method="execute">
811
<schedule>20 2 * * *</schedule>
912
</job>

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
1616
<arguments>
1717
<argument name="collections" xsi:type="array">
18+
<item name="baldwin_urldataintegritychecker_grid_catalog_category_urlkey_data_source" xsi:type="string">Baldwin\UrlDataIntegrityChecker\Model\ResourceModel\Catalog\Category\UrlKeyCollection</item>
1819
<item name="baldwin_urldataintegritychecker_grid_catalog_category_urlpath_data_source" xsi:type="string">Baldwin\UrlDataIntegrityChecker\Model\ResourceModel\Catalog\Category\UrlPathCollection</item>
1920
<item name="baldwin_urldataintegritychecker_grid_catalog_product_urlkey_data_source" xsi:type="string">Baldwin\UrlDataIntegrityChecker\Model\ResourceModel\Catalog\Product\UrlKeyCollection</item>
2021
<item name="baldwin_urldataintegritychecker_grid_catalog_product_urlpath_data_source" xsi:type="string">Baldwin\UrlDataIntegrityChecker\Model\ResourceModel\Catalog\Product\UrlPathCollection</item>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
<body>
4+
<referenceContainer name="content">
5+
<uiComponent name="baldwin_urldataintegritychecker_grid_catalog_category_urlkey"/>
6+
</referenceContainer>
7+
<referenceContainer name="page.main.actions">
8+
<block class="Baldwin\UrlDataIntegrityChecker\Block\Adminhtml\Metadata" name="baldwin.urldataintegritychecker.block.adminhtml.metadata" template="Baldwin_UrlDataIntegrityChecker::metadata.phtml">
9+
<arguments>
10+
<argument name="storage_identifier" xsi:type="string">category-url-key</argument>
11+
</arguments>
12+
</block>
13+
</referenceContainer>
14+
</body>
15+
</page>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0"?>
2+
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
3+
<argument name="data" xsi:type="array">
4+
<item name="js_config" xsi:type="array">
5+
<item name="provider" xsi:type="string">baldwin_urldataintegritychecker_grid_catalog_category_urlkey.baldwin_urldataintegritychecker_grid_catalog_category_urlkey_data_source</item>
6+
<item name="deps" xsi:type="string">baldwin_urldataintegritychecker_grid_catalog_category_urlkey.baldwin_urldataintegritychecker_grid_catalog_category_urlkey_data_source</item>
7+
</item>
8+
<item name="spinner" xsi:type="string">columns</item>
9+
<item name="buttons" xsi:type="array">
10+
<item name="refresh" xsi:type="array">
11+
<item name="name" xsi:type="string">refresh</item>
12+
<item name="label" xsi:type="string" translate="true">Refresh</item>
13+
<item name="class" xsi:type="string">primary</item>
14+
<item name="url" xsi:type="string">*/*/refresh</item>
15+
</item>
16+
</item>
17+
</argument>
18+
<dataSource name="baldwin_urldataintegritychecker_grid_catalog_category_urlkey_data_source">
19+
<argument name="dataProvider" xsi:type="configurableObject">
20+
<argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
21+
<argument name="name" xsi:type="string">baldwin_urldataintegritychecker_grid_catalog_category_urlkey_data_source</argument>
22+
<argument name="primaryFieldName" xsi:type="string">just-by-specifying-some-attribute-which-doesnt-exists-works-here</argument>
23+
<argument name="requestFieldName" xsi:type="string">just-by-specifying-some-attribute-which-doesnt-exists-works-here</argument>
24+
<argument name="data" xsi:type="array">
25+
<item name="config" xsi:type="array">
26+
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
27+
<item name="update_url" xsi:type="url" path="mui/index/render"/>
28+
<item name="storageConfig" xsi:type="array">
29+
<item name="indexField" xsi:type="string">just-by-specifying-some-attribute-which-doesnt-exists-works-here</item>
30+
</item>
31+
</item>
32+
</argument>
33+
</argument>
34+
</dataSource>
35+
<listingToolbar name="listing_top">
36+
<paging name="listing_paging"/>
37+
</listingToolbar>
38+
<columns name="columns">
39+
<column name="catId">
40+
<argument name="data" xsi:type="array">
41+
<item name="config" xsi:type="array">
42+
<item name="filter" xsi:type="string">textRange</item>
43+
<item name="sorting" xsi:type="string">asc</item>
44+
<item name="label" xsi:type="string" translate="true">Category ID</item>
45+
</item>
46+
</argument>
47+
</column>
48+
<column name="name">
49+
<argument name="data" xsi:type="array">
50+
<item name="config" xsi:type="array">
51+
<item name="filter" xsi:type="string">text</item>
52+
<item name="label" xsi:type="string" translate="true">Name</item>
53+
</item>
54+
</argument>
55+
</column>
56+
<column name="storeId">
57+
<argument name="data" xsi:type="array">
58+
<item name="config" xsi:type="array">
59+
<item name="filter" xsi:type="string">text</item>
60+
<item name="label" xsi:type="string" translate="true">Store ID</item>
61+
</item>
62+
</argument>
63+
</column>
64+
<column name="problem">
65+
<argument name="data" xsi:type="array">
66+
<item name="config" xsi:type="array">
67+
<item name="filter" xsi:type="string">text</item>
68+
<item name="label" xsi:type="string" translate="true">Problem</item>
69+
</item>
70+
</argument>
71+
</column>
72+
</columns>
73+
</listing>

0 commit comments

Comments
 (0)