Skip to content

Commit

Permalink
'Entry Types' option, dynamic field fetching, and other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
chasegiunta committed Aug 18, 2017
1 parent efb1fff commit 09c7b2a
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 29 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Very simple implementation. Doesn't support Matrix, Super Table, Neo, or other a
## Batch Roadmap

* Set multiple fields & values
* Fetch field handles dynamically
* Add support for other elements
* Allow filtering through twig

Expand Down
4 changes: 2 additions & 2 deletions batch/BatchPlugin.php
100644 → 100755
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 fields value across users or entries');
return Craft::t('Easily batch set field values across users or entries');
}

/**
Expand Down Expand Up @@ -95,7 +95,7 @@ public function getSchemaVersion()
*/
public function getDeveloper()
{
return 'ChaseGiunta';
return 'Chase Giunta';
}

/**
Expand Down
88 changes: 76 additions & 12 deletions batch/controllers/BatchController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,98 @@ class BatchController extends BaseController
* @var bool|array Allows anonymous access to this controller's actions.
* @access protected
*/
protected $allowAnonymous = array('actionIndex',
);
protected $allowAnonymous = true;

/**
* Handle a request going to our plugin's index action URL, e.g.: actions/batch
*/
public function actionIndex()
{
{
if (craft()->request->isPostRequest())
{
$elementType = craft()->request->getPost('elementTypeSetting');
$section = craft()->request->getPost('sectionSetting');
$entryType = craft()->request->getPost('entryTypeSetting');
$userGroup = craft()->request->getPost('userGroupSetting');
$status = craft()->request->getPost('statusSetting');
$locale = craft()->request->getPost('localeSetting');
$field = craft()->request->getPost('fieldSetting');
$value = craft()->request->getPost('valueSetting');

$description = "Applying Batch Change";
craft()->tasks->createTask('Batch', $description, array(
'elementType' => $elementType,
'section' => $section,
'userGroup' => $userGroup,
'status' => $status,
'locale' => $locale,
'field' => $field,
'value' => $value
));
if (
craft()->tasks->createTask('Batch', $description, array(
'elementType' => $elementType,
'section' => $section,
'entryType' => $entryType,
'userGroup' => $userGroup,
'status' => $status,
'locale' => $locale,
'field' => $field,
'value' => $value
))
) {
craft()->userSession->setNotice(Craft::t('Batch task created'));
} else {
craft()->userSession->setError(Craft::t('Error creating batch task.'));
}
}
}

public function actionFetchTypes()
{
if (craft()->request->isPostRequest())
{
$sectionHandle = craft()->request->getPost('section');
$section = craft()->sections->getSectionByHandle($sectionHandle);

$types = $section->getEntryTypes();
$response = [
'success' => true,
'types' => $types,
'section' => craft()->request->getPost('section')
];

$this->returnJson($response);
}
}

public function actionFetchFields()
{
if (craft()->request->isPostRequest())
{
$elementType = craft()->request->getPost('elementType');
$sectionHandle = craft()->request->getPost('section');
$entryTypeHandle = craft()->request->getPost('entryType');
$section = craft()->sections->getSectionByHandle($sectionHandle);
$types = $section->getEntryTypes();

if ($elementType != 'User') {
if ($entryTypeHandle == 'null') {
$fields = $section->getEntryTypes()[0]->getFieldLayout()->getFields();
} else {
foreach ($types as $type) {
if($type->handle == $entryTypeHandle) {
$fields = $type->getFieldLayout()->getFields();
}
}
}
} else {
$currentUser = craft()->userSession->getUser();
$fields = $currentUser->getFieldLayout()->getFields();
}

$fieldsArray = [];
foreach ($fields as $field) {
$fieldsArray[] = $field->getField();
}

$response = [
'success' => true,
'fields' => $fieldsArray
];
$this->returnJson($response);

}
}
}
52 changes: 50 additions & 2 deletions batch/resources/js/Batch_Script.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author ChaseGiunta
* @copyright Copyright (c) 2017 ChaseGiunta
* @link google.com
* @link chasegiunta.com
* @package Batch
* @since 1.0.0
*/
Expand All @@ -14,6 +14,54 @@ var batchSettings = new Vue({
el: '#main',
delimiters: ['${', '}'],
data: {
elementType: 'Entry'
elementType: 'Entry',
section: '',
entryType: '',
entryTypes: {},
fields: ''
},
created() {
var e = document.getElementById("sectionSetting");
var value = e.options[e.selectedIndex].value;
this.section = value;
this.fetchTypes();
},
methods: {
fetchTypesAndFields() {
this.fetchTypes();
this.fetchFields();
},
fetchTypes() {

var data = {
section: this.section
};
var _this = this;
Craft.postActionRequest('batch/fetchTypes', data, function(response) {
_this.entryTypes = response.types;
_this.entryType = 'null';

if (!response.success) {
Craft.cp.displayError('Error fetching entry types');
}
});

},
fetchFields() {
if (((this.entryType != 'null' || this.entryTypes.length == 1) && this.section != 'null') || this.elementType == 'User') {
var data = {
section: this.section,
entryType: this.entryType,
elementType: this.elementType
};
var _this = this;
Craft.postActionRequest('batch/fetchFields', data, function(response) {
_this.fields = response.fields;
if (!response.success) {
Craft.cp.displayError('Error fetching fields');
}
});
}
}
}
})
6 changes: 6 additions & 0 deletions batch/resources/js/vue.min.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions batch/tasks/BatchTask.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ protected function defineSettings()
{
return array(
'elementType' => AttributeType::Mixed,
'section' => AttributeType::Mixed,
'status' => AttributeType::Mixed,
'locale' => AttributeType::Mixed,
'userGroup' => AttributeType::Mixed,
'section' => AttributeType::Mixed,
'entryType' => AttributeType::Mixed,
'status' => AttributeType::Mixed,
'locale' => AttributeType::Mixed,
'userGroup' => AttributeType::Mixed,

'field' => AttributeType::Mixed,
'value' => AttributeType::Mixed,
Expand Down Expand Up @@ -93,6 +94,7 @@ public function getCriteria()
{
$elementType = $this->getSettings()->elementType;
$section = $this->getSettings()->section;
$entryType = $this->getSettings()->entryType;
$status = $this->getSettings()->status;
$locale = $this->getSettings()->locale;
$userGroup = $this->getSettings()->userGroup;
Expand All @@ -108,6 +110,9 @@ public function getCriteria()
if (!empty($section)) {
$criteria->section = $section;
}
if (!empty($entryType)) {
$criteria->section = $section;
}
if (!empty($status)) {
$criteria->status = $status;
}
Expand Down
12 changes: 8 additions & 4 deletions batch/tasks/Batch_SubStepTask.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ public function runStep($step)
$criteria = $this->getSettings()->criteria;
$elementType = $this->getSettings()->elementType;
$step = $this->getSettings()->step;
$field = $this->getSettings()->field;
$field = trim($this->getSettings()->field);
$value = $this->getSettings()->value;

$element = $criteria[$step];

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

if ($elementType === 'Entry') {
craft()->entries->saveEntry($element);
Expand Down
43 changes: 39 additions & 4 deletions batch/templates/settings/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@

{% import "_includes/forms" as forms %}

{% includejsfile "//unpkg.com/vue" %}
{% 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" %}

{% set locales = craft.i18n.getSiteLocales() %}
{% set userGroups = craft.userGroups.getAllGroups %}
{% set sections = craft.sections.getAllSections() %}
{% if sections|length %}
{% set entryTypes = sections[0].getEntryTypes() %}
{% endif %}

<form class="" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="batch">
Expand All @@ -37,7 +45,7 @@
</div>
<div class="input ltr">
<div class="select">
<select id="elementTypeSetting" name="elementTypeSetting" v-model="elementType">
<select id="elementTypeSetting" name="elementTypeSetting" v-model="elementType" @change="fetchFields()">
<option value="Entry">Entries</option>
<option value="User">Users</option>
</select>
Expand All @@ -51,7 +59,7 @@
</div>
<div class="input ltr">
<div class="select">
<select id="sectionSetting" name="sectionSetting">
<select id="sectionSetting" name="sectionSetting" v-model="section" @change="fetchTypesAndFields()">
{% for section in sections %}
<option value="{{ section.handle }}">{{ section }}</option>
{% endfor %}
Expand All @@ -61,6 +69,20 @@
</div>
</div>

<div id="entryType-field" class="field" data-cpfieldlinks="true" v-if="elementType == 'Entry' && entryTypes.length > 1" v-cloak>
<div class="heading">
<label id="entryType-label" for="entryTypeSetting">Entry Type</label>
</div>
<div class="input ltr">
<div class="select">
<select id="entryType" name="entryTypeSetting" v-model="entryType" @change="fetchFields()">
<option :value="type.handle" v-for="type in entryTypes" :key="type.id">${type.name}</option>
<option value="null">All</option>
</select>
</div>
</div>
</div>

<div id="sectionUserGroup-field" class="field" data-cpfieldlinks="true" v-if="elementType == 'User'" v-cloak>
<div class="heading">
<label id="sectionUserGroup-label" for="userGroupSetting">User Group</label>
Expand Down Expand Up @@ -132,7 +154,20 @@
</div>
{% endif %}

<div id="fieldSetting-field" class="field" data-cpfieldlinks="true">
<div id="fieldSetting-field" class="field" data-cpfieldlinks="true" v-if="((entryType != 'null' || entryTypes.length == 1) && section != 'null') || elementType == 'User'">
<div class="heading">
<label id="fieldSetting-label" for="fieldSetting">Field</label>
</div>
<div class="input ltr">
<div class="select">
<select id="fieldSetting" name="fieldSetting">
<option v-for="field in fields" :value="field.handle">${field.name}</option>
</select>
</div>
</div>
</div>

<div id="fieldSetting-field" class="field" data-cpfieldlinks="true" v-else>
<div class="heading">
<label id="fieldSetting-label" for="fieldSetting">Field Handle</label>
</div>
Expand Down

0 comments on commit 09c7b2a

Please sign in to comment.