init
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\SeventeenTrack\Block\Adminhtml\Log;
|
||||
|
||||
use Esun\SeventeenTrack\Model\ApiLogFactory;
|
||||
use Magento\Backend\Block\Template;
|
||||
use Magento\Backend\Block\Template\Context;
|
||||
|
||||
class View extends Template
|
||||
{
|
||||
private ?array $record = null;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly ApiLogFactory $apiLogFactory,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function getRecord(): array
|
||||
{
|
||||
if ($this->record === null) {
|
||||
$model = $this->apiLogFactory->create()->load((int)$this->getRequest()->getParam('id'));
|
||||
$this->record = $model->getId() ? $model->getData() : [];
|
||||
}
|
||||
return $this->record;
|
||||
}
|
||||
|
||||
public function getBackUrl(): string
|
||||
{
|
||||
return $this->getUrl('esun_seventeen_track/log/index');
|
||||
}
|
||||
|
||||
public function prettyPayload(mixed $payload): string
|
||||
{
|
||||
if ($payload === null || $payload === '') {
|
||||
return (string)__('No data recorded.');
|
||||
}
|
||||
try {
|
||||
$decoded = is_string($payload) ? json_decode($payload, true, 512, JSON_THROW_ON_ERROR) : $payload;
|
||||
return (string)json_encode(
|
||||
$decoded,
|
||||
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
|
||||
);
|
||||
} catch (\JsonException) {
|
||||
return is_string($payload) ? $payload : (string)__('Unable to display data.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\SeventeenTrack\Block\Order;
|
||||
|
||||
use Esun\SeventeenTrack\Model\Config;
|
||||
use Magento\Framework\Locale\ResolverInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Sales\Model\Order;
|
||||
|
||||
class Tracking extends Template
|
||||
{
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly Registry $registry,
|
||||
private readonly Config $config,
|
||||
private readonly ResolverInterface $localeResolver,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function canDisplay(): bool
|
||||
{
|
||||
return $this->config->isWidgetEnabled() && $this->getTracks() !== [];
|
||||
}
|
||||
|
||||
public function getTracks(): array
|
||||
{
|
||||
$order = $this->registry->registry('current_order');
|
||||
if (!$order instanceof Order) {
|
||||
return [];
|
||||
}
|
||||
$tracks = [];
|
||||
foreach ($order->getShipmentsCollection() as $shipment) {
|
||||
foreach ($shipment->getAllTracks() as $track) {
|
||||
$number = trim((string)$track->getTrackNumber());
|
||||
if ($number !== '') {
|
||||
$tracks[$number] = [
|
||||
'number' => $number,
|
||||
'title' => (string)($track->getTitle() ?: __('Shipment')),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values($tracks);
|
||||
}
|
||||
|
||||
public function getWidgetHeight(): int
|
||||
{
|
||||
return $this->config->getWidgetHeight();
|
||||
}
|
||||
|
||||
public function getWidgetLanguage(): string
|
||||
{
|
||||
$locale = str_replace('_', '-', $this->localeResolver->getLocale());
|
||||
$map = ['zh-CN' => 'zh-cn', 'zh-HK' => 'zh-hk', 'zh-TW' => 'zh-tw', 'pt-BR' => 'pt'];
|
||||
return $map[$locale] ?? strtolower(substr($locale, 0, 2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user