108 lines
4.4 KiB
PHP
108 lines
4.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Agent\Block;
|
|
|
|
use Esun\Agent\Model\Map\CountryPosition;
|
|
use Esun\Agent\Model\ResourceModel\Agent\CollectionFactory;
|
|
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
|
|
use Magento\Framework\Filesystem\Driver\File;
|
|
use Magento\Framework\Module\Dir\Reader as ModuleDirReader;
|
|
use Magento\Framework\UrlInterface;
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
use RuntimeException;
|
|
|
|
class AgentLocator extends Template
|
|
{
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly ModuleDirReader $moduleDirReader,
|
|
private readonly File $file,
|
|
private readonly CollectionFactory $collectionFactory,
|
|
private readonly StoreManagerInterface $storeManager,
|
|
private readonly CountryPosition $countryPosition,
|
|
private readonly CountryCollectionFactory $countryCollectionFactory,
|
|
array $data = []
|
|
) {
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
public function getCountryOptions(): array
|
|
{
|
|
return $this->countryCollectionFactory->create()
|
|
->loadByStore()
|
|
->toOptionArray(false);
|
|
}
|
|
|
|
public function getContactBackgroundUrl(): string
|
|
{
|
|
return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA)
|
|
. 'wysiwyg/about/icons/esun-contact-bk.png';
|
|
}
|
|
|
|
public function getConfigJson(): string
|
|
{
|
|
$moduleDir = $this->moduleDirReader->getModuleDir('', 'Esun_Agent');
|
|
$dataFile = $moduleDir . '/view/frontend/web/data/agents.json';
|
|
|
|
if (!$this->file->isExists($dataFile)) {
|
|
throw new RuntimeException('Agent data file does not exist.');
|
|
}
|
|
|
|
$data = json_decode($this->file->fileGetContents($dataFile), true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
$data['agents'] = [];
|
|
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
|
|
$collection = $this->collectionFactory->create()
|
|
->addFieldToFilter('is_active', 1)
|
|
->setOrder('sort_order', 'ASC')
|
|
->setOrder('entity_id', 'ASC');
|
|
|
|
foreach ($collection as $agentModel) {
|
|
$logo = (string) $agentModel->getData('logo');
|
|
$position = $this->countryPosition->get((string) $agentModel->getData('country_code'));
|
|
$data['agents'][] = [
|
|
'id' => (int) $agentModel->getId(),
|
|
'system_name' => (string) $agentModel->getData('system_name'),
|
|
'name' => (string) $agentModel->getData('name'),
|
|
'continent' => (string) $agentModel->getData('continent'),
|
|
'country_code' => (string) $agentModel->getData('country_code'),
|
|
'country' => (string) $agentModel->getData('country'),
|
|
'website' => (string) $agentModel->getData('website'),
|
|
'address' => (string) $agentModel->getData('address'),
|
|
'note' => (string) $agentModel->getData('note'),
|
|
'map_x' => $position['x'] ?? null,
|
|
'map_y' => $position['y'] ?? null,
|
|
'sort_order' => (int) $agentModel->getData('sort_order'),
|
|
'logo_url' => $logo !== ''
|
|
? $mediaUrl . ltrim($logo, '/')
|
|
: $this->getViewFileUrl('Esun_Agent::images/agents/placeholder.svg'),
|
|
];
|
|
}
|
|
unset($agent);
|
|
|
|
$config = [
|
|
'data' => $data,
|
|
'mapUrl' => $this->getViewFileUrl('Esun_Agent::images/world-map-dots.svg'),
|
|
'labels' => [
|
|
'agents' => (string) __('Agents'),
|
|
'exploreAll' => (string) __('Explore All'),
|
|
'noResults' => (string) __('No agents match the selected filters.'),
|
|
'website' => (string) __('Website'),
|
|
'filteredResults' => (string) __('Filtered Results (%1 matched partners)'),
|
|
'previousPage' => (string) __('Previous page'),
|
|
'nextPage' => (string) __('Next page'),
|
|
'page' => (string) __('Page %1'),
|
|
],
|
|
];
|
|
|
|
return (string) json_encode(
|
|
$config,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR
|
|
);
|
|
}
|
|
}
|