77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Block\Adminhtml\Sync;
|
|
|
|
use Esun\LingxingErp\Model\Sync\DetailProvider;
|
|
use Magento\Backend\Block\Template;
|
|
use Magento\Backend\Block\Template\Context;
|
|
|
|
class Details extends Template
|
|
{
|
|
private ?array $record = null;
|
|
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly DetailProvider $detailProvider,
|
|
array $data = []
|
|
) {
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
public function getSyncType(): string
|
|
{
|
|
return (string)$this->getData('sync_type');
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function getRecord(): array
|
|
{
|
|
if ($this->record === null) {
|
|
$this->record = $this->detailProvider->getRecord(
|
|
$this->getSyncType(),
|
|
(int)$this->getRequest()->getParam('id')
|
|
) ?? [];
|
|
}
|
|
return $this->record;
|
|
}
|
|
|
|
/** @return array<int, array<string, mixed>> */
|
|
public function getLogs(): array
|
|
{
|
|
$record = $this->getRecord();
|
|
return $record === [] ? [] : $this->detailProvider->getLogs($this->getSyncType(), $record);
|
|
}
|
|
|
|
public function getBackUrl(): string
|
|
{
|
|
return $this->getUrl('esun_lingxing/' . $this->getSyncType() . '/index');
|
|
}
|
|
|
|
public function prettyPayload(mixed $payload): string
|
|
{
|
|
if ($payload === null || $payload === '') {
|
|
return (string)__('No data recorded.');
|
|
}
|
|
if (is_string($payload)) {
|
|
try {
|
|
$decoded = json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
|
|
return (string)json_encode(
|
|
$decoded,
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
|
|
);
|
|
} catch (\JsonException) {
|
|
return $payload;
|
|
}
|
|
}
|
|
try {
|
|
return (string)json_encode(
|
|
$payload,
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
|
|
);
|
|
} catch (\JsonException) {
|
|
return (string)__('Unable to display data.');
|
|
}
|
|
}
|
|
}
|