From b51f2d37b066c0a8f2dc70c941da3bf1433e7e0a Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Fri, 12 Jul 2019 08:43:19 +0200 Subject: [PATCH] [ProductBundle] specific price rules: keep id on save --- .../ProductSpecificPriceRules.php | 10 +- .../tags/coreShopProductSpecificPriceRules.js | 128 ++++++++++++++++-- .../pimcore/js/specificprice/object/item.js | 9 ++ 3 files changed, 135 insertions(+), 12 deletions(-) diff --git a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php index d85bbe3b3f..23d6f71454 100644 --- a/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php +++ b/src/CoreShop/Bundle/ProductBundle/CoreExtension/ProductSpecificPriceRules.php @@ -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); diff --git a/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/coreExtension/tags/coreShopProductSpecificPriceRules.js b/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/coreExtension/tags/coreShopProductSpecificPriceRules.js index bce7508b50..761c96d19b 100644 --- a/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/coreExtension/tags/coreShopProductSpecificPriceRules.js +++ b/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/coreExtension/tags/coreShopProductSpecificPriceRules.js @@ -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, @@ -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 () { diff --git a/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/specificprice/object/item.js b/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/specificprice/object/item.js index 6f1e08e38b..89eac3817f 100644 --- a/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/specificprice/object/item.js +++ b/src/CoreShop/Bundle/ProductBundle/Resources/public/pimcore/js/specificprice/object/item.js @@ -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, @@ -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;