-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from lsst-epo/3-add-element-status-event
Added `element-event-status` event
- Loading branch information
Showing
8 changed files
with
427 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace lsst\nextbuilds\behaviors; | ||
|
||
use Craft; | ||
use craft\base\Element; | ||
use lsst\nextbuilds\services\ElementStatusEvents; | ||
use lsst\nextbuilds\events\StatusChangeEvent; | ||
use lsst\nextbuilds\commands\ScheduledElements; | ||
use yii\base\Behavior; | ||
use yii\base\Event; | ||
use yii\caching\CacheInterface; | ||
|
||
/** | ||
* | ||
*/ | ||
class ElementStatusBehavior extends Behavior | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $statusBeforeSave = ''; | ||
public $previousStatus = ""; | ||
|
||
/** | ||
* Saves the status of an element before it is saved | ||
*/ | ||
public function rememberPreviousStatus() | ||
{ | ||
/** @var Element $element */ | ||
$element = $this->owner; | ||
|
||
$originalElement = Craft::$app->getElements()->getElementById( | ||
$element->id, | ||
get_class($element), | ||
$element->siteId | ||
); | ||
|
||
$this->statusBeforeSave = $originalElement === null ?: $originalElement->getStatus(); | ||
} | ||
|
||
/** | ||
* Triggers an event if the status has changed | ||
*/ | ||
public function fireEventOnChange() | ||
{ | ||
/** @var Element $element */ | ||
$element = $this->owner; | ||
|
||
if ($this->statusBeforeSave === $element->getStatus()) { | ||
return; | ||
} | ||
|
||
if (Craft::$app->getRequest()->getIsConsoleRequest()) { | ||
Craft::$container->set(CacheInterface::class, Craft::$app->getCache()); | ||
Craft::$app->controllerMap['element-status-events'] = ScheduledElements::class; | ||
} | ||
|
||
if (Event::hasHandlers(ElementStatusEvents::class, ElementStatusEvents::EVENT_STATUS_CHANGED)) { | ||
Event::trigger( | ||
ElementStatusEvents::class, | ||
ElementStatusEvents::EVENT_STATUS_CHANGED, | ||
new StatusChangeEvent([ | ||
'element' => $element, | ||
'statusBeforeSave' => $this->statusBeforeSave | ||
]) | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.