Skip to content

Commit

Permalink
Add offset option, remove subtask, fix locale detection
Browse files Browse the repository at this point in the history
  • Loading branch information
chasegiunta committed Sep 1, 2017
1 parent e0c8563 commit 8324cc6
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 143 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Batch Changelog

## 1.3.0 - 2017.09.01

### Added
* Option to offset steps to prevent stalled tasks

### Changed
* Removed subtasks, which wasn't needed, to improve execution

### Fixed
* Only show locales dropdown when applicable

## 1.2.0 - 2017.08.20

### Added
Expand Down
2 changes: 1 addition & 1 deletion batch/BatchPlugin.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getReleaseFeedUrl()
*/
public function getVersion()
{
return '1.2.0';
return '1.3.0';
}

/**
Expand Down
2 changes: 2 additions & 0 deletions batch/controllers/BatchController.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function actionIndex()
$status = craft()->request->getPost('statusSetting');
$locale = craft()->request->getPost('localeSetting');
$action = craft()->request->getPost('actionSetting');
$offset = craft()->request->getPost('offsetSetting');
$transferTo = craft()->request->getPost('transferContentTo');
$fieldType = craft()->request->getPost('fieldTypeSetting');
$field = craft()->request->getPost('fieldSetting');
Expand All @@ -50,6 +51,7 @@ public function actionIndex()
'status' => $status,
'locale' => $locale,
'action' => $action,
'offset' => $offset,
'transferTo' => $transferTo,
'fieldType' => $fieldType,
'field' => $field,
Expand Down
119 changes: 86 additions & 33 deletions batch/tasks/BatchTask.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class BatchTask extends BaseTask
{
public $criteria;
/**
* Defines the settings.
*
Expand All @@ -31,6 +32,7 @@ protected function defineSettings()
'status' => AttributeType::Mixed,
'locale' => AttributeType::Mixed,
'action' => AttributeType::Mixed,
'offset' => AttributeType::Mixed,
'transferTo' => AttributeType::Mixed,
'fieldType' => AttributeType::Mixed,
'userGroup' => AttributeType::Mixed,
Expand Down Expand Up @@ -69,28 +71,68 @@ public function getTotalSteps()
*/
public function runStep($step)
{
$criteria = $this->getCriteria();
$criteria = $this->criteria;
$elementType = $this->getSettings()->elementType;
$fieldType = $this->getSettings()->fieldType;
$action = $this->getSettings()->action;
$transferTo = $this->getSettings()->transferTo;

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

$description = "Saving $criteria[$step]";
$element = $criteria[$step];

if ($action == 'delete') {
$description = "Deleting $criteria[$step]";
} else {
$description = "Saving $criteria[$step]";
}

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

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

}

if ($elementType === 'Entry') {
craft()->entries->saveEntry($element);
return true;
} else if ($elementType === 'User') {
craft()->users->saveUser($element);
return true;
}
}

foreach($criteria as $item) {
return $this->runSubTask('Batch_SubStep', $description, array(
'criteria' => $criteria,
'elementType' => $elementType,
'fieldType' => $fieldType,
'action' => $action,
'transferTo' => $transferTo,
'step' => $step,
'field' => $field,
'value' => $value
));
if ($action == 'delete') {
if ($elementType === 'Entry') {
craft()->entries->deleteEntry($element);
return true;
} else if ($elementType === 'User') {
if(!empty($transferTo)) {
$userModel = craft()->users->getUserById($transferTo[0]);
craft()->users->deleteUser($element, $userModel);
return true;
} else {
craft()->users->deleteUser($element, null);
return true;
}
}
}

}

/**
Expand All @@ -107,35 +149,46 @@ public function getCriteria()
$status = $this->getSettings()->status;
$locale = $this->getSettings()->locale;
$userGroup = $this->getSettings()->userGroup;
$offset = $this->getSettings()->offset;

if (empty($elementType)) {
return false;
}

if (!empty($elementType)) {
if ($elementType == 'Entry') {
$scopeResolution = ElementType::Entry;
} else if ($elementType == 'User') {
$scopeResolution = ElementType::User;
if ($elementType == 'Entry') {

$scopeResolution = ElementType::Entry;
$criteria = craft()->elements->getCriteria($scopeResolution);
if (!empty($section)) {
$criteria->section = $section;
}
if (!empty($entryType)) {
$criteria->type = $entryType;
}
if (!empty($locale)) {
$criteria->locale = $locale;
$criteria->localeEnabled = false;
}

} else if ($elementType == 'User') {

$scopeResolution = ElementType::User;
$criteria = craft()->elements->getCriteria($scopeResolution);
$criteria->group = $userGroup;

}
if (!empty($section)) {
$criteria->section = $section;
}
if (!empty($entryType)) {
$criteria->type = $entryType;
}

if (!empty($status)) {
$criteria->status = $status;
}
if (!empty($locale)) {
$criteria->locale = $locale;
}
if ($elementType === 'Entry') {
$criteria->localeEnabled = false;
} else if ($elementType === 'User') {
$criteria->group = $userGroup;
$criteria->status = $status == 'null' ? null : $status;
}

$criteria->offset = $offset ?? 0;

$criteria->limit = null;
$criteria->find();

// Set Criteria on class var
$this->criteria = $criteria;
return $criteria;
}
}
107 changes: 0 additions & 107 deletions batch/tasks/Batch_SubStepTask.php

This file was deleted.

38 changes: 36 additions & 2 deletions batch/templates/settings/index.twig
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,17 @@
</td>
</tr>

{% if locales|length %}
{% if locales|length > 1 %}
<tr v-if="elementType == 'Entry'">
<td>
<div class="field" data-cpfieldlinks="true">
<div class="heading">
<label id="localeSetting-label" for="localeSetting">Locale</label>
<div class="instructions"><p>Change will be applied <br>regardless of whether locale is enabled.</p></div>
<span class="info">
<span style="display:inline-block;">
Change will be applied regardless of whether locale is enabled.
</span>
</span>
</div>
</div>
</td>
Expand Down Expand Up @@ -327,6 +331,29 @@
</td>
</tr>

<tr>
<td>
<div class="field" data-cpfieldlinks="true">
<div class="heading">
<label id="offsetSetting-label" for="fieldSetting">Offset</label>
<span class="info">
<span style="display:inline-block;">
Tasks with many steps can be memory-intensive.
If the Batch task is stalling after running for a while, try running the same batch again with an offset.
</span>
</span>
</div>
</div>
</td>
<td>
<div class="field" data-cpfieldlinks="true">
<div class="input ltr">
<input type="number" id="offsetSetting" name="offsetSetting" autocomplete="off" class="text">
</div>
</div>
</td>
</tr>


</tbody>
</table>
Expand All @@ -336,6 +363,13 @@
<div class="buttons">
<input class="btn submit " type="submit" value="Run Batch">
</div>
<div class="field" data-cpfieldlinks="true">
<div class="heading">
<div class="instructions">
<p>It would be wise to backup your database before running Batch.</p>
</div>
</div>
</div>

</form>

Expand Down
10 changes: 10 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
[
{
"version": "1.3.0",
"downloadUrl": "https://github.com/chasegiunta/batch/archive/master.zip",
"date": "2017-09-01T11:00:00-11:00",
"notes": [
"[Added] Option to offset steps to prevent stalled tasks",
"[Improved] Removed subtasks, which wasn't needed, to improve execution",
"[Fixed] Only show locales dropdown when applicable"
]
},
{
"version": "1.2.0",
"downloadUrl": "https://github.com/chasegiunta/batch/archive/master.zip",
Expand Down

0 comments on commit 8324cc6

Please sign in to comment.