Skip to content

Commit 503245f

Browse files
author
Idolov, Stanislav(sidolov)
committed
Merge pull request #347 from magento-folks/catalog-promo-interface
[Folks] Catalog promo interface
2 parents b1b05c8 + ea478e7 commit 503245f

File tree

22 files changed

+449
-69
lines changed

22 files changed

+449
-69
lines changed

app/code/Magento/CatalogRule/Block/Adminhtml/Edit/SaveAndApplyButton.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ public function getButtonData()
2323
'class' => 'save',
2424
'on_click' => '',
2525
'sort_order' => 80,
26+
'data_attribute' => [
27+
'mage-init' => [
28+
'Magento_Ui/js/form/button-adapter' => [
29+
'actions' => [
30+
[
31+
'targetName' => 'catalog_rule_form.catalog_rule_form',
32+
'actionName' => 'save',
33+
'params' => [
34+
true,
35+
['auto_apply' => 1],
36+
]
37+
]
38+
]
39+
]
40+
],
41+
42+
]
2643
];
2744
}
2845
return $data;

app/code/Magento/CatalogRule/Block/Adminhtml/Promo/Catalog/Edit/Tab/Conditions.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,39 @@ protected function _prepareForm()
126126
{
127127
$model = $this->_coreRegistry->registry('current_promo_catalog_rule');
128128

129+
/** @var \Magento\Framework\Data\Form $form */
130+
$form = $this->addTabToForm($model);
131+
$this->setForm($form);
132+
133+
return parent::_prepareForm();
134+
}
135+
136+
/**
137+
* @param \Magento\CatalogRule\Api\Data\RuleInterface $model
138+
* @param string $fieldsetId
139+
* @param string $formName
140+
* @return \Magento\Framework\Data\Form
141+
* @throws \Magento\Framework\Exception\LocalizedException
142+
*/
143+
protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $formName = 'catalog_rule_form')
144+
{
129145
/** @var \Magento\Framework\Data\Form $form */
130146
$form = $this->_formFactory->create();
131147
$form->setHtmlIdPrefix('rule_');
132148

133-
$renderer = $this->_rendererFieldset->setTemplate(
134-
'Magento_CatalogRule::promo/fieldset.phtml'
135-
)->setNewChildUrl(
136-
$this->getUrl('catalog_rule/promo_catalog/newConditionHtml/form/rule_conditions_fieldset')
149+
$newChildUrl = $this->getUrl(
150+
'catalog_rule/promo_catalog/newConditionHtml/form/' . $model->getConditionsFieldSetId($formName),
151+
['form_namespace' => $formName]
137152
);
138153

154+
$renderer = $this->_rendererFieldset->setTemplate('Magento_CatalogRule::promo/fieldset.phtml')
155+
->setNewChildUrl($newChildUrl)
156+
->setConditionsFieldSetId($model->getConditionsFieldSetId($formName));
157+
139158
$fieldset = $form->addFieldset(
140-
'conditions_fieldset',
159+
$fieldsetId,
141160
['legend' => __('Conditions (don\'t add conditions if rule is applied to all products)')]
142-
)->setRenderer(
143-
$renderer
144-
);
161+
)->setRenderer($renderer);
145162

146163
$fieldset->addField(
147164
'conditions',
@@ -151,17 +168,29 @@ protected function _prepareForm()
151168
'label' => __('Conditions'),
152169
'title' => __('Conditions'),
153170
'required' => true,
154-
'data-form-part' => 'catalog_rule_form'
171+
'data-form-part' => $formName
155172
]
156-
)->setRule(
157-
$model
158-
)->setRenderer(
159-
$this->_conditions
160-
);
173+
)
174+
->setRule($model)
175+
->setRenderer($this->_conditions);
161176

162177
$form->setValues($model->getData());
163-
$this->setForm($form);
178+
$this->setConditionFormName($model->getConditions(), $formName);
179+
return $form;
180+
}
164181

165-
return parent::_prepareForm();
182+
/**
183+
* @param \Magento\Rule\Model\Condition\AbstractCondition $conditions
184+
* @param string $formName
185+
* @return void
186+
*/
187+
private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName)
188+
{
189+
$conditions->setFormName($formName);
190+
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
191+
foreach ($conditions->getConditions() as $condition) {
192+
$this->setConditionFormName($condition, $formName);
193+
}
194+
}
166195
}
167196
}

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Edit.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ public function execute()
3838
if (!empty($data)) {
3939
$model->addData($data);
4040
}
41-
$model->getConditions()->setJsFormObject('rule_conditions_fieldset');
4241
$model->getConditions()->setFormName('catalog_rule_form');
42+
$model->getConditions()->setJsFormObject(
43+
$model->getConditionsFieldSetId($model->getConditions()->getFormName())
44+
);
4345

4446
$this->_coreRegistry->register('current_promo_catalog_rule', $model);
4547

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/NewConditionHtml.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class NewConditionHtml extends \Magento\CatalogRule\Controller\Adminhtml\Promo\C
1616
public function execute()
1717
{
1818
$id = $this->getRequest()->getParam('id');
19+
$formName = $this->getRequest()->getParam('form_namespace');
1920
$typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
2021
$type = $typeArr[0];
2122

@@ -31,6 +32,7 @@ public function execute()
3132

3233
if ($model instanceof AbstractCondition) {
3334
$model->setJsFormObject($this->getRequest()->getParam('form'));
35+
$model->setFormName($formName);
3436
$html = $model->asHtmlRecursive();
3537
} else {
3638
$html = '';

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,35 @@
66
*/
77
namespace Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog;
88

9+
use Magento\Backend\App\Action\Context;
910
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Registry;
12+
use Magento\Framework\Stdlib\DateTime\Filter\Date;
13+
use Magento\Framework\App\Request\DataPersistorInterface;
1014

1115
class Save extends \Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog
1216
{
17+
/**
18+
* @var DataPersistorInterface
19+
*/
20+
protected $dataPersistor;
21+
22+
/**
23+
* @param Context $context
24+
* @param Registry $coreRegistry
25+
* @param Date $dateFilter
26+
* @param DataPersistorInterface $dataPersistor
27+
*/
28+
public function __construct(
29+
Context $context,
30+
Registry $coreRegistry,
31+
Date $dateFilter,
32+
DataPersistorInterface $dataPersistor
33+
) {
34+
$this->dataPersistor = $dataPersistor;
35+
parent::__construct($context, $coreRegistry, $dateFilter);
36+
}
37+
1338
/**
1439
* @return void
1540
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -43,6 +68,7 @@ public function execute()
4368
$this->messageManager->addError($errorMessage);
4469
}
4570
$this->_getSession()->setPageData($data);
71+
$this->dataPersistor->set('catalog_rule', $data);
4672
$this->_redirect('catalog_rule/*/edit', ['id' => $model->getId()]);
4773
return;
4874
}
@@ -54,11 +80,15 @@ public function execute()
5480

5581
$model->loadPost($data);
5682

57-
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($model->getData());
83+
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($data);
84+
$this->dataPersistor->set('catalog_rule', $data);
85+
5886
$ruleRepository->save($model);
5987

6088
$this->messageManager->addSuccess(__('You saved the rule.'));
6189
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData(false);
90+
$this->dataPersistor->clear('catalog_rule');
91+
6292
if ($this->getRequest()->getParam('auto_apply')) {
6393
$this->getRequest()->setParam('rule_id', $model->getId());
6494
$this->_forward('applyRules');
@@ -84,7 +114,8 @@ public function execute()
84114
__('Something went wrong while saving the rule data. Please review the error log.')
85115
);
86116
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
87-
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($model->getData());
117+
$this->_objectManager->get('Magento\Backend\Model\Session')->setPageData($data);
118+
$this->dataPersistor->set('catalog_rule', $data);
88119
$this->_redirect('catalog_rule/*/edit', ['id' => $this->getRequest()->getParam('rule_id')]);
89120
return;
90121
}

app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,30 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
285285
}
286286
return $this;
287287
}
288+
289+
/**
290+
* Delete the object
291+
*
292+
* @param \Magento\Framework\Model\AbstractModel $object
293+
* @return $this
294+
* @throws \Exception
295+
*/
296+
public function delete(AbstractModel $object)
297+
{
298+
$this->transactionManager->start($this->getConnection());
299+
try {
300+
$object->beforeDelete();
301+
$this->_beforeDelete($object);
302+
$this->entityManager->delete('Magento\CatalogRule\Api\Data\RuleInterface', $object);
303+
$this->_afterDelete($object);
304+
$object->isDeleted(true);
305+
$object->afterDelete();
306+
$this->transactionManager->commit();
307+
$object->afterDeleteCommit();
308+
} catch (\Exception $exception) {
309+
$this->transactionManager->rollBack();
310+
throw $exception;
311+
}
312+
return $this;
313+
}
288314
}

app/code/Magento/CatalogRule/Model/Rule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,15 @@ protected function dataDiff($array1, $array2)
572572
return $result;
573573
}
574574

575+
/**
576+
* @param string $formName
577+
* @return string
578+
*/
579+
public function getConditionsFieldSetId($formName = '')
580+
{
581+
return $formName . 'rule_conditions_fieldset_' . $this->getId();
582+
}
583+
575584
//@codeCoverageIgnoreStart
576585
/**
577586
* {@inheritdoc}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogRule\Model\Rule\Action;
7+
8+
class SimpleActionOptionsProvider
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public function toOptionArray()
14+
{
15+
return [
16+
[
17+
'label' => __('Apply as percentage of original'),
18+
'value' => 'by_percent'
19+
],
20+
[
21+
'label' => __('Apply as fixed amount'),
22+
'value' => 'by_fixed'
23+
],
24+
[
25+
'label' => __('Adjust final price to this percentage'),
26+
'value' => 'to_percent'
27+
],
28+
[
29+
'label' => __('Adjust final price to discount value'),
30+
'value' => 'to_fixed'
31+
]
32+
];
33+
}
34+
}

0 commit comments

Comments
 (0)