Skip to content

Commit d67befa

Browse files
dimadidrDima Dromovbgiamarinotoddbc
authored
Add stores to catalog diagnostics (#193)
* Add stores to catalog diagnostics * Define getStores function as private * Update js render in template * Put js to separate file * Format js file * Fix some static test errors. * Indent within if to cleanup formatting. No code changes otherwise. * Correct escaping of translated strings. --------- Co-authored-by: Dima Dromov <[email protected]> Co-authored-by: Ben Giamarino <[email protected]> Co-authored-by: Ben Giamarino <[email protected]> Co-authored-by: Todd Christensen <[email protected]>
1 parent 98958df commit d67befa

File tree

4 files changed

+152
-87
lines changed

4 files changed

+152
-87
lines changed

app/code/Meta/Catalog/Block/Adminhtml/Diagnostics.php

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Meta\Catalog\Block\Adminhtml;
1919

2020
use Exception;
21+
use Magento\Store\Api\Data\StoreInterface;
2122
use Meta\BusinessExtension\Helper\FBEHelper;
2223
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2324
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
@@ -27,6 +28,7 @@
2728
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
2829
use Magento\Backend\Block\Template;
2930
use Psr\Log\LoggerInterface;
31+
use Magento\Store\Api\StoreRepositoryInterface;
3032

3133
/**
3234
* @api
@@ -38,16 +40,6 @@ class Diagnostics extends Template
3840
*/
3941
private $fbeHelper;
4042

41-
/**
42-
* @var int
43-
*/
44-
private $storeId;
45-
46-
/**
47-
* @var mixed|null
48-
*/
49-
private $catalogId;
50-
5143
/**
5244
* @var SystemConfig
5345
*/
@@ -69,12 +61,20 @@ class Diagnostics extends Template
6961
private $productCollectionFactory;
7062

7163
/**
64+
* @var StoreRepositoryInterface
65+
*/
66+
public $storeRepo;
67+
68+
/**
69+
* Construct
70+
*
7271
* @param Context $context
7372
* @param SystemConfig $systemConfig
7473
* @param GraphAPIAdapter $graphApiAdapter
7574
* @param FBEHelper $fbeHelper
7675
* @param LoggerInterface $logger
7776
* @param CollectionFactory $productCollectionFactory
77+
* @param StoreRepositoryInterface $storeRepo
7878
* @param array $data
7979
*/
8080
public function __construct(
@@ -84,44 +84,57 @@ public function __construct(
8484
FBEHelper $fbeHelper,
8585
LoggerInterface $logger,
8686
CollectionFactory $productCollectionFactory,
87+
StoreRepositoryInterface $storeRepo,
8788
array $data = []
8889
) {
8990
$this->systemConfig = $systemConfig;
9091
$this->graphApiAdapter = $graphApiAdapter;
9192
$this->fbeHelper = $fbeHelper;
92-
$this->storeId = $this->fbeHelper->getStore()->getId();
93-
$this->catalogId = $this->systemConfig->getCatalogId($this->storeId);
9493
$this->logger = $logger;
9594
$this->productCollectionFactory = $productCollectionFactory;
95+
$this->storeRepo = $storeRepo;
9696
parent::__construct($context, $data);
9797
}
9898

9999
/**
100-
* Get report
100+
* Get reports
101101
*
102102
* @return array
103103
*/
104-
public function getReport()
104+
public function getReports()
105105
{
106-
$report = [];
106+
$reports = [];
107+
$stores = $this->getStores();
107108
try {
108-
$response = $this->graphApiAdapter->getCatalogDiagnostics($this->catalogId);
109-
if (isset($response['diagnostics']['data'])) {
110-
$report = $response['diagnostics']['data'];
109+
foreach ($stores as $key => $store) {
110+
if ($key === 'admin') {
111+
continue;
112+
}
113+
$catalogId = $this->systemConfig->getCatalogId($store->getId());
114+
$response = $this->graphApiAdapter->getCatalogDiagnostics($catalogId);
115+
if (isset($response['diagnostics']['data'])) {
116+
$reports[$key] = [];
117+
$reports[$key]['data'] = $response['diagnostics']['data'];
118+
$reports[$key]['catalog_id'] = $catalogId;
119+
$reports[$key]['store_id'] = $store->getId();
120+
$reports[$key]['store_name'] = $store->getName();
121+
}
111122
}
112123
} catch (Exception $e) {
113124
$this->logger->critical($e->getMessage());
114125
}
115-
return $report;
126+
return $reports;
116127
}
117128

118129
/**
119130
* Get sample affected items
120131
*
121132
* @param array $diagnosticItem
133+
* @param int $catalogId
134+
* @param int $storeId
122135
* @return array
123136
*/
124-
public function getSampleAffectedItems(array $diagnosticItem)
137+
public function getSampleAffectedItems(array $diagnosticItem, int $catalogId, int $storeId)
125138
{
126139
if (!array_key_exists('sample_affected_items', $diagnosticItem)) {
127140
return [];
@@ -132,12 +145,12 @@ public function getSampleAffectedItems(array $diagnosticItem)
132145
return $a['id'];
133146
}, $diagnosticItem['sample_affected_items']);
134147

135-
$fbProducts = $this->graphApiAdapter->getProductsByFacebookProductIds($this->catalogId, $fbIds);
148+
$fbProducts = $this->graphApiAdapter->getProductsByFacebookProductIds($catalogId, $fbIds);
136149
$retailerIds = array_map(function ($a) {
137150
return $a['retailer_id'];
138151
}, $fbProducts['data']);
139152

140-
return $this->getProducts($retailerIds);
153+
return $this->getProducts($retailerIds, $storeId);
141154
} catch (Exception $e) {
142155
$this->logger->critical($e->getMessage());
143156
}
@@ -149,13 +162,14 @@ public function getSampleAffectedItems(array $diagnosticItem)
149162
* Get admin url
150163
*
151164
* @param ProductInterface $product
165+
* @param int $store
152166
* @return string
153167
*/
154-
public function getAdminUrl(ProductInterface $product)
168+
public function getAdminUrl(ProductInterface $product, int $store = null)
155169
{
156170
$params = ['id' => $product->getId()];
157-
if ($this->getRequest()->getParam('store')) {
158-
$params['store'] = $this->getRequest()->getParam('store');
171+
if ($store) {
172+
$params['store'] = $store;
159173
}
160174
return $this->getUrl('catalog/product/edit', $params);
161175
}
@@ -164,16 +178,17 @@ public function getAdminUrl(ProductInterface $product)
164178
* Get products
165179
*
166180
* @param array $retailerIds
181+
* @param int $storeId
167182
* @return array
168183
*/
169-
private function getProducts(array $retailerIds)
184+
private function getProducts(array $retailerIds, int $storeId)
170185
{
171186
$collection = $this->productCollectionFactory->create();
172187
$collection->addAttributeToSelect('*')
173-
->addStoreFilter($this->storeId)
174-
->setStoreId($this->storeId);
188+
->addStoreFilter($storeId)
189+
->setStoreId($storeId);
175190

176-
$productIdentifierAttr = $this->systemConfig->getProductIdentifierAttr($this->storeId);
191+
$productIdentifierAttr = $this->systemConfig->getProductIdentifierAttr($storeId);
177192
if ($productIdentifierAttr === IdentifierConfig::PRODUCT_IDENTIFIER_SKU) {
178193
$collection->addAttributeToFilter('sku', ['in' => $retailerIds]);
179194
} elseif ($productIdentifierAttr === IdentifierConfig::PRODUCT_IDENTIFIER_ID) {
@@ -184,4 +199,14 @@ private function getProducts(array $retailerIds)
184199

185200
return $collection->getItems();
186201
}
202+
203+
/**
204+
* Get stores
205+
*
206+
* @return StoreInterface[]
207+
*/
208+
private function getStores()
209+
{
210+
return $this->storeRepo->getList();
211+
}
187212
}

app/code/Meta/Catalog/view/adminhtml/layout/fbeadmin_diagnostic_index.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Catalog Diagnostics
77
</title>
88
<css src="Meta_BusinessExtension::css/fbe.css"/>
9+
<link src="Meta_Catalog::js/diagnostics.js"/>
910
</head>
1011
<body>
1112
<referenceContainer name="content">
Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,89 @@
11
<?php
22
/** @var Meta\Catalog\Block\Adminhtml\Diagnostics $block */
33
/** @var Magento\Framework\Escaper $escaper */
4-
$report = $block->getReport();
4+
/** @var Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
5+
6+
$reports = $block->getReports();
57
?>
68

7-
<?php if (!empty($report)): ?>
8-
<style>
9-
#catalog-diagnostics-report th, td {
10-
border: 1px solid black;
11-
}
12-
#catalog-diagnostics-report th, td {
13-
padding: 15px;
14-
}
15-
.sample-affected-items {
16-
margin-bottom: 15px;
17-
}
18-
.sample-affected-items ul {
19-
margin-left: 15px;
20-
}
21-
.diagnostic-description {
22-
padding-left: 25px;
23-
}
24-
</style>
25-
<table id="catalog-diagnostics-report">
26-
<tr>
27-
<th><?= $escaper->escapeHtml(__('Title')) ?></th>
28-
<th><?= $escaper->escapeHtml(__('Severity')) ?></th>
29-
<th><?= $escaper->escapeHtml(__('Type')) ?></th>
30-
<th><?= $escaper->escapeHtml(__('Diagnostics Description')) ?></th>
31-
</tr>
9+
<?php if (!empty($reports)): ?>
10+
<style>
11+
#catalog-diagnostics-report th, td {
12+
border: 1px solid black;
13+
}
14+
#catalog-diagnostics-report th, td {
15+
padding: 15px;
16+
}
17+
.sample-affected-items {
18+
margin-bottom: 15px;
19+
}
20+
.sample-affected-items ul {
21+
margin-left: 15px;
22+
}
23+
.diagnostic-description {
24+
padding-left: 25px;
25+
}
26+
.store {
27+
margin-bottom: 15px;
28+
}
29+
.store-info {
30+
display: none;
31+
}
32+
</style>
33+
<select class="store">
34+
<?php foreach ($reports as $report): ?>
35+
<option value="<?= $escaper->escapeHtmlAttr($report['store_id']) ?>">
36+
<?= $escaper->escapeHtml(__($report['store_name'])) ?>
37+
</option>
38+
<?php endforeach; ?>
39+
</select>
40+
41+
<?php foreach ($reports as $report): ?>
42+
<?php $catalog = $report['catalog_id']; ?>
43+
<?php $storeId = $report['store_id']; ?>
44+
<table class="store-info store-<?= $escaper->escapeHtmlAttr($report['store_id']) ?>">
45+
<tr>
46+
<th><?= $escaper->escapeHtml(__('Title')) ?></th>
47+
<th><?= $escaper->escapeHtml(__('Severity')) ?></th>
48+
<th><?= $escaper->escapeHtml(__('Type')) ?></th>
49+
<th><?= $escaper->escapeHtml(__('Diagnostics Description')) ?></th>
50+
</tr>
3251

33-
<?php foreach ($report as $item): ?>
34-
<tr>
35-
<td><?= $escaper->escapeHtml($item['title']) ?></td>
36-
<td><?= $escaper->escapeHtml($item['severity']) ?></td>
37-
<td><?= $escaper->escapeHtml($item['type']) ?></td>
38-
<td class="diagnostic-description">
39-
<?php $diagnostics = $item['diagnostics']; ?>
40-
<ul>
41-
<?php foreach ($diagnostics as $diagnostic): ?>
42-
<li><div><?= $escaper->escapeHtml($diagnostic['description']) ?></div>
43-
<?php $products = $block->getSampleAffectedItems($diagnostic); ?>
44-
<?php if ($products): ?>
45-
<div class="sample-affected-items">
46-
<p><?= $escaper->escapeHtml(__('Sample affected items:')) ?></p>
47-
<ul>
48-
<?php foreach ($products as $product): ?>
49-
<li>
50-
<?= $escaper->escapeHtml($product->getName()) ?>
51-
(<a href="<?= $escaper->escapeUrl($block->getAdminUrl($product)) ?>">
52-
<?= $escaper->escapeHtml($product->getSku()) ?>
53-
</a>)
54-
</li>
55-
<?php endforeach; ?>
56-
</ul>
57-
</div>
58-
<?php endif; ?>
59-
</li>
60-
<?php endforeach; ?>
61-
</ul>
62-
</td>
63-
</tr>
52+
<?php foreach ($report['data'] as $item): ?>
53+
<tr>
54+
<td><?= $escaper->escapeHtml($item['title']) ?></td>
55+
<td><?= $escaper->escapeHtml($item['severity']) ?></td>
56+
<td><?= $escaper->escapeHtml($item['type']) ?></td>
57+
<td class="diagnostic-description">
58+
<?php $diagnostics = $item['diagnostics']; ?>
59+
<ul>
60+
<?php foreach ($diagnostics as $diagnostic): ?>
61+
<li><div><?= $escaper->escapeHtml($diagnostic['description']) ?></div>
62+
<?php $products = $block->getSampleAffectedItems($diagnostic, $catalog, $storeId); ?>
63+
<?php if ($products): ?>
64+
<div class="sample-affected-items">
65+
<p><?= $escaper->escapeHtml(__('Sample affected items:')) ?></p>
66+
<ul>
67+
<?php foreach ($products as $product): ?>
68+
<?php $adminUrl = $block->getAdminUrl($product, $store); ?>
69+
<li>
70+
<?= $escaper->escapeHtml($product->getName()) ?>
71+
(<a href="<?= $escaper->escapeHtml($adminUrl) ?>">
72+
<?= $escaper->escapeHtml($product->getSku()) ?>
73+
</a>)
74+
</li>
75+
<?php endforeach; ?>
76+
</ul>
77+
</div>
78+
<?php endif; ?>
79+
</li>
80+
<?php endforeach; ?>
81+
</ul>
82+
</td>
83+
</tr>
84+
<?php endforeach; ?>
85+
</table>
6486
<?php endforeach; ?>
65-
</table>
6687
<?php else: ?>
6788
<p><?= $escaper->escapeHtml(__('Report is not available')) ?></p>
6889
<?php endif; ?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require([
2+
'jquery'
3+
], function ($) {
4+
'use strict';
5+
6+
$(document).on('ready', function () {
7+
let storeId = $('.store').val();
8+
9+
$('.store-' + storeId).show();
10+
});
11+
12+
$('.store').on('change', function () {
13+
let storeId = $(this).val();
14+
15+
$('.store-info').hide();
16+
$('.store-' + storeId).show();
17+
});
18+
});

0 commit comments

Comments
 (0)