Skip to content

Commit

Permalink
Merge pull request #11 from venveo/develop
Browse files Browse the repository at this point in the history
New strategies
  • Loading branch information
Mosnar authored Jul 22, 2019
2 parents c85fbcd + 90d4895 commit 6e40edb
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Bulk Edit Changelog

## Unreleased
### Added
- Add support for new strategies: ADD, MULTIPLY, DIVIDE
- Number fields now have the following strategies: REPLACE, ADD, SUBTRACT, MULTIPLY, DIVIDE

## 1.1.1 - 2019-07-22
### Fixed
- Fixed potential issues with merge strategies defaulting to replace
Expand Down
24 changes: 24 additions & 0 deletions src/base/AbstractFieldProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
}
68 changes: 68 additions & 0 deletions src/fields/processors/NumberFieldProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace venveo\bulkedit\fields\processors;

use craft\base\Element;
use craft\base\Field;
use craft\fields\Number;
use venveo\bulkedit\base\AbstractFieldProcessor;
use venveo\bulkedit\services\BulkEdit;

class NumberFieldProcessor extends AbstractFieldProcessor
{

/**
* The fully qualified class name for the element this processor works on
* @return array
*/
public static function getSupportedFields(): array
{
return [
Number::class
];
}

/**
* Returns the supported strategies for this field type
* @return array
*/
public static function getSupportedStrategies(): array
{
return [BulkEdit::STRATEGY_REPLACE, BulkEdit::STRATEGY_SUBTRACT, BulkEdit::STRATEGY_ADD, BulkEdit::STRATEGY_MULTIPLY, BulkEdit::STRATEGY_DIVIDE];
}

public static function performAddition(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 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);
}
}
1 change: 0 additions & 1 deletion src/fields/processors/PlainTextProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static function getSupportedFields(): array
{
$fields = [
PlainText::class,
Number::class,
Color::class,
Checkboxes::class,
Dropdown::class,
Expand Down
16 changes: 15 additions & 1 deletion src/services/BulkEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -336,7 +349,8 @@ public function getFieldProcessors(): array
}
$processors = [
PlainTextProcessor::class,
RelationFieldProcessor::class
RelationFieldProcessor::class,
NumberFieldProcessor::class
];

$event = new RegisterComponentTypesEvent();
Expand Down

0 comments on commit 6e40edb

Please sign in to comment.