diff --git a/src/base/AbstractFieldProcessor.php b/src/base/AbstractFieldProcessor.php index 1db6ac9..9dd0e8f 100644 --- a/src/base/AbstractFieldProcessor.php +++ b/src/base/AbstractFieldProcessor.php @@ -25,6 +25,21 @@ public static function performSubtraction(Element $element, Field $field, $value throw new \RuntimeException('Subtraction not implemented for this field type'); } + public static function performAddition(Element $element, Field $field, $value): void + { + throw new \RuntimeException('Addition not implemented for this field type'); + } + + public static function performDivision(Element $element, Field $field, $value): void + { + throw new \RuntimeException('Division not implemented for this field type'); + } + + public static function performMultiplication(Element $element, Field $field, $value): void + { + throw new \RuntimeException('Multiplication not implemented for this field type'); + } + /** * @param FieldInterface $field * @return bool @@ -51,6 +66,15 @@ public static function processElementField(Element $element, Field $field, $stra case BulkEdit::STRATEGY_SUBTRACT: static::performSubtraction($element, $field, $newValue); break; + case BulkEdit::STRATEGY_ADD: + static::performAddition($element, $field, $newValue); + break; + case BulkEdit::STRATEGY_MULTIPLY: + static::performMultiplication($element, $field, $newValue); + break; + case BulkEdit::STRATEGY_DIVIDE: + static::performDivision($element, $field, $newValue); + break; } } } \ No newline at end of file diff --git a/src/fields/processors/NumberFieldProcessor.php b/src/fields/processors/NumberFieldProcessor.php new file mode 100644 index 0000000..4b9a71d --- /dev/null +++ b/src/fields/processors/NumberFieldProcessor.php @@ -0,0 +1,68 @@ +handle; + $originalValue = (int)$element->getFieldValue($fieldHandle); + $value = $value['value'] ?? 0; + $element->setFieldValue($fieldHandle, $originalValue + (int)$value); + } + + public static function performSubtraction(Element $element, Field $field, $value): void + { + $fieldHandle = $field->handle; + $originalValue = (int)$element->getFieldValue($fieldHandle); + + $value = $value['value'] ?? 0; + $element->setFieldValue($fieldHandle, $originalValue - (int)$value); + } + + public static function performMultiplication(Element $element, Field $field, $value): void + { + $fieldHandle = $field->handle; + $originalValue = (int)$element->getFieldValue($fieldHandle); + + $value = $value['value'] ?? 0; + $element->setFieldValue($fieldHandle, $originalValue * (int)$value); + } + + public static function performDivision(Element $element, Field $field, $value): void + { + $fieldHandle = $field->handle; + $originalValue = (int)$element->getFieldValue($fieldHandle); + + $value = $value['value'] ?? 1; + $element->setFieldValue($fieldHandle, $originalValue / (int)$value); + } +} diff --git a/src/fields/processors/PlainTextProcessor.php b/src/fields/processors/PlainTextProcessor.php index fd461aa..d316319 100644 --- a/src/fields/processors/PlainTextProcessor.php +++ b/src/fields/processors/PlainTextProcessor.php @@ -31,7 +31,6 @@ public static function getSupportedFields(): array { $fields = [ PlainText::class, - Number::class, Color::class, Checkboxes::class, Dropdown::class, diff --git a/src/services/BulkEdit.php b/src/services/BulkEdit.php index aef69c9..f54602c 100644 --- a/src/services/BulkEdit.php +++ b/src/services/BulkEdit.php @@ -24,6 +24,7 @@ use venveo\bulkedit\elements\processors\EntryProcessor; use venveo\bulkedit\elements\processors\ProductProcessor; use venveo\bulkedit\elements\processors\UserProcessor; +use venveo\bulkedit\fields\processors\NumberFieldProcessor; use venveo\bulkedit\fields\processors\PlainTextProcessor; use venveo\bulkedit\fields\processors\RelationFieldProcessor; use venveo\bulkedit\models\FieldWrapper; @@ -41,6 +42,9 @@ class BulkEdit extends Component public const STRATEGY_REPLACE = 'replace'; public const STRATEGY_MERGE = 'merge'; public const STRATEGY_SUBTRACT = 'subtract'; + public const STRATEGY_ADD = 'add'; + public const STRATEGY_MULTIPLY = 'multiply'; + public const STRATEGY_DIVIDE = 'divide'; public const EVENT_REGISTER_ELEMENT_PROCESSORS = 'registerElementProcessors'; public const EVENT_REGISTER_FIELD_PROCESSORS = 'registerFieldProcessors'; @@ -232,6 +236,15 @@ public function getSupportedStrategiesForField(FieldInterface $field): array case self::STRATEGY_SUBTRACT: $availableStrategies[] = ['value' => self::STRATEGY_SUBTRACT, 'label' => 'Subtract']; break; + case self::STRATEGY_ADD: + $availableStrategies[] = ['value' => self::STRATEGY_ADD, 'label' => 'Add']; + break; + case self::STRATEGY_DIVIDE: + $availableStrategies[] = ['value' => self::STRATEGY_DIVIDE, 'label' => 'Divide']; + break; + case self::STRATEGY_MULTIPLY: + $availableStrategies[] = ['value' => self::STRATEGY_MULTIPLY, 'label' => 'Multiply']; + break; } } @@ -336,7 +349,8 @@ public function getFieldProcessors(): array } $processors = [ PlainTextProcessor::class, - RelationFieldProcessor::class + RelationFieldProcessor::class, + NumberFieldProcessor::class ]; $event = new RegisterComponentTypesEvent();