Skip to content

Commit

Permalink
Set Status action updates
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
brandonkelly committed Feb 3, 2017
1 parent 99c9fa8 commit 54e6f32
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 64 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Craft CMS 3.0 Working Changelog

## Unreleased

### Changed
- #2: The “Set status” batch element action now goes through the normal element save process, rather than directly modifying the DB values, ensuring that the elements validate before enabling them.
- The “Set status” batch element action now updates elements’ site statuses in addition to their global statuses, when setting the status to Enabled.

### Removed
- Removed the `afterSetStatus` event from `craft\elements\actions\SetStatus`.

### Fixed
- Fixed a bug where saving a disabled entry or draft without a post/expiry date would default to the currently-set date on the entry/draft, rather than clearing out the field.
- Fixed some asterisk icons.
Expand Down
19 changes: 1 addition & 18 deletions src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,7 @@ protected static function defineActions(string $source = null): array

// Set Status
if ($canSetStatus) {
/** @var SetStatus $setStatusAction */
$setStatusAction = Craft::$app->getElements()->createAction(SetStatus::class);
$setStatusAction->on(SetStatus::EVENT_AFTER_SET_STATUS,
function(SetStatusEvent $event) {
if ($event->status == self::STATUS_ENABLED) {
// Set a Post Date as well
Craft::$app->getDb()->createCommand()
->update(
'{{%entries}}',
['postDate' => Db::prepareDateForDb(new \DateTime())],
[
'id' => $event->elementIds,
'postDate' => null,
])
->execute();
}
});
$actions[] = $setStatusAction;
$actions[] = SetStatus::class;
}

// Edit
Expand Down
83 changes: 37 additions & 46 deletions src/elements/actions/SetStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class SetStatus extends ElementAction
*/
public $status;

// Constants
// =========================================================================

/**
* @event SetStatusEvent The event that is triggered after the statuses have been updated.
*/
const EVENT_AFTER_SET_STATUS = 'afterSetStatus';

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -81,48 +73,47 @@ public function getTriggerHtml()
*/
public function performAction(ElementQueryInterface $query): bool
{
/** @var ElementQuery $query */
// Figure out which element IDs we need to update
if ($this->status == Element::STATUS_ENABLED) {
$sqlNewStatus = '1';
} else {
$sqlNewStatus = '0';
$elementsService = Craft::$app->getElements();
$enabled = ($this->status === Element::STATUS_ENABLED);

/** @var Element[] $elements */
$elements = $query->all();
$someFailed = false;

foreach ($elements as $element) {
// Skip if there's nothing to change
if ($element->enabled == $enabled && (!$enabled || $element->enabledForSite)) {
continue;
}

if ($enabled) {
// Also enable for this site
$element->enabled = $element->enabledForSite = true;
} else {
$element->enabled = false;
}

if ($elementsService->saveElement($element) === false) {
// Validation error
$someFailed = true;
}
}

$elementIds = $query->ids();

// Update their statuses
Craft::$app->getDb()->createCommand()
->update(
'{{%elements}}',
['enabled' => $sqlNewStatus],
['id' => $elementIds])
->execute();

if ($this->status == Element::STATUS_ENABLED) {
// Enable for the site as well
Craft::$app->getDb()->createCommand()
->update(
'{{%elements_i18n}}',
['enabled' => $sqlNewStatus],
[
'elementId' => $elementIds,
'siteId' => $query->siteId,
])
->execute();
}

// Clear their template caches
Craft::$app->getTemplateCaches()->deleteCachesByElementId($elementIds);
if ($someFailed === true) {
if (count($elements) === 1) {
$this->setMessage(Craft::t('app', 'Could not update status due to a validation error.'));
} else {
$this->setMessage(Craft::t('app', 'Could not update some statuses due to validation errors.'));
}

// Fire an 'afterSetStatus' event
$this->trigger(self::EVENT_AFTER_SET_STATUS, new SetStatusEvent([
'elementQuery' => $query,
'elementIds' => $elementIds,
'status' => $this->status,
]));
return false;
}

$this->setMessage(Craft::t('app', 'Statuses updated.'));
if (count($elements) === 1) {
$this->setMessage(Craft::t('app', 'Status updated.'));
} else {
$this->setMessage(Craft::t('app', 'Statuses updated.'));
}

return true;
}
Expand Down

0 comments on commit 54e6f32

Please sign in to comment.