-
Notifications
You must be signed in to change notification settings - Fork 3
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
28ee5e5
commit 75b3d43
Showing
9 changed files
with
489 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DevStone\UsageCalculator\Block\Adminhtml\Usage\Edit; | ||
|
||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Backend\Block\Widget\Form\Renderer\Fieldset; | ||
use Magento\SalesRule\Model\Rule; | ||
|
||
/** | ||
* Block for rendering Conditions tab on Sales Rules creation page. | ||
* | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class Conditions extends \Magento\Backend\Block\Widget\Form\Generic implements | ||
\Magento\Ui\Component\Layout\Tabs\TabInterface | ||
{ | ||
/** | ||
* Core registry | ||
* | ||
* @var \Magento\Backend\Block\Widget\Form\Renderer\Fieldset | ||
*/ | ||
protected $_rendererFieldset; | ||
|
||
/** | ||
* @var \Magento\Rule\Block\Conditions | ||
*/ | ||
protected $_conditions; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_nameInLayout = 'conditions_apply_to'; | ||
|
||
/** | ||
* @var \Magento\CatalogRule\Model\RuleFactory | ||
*/ | ||
private $ruleFactory; | ||
|
||
/** | ||
* @param \Magento\Backend\Block\Template\Context $context | ||
* @param \Magento\Framework\Registry $registry | ||
* @param \Magento\Framework\Data\FormFactory $formFactory | ||
* @param \Magento\Rule\Block\Conditions $conditions | ||
* @param \Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset | ||
* @param array $data | ||
* @param \Magento\CatalogRule\ModelRuleFactory|null $ruleFactory | ||
*/ | ||
public function __construct( | ||
\Magento\Backend\Block\Template\Context $context, | ||
\Magento\Framework\Registry $registry, | ||
\Magento\Framework\Data\FormFactory $formFactory, | ||
\Magento\Rule\Block\Conditions $conditions, | ||
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset, | ||
array $data = [], | ||
\Magento\CatalogRule\Model\RuleFactory $ruleFactory = null | ||
) { | ||
$this->_rendererFieldset = $rendererFieldset; | ||
$this->_conditions = $conditions; | ||
$this->ruleFactory = $ruleFactory ?: ObjectManager::getInstance() | ||
->get(\Magento\CatalogRule\Model\RuleFactory::class); | ||
parent::__construct($context, $registry, $formFactory, $data); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getTabClass() | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTabUrl() | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function isAjaxLoaded() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTabLabel() | ||
{ | ||
return __('Conditions'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTabTitle() | ||
{ | ||
return __('Conditions'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function canShowTab() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function isHidden() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Prepare form before rendering HTML | ||
* | ||
* @return $this | ||
*/ | ||
protected function _prepareForm() | ||
{ | ||
$model = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE); | ||
$form = $this->addTabToForm($model); | ||
$this->setForm($form); | ||
|
||
return parent::_prepareForm(); | ||
} | ||
|
||
/** | ||
* Handles addition of conditions tab to supplied form. | ||
* | ||
* @param Rule $model | ||
* @param string $fieldsetId | ||
* @param string $formName | ||
* @return \Magento\Framework\Data\Form | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $formName = 'devstone_usagecalculator_usage_form') | ||
{ | ||
if (!$model) { | ||
$id = $this->getRequest()->getParam('id'); | ||
$model = $this->ruleFactory->create(); | ||
$model->load($id); | ||
} | ||
$conditionsFieldSetId = $model->getConditionsFieldSetId($formName); | ||
$newChildUrl = $this->getUrl( | ||
'sales_rule/promo_quote/newConditionHtml/form/' . $conditionsFieldSetId, | ||
['form_namespace' => $formName] | ||
); | ||
|
||
/** @var \Magento\Framework\Data\Form $form */ | ||
$form = $this->_formFactory->create(); | ||
$form->setHtmlIdPrefix('rule_'); | ||
|
||
$renderer = $this->getLayout()->createBlock(Fieldset::class); | ||
$renderer->setTemplate( | ||
'Magento_CatalogRule::promo/fieldset.phtml' | ||
)->setNewChildUrl( | ||
$newChildUrl | ||
)->setFieldSetId( | ||
$conditionsFieldSetId | ||
); | ||
|
||
$fieldset = $form->addFieldset( | ||
$fieldsetId, | ||
[ | ||
'legend' => __( | ||
'Apply the rule only if the following conditions are met (leave blank for all products).' | ||
) | ||
] | ||
)->setRenderer( | ||
$renderer | ||
); | ||
$fieldset->addField( | ||
'conditions', | ||
'text', | ||
[ | ||
'name' => 'conditions', | ||
'label' => __('Conditions'), | ||
'title' => __('Conditions'), | ||
'required' => true, | ||
'data-form-part' => $formName | ||
] | ||
)->setRule( | ||
$model | ||
)->setRenderer( | ||
$this->_conditions | ||
); | ||
|
||
$form->setValues($model->getData()); | ||
$this->setConditionFormName($model->getConditions(), $formName); | ||
return $form; | ||
} | ||
|
||
/** | ||
* Handles addition of form name to condition and its conditions. | ||
* | ||
* @param \Magento\Rule\Model\Condition\AbstractCondition $conditions | ||
* @param string $formName | ||
* @return void | ||
*/ | ||
private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName) | ||
{ | ||
$conditions->setFormName($formName); | ||
if ($conditions->getConditions() && is_array($conditions->getConditions())) { | ||
foreach ($conditions->getConditions() as $condition) { | ||
$this->setConditionFormName($condition, $formName); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.