![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Model/ShippingMethods/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Model\ShippingMethods; use Extmag\Shiplab\Model\ResourceModel\ShippingMethods\Collection; use Extmag\Shiplab\Model\ResourceModel\ShippingMethods\CollectionFactory; use Extmag\Shiplab\Model\ShippingMethods; use Extmag\Shiplab\Model\Source\AllShippingMethods; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\App\RequestInterface; use Magento\Ui\DataProvider\Modifier\PoolInterface; use Magento\Ui\DataProvider\ModifierPoolDataProvider; class DataProvider extends ModifierPoolDataProvider { /** * @var Collection */ protected $collection; /** * @var DataPersistorInterface */ protected $dataPersistor; /** * @var array */ protected $loadedData; /** * @var RequestInterface */ protected $request; protected AllShippingMethods $allShippingMethods; /** * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param CollectionFactory $shippingMethodsCollectionFactory * @param DataPersistorInterface $dataPersistor * @param RequestInterface $request * @param AllShippingMethods $allShippingMethods * @param array $meta * @param array $data * @param PoolInterface|null $pool */ public function __construct( $name, $primaryFieldName, $requestFieldName, CollectionFactory $shippingMethodsCollectionFactory, DataPersistorInterface $dataPersistor, RequestInterface $request, AllShippingMethods $allShippingMethods, array $meta = [], array $data = [], PoolInterface $pool = null ) { $this->collection = $shippingMethodsCollectionFactory->create(); $this->dataPersistor = $dataPersistor; parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool); $this->request = $request; $this->meta = $this->prepareMeta($this->meta); $this->allShippingMethods = $allShippingMethods; } /** * @param array $meta * @return array */ public function prepareMeta(array $meta) { $id = $this->request->getParam('entity_id'); $meta['general'] = [ 'children' => [ 'carrier' => [ 'arguments' => [ 'data' => [ 'config' => [ 'disabled' => !($id === null), ], ], ], ], ], ]; return $meta; } /** * @return array */ public function getData() { if (isset($this->loadedData)) { return $this->loadedData; } $items = $this->collection->getItems(); /** @var $page ShippingMethods */ foreach ($items as $page) { $this->loadedData[$page->getId()] = $page->getData(); } $data = $this->dataPersistor->get('extmag_shiplab_shippingmethods'); if (!empty($data)) { $page = $this->collection->getNewEmptyItem(); $page->setData($data); $this->loadedData[$page->getId()] = $page->getData(); $this->dataPersistor->clear('extmag_shiplab_shippingmethods'); } if (!empty($page)) { if (!empty($this->loadedData[$page->getId()]['price_rule_ids'])) { $this->loadedData[$page->getId()]['price_rule_ids'] = json_decode($this->loadedData[$page->getId()]['price_rule_ids'], true); } else { $this->loadedData[$page->getId()]['price_rule_ids'] = []; } $methodsNamesMapping = json_decode( $this->loadedData[$page->getId()]['methods_names_mapping'] ?? '{}', true ) ?? []; $methodOriginNames = $this->allShippingMethods->getAllNamesByOrigins($page->getCarrier()); $methodsNamesComposed = []; foreach ($methodOriginNames as $key => $value) { foreach ($value as $k => $val) { $originName = $key . ' - ' . $val . ' (' . $k . ')'; $customName = ''; foreach ($methodsNamesMapping as $mKey => $item) { if ($mKey == $originName) { $customName = $item; } } $methodsNamesComposed[] = [ 'origin_name' => $originName, 'custom_name' => $customName, ]; } } $this->loadedData[$page->getId()]['methods_names_mapping'] = $methodsNamesComposed; } else { $this->loadedData[null]['price_rule_ids'] = []; $this->loadedData[null]['methods_names_mapping'] = []; } if (!empty($page) && !empty($this->loadedData[$page->getId()]['logo']) && !is_array($this->loadedData[$page->getId()]['logo'])) { $this->loadedData[$page->getId()]['logo'] = json_decode($this->loadedData[$page->getId()]['logo'], true); } return $this->loadedData; } }