Skip to content

Commit

Permalink
Fixed merge strategies and finish basic abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosnar committed Jul 22, 2019
1 parent 94a3113 commit 452d129
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 121 deletions.
25 changes: 21 additions & 4 deletions src/base/AbstractFieldProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
use craft\base\Element;
use craft\base\Field;
use craft\base\FieldInterface;
use venveo\bulkedit\services\BulkEdit;

abstract class AbstractFieldProcessor implements FieldProcessorInterface
{
public static function performMerge(Element $element, Field $field, $value): void
{

throw new \RuntimeException('Merge not implemented for this field type');
}

public static function performReplacement(Element $element, Field $field, $value): void
{

$fieldHandle = $field->handle;
$element->setFieldValue($fieldHandle, $value);
}

public static function performSubtraction(Element $element, Field $field, $value): void
{

throw new \RuntimeException('Subtraction not implemented for this field type');
}

/**
Expand All @@ -29,11 +31,26 @@ public static function performSubtraction(Element $element, Field $field, $value
*/
public static function supportsField(FieldInterface $field): bool
{
foreach (self::getSupportedFields() as $fieldType) {
foreach (static::getSupportedFields() as $fieldType) {
if ($field instanceof $fieldType) {
return true;
}
}
return false;
}

public static function processElementField(Element $element, Field $field, $strategy, $newValue): void
{
switch ($strategy) {
case BulkEdit::STRATEGY_REPLACE:
static::performReplacement($element, $field, $newValue);
break;
case BulkEdit::STRATEGY_MERGE:
static::performMerge($element, $field, $newValue);
break;
case BulkEdit::STRATEGY_SUBTRACT:
static::performSubtraction($element, $field, $newValue);
break;
}
}
}
2 changes: 2 additions & 0 deletions src/base/FieldProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static function getSupportedFields(): array;
public static function getSupportedStrategies(): array;

public static function performReplacement(Element $element, Field $field, $value): void;

public static function performSubtraction(Element $element, Field $field, $value): void;

public static function performMerge(Element $element, Field $field, $value): void;
}
2 changes: 1 addition & 1 deletion src/controllers/BulkEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function actionSaveContext()
$elementIds = explode(',', $elementIds);

try {
Plugin::$plugin->bulkEdit->saveContext($elementType, $siteId, $elementIds, $fieldIds, $keyedFieldValues);
Plugin::$plugin->bulkEdit->saveContext($elementType, $siteId, $elementIds, $fieldIds, $keyedFieldValues, $fieldStrategies);

return $this->asJson([
'success' => true
Expand Down
6 changes: 0 additions & 6 deletions src/fields/processors/PlainTextProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,4 @@ public static function getSupportedStrategies(): array
{
return [BulkEdit::STRATEGY_REPLACE];
}


public static function performReplacement(Element $element, Field $field, $value): void
{

}
}
54 changes: 54 additions & 0 deletions src/fields/processors/RelationFieldProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace venveo\bulkedit\fields\processors;

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

class RelationFieldProcessor extends AbstractFieldProcessor
{

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

return $fields;
}

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

public static function performSubtraction(Element $element, Field $field, $value): void
{
$fieldHandle = $field->handle;
$originalValue = $element->getFieldValue($fieldHandle);
$ids = $originalValue->ids();
$ids = array_diff($ids, $value);
$element->setFieldValue($fieldHandle, $ids);
}

public static function performMerge(Element $element, Field $field, $value): void
{
$originalValue = $element->getFieldValue($field->handle);
$fieldHandle = $field->handle;
$ids = $originalValue->ids();
$ids = array_merge($ids, $value);
$element->setFieldValue($fieldHandle, $ids);
}
}
2 changes: 1 addition & 1 deletion src/queue/jobs/SaveBulkEditJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function execute($queue = null)
throw new \Exception('Unexpected element type encountered!');
}

$history = Plugin::$plugin->bulkEdit->getPendingHistoryForElement($this->context, $element->id)->all();
$history = Plugin::$plugin->bulkEdit->getPendingHistoryFromContext($this->context, $element->id)->all();
try {
Craft::info('Starting processing bulk edit job', __METHOD__);
Plugin::$plugin->bulkEdit->processHistoryItemsForElement($history, $element);
Expand Down
Loading

0 comments on commit 452d129

Please sign in to comment.