init
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Controller\Index;
|
||||
|
||||
use Stemcell\ChineseDirectory\Model\CollectionFactory;
|
||||
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
|
||||
use Magento\Directory\Model\RegionFactory;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Stemcell\Customer\Api\Data\Http\JsonResponse;
|
||||
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
|
||||
|
||||
class Index implements HttpGetActionInterface
|
||||
{
|
||||
protected CollectionFactory $collectionFactory;
|
||||
|
||||
protected RequestInterface $request;
|
||||
|
||||
protected JsonResponse $jsonResponse;
|
||||
|
||||
protected ResultJsonFactory $resultJsonFactory;
|
||||
|
||||
protected RegionCollectionFactory $regionCollectionFactory;
|
||||
|
||||
const TYPE = ['city', 'district', 'town'];
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $collectionFactory,
|
||||
RequestInterface $request,
|
||||
JsonResponse $jsonResponse,
|
||||
RegionCollectionFactory $regionCollectionFactory,
|
||||
ResultJsonFactory $resultJsonFactory
|
||||
) {
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
$this->request = $request;
|
||||
$this->jsonResponse = $jsonResponse;
|
||||
$this->regionCollectionFactory = $regionCollectionFactory;
|
||||
$this->resultJsonFactory = $resultJsonFactory;
|
||||
}
|
||||
|
||||
protected function getParentCode($type, $parentCode) {
|
||||
if ($type==='city') {
|
||||
$parentCode = $this->regionCollectionFactory->create()
|
||||
->addFieldToFilter('main_table.region_id', $parentCode)
|
||||
->getFirstItem()
|
||||
->getData('code');
|
||||
}
|
||||
|
||||
return $parentCode;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$resultJson = $this->resultJsonFactory->create();
|
||||
$type = strtolower($this->request->getParam('type'));
|
||||
$parentCode = $this->request->getParam('administrativeId');
|
||||
$parentCode = $this->getParentCode($type, $parentCode);
|
||||
|
||||
if (!in_array($type, self::TYPE)) {
|
||||
$this->jsonResponse->setErrors(true)->setMessages([__('Invalid type')]);
|
||||
return $resultJson->setData($this->jsonResponse);
|
||||
}
|
||||
|
||||
$collection = $this->collectionFactory->create($type)
|
||||
->addFieldToFilter('parent_code', $parentCode)
|
||||
->addFieldToSelect(['name'])
|
||||
->load();
|
||||
|
||||
$this->jsonResponse->setErrors(false)->setData([
|
||||
'administrative' => $collection->getData()
|
||||
])
|
||||
->setData(['parentCode' => $parentCode]);
|
||||
return $resultJson->setData($this->jsonResponse);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user