Skip to content

Commit

Permalink
Merge pull request #15 from venveo/develop
Browse files Browse the repository at this point in the history
Compatibility improvements & tweaks
  • Loading branch information
Mosnar authored Jan 24, 2020
2 parents 6e40edb + 6f21bdb commit a366fae
Show file tree
Hide file tree
Showing 22 changed files with 320 additions and 262 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# Bulk Edit Changelog

## Unreleased
## 2.0.0 - 2020-01-24
{warning} The FieldProcessorInterface has slightly changed to better
support old versions of PHP. If you wrote your own FieldProcessor,
ensure it has been updates prior to updating to 2.0.0

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

### Changed
- Changed FieldProcessorInterface to remove void declaration

### Fixed
- Improved compatibility with older versions of php

## 1.1.1 - 2019-07-22
### Fixed
- Fixed potential issues with merge strategies defaulting to replace
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.1",
"version": "2.0.0",
"keywords": [
"craft",
"cms",
Expand Down
41 changes: 20 additions & 21 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace venveo\bulkedit;

use Craft;
use craft\base\Element;
use craft\base\Plugin as BasePlugin;
use craft\commerce\elements\Product;
Expand All @@ -34,19 +35,17 @@
class Plugin extends BasePlugin
{

const PERMISSION_BULKEDIT_ENTRIES = 'PERMISSION_BULKEDIT_ENTRIES';
const PERMISSION_BULKEDIT_PRODUCTS = 'PERMISSION_BULKEDIT_PRODUCTS';
const PERMISSION_BULKEDIT_ASSETS = 'PERMISSION_BULKEDIT_ASSETS';
const PERMISSION_BULKEDIT_CATEGORIES = 'PERMISSION_BULKEDIT_CATEGORIES';
const PERMISSION_BULKEDIT_USERS = 'PERMISSION_BULKEDIT_USERS';
/**
* @var Plugin
*/
public static $plugin;

public $schemaVersion = '1.1.0';

public const PERMISSION_BULKEDIT_ENTRIES = 'PERMISSION_BULKEDIT_ENTRIES';
public const PERMISSION_BULKEDIT_PRODUCTS = 'PERMISSION_BULKEDIT_PRODUCTS';
public const PERMISSION_BULKEDIT_ASSETS = 'PERMISSION_BULKEDIT_ASSETS';
public const PERMISSION_BULKEDIT_CATEGORIES = 'PERMISSION_BULKEDIT_CATEGORIES';
public const PERMISSION_BULKEDIT_USERS = 'PERMISSION_BULKEDIT_USERS';

public function init()
{
parent::init();
Expand All @@ -57,62 +56,62 @@ public function init()
Event::on(UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, function (RegisterUserPermissionsEvent $event) {
$permissions = [];
$permissions[self::PERMISSION_BULKEDIT_ENTRIES] = [
'label' => \Craft::t('venveo-bulk-edit', 'Bulk Edit Entries')
'label' => Craft::t('venveo-bulk-edit', 'Bulk Edit Entries')
];
$permissions[self::PERMISSION_BULKEDIT_ASSETS] = [
'label' => \Craft::t('venveo-bulk-edit', 'Bulk Edit Assets')
'label' => Craft::t('venveo-bulk-edit', 'Bulk Edit Assets')
];
$permissions[self::PERMISSION_BULKEDIT_CATEGORIES] = [
'label' => \Craft::t('venveo-bulk-edit', 'Bulk Edit Categories')
'label' => Craft::t('venveo-bulk-edit', 'Bulk Edit Categories')
];
$permissions[self::PERMISSION_BULKEDIT_USERS] = [
'label' => \Craft::t('venveo-bulk-edit', 'Bulk Edit Users')
'label' => Craft::t('venveo-bulk-edit', 'Bulk Edit Users')
];

if (\Craft::$app->plugins->isPluginInstalled('commerce')) {
if (Craft::$app->plugins->isPluginInstalled('commerce')) {
$permissions[self::PERMISSION_BULKEDIT_PRODUCTS] = [
'label' => \Craft::t('venveo-bulk-edit', 'Bulk Edit Products')
'label' => Craft::t('venveo-bulk-edit', 'Bulk Edit Products')
];
}

$event->permissions[\Craft::t('venveo-bulk-edit', 'Bulk Edit')] = $permissions;
$event->permissions[Craft::t('venveo-bulk-edit', 'Bulk Edit')] = $permissions;
});

if (\Craft::$app->request->isCpRequest) {
if (\Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ENTRIES)) {
if (Craft::$app->request->isCpRequest) {
if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ENTRIES)) {
Event::on(Entry::class, Element::EVENT_REGISTER_ACTIONS,
function (RegisterElementActionsEvent $event) {
$event->actions[] = BulkEditElementAction::class;
}
);
}

if (\Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_CATEGORIES)) {
if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_CATEGORIES)) {
Event::on(Category::class, Element::EVENT_REGISTER_ACTIONS,
function (RegisterElementActionsEvent $event) {
$event->actions[] = BulkEditElementAction::class;
}
);
}

if (\Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ASSETS)) {
if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_ASSETS)) {
Event::on(Asset::class, Element::EVENT_REGISTER_ACTIONS,
function (RegisterElementActionsEvent $event) {
$event->actions[] = BulkEditElementAction::class;
}
);
}

if (\Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_USERS)) {
if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_USERS)) {
Event::on(User::class, Element::EVENT_REGISTER_ACTIONS,
function (RegisterElementActionsEvent $event) {
$event->actions[] = BulkEditElementAction::class;
}
);
}

if (\Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_PRODUCTS)) {
if (\Craft::$app->plugins->isPluginInstalled('commerce') && class_exists(Product::class)) {
if (Craft::$app->user->checkPermission(self::PERMISSION_BULKEDIT_PRODUCTS)) {
if (Craft::$app->plugins->isPluginInstalled('commerce') && class_exists(Product::class)) {
Event::on(Product::class, Element::EVENT_REGISTER_ACTIONS,
function (RegisterElementActionsEvent $event) {
$event->actions[] = BulkEditElementAction::class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace venveo\bulkedit\assetbundles\bulkeditelementaction;

use Craft;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ Craft.BulkEditModal = Garnish.Modal.extend(
this._initSpinner();
this.requestId++;

var viewParams = Craft.elementIndex.getViewParams();
Craft.postActionRequest('venveo-bulk-edit/bulk-edit/get-fields',
{
elementIds: elementIds,
requestId: this.requestId,
elementType: Craft.elementIndex.elementType
viewParams: viewParams
}, function(response, textStatus) {
if (textStatus === 'success') {
if (response.success) {
Expand Down Expand Up @@ -136,6 +137,7 @@ Craft.BulkEditModal = Garnish.Modal.extend(

_bindEventHandlersForFieldSelect: function() {
this.$container.find('#field-edit-cancel').on('click', this.hide.bind(this));
this.$container.find('#select-all-elements').on('click', this._handleSelectAllElementsClicked.bind(this));
this.$container.find('#fields-table .lightswitch').on('change', this._handleFieldSelect.bind(this));
this.$container.find('.submit').on('click', this._handleFieldSelectSubmit.bind(this));
},
Expand Down Expand Up @@ -164,6 +166,11 @@ Craft.BulkEditModal = Garnish.Modal.extend(
}.bind(this));
},

_handleSelectAllElementsClicked: function(e) {
e.preventDefault();
this._initSpinner();
},

_handleFieldSelectSubmit: function(e) {
e.preventDefault();
const data = this.$container.find('form').serialize();
Expand Down
4 changes: 3 additions & 1 deletion src/base/AbstractElementTypeProcessor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace venveo\bulkedit\base;

abstract class AbstractElementTypeProcessor implements ElementTypeProcessorInterface {
abstract class AbstractElementTypeProcessor implements ElementTypeProcessorInterface
{

}
65 changes: 33 additions & 32 deletions src/base/AbstractFieldProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,11 @@
use craft\base\Element;
use craft\base\Field;
use craft\base\FieldInterface;
use RuntimeException;
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');
}

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 @@ -54,7 +24,7 @@ public static function supportsField(FieldInterface $field): bool
return false;
}

public static function processElementField(Element $element, Field $field, $strategy, $newValue): void
public static function processElementField(Element $element, Field $field, $strategy, $newValue)
{
switch ($strategy) {
case BulkEdit::STRATEGY_REPLACE:
Expand All @@ -77,4 +47,35 @@ public static function processElementField(Element $element, Field $field, $stra
break;
}
}

public static function performReplacement(Element $element, Field $field, $value)
{
$fieldHandle = $field->handle;
$element->setFieldValue($fieldHandle, $value);
}

public static function performMerge(Element $element, Field $field, $value)
{
throw new RuntimeException('Merge not implemented for this field type');
}

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)
{
throw new RuntimeException('Addition not implemented for this field type');
}

public static function performMultiplication(Element $element, Field $field, $value)
{
throw new RuntimeException('Multiplication not implemented for this field type');
}

public static function performDivision(Element $element, Field $field, $value)
{
throw new RuntimeException('Division not implemented for this field type');
}
}
4 changes: 3 additions & 1 deletion src/base/ElementTypeProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace venveo\bulkedit\base;

use craft\web\User;

interface ElementTypeProcessorInterface {
interface ElementTypeProcessorInterface
{

/**
* Gets a unique list of field layouts from a list of element IDs
Expand Down
6 changes: 3 additions & 3 deletions src/base/FieldProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static function getSupportedFields(): array;
*/
public static function getSupportedStrategies(): array;

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

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

public static function performMerge(Element $element, Field $field, $value): void;
public static function performMerge(Element $element, Field $field, $value);
}
Loading

0 comments on commit a366fae

Please sign in to comment.