]> _ Git - cubist_cms-back.git/commitdiff
wip #6571 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 29 Dec 2023 14:41:49 +0000 (15:41 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 29 Dec 2023 14:41:49 +0000 (15:41 +0100)
src/app/Magic/Fields/Field.php
src/resources/views/when.blade.php

index cb49bec319f18de9fb2ea21401cd25d765613c47..8bae1708932fc4388c77e50678aea015c9174967 100644 (file)
@@ -406,8 +406,8 @@ class Field implements \ArrayAccess
         $wrapperAttributes['data-default'] = $this->filterDefault($this->getAttribute('default'));
         $wrapperAttributes['data-name'] = $this->getAttribute('name');
 
-        if ($this->hasAttribute('when')) {
-            $wrapperAttributes['data-when'] = json_encode($this->getAttribute('when'));
+        if ($when = $this->when()) {
+            $wrapperAttributes['data-when'] = $when;
         }
 
         if (!$this->getAttribute('non_default_tracking')) {
@@ -416,6 +416,33 @@ class Field implements \ArrayAccess
         $this->setAttribute('wrapperAttributes', $wrapperAttributes);
     }
 
+    protected function when(): bool|string
+    {
+        $defaultOperator = '=';
+        if (!$this->hasAttribute('when')) {
+            return false;
+        }
+
+        $when = $this->getAttribute('when');
+        $res = [];
+        foreach ($when as $attr => $w) {
+            if (is_scalar($w)) {
+                $w = ['values' => [$w], 'operator' => $defaultOperator];
+            } else {
+                if (isset($w['values'])) {
+                    if (!isset($w['operator'])) {
+                        $w['operator'] = $defaultOperator;
+                    }
+                } else {
+                    $w = ['values' => $w, 'operator' => $defaultOperator];
+                }
+            }
+            $res[$attr] = $w;
+        }
+
+        return json_encode($res);
+    }
+
     public function setPermissions()
     {
         if (null !== $this->getAttribute('can', null)) {
index e5ce471435bc53f1ee100bb1130eaee81e1d59ec..1396342ee044ac0e6b51908fd616b83e8900c7c4 100644 (file)
                     var when = $(this).data('when-normalized');
 
                     var match = true;
-                    var order = 1000;
+                    var order = 10000;
                     $.each(when, function (property, conditions) {
                         var e = findCloserInput(element, property);
-                        if ($(element).is('[bp-field-name]') && $(element).attr('bp-field-name').indexOf('logattempts') >= 0) {
-                            console.log(property, element, e);
-                        }
                         var val;
                         if ($(e).length === 0) {
+                            console.log(property, 'property value not found')
                             match = false;
                             return false;
                         }
                         }
 
                         var matchproperty = false;
-                        $.each(conditions, function (k, condition) {
-                            if ((condition.id === '_not_empty_' && val.toString().length > 0) ||
-                                condition.id === val) {
-
+                        $.each(conditions.values, function (k, condition) {
+                            if (checkCondition(condition, conditions.operator, val)) {
+                                console.log('match', property, conditions, val);
                                 matchproperty = true;
                                 order = Math.min(order, condition.order);
-                                return false;
                             }
                         });
                         if (matchproperty === false) {
+                            console.log('not match', property, conditions, val);
                             match = false
                             return false;
                         }
                 //orderWhen();
             };
 
+            function checkCondition(condition, operator, val) {
+                if (condition.id === '_not_empty_' || condition.operator === 'not_empty') {
+                    return val.toString().length > 0;
+                }
+                if (operator === '=') {
+                    return condition.id == val;
+                }
+                if (operator === '!=' || operator === '!') {
+                    return condition.id != val;
+                }
+            }
+
             function isNumber(n) {
                 return typeof n === 'number' && !isNaN(n);
             }
             }
 
             function normalizeWhen(w) {
-                var res = {};
                 $.each(w, function (property, conditions) {
-                    if (!Array.isArray(conditions)) {
-                        conditions = [conditions];
-                    }
                     var normalizedConditions = [];
-                    $.each(conditions, function (k, condition) {
+                    $.each(conditions.values, function (k, condition) {
                         if (!isObject(condition)) {
                             condition = {id: condition};
                         }
                         condition.id = condition.id.toString();
                         normalizedConditions[k] = condition;
                     });
-                    res[property] = normalizedConditions;
+                    w[property].values = normalizedConditions;
                 });
-                return res;
+                return w;
             }
 
             function isObject(value) {
 
                 let found;
                 if ($(element).is('form')) {
-                    found = $(element).find('[data-name="' + inputName + '"]').eq(0);
+                    found = $(element).find('[bp-field-name="' + inputName + '"]').eq(0);
                 } else {
-                    found = $(element).siblings('[data-name="' + inputName + '"]');
+                    found = $(element).siblings('[bp-field-name="' + inputName + '"]');
                 }
                 if (found.length === 0) {
                     let parent = $(element).parent();