Skip to content

Commit

Permalink
Add example module and sendout condition rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 22, 2024
1 parent 24e7177 commit 7f25e50
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 19 deletions.
1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

return static function(ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/examples',
__DIR__ . '/src',
__DIR__ . '/tests/pest',
__FILE__,
Expand Down
32 changes: 32 additions & 0 deletions examples/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace modules;

use Craft;
use craft\base\conditions\BaseCondition;
use craft\events\RegisterConditionRuleTypesEvent;
use modules\conditions\sendouts\LastEntryHasImageConditionRule;
use modules\conditions\sendouts\MondayMorningSendoutConditionRule;
use modules\conditions\sendouts\RecentEntriesPublishedConditionRule;
use putyourlightson\campaign\elements\conditions\sendouts\SendoutScheduleCondition;
use yii\base\Event;

class Module extends \yii\base\Module
{
public function init(): void
{
Craft::setAlias('@modules', __DIR__);

parent::init();

Event::on(
SendoutScheduleCondition::class,
BaseCondition::EVENT_REGISTER_CONDITION_RULE_TYPES,
function(RegisterConditionRuleTypesEvent $event) {
$event->conditionRuleTypes[] = LastEntryHasImageConditionRule::class;
$event->conditionRuleTypes[] = MondayMorningSendoutConditionRule::class;
$event->conditionRuleTypes[] = RecentEntriesPublishedConditionRule::class;
}
);
}
}
10 changes: 6 additions & 4 deletions examples/conditions/segments/ContactIsUserConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @copyright Copyright (c) PutYourLightsOn
*/

namespace modules\sitemodule\conditions\segments;
namespace modules\conditions\segments;

use craft\base\conditions\BaseConditionRule;
use craft\base\ElementInterface;
Expand Down Expand Up @@ -41,9 +41,11 @@ public function getExclusiveQueryParams(): array
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var ContactElementQuery $query */
$query->andWhere(['not', [
'userId' => null,
]]);
$query->andWhere([
'not', [
'userId' => null,
],
]);
}

/**
Expand Down
15 changes: 5 additions & 10 deletions examples/conditions/sendouts/LastEntryHasImageConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @copyright Copyright (c) PutYourLightsOn
*/

namespace modules\sitemodule\conditions\sendouts;
namespace modules\conditions\sendouts;

use craft\base\conditions\BaseConditionRule;
use craft\base\ElementInterface;
Expand All @@ -13,17 +13,12 @@

class LastEntryHasImageConditionRule extends BaseConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public string $operator = '';

/**
* @inheritdoc
*/
public function getLabel(): string
{
return 'Send only if the last entry has an image.';
return 'Send only if the last entry has an image';
}

/**
Expand All @@ -46,12 +41,12 @@ public function modifyQuery(ElementQueryInterface $query): void
*/
public function matchElement(ElementInterface $element): bool
{
// Get the last entry, eager-loading the image
// Get the last entry, eager-loading the images.
$lastEntry = Entry::find()
->with('image')
->with('images')
->one();

if ($lastEntry && $lastEntry->image->isNotEmpty()) {
if ($lastEntry && $lastEntry->images->isNotEmpty()) {
return true;
}

Expand Down
49 changes: 49 additions & 0 deletions examples/conditions/sendouts/MondayMorningSendoutConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @copyright Copyright (c) PutYourLightsOn
*/

namespace modules\conditions\sendouts;

use craft\base\conditions\BaseConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use DateTime;

class MondayMorningSendoutConditionRule extends BaseConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return 'Send only between 10:00 and 11:00 on Mondays';
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return [self::class];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
$now = new DateTime();

// Return whether it is now between 10:00 and 11:00 on Monday.
return $now->format('H') === '10' && $now->format('N') === '1';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @copyright Copyright (c) PutYourLightsOn
*/

namespace modules\sitemodule\conditions\sendouts;
namespace modules\conditions\sendouts;

use craft\base\conditions\BaseSelectConditionRule;
use craft\base\ElementInterface;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function modifyQuery(ElementQueryInterface $query): void
public function matchElement(ElementInterface $element): bool
{
// Return whether any entries were published in the previous period
$date = (new DateTime())->modify('-1 '. $this->value);
$date = (new DateTime())->modify('-1 ' . $this->value);

return Entry::find()
->after($date)
Expand Down
41 changes: 41 additions & 0 deletions lib/craft/behaviors/CustomFieldBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @link http://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license http://craftcms.com/license
*/

namespace craft\behaviors;

use craft\elements\Asset;
use craft\elements\db\AssetQuery;
use craft\elements\ElementCollection;
use yii\base\Behavior;

/**
* Based on https://github.com/craftcms/cms/blob/develop/lib/craft/behaviors/CustomFieldBehavior.php
*
* @internal
*/
class CustomFieldBehavior extends Behavior
{
/**
* Custom fields required for PHPStan.
*/
public AssetQuery|ElementCollection|Asset $images;

/**
* @var bool Whether the behavior should provide methods based on the field handles.
*/
public bool $hasMethods = false;

/**
* @var bool Whether properties on the class should be settable directly.
*/
public bool $canSetProperties = true;

/**
* @var string[] List of supported field handles.
*/
public static $fieldHandles = [];
}
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ includes:
parameters:
level: 5
paths:
- examples
- src
scanFiles:
- lib/craft/behaviors/CustomFieldBehavior.php
7 changes: 4 additions & 3 deletions src/models/RecurringScheduleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ public function canSendNow(SendoutElement $sendout): bool
$now = new DateTime();

// Ensure not already sent today
$format = 'Y-m-d';
if ($sendout->lastSent !== null && $sendout->lastSent->format($format) == $now->format($format)) {
return false;
if ($sendout->lastSent !== null) {
if ($sendout->lastSent->format('Y-m-d') == $now->format('Y-m-d')) {
return false;
}
}

$diff = $now->diff($sendout->sendDate);
Expand Down

0 comments on commit 7f25e50

Please sign in to comment.