Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ProductBundle] specific price rules: keep id on save #1045

Merged
merged 1 commit into from
Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,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