Skip to content

Commit

Permalink
Merge pull request #1045 from dpfaffenbauer/issue/832
Browse files Browse the repository at this point in the history
[ProductBundle] specific price rules: keep id on save
  • Loading branch information
dpfaffenbauer authored Jul 21, 2019
2 parents d800f23 + b51f2d3 commit cca705c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,15 @@ public function getDataFromEditmode($data, $object = null, $params = [])

if ($data && $object instanceof Concrete) {
foreach ($data as $dataRow) {
$form = $this->getFormFactory()->createNamed('', ProductSpecificPriceRuleType::class);
$ruleId = isset($dataRow['id']) && is_numeric($dataRow['id']) ? $dataRow['id'] : null;

$storedRule = null;

if ($ruleId !== null) {
$storedRule = $this->getProductSpecificPriceRuleRepository()->find($ruleId);
}

$form = $this->getFormFactory()->createNamed('', ProductSpecificPriceRuleType::class, $storedRule);

$form->submit($dataRow);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,107 @@ pimcore.object.tags.coreShopProductSpecificPriceRules = Class.create(pimcore.obj
this.panels = [];
this.conditions = data.conditions;
this.actions = data.actions;
this.eventDispatcherKey = pimcore.eventDispatcher.registerTarget(this.eventDispatcherKey, this);
},

postSaveObject: function (object, task) {

var fieldName = this.getName();

if (object.id !== this.object.id) {
return;
}

if (this.isDirty()) {
this.reloadPriceRuleData(object, task, fieldName);
}
},

reloadPriceRuleData: function (object, task, fieldName) {
this.component.setLoading(true);
Ext.Ajax.request({
url: '/admin/object/get',
params: {id: object.id},
ignoreErrors: true,
success: function (response) {

this.dirty = false;

var refreshedObject = null,
refreshedObjectData = null;
try {
refreshedObject = Ext.decode(response.responseText);
if (!refreshedObject.hasOwnProperty('data') || !refreshedObject.data.hasOwnProperty(fieldName)) {
this.component.setLoading(false);
return;
}
refreshedObjectData = refreshedObject.data[fieldName];
} catch (e) {
console.log(e);
}

this.component.setLoading(false);
if (refreshedObjectData !== null) {
this.dispatchPostSaveToPanels(object, refreshedObjectData, task, fieldName);
}
}.bind(this),
failure: function () {
this.component.setLoading(false);
}.bind(this),
});
},

dispatchPostSaveToPanels: function (object, refreshedData, task, fieldName) {

var refreshAllPanels = false;

if (!refreshedData.hasOwnProperty('rules') || !Ext.isArray(refreshedData.rules)) {
return;
}

Ext.each(this.panels, function (panel) {
if (panel.getId() === null) {
refreshAllPanels = true;
return false;
}
});

if (refreshAllPanels === true) {
this.rebuildPriceRules(refreshedData.rules);
} else {
this.rebuildPriceRuleData(object, refreshedData.rules, task, fieldName);
}
},

rebuildPriceRuleData: function (object, refreshedRuleData, task, fieldName) {
Ext.each(this.panels, function (panelClass) {
var newRulePanelData = null;
Ext.Array.each(refreshedRuleData, function (ruleData) {
if (ruleData.hasOwnProperty('id') && ruleData.id === panelClass.getId()) {
newRulePanelData = ruleData;
return false;
}
});
if (newRulePanelData !== null) {
panelClass.postSaveObject(object, newRulePanelData, task, fieldName);
}
});
},

rebuildPriceRules: function (refreshedRuleData) {

var lastActiveItem = this.getTabPanel().getActiveTab(),
activeTabIndex = this.getTabPanel().items.findIndex('id', lastActiveItem.id);

this.getTabPanel().removeAll();

this.data = refreshedRuleData;
this.panels = [];

this.showPriceRules(activeTabIndex);
},


getGridColumnConfig: function (field) {
return {
header: ts(field.label), width: 150, sortable: false, dataIndex: field.key,
Expand Down Expand Up @@ -95,23 +194,30 @@ pimcore.object.tags.coreShopProductSpecificPriceRules = Class.create(pimcore.obj
return this.layout;
},

showPriceRules: function () {
showPriceRules: function (lastActiveItemIndex) {
Ext.each(this.data, function (data) {
var panel = new coreshop.product.specificprice.object.item(this, data, data.id, 'productSpecificPriceRule');
this.createItemPanel(data, data.id);
}.bind(this));

this.panels.push(panel);
if (this.panels.length > 0) {
var activePanel = lastActiveItemIndex && this.panels[lastActiveItemIndex] ? this.panels[lastActiveItemIndex].panel : this.panels[0].panel;
this.getTabPanel().setActiveItem(activePanel);
}
},

panel.panel.on('beforedestroy', function () {
var index = this.panels.indexOf(panel);
this.panels.splice(index, 1);
createItemPanel: function (data, id) {
var panelItem = new coreshop.product.specificprice.object.item(this, data, id, 'productSpecificPriceRule');

this.dirty = true;
}.bind(this));
this.panels.push(panelItem);

panelItem.panel.on('beforedestroy', function () {
var index = this.panels.indexOf(panelItem);
this.panels.splice(index, 1);

this.dirty = true;
}.bind(this));

if (this.panels.length > 0) {
this.getTabPanel().setActiveItem(this.panels[0].panel);
}
return panelItem;
},

getTabPanel: function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ coreshop.product.specificprice.object.item = Class.create(coreshop.rules.item, {

iconCls: 'coreshop_icon_price_rule',

postSaveObject: function (object, refreshedRuleData, task, fieldName) {
// remove dirty flag!
//this.settingsForm.getForm().setValues(this.settingsForm.getForm().getValues());
},

getPanel: function () {
this.panel = new Ext.TabPanel({
activeTab: 0,
Expand Down Expand Up @@ -122,6 +127,10 @@ coreshop.product.specificprice.object.item = Class.create(coreshop.rules.item, {
return {};
},

getId: function () {
return this.data.id ? this.data.id : null;
},

isDirty: function () {
if (this.settingsForm.form.monitor && this.settingsForm.getForm().isDirty()) {
return true;
Expand Down

0 comments on commit cca705c

Please sign in to comment.