Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdrosas87 committed Sep 27, 2024
0 parents commit 6b7f445
Show file tree
Hide file tree
Showing 8 changed files with 556 additions and 0 deletions.
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Element Status Events Changelog

## 2.0.1 - 2022-04-26
- Remove deprecated `enabledForSite()` element query parameter

## 2.0.0 - 2019-03-28
### Added
- Added event object `StatusChangeEvent` with access to element via `getElement()` and check methods:
- `changedTo(string $nameOfStatus)`
- `changedToPublished()`
- `changedToUnpublished()`

- Added Craft CLI command `element-status-events/scheduled` to take scheduled elements into account.

### Changed
- No need to bootstrap or register the extension.
- Extension implements `BootstrapInterface`.
- Event `ElementStatusEvents::EVENT_STATUS_CHANGED`.

## 1.3.0 - 2019-01-18
### Changed
- Converted from module to extension.

## 1.2.1 - 2019-01-18
### Fixed
- Fixed missing variable.

## 1.2.0 - 2019-01-17
### Changed
- Removed the `ElementStatusService::EVENT_ELEMENT_STATUSES_CHANGED` event as it cannot be guaranteed that it will be triggered under every circumstance.

## 1.1.0 - 2019-01-15
### Changed
- Changed the module to use a service so that it can be loaded from any other module or plugin without having to be bootstrapped.

## 1.0.0 - 2019-01-14
- Initial release.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2022 Cast Iron Coding

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Element Status Events for Craft CMS 3

The Element Status Events extension provides events that are triggered whenever an element’s status changes. It is intended to be used a helper component for other Craft modules and plugins.

To get an understanding of how the module works, read the [Challenge #6 – The Chicken or the Egg](https://craftcodingchallenge.com/challenge-6-the-chicken-or-the-egg) solution.

## Requirements

This extension requires Craft CMS 3.0.0 or later.

## Usage

Install it manually using composer or add it as a dependency to your plugin.
```
composer require putyourlightson/craft-element-status-events
```

If you work with scheduled Entries (future published or expired), make sure to set up cron that calls:
```
php craft element-status-events/scheduled
```


## Events

Whenever an element’s status is changed, `ElementStatusEvents::EVENT_STATUS_CHANGED` is fired. The `StatusChangeEvent` object provides information about the change.

```php

use putyourlightson\elementstatusevents\ElementStatusEvents;
use putyourlightson\elementstatusevents\events\StatusChangeEvent;

// ...

Event::on(
ElementStatusEvents::class,
ElementStatusEvents::EVENT_STATUS_CHANGED,
function(StatusChangeEvent $event) {
$oldStatus = $event->statusBeforeSave;
$newStatus = $event->element->getStatus();
$isLive = $event->changedToPublished();
$isDeath = $event->changedToUnpublished();
$isScheduled = $event->changedTo('pending');
}
);
```


## License

This extension is licensed for free under the MIT License.

<small>Created by [PutYourLightsOn](https://putyourlightson.com/) in cooperation with [Oliver Stark](https://github.com/ostark)</small>
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "putyourlightson/craft-element-status-events",
"description": "Element status events extension for Craft CMS.",
"version": "2.1.0",
"type": "yii2-extension",
"homepage": "https://github.com/lsst-epo/craft-element-status-events",
"license": "MIT",
"keywords": [
"craft",
"cms",
"craftcms",
"element",
"status",
"events"
],
"authors": [
{
"name": "Ben Croker",
"homepage": "https://putyourlightson.com",
"role": "Developer"
},
{
"name": "Oliver Stark",
"homepage": "https://www.fortrabbit.com",
"role": "Developer"
}
],
"require": {
"craftcms/cms": "^4.0.0"
},
"autoload": {
"psr-4": {
"putyourlightson\\elementstatusevents\\": "src/"
}
},
"support": {
"docs": "https://github.com/lsst-epo/craft-element-status-events",
"source": "https://github.com/lsst-epo/craft-element-status-events",
"issues": "https://github.com/lsst-epo/craft-element-status-events/issues"
},
"extra": {
"bootstrap": "putyourlightson\\elementstatusevents\\ElementStatusEvents"
}
}
104 changes: 104 additions & 0 deletions src/ElementStatusEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace putyourlightson\elementstatusevents;

use Craft;
use craft\web\Application as CraftWebApp;
use craft\console\Application as CraftConsoleApp;
use putyourlightson\elementstatusevents\commands\ScheduledElements;
use yii\base\Application as YiiApp;
use yii\base\BootstrapInterface;
use yii\base\Component;
use craft\base\Element;
use craft\events\ElementEvent;
use craft\services\Elements;
use putyourlightson\elementstatusevents\behaviors\ElementStatusBehavior;
use yii\base\Event;
use yii\caching\CacheInterface;

/**
* Class ElementStatusEvents
*
* @package putyourlightson\elementstatusevents
*/
class ElementStatusEvents extends Component implements BootstrapInterface
{
// Constants
// =========================================================================

const EVENT_STATUS_CHANGED = 'statusChanged';

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

/**
* Register console command
*
* @param CraftConsoleApp $app
* @param string $group
*/
public static function registerScheduledCommand(CraftConsoleApp $app, $group = 'element-status-events')
{
$app->controllerMap[$group] = ScheduledElements::class;
}

/**
* Bootstrap the extension
*
* @param YiiApp $app
*/
public function bootstrap($app)
{
// Make sure it's Craft
if (!($app instanceof CraftWebApp || $app instanceof CraftConsoleApp)) {
return;
}

Event::on(Elements::class, Elements::EVENT_BEFORE_SAVE_ELEMENT, [$this, 'rememberPreviousStatus']);
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, [$this, 'fireEventOnChange']);

if ($app instanceof CraftConsoleApp) {
// Tell Craft about the concrete implementation of CacheInterface
Craft::$container->set(CacheInterface::class, Craft::$app->getCache());
self::registerScheduledCommand($app);
}
}

/**
* Register event listener
*
* @param ElementEvent $event
*/
public function rememberPreviousStatus(ElementEvent $event)
{
/** @var Element|ElementStatusBehavior $element */
$element = $event->element;

// Attach behavior to access the status later
$element->attachBehavior('elementStatusEvents', ElementStatusBehavior::class);

// No need to remember anything
if ($event->isNew) {
return;
}

$element->rememberPreviousStatus();
}

/**
* Register event listener
*
* @param ElementEvent $event
*/
public function fireEventOnChange(ElementEvent $event)
{
/** @var Element|ElementStatusBehavior $element */
$element = $event->element;

// Fire ElementStatusEvents::EVENT_STATUS_CHANGED
if ($element->getBehavior('elementStatusEvents') !== null) {
$element->fireEventOnChange();
}
}

}
66 changes: 66 additions & 0 deletions src/behaviors/ElementStatusBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace putyourlightson\elementstatusevents\behaviors;

use Craft;
use craft\base\Element;
use putyourlightson\elementstatusevents\ElementStatusEvents;
use putyourlightson\elementstatusevents\events\StatusChangeEvent;
use yii\base\Behavior;
use yii\base\Event;

class ElementStatusBehavior extends Behavior
{
// Properties
// =========================================================================

/**
* @var string
*/
public $statusBeforeSave = '';

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

/**
* 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;

// Nothing changed?
if ($this->statusBeforeSave === $element->getStatus()) {
return;
}

if (Event::hasHandlers(ElementStatusEvents::class, ElementStatusEvents::EVENT_STATUS_CHANGED)) {
Event::trigger(
ElementStatusEvents::class,
ElementStatusEvents::EVENT_STATUS_CHANGED,
new StatusChangeEvent([
'element' => $element,
'statusBeforeSave' => $this->statusBeforeSave
])
);
}
}
}
Loading

0 comments on commit 6b7f445

Please sign in to comment.