-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f78d2e
commit 32e9548
Showing
15 changed files
with
703 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Block/Adminhtml/System/Config/Form/Field/AdvancedShipmentMapping.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/* | ||
* Copyright © Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field; | ||
|
||
use Exception; | ||
use Magento\Backend\Block\Template\Context; | ||
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray; | ||
use Magento\Framework\DataObject; | ||
use Magento\Framework\View\Element\BlockInterface as ElementBlockInterface; | ||
use Magmodules\Channable\Api\Log\RepositoryInterface as LogRepository; | ||
|
||
/** | ||
* Represents a table for advanced shipping method mapping in the admin configuration | ||
*/ | ||
class AdvancedShipmentMapping extends AbstractFieldArray | ||
{ | ||
public const OPTION_PATTERN = 'option_%s'; | ||
public const SELECTED = 'selected="selected"'; | ||
public const RENDERERS = [ | ||
'channel' => Renderer\Channels::class, | ||
'method' => Renderer\ShippingMethods::class, | ||
]; | ||
|
||
private array $renderers = []; | ||
private LogRepository $logger; | ||
|
||
public const COLUMN_CHANNEL = 'channel'; | ||
public const COLUMN_CHANNABLE_CARRIER = 'channable_carrier'; | ||
public const COLUMN_METHOD = 'method'; | ||
|
||
public function __construct( | ||
Context $context, | ||
LogRepository $logger, | ||
array $data = [] | ||
) { | ||
$this->logger = $logger; | ||
parent::__construct($context, $data); | ||
} | ||
|
||
/** | ||
* Prepare columns for rendering. | ||
* | ||
* @return void | ||
*/ | ||
protected function _prepareToRender() | ||
{ | ||
$this->addColumn(self::COLUMN_CHANNEL, [ | ||
'label' => (string)__('Channel'), | ||
'class' => 'required-entry', | ||
'renderer' => $this->getRenderer('channel'), | ||
]); | ||
$this->addColumn(self::COLUMN_CHANNABLE_CARRIER, [ | ||
'label' => (string)__('Carrier'), | ||
'class' => 'required-entry', | ||
]); | ||
$this->addColumn(self::COLUMN_METHOD, [ | ||
'label' => (string)__('Magento Method'), | ||
'class' => 'required-entry', | ||
'renderer' => $this->getRenderer('method'), | ||
]); | ||
$this->_addAfter = false; | ||
$this->_addButtonLabel = (string)__('Add'); | ||
} | ||
|
||
/** | ||
* Get renderer for a specific column type. | ||
* | ||
* @param string $type | ||
* @return ElementBlockInterface | ||
*/ | ||
public function getRenderer(string $type): ElementBlockInterface | ||
{ | ||
if (!isset($this->renderers[$type])) { | ||
try { | ||
$this->renderers[$type] = $this->getLayout()->createBlock( | ||
self::RENDERERS[$type], | ||
'', | ||
['data' => ['is_render_to_js_template' => true]] | ||
); | ||
} catch (Exception $e) { | ||
$this->logger->addErrorLog('RendererCreationError', $e->getMessage()); | ||
} | ||
} | ||
|
||
return $this->renderers[$type]; | ||
} | ||
|
||
/** | ||
* Prepare extra attributes for array row rendering. | ||
* | ||
* @param DataObject $row | ||
* @return void | ||
*/ | ||
protected function _prepareArrayRow(DataObject $row) | ||
{ | ||
$options = []; | ||
foreach (array_keys(self::RENDERERS) as $element) { | ||
if ($elementData = $row->getData($element)) { | ||
$renderer = $this->getRenderer($element); | ||
$options[sprintf(self::OPTION_PATTERN, $renderer->calcOptionHash($elementData))] = self::SELECTED; | ||
} | ||
} | ||
$row->setData('option_extra_attrs', $options); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
Block/Adminhtml/System/Config/Form/Field/Renderer/Channels.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field\Renderer; | ||
|
||
use Magento\Framework\View\Element\Context; | ||
use Magento\Framework\View\Element\Html\Select; | ||
use Magmodules\Channable\Model\Config\Source\Channels as ChannelsSource; | ||
|
||
class Channels extends Select | ||
{ | ||
|
||
private ChannelsSource $channels; | ||
|
||
public function __construct( | ||
Context $context, | ||
ChannelsSource $channels, | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $data); | ||
$this->channels = $channels; | ||
} | ||
|
||
/** | ||
* Render block HTML. | ||
* | ||
* @return string | ||
*/ | ||
protected function _toHtml(): string | ||
{ | ||
if (empty($this->getOptions())) { | ||
$this->addOption(null, '-- select --'); | ||
foreach ($this->channels->toOptionArray() as $channel) { | ||
$this->addOption($channel['value'], $channel['label']); | ||
} | ||
} | ||
|
||
return parent::_toHtml(); | ||
} | ||
|
||
/** | ||
* Sets the name for the input element. | ||
* | ||
* @param string $value | ||
* @return $this | ||
*/ | ||
public function setInputName(string $value): self | ||
{ | ||
$this->setData('name', $value); | ||
return $this; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Block/Adminhtml/System/Config/Form/Field/Renderer/ShippingMethods.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Copyright © Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field\Renderer; | ||
|
||
use Magento\Framework\View\Element\Context; | ||
use Magento\Framework\View\Element\Html\Select; | ||
use Magmodules\Channable\Model\Config\Source\ShippingMethods as ShippingMethodsSource; | ||
|
||
class ShippingMethods extends Select | ||
{ | ||
|
||
private ShippingMethodsSource $shippingMethods; | ||
|
||
public function __construct( | ||
Context $context, | ||
ShippingMethodsSource $shippingMethods, | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $data); | ||
$this->shippingMethods = $shippingMethods; | ||
} | ||
|
||
/** | ||
* Render block HTML. | ||
* | ||
* @return string | ||
*/ | ||
protected function _toHtml(): string | ||
{ | ||
if (empty($this->getOptions())) { | ||
foreach ($this->shippingMethods->toOptionArray() as $method) { | ||
$this->addOption($method['value'], $method['label']); | ||
} | ||
} | ||
|
||
return parent::_toHtml(); | ||
} | ||
|
||
/** | ||
* Sets the name for the input element. | ||
* | ||
* @param string $value | ||
* @return $this | ||
*/ | ||
public function setInputName(string $value): self | ||
{ | ||
$this->setName($value); | ||
return $this; | ||
} | ||
} |
Oops, something went wrong.