-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from venveo/develop
Develop
- Loading branch information
Showing
9 changed files
with
375 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.