Skip to content

Commit

Permalink
Merge pull request #9 from venveo/change/abstract-field-processors
Browse files Browse the repository at this point in the history
Abstract field processors
  • Loading branch information
Mosnar authored Jul 22, 2019
2 parents 46f19e2 + 9f10a0b commit 3b60469
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 112 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Bulk Edit Changelog

## 1.1.1 - 2019-07-22
### Fixed
- Fixed potential issues with merge strategies defaulting to replace

### Changed
- Abstracted field handling

### Added
- `EVENT_REGISTER_FIELD_PROCESSORS`

### Removed
- `EVENT_REGISTER_SUPPORTED_FIELDS` use `EVENT_REGISTER_FIELD_PROCESSORS` instead

## 1.1.0.1 - 2019-07-21
### Fixed
- Fixed an issue with bulkEdit component not being set
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "venveo/craft-bulkedit",
"description": "Bulk edit entries",
"type": "craft-plugin",
"version": "1.1.0.1",
"version": "1.1.1",
"keywords": [
"craft",
"cms",
Expand Down
56 changes: 56 additions & 0 deletions src/base/AbstractFieldProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace venveo\bulkedit\base;

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');
}

/**
* @param FieldInterface $field
* @return bool
*/
public static function supportsField(FieldInterface $field): bool
{
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;
}
}
}
27 changes: 27 additions & 0 deletions src/base/FieldProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace venveo\bulkedit\base;

use craft\base\Element;
use craft\base\Field;

interface FieldProcessorInterface
{
/**
* An array of class names for supported fields
* @return array
*/
public static function getSupportedFields(): array;

/**
* Returns the supported strategies for this field type
* @return 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
62 changes: 62 additions & 0 deletions src/fields/processors/PlainTextProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace venveo\bulkedit\fields\processors;

use craft\base\Element;
use craft\base\Field;
use craft\fields\Checkboxes;
use craft\fields\Color;
use craft\fields\Date;
use craft\fields\Dropdown;
use craft\fields\Email;
use craft\fields\Lightswitch;
use craft\fields\MultiSelect;
use craft\fields\Number;
use craft\fields\PlainText;
use craft\fields\RadioButtons;
use craft\fields\Table;
use craft\redactor\Field as RedactorField;
use craft\fields\Url;
use venveo\bulkedit\base\AbstractFieldProcessor;
use venveo\bulkedit\services\BulkEdit;

class PlainTextProcessor extends AbstractFieldProcessor
{

/**
* The fully qualified class name for the element this processor works on
* @return array
*/
public static function getSupportedFields(): array
{
$fields = [
PlainText::class,
Number::class,
Color::class,
Checkboxes::class,
Dropdown::class,
Date::class,
Table::class,
RadioButtons::class,
Lightswitch::class,
Url::class,
Email::class,
MultiSelect::class
];

if (\Craft::$app->plugins->isPluginInstalled('redactor')) {
$fields[] = RedactorField::class;
}

return $fields;
}

/**
* Returns the supported strategies for this field type
* @return array
*/
public static function getSupportedStrategies(): array
{
return [BulkEdit::STRATEGY_REPLACE];
}
}
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 3b60469

Please sign in to comment.