From 95e60414b1c66e01987df2d917f3930b2a236fe4 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Fri, 2 Oct 2020 16:03:03 +0200
Subject: [PATCH] refactor: AttributeGroup -> displays values at variants and
 is selectable at normal products

---
 .../getSetFieldAttributesToProductsCmd.php    |   4 +-
 ajax/products/update.php                      |   7 +-
 bin/controls/fields/types/AttributeGroup.js   | 135 +++++++++++++++++-
 bin/controls/products/Product.js              |  13 +-
 bin/controls/products/ProductVariant.js       |   2 +
 5 files changed, 146 insertions(+), 15 deletions(-)

diff --git a/ajax/products/getSetFieldAttributesToProductsCmd.php b/ajax/products/getSetFieldAttributesToProductsCmd.php
index ad03217c..5017cb92 100644
--- a/ajax/products/getSetFieldAttributesToProductsCmd.php
+++ b/ajax/products/getSetFieldAttributesToProductsCmd.php
@@ -12,9 +12,7 @@
     function ($fieldId) {
         $cmsDir = QUI::conf('globals', 'cms_dir');
 
-        return 'php '.$cmsDir.'quiqqer.php --username=USERNAME --password=PASSWORD'
-               .' --tool=products:set-field-attributes-to-products'
-               .' --fieldId='.$fieldId;
+        return $cmsDir.'console products:set-field-attributes-to-products --fieldId='.$fieldId;
     },
     ['fieldId'],
     'Permission::checkAdminUser'
diff --git a/ajax/products/update.php b/ajax/products/update.php
index 5cd0edb8..2c394277 100644
--- a/ajax/products/update.php
+++ b/ajax/products/update.php
@@ -4,6 +4,9 @@
  * This file contains package_quiqqer_products_ajax_products_update
  */
 
+use \QUI\ERP\Products\Product\Types\VariantParent;
+use \QUI\ERP\Products\Product\Types\VariantChild;
+
 /**
  * Update a product
  *
@@ -41,7 +44,9 @@ function ($productId, $categories, $categoryId, $fields) {
                     continue;
                 }
 
-                if ($ProductField instanceof QUI\ERP\Products\Field\Types\AttributeGroup) {
+                if ($ProductField instanceof QUI\ERP\Products\Field\Types\AttributeGroup
+                    && ($Product instanceof VariantParent || $Product instanceof VariantChild)
+                ) {
                     continue;
                 }
 
diff --git a/bin/controls/fields/types/AttributeGroup.js b/bin/controls/fields/types/AttributeGroup.js
index b0932d6d..4c371a72 100644
--- a/bin/controls/fields/types/AttributeGroup.js
+++ b/bin/controls/fields/types/AttributeGroup.js
@@ -5,12 +5,17 @@
 define('package/quiqqer/products/bin/controls/fields/types/AttributeGroup', [
 
     'qui/QUI',
-    'qui/controls/Control'
+    'qui/controls/Control',
+    'package/quiqqer/products/bin/Fields',
+    'Locale',
 
-], function (QUI, QUIControl) {
+    'css!package/quiqqer/products/bin/controls/fields/types/AttributeGroup.css'
+
+], function (QUI, QUIControl, Fields, QUILocale) {
     "use strict";
 
     return new Class({
+
         Extends: QUIControl,
         Type   : 'package/quiqqer/products/bin/controls/fields/types/AttributeGroup',
 
@@ -18,9 +23,15 @@ define('package/quiqqer/products/bin/controls/fields/types/AttributeGroup', [
             '$onImport'
         ],
 
+        options: {
+            fieldId: false
+        },
+
         initialize: function (options) {
             this.parent(options);
 
+            this.$initValue = false;
+
             this.addEvents({
                 onImport: this.$onImport
             });
@@ -30,7 +41,123 @@ define('package/quiqqer/products/bin/controls/fields/types/AttributeGroup', [
          * event : on import
          */
         $onImport: function () {
-            this.getElm().addClass('field-container-field');
+            this.$Input = this.getElm();
+
+            this.$Elm = new Element('div').wraps(this.$Input);
+            this.$Elm.addClass('field-container-field');
+            this.$Elm.addClass('field-container-field-no-padding');
+            this.$Elm.addClass('quiqqer-products-field-attributeGroup');
+            this.$Elm.set('data-quiid', this.getId());
+
+            this.$Select = new Element('select').inject(this.$Elm);
+            this.$Select.addClass('quiqqer-products-field-attributeGroup-select');
+            this.$Select.addEvent('change', function (e) {
+                this.$Input.value = e.target.value;
+            }.bind(this));
+
+            this.$Select.name = this.$Input.name;
+            this.$Select.set('data-quiid', this.getId());
+
+            this.$Input.name = '';
+
+            this.$loadOptions().catch(function (err) {
+                console.error(err);
+            });
+        },
+
+        /**
+         * Set data to the field
+         *
+         * @param val
+         */
+        setData: function (val) {
+            this.$initValue = val;
+
+            if (!this.$Select && this.$Elm) {
+                this.$Elm.value = val;
+            }
+
+            if (this.$Select) {
+                this.$Select.value = val;
+            }
+
+            this.addEvent('onLoad', function () {
+                this.$Select.value = val;
+            }.bind(this));
+        },
+
+        /**
+         * load the options for the field
+         *
+         * @return {Promise<void>}
+         */
+        $loadOptions: function () {
+            var self    = this,
+                fieldId = parseInt(this.$Select.get('name').replace('field-', '')),
+                value   = this.$initValue;
+
+            self.$Select.set('disabled', true);
+            self.$Select.set('html', '');
+
+            return Fields.getFieldOptions(fieldId).then(function (options) {
+                if (typeof options.entries === 'undefined') {
+                    return;
+                }
+
+                var i, len, text;
+                var current = QUILocale.getCurrent();
+                var entries = options.entries;
+
+                if (!entries.length) {
+                    new Element('option', {
+                        html : '---',
+                        value: ''
+                    }).inject(self.$Select);
+                } else if (entries.length > 1) {
+                    new Element('option', {
+                        html : '',
+                        value: ''
+                    }).inject(self.$Select);
+                }
+
+                for (i = 0, len = entries.length; i < len; i++) {
+                    if (typeof entries[i].title[current] !== 'undefined') {
+                        text = entries[i].title[current];
+                    } else if (typeOf(entries[i].title) === 'object') {
+                        text = entries[i].title[Object.keys(entries[i].title)[0]];
+                    } else {
+                        text = '';
+                    }
+
+                    new Element('option', {
+                        html : text,
+                        value: entries[i].valueId
+                    }).inject(self.$Select);
+                }
+
+                if (value) {
+                    self.$Select.value = value;
+                } else {
+                    var selected = entries.filter(function (entry) {
+                        return entry.selected;
+                    });
+
+                    if (selected.length) {
+                        self.$Select.value = selected[0].valueId;
+                    }
+                }
+
+                // if variant, than disable
+                // varianten können ihre attribute listen nicht mehr ändern
+                // da es sonst wegen doppelten varianten probleme geben kann
+                var isVariantPanel = self.getElm().getParent('.panel-product-variant');
+
+                if (!isVariantPanel) {
+                    self.$Select.set('disabled', false);
+                }
+
+                self.fireEvent('load', [self]);
+            });
         },
 
         /**
@@ -39,7 +166,7 @@ define('package/quiqqer/products/bin/controls/fields/types/AttributeGroup', [
          * @returns {String}
          */
         getValue: function () {
-            return this.getElm().value;
+            return this.$Select.value;
         }
     });
 });
diff --git a/bin/controls/products/Product.js b/bin/controls/products/Product.js
index 77736dbd..6e51b234 100644
--- a/bin/controls/products/Product.js
+++ b/bin/controls/products/Product.js
@@ -488,13 +488,12 @@ define('package/quiqqer/products/bin/controls/products/Product', [
                         if (field.type === 'TextareaMultiLang' ||
                             field.type === 'Textarea' ||
                             field.type === 'Folder' ||
-                            field.type === 'Products' ||
-                            field.type === 'AttributeGroup'
+                            field.type === 'Products'
                         ) {
                             continue;
                         }
 
-                        // wenn es ein feld ist, welcher der kunde ausfüllen muss
+                        // wenn es ein feld ist, welches der kunde ausfüllen muss
                         // nicht anzeigen
                         if (field.custom) {
                             continue;
@@ -759,9 +758,9 @@ define('package/quiqqer/products/bin/controls/products/Product', [
                 var Input = Form.elements['field-' + field.id];
 
                 if (typeof Input !== 'undefined') {
-                    if (typeOf(field.value) !== 'string'
-                        && field.value !== null
-                        && typeOf(field.value) !== 'number'
+                    if (typeOf(field.value) !== 'string' &&
+                        field.value !== null &&
+                        typeOf(field.value) !== 'number'
                     ) {
                         field.value = JSON.encode(field.value);
                     }
@@ -772,7 +771,7 @@ define('package/quiqqer/products/bin/controls/products/Product', [
                         var Control = QUI.Controls.getById(Input.get('data-quiid'));
 
                         if (typeof Control.setData === 'function') {
-                            Control.setData(Input.value);
+                            Control.setData(field.value);
                         }
                     }
                 }
diff --git a/bin/controls/products/ProductVariant.js b/bin/controls/products/ProductVariant.js
index 74b59103..f368e41c 100644
--- a/bin/controls/products/ProductVariant.js
+++ b/bin/controls/products/ProductVariant.js
@@ -115,6 +115,8 @@ define('package/quiqqer/products/bin/controls/products/ProductVariant', [
             this.$ActionSeparator = this.getButtons('actionSeparator');
             this.$CopyButton      = this.getButtons('copy');
 
+            this.$Elm.addClass('panel-product-variant');
+
             this.parent().then(function () {
                 return self.$checkProductParent();
             }).then(function () {
-- 
GitLab