Skip to content

Commit

Permalink
Change layout, add delete option, add lightswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
chasegiunta committed Aug 20, 2017
1 parent ce2a200 commit e0c8563
Show file tree
Hide file tree
Showing 14 changed files with 486 additions and 176 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Batch Changelog

## 1.2.0 - 2017.08.20

### Added
* Support for batch deletion of entries & users
* Lightswitch field appears when setting a lightswitch field value
* Support for deleting all blocks within a matrix field

### Changed
* Now displays batch configuration in a table layout

### Fixed
* Entry type filtering

## 1.1.0 - 2017.08.18

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Batch plugin for Craft CMS

Easily batch set field values across users or entries.
Perform batch actions on your elements. Easily set field values across your elements, or filter down elements for deletion.

![Screenshot](batch/resources/screenshots/plugin_logo.png)

Expand Down
4 changes: 2 additions & 2 deletions batch/BatchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getName()
*/
public function getDescription()
{
return Craft::t('Easily batch set field values across users or entries');
return Craft::t('Perform batch actions on your elements');
}

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ public function getReleaseFeedUrl()
*/
public function getVersion()
{
return '1.1.0';
return '1.2.0';
}

/**
Expand Down
25 changes: 25 additions & 0 deletions batch/controllers/BatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function actionIndex()
$userGroup = craft()->request->getPost('userGroupSetting');
$status = craft()->request->getPost('statusSetting');
$locale = craft()->request->getPost('localeSetting');
$action = craft()->request->getPost('actionSetting');
$transferTo = craft()->request->getPost('transferContentTo');
$fieldType = craft()->request->getPost('fieldTypeSetting');
$field = craft()->request->getPost('fieldSetting');
$value = craft()->request->getPost('valueSetting');

Expand All @@ -46,6 +49,9 @@ public function actionIndex()
'userGroup' => $userGroup,
'status' => $status,
'locale' => $locale,
'action' => $action,
'transferTo' => $transferTo,
'fieldType' => $fieldType,
'field' => $field,
'value' => $value
))
Expand Down Expand Up @@ -113,4 +119,23 @@ public function actionFetchFields()

}
}

public function actionGetFieldType()
{
$fieldHandle = craft()->request->getPost('fieldHandle');
$fieldModel = craft()->fields->getFieldByHandle($fieldHandle);

if (!empty($fieldModel)) {
$fieldType = $fieldModel->type;
$response = [
'success' => true,
'fieldType' => $fieldType
];
} else {
$response = [
'success' => false
];
}
$this->returnJson($response);
}
}
27 changes: 16 additions & 11 deletions batch/resources/css/Batch_Style.css
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
* @since 1.0.0
*/

input#sectionSetting,
input#userGroupSetting,
input#valueSetting,
input#fieldSetting {
color: #6e757d;
background-color: #EBEDEF;
font-family: Menlo, monospace;
}
[v-cloak] {
display: none
}
input#sectionSetting,
input#userGroupSetting,
input#valueSetting,
input#fieldSetting {
color: #6e757d;
background-color: #EBEDEF;
font-family: Menlo, monospace;
}
[v-cloak] {
display: none
}
@media only screen and (min-width: 993px) {
td:first-child {
min-width: 250px;
}
}
68 changes: 64 additions & 4 deletions batch/resources/js/Batch_Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @since 1.0.0
*/

window.timeout = null;

var batchSettings = new Vue({
el: '#main',
delimiters: ['${', '}'],
Expand All @@ -18,21 +20,48 @@ var batchSettings = new Vue({
section: '',
entryType: '',
entryTypes: {},
fields: ''
action: 'setValue',
fields: '',
field: '',
fieldType: '',
value: 1
},
created() {
var e = document.getElementById("sectionSetting");
var value = e.options[e.selectedIndex].value;
this.section = value;
this.fetchTypes();
},
computed: {
showField() {
if(this.action != 'delete') {
if(this.section != 'null') {
if(this.entryType != 'null' || this.entryTypes.length == 1) {
return true
}
}
if(this.elementType == 'User') {
return true
}
}
},
showHandle() {
if(this.action != 'delete') {
if(this.entryType == 'null') {
return true
}
}
}
},
methods: {
fetchTypesAndFields() {
this.fetchTypes();
this.fetchFields();
var _this = this;
setTimeout(function () {
_this.fetchFields()
}, 500);
},
fetchTypes() {

var data = {
section: this.section
};
Expand All @@ -45,7 +74,6 @@ var batchSettings = new Vue({
Craft.cp.displayError('Error fetching entry types');
}
});

},
fetchFields() {
if (((this.entryType != 'null' || this.entryTypes.length == 1) && this.section != 'null') || this.elementType == 'User') {
Expand All @@ -57,11 +85,43 @@ var batchSettings = new Vue({
var _this = this;
Craft.postActionRequest('batch/fetchFields', data, function(response) {
_this.fields = response.fields;
_this.field = response.fields[0].handle;
if (!response.success) {
Craft.cp.displayError('Error fetching fields');
}
});
}
},
getFieldType() {
var timeoutValue = (this.entryType != null || this.entryTypes.length == 1) ? 0 : 500;
clearTimeout(window.timeout);
var _this = this;
window.timeout = setTimeout(function () {
if (_this.field.trim().length > 0) { //ensure we're not checking for empty strings
var field = document.getElementById('fieldSetting');
var data = {
fieldHandle: _this.field
}
Craft.postActionRequest('batch/getFieldType', data, function(response) {
if (response !== null) {
_this.fieldType = response.fieldType;
if (!response.success) {
setTimeout(function () {
Craft.cp.displayNotice('Field with name, "'+_this.field+'", doesn\'t exist');
}, 2000);
}
}
})
}
}, timeoutValue);
},
fooBar() {
if(this.elementType != 'User' && this.action != 'delete') {
document.getElementsByName('transferContentTo')[0].setAttribute("disabled","disabled")
} else {
document.getElementsByName('transferContentTo')[0].removeAttribute("disabled")
}
}

}
})
Binary file removed batch/resources/screenshots/entry-screenshot.png
Binary file not shown.
Binary file modified batch/resources/screenshots/plugin-screenshot.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed batch/resources/screenshots/user-screenshot.png
Binary file not shown.
11 changes: 10 additions & 1 deletion batch/tasks/BatchTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ protected function defineSettings()
'entryType' => AttributeType::Mixed,
'status' => AttributeType::Mixed,
'locale' => AttributeType::Mixed,
'action' => AttributeType::Mixed,
'transferTo' => AttributeType::Mixed,
'fieldType' => AttributeType::Mixed,
'userGroup' => AttributeType::Mixed,

'field' => AttributeType::Mixed,
Expand Down Expand Up @@ -68,6 +71,9 @@ public function runStep($step)
{
$criteria = $this->getCriteria();
$elementType = $this->getSettings()->elementType;
$fieldType = $this->getSettings()->fieldType;
$action = $this->getSettings()->action;
$transferTo = $this->getSettings()->transferTo;
$field = $this->getSettings()->field;
$value = $this->getSettings()->value;

Expand All @@ -77,6 +83,9 @@ public function runStep($step)
return $this->runSubTask('Batch_SubStep', $description, array(
'criteria' => $criteria,
'elementType' => $elementType,
'fieldType' => $fieldType,
'action' => $action,
'transferTo' => $transferTo,
'step' => $step,
'field' => $field,
'value' => $value
Expand Down Expand Up @@ -111,7 +120,7 @@ public function getCriteria()
$criteria->section = $section;
}
if (!empty($entryType)) {
$criteria->section = $section;
$criteria->type = $entryType;
}
if (!empty($status)) {
$criteria->status = $status;
Expand Down
51 changes: 40 additions & 11 deletions batch/tasks/Batch_SubStepTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ protected function defineSettings()
'criteria' => AttributeType::Mixed,
'step' => AttributeType::Mixed,
'elementType' => AttributeType::Mixed,
'action' => AttributeType::Mixed,
'transferTo' => AttributeType::Mixed,
'fieldType' => AttributeType::Mixed,
'field' => AttributeType::Mixed,
'value' => AttributeType::Mixed,
);
Expand Down Expand Up @@ -53,24 +56,50 @@ public function runStep($step)
{
$criteria = $this->getSettings()->criteria;
$elementType = $this->getSettings()->elementType;
$fieldType = $this->getSettings()->fieldType;
$action = $this->getSettings()->action;
$transferTo = $this->getSettings()->transferTo;

$step = $this->getSettings()->step;
$field = trim($this->getSettings()->field);
$value = $this->getSettings()->value;

$element = $criteria[$step];

if ($field == 'enabled') {
$element->enabled = $value;
} else {
$element->getContent()->setAttributes(array(
$field => $value )
);
}
if ($action == 'setValue') {
if ($field == 'enabled') {
$element->enabled = $value;
} else {

if ($fieldType == 'Matrix') {
$fieldModel = craft()->fields->getFieldByHandle($field);
foreach ($element->$field as $block) {
craft()->matrix->deleteBlockById($block->id);
}
} else {
$element->getContent()->setAttributes(array(
$field => $value )
);
}

}

if ($elementType === 'Entry') {
craft()->entries->saveEntry($element);
} else if ($elementType === 'User') {
craft()->users->saveUser($element);
if ($elementType === 'Entry') {
craft()->entries->saveEntry($element);
} else if ($elementType === 'User') {
craft()->users->saveUser($element);
}
} else if ($action == 'delete') {
if ($elementType === 'Entry') {
craft()->entries->deleteEntry($element);
} else if ($elementType === 'User') {
if(!empty($transferTo)) {
$userModel = craft()->users->getUserById($transferTo[0]);
craft()->users->deleteUser($element, $userModel);
} else {
craft()->users->deleteUser($element, null);
}
}
}

return true;
Expand Down
11 changes: 10 additions & 1 deletion batch/templates/_layouts/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@
{ label: "Plugins"|t, url: url('settings/plugins') }
] %}

{% set title = "Batch"|t %}
{% set title = "Batch"|t %}

{% if craft.config.devMode == 0 %}
{% includeJsResource "batch/js/vue.min.js" %}
{% else %}
{% includejsfile "//unpkg.com/vue" %}
{% endif %}

{% includeCssResource "batch/css/Batch_Style.css" %}
{% includeJsResource "batch/js/Batch_Script.js" %}
Loading

0 comments on commit e0c8563

Please sign in to comment.