Skip to content

Commit

Permalink
Cleaned up some queue logging & disabled custom field support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mosnar committed Oct 1, 2018
1 parent 2b30533 commit 4c32d9d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 64 deletions.
29 changes: 7 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Bulk Edit plugin for Craft CMS 3.x
This plugin is built to offer extended editing functionality of a set of Craft elements.
This plugin is built to offer extended editing functionality of a set of Craft entries.

## Steps to use:
1. Select the subset of entries you would like to edit

![Screenshot](resources/img/plugin-logo.png)
![Screenshot](resources/img/screenshot1.png)
![Screenshot](resources/img/screenshot2.png)
![Screenshot](resources/img/screenshot3.png)

## Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later.
This plugin requires Craft CMS 3.0.0 or later.

## Installation

Expand All @@ -24,22 +24,7 @@ To install the plugin, follow these instructions.

3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Bulk Edit.

## Bulk Edit Overview

-Insert text here-

## Configuring Bulk Edit

-Insert text here-

## Using Bulk Edit

-Insert text here-

## Bulk Edit Roadmap

Some things to do, and ideas for potential features:

* Release it
## Known Issues
* I've had to disabled custom field support as well as Matrix support for the moment. Some refactoring will need to be done to support these types of fields.

Brought to you by [Venveo](https://venveo.com)
5 changes: 4 additions & 1 deletion src/controllers/BulkEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function actionGetFields(): Response
$view = \Craft::$app->getView();
$modalHtml = $view->renderTemplate('bulkedit/elementactions/BulkEdit/_fields', [
'layouts' => $layouts,
'bulkedit' => $service,
'elementIds' => $elementIds,
'site' => $site
]);
Expand Down Expand Up @@ -118,7 +119,9 @@ public function actionGetEditScreen(): Response {
/** @var Field $field */
foreach ($fields as $field) {
$fieldModel = \Craft::$app->fields->getFieldById($field->id);
$fieldModels[] = $fieldModel;
if ($fieldModel && BulkEdit::$plugin->bulkEdit->isFieldSupported($fieldModel)) {
$fieldModels[] = $fieldModel;
}
}
} catch (\Exception $e) {
throw $e;
Expand Down
8 changes: 8 additions & 0 deletions src/queue/jobs/SaveBulkEditJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public function execute($queue = null)
throw $e;
}

if (($key + 1) === $totalSteps) {
try {
$this->context->delete();
} catch (\Exception $e) {
throw new Exception('Couldn’t delete context: ' . $e->getMessage());
}
}

$this->setProgress($queue, ($key + 1) / $totalSteps);
}
} catch (\Exception $e) {
Expand Down
92 changes: 53 additions & 39 deletions src/services/BulkEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,36 @@
namespace venveo\bulkedit\services;

use Craft;
use craft\base\Field;
use craft\elements\actions\Edit;
use craft\elements\Category;
use craft\base\Component;
use craft\base\FieldInterface;
use craft\elements\Entry;
use craft\models\FieldGroup;
use craft\records\Element;
use craft\fields\BaseRelationField;
use craft\fields\Checkboxes;
use craft\fields\Color;
use craft\fields\Date;
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\fields\Url;
use craft\records\FieldLayout;
use venveo\bulkedit\BulkEdit as Plugin;

use craft\base\Component;
use venveo\bulkedit\records\EditContext;
use venveo\bulkedit\records\History;

/**
* ElementEditor Service
*
* All of your plugin’s business logic should go in services, including saving data,
* retrieving data, etc. They provide APIs that your controllers, template variables,
* and other plugins can interact with.
*
* https://craftcms.com/docs/plugins/services
*
* @author Venveo
* @package BulkEdit
* @since 1.0.0
*/
class BulkEdit extends Component
{
// Public Methods
// =========================================================================


/**
* Get all distinct field layouts from a set of elements
*
* @param $elementIds
* @return FieldLayout
*/
public function getFieldLayoutsForElementIds($elementIds)
{
Expand All @@ -57,30 +51,27 @@ public function getFieldLayoutsForElementIds($elementIds)
->from('{{%fieldlayouts}} fieldlayouts')
->leftJoin('{{%elements}} elements', 'elements.fieldLayoutId = fieldlayouts.id')
->where(['IN', 'elements.id', $elementIds])
->with(['fields'])
->all();

return $layouts;
}

public function getBaseElementForFieldIds($fieldIds) {
$elements = [];
//
// foreach($fieldIds as $fieldId) {
//
//// \Craft::$app->elements->getPlaceholderElement()
// }
$layoutsModels = [];
/** @var FieldLayout $layout */
foreach($layouts as $layout) {
$layoutsModels[$layout->id] = ['fields' => \Craft::$app->fields->getFieldsByLayoutId($layout->id)];
}
return $layoutsModels;
}

public function getBulkEditContextFromId($id) {
public function getBulkEditContextFromId($id)
{
return EditContext::findOne($id);
}

/**
* @param EditContext $context
* @return array
*/
public function getPendingElementIdsFromContext(EditContext $context) {
public function getPendingElementIdsFromContext(EditContext $context): array
{
$items = array_keys(History::find()
->limit(null)
->where(['=', 'contextId', $context->id])
Expand All @@ -93,7 +84,7 @@ public function getPendingElementIdsFromContext(EditContext $context) {
* @param EditContext $context
* @return \yii\db\ActiveQueryInterface
*/
public function getPendingHistoryFromContext(EditContext $context)
public function getPendingHistoryFromContext(EditContext $context): \yii\db\ActiveQueryInterface
{
return $context->getHistoryItems()->where(['=', 'status', 'pending']);
}
Expand All @@ -103,14 +94,16 @@ public function getPendingHistoryFromContext(EditContext $context)
* @param $elementId
* @return \yii\db\ActiveQueryInterface
*/
public function getPendingHistoryForElement(EditContext $context, $elementId) {
public function getPendingHistoryForElement(EditContext $context, $elementId): \yii\db\ActiveQueryInterface
{
$items = $this->getPendingHistoryFromContext($context);
$items->where(['=', 'elementId', $elementId]);
return $items;
}

/**
* Takes an array of history changes for a particular element and saves it to that element.
*
* @param $historyItems
* @param \craft\base\Element $element
* @return \craft\base\Element
Expand All @@ -132,6 +125,7 @@ public function processHistoryItemsForElement($historyItems, \craft\base\Element

switch (get_class($element)) {
case Entry::class:
// Save a revision
\Craft::$app->entryRevisions->saveVersion($element);
break;
default:
Expand All @@ -148,8 +142,28 @@ public function processHistoryItemsForElement($historyItems, \craft\base\Element
}
}


public function tryToSaveVersion(\craft\base\Element $element, $context) {
// TODO Save a version and store the version ID somewhere
public function isFieldSupported(FieldInterface $field): bool
{
$supportedFields = [
PlainText::class,
Number::class,
BaseRelationField::class,
Color::class,
Checkboxes::class,
Date::class,
Table::class,
RadioButtons::class,
Lightswitch::class,
Url::class,
Email::class,
MultiSelect::class
];

foreach ($supportedFields as $fieldItem) {
if ($field instanceof $fieldItem) {
return true;
}
}
return false;
}
}
2 changes: 2 additions & 0 deletions src/templates/elementactions/BulkEdit/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
} only %}
{% endnamespace %}
</div>
{% else %}
<p>It doesn't look like there are any fields that support bulk editing on these elements.</p>
{% endfor %}

<div class="field-edit-modal-bar">
Expand Down
8 changes: 6 additions & 2 deletions src/templates/elementactions/BulkEdit/_fields.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
</th>
</thead>
{% for layout in layouts %}
{% for fieldlayoutfield in layout.fields %}
{% set field = fieldlayoutfield.field %}
{% for field in layout.fields %}
{% set fieldUnsupported = false %}
{% if not bulkedit.isFieldSupported(field) %}
{% set fieldUnsupported = true %}
{% endif %}
<tr>
<td>
<span>
Expand All @@ -46,6 +49,7 @@
id: 'fieldEnabled-'~field.handle,
name: 'fieldIds[]',
on: false,
disabled: fieldUnsupported,
value: field.id,
}) }}
</span>
Expand Down

0 comments on commit 4c32d9d

Please sign in to comment.