diff --git a/ecs.php b/ecs.php index e8d61035..3171c7e7 100644 --- a/ecs.php +++ b/ecs.php @@ -5,6 +5,7 @@ return static function(ECSConfig $ecsConfig): void { $ecsConfig->paths([ + __DIR__ . '/examples', __DIR__ . '/src', __DIR__ . '/tests/pest', __FILE__, diff --git a/examples/Module.php b/examples/Module.php new file mode 100644 index 00000000..3e2b9dd4 --- /dev/null +++ b/examples/Module.php @@ -0,0 +1,32 @@ +conditionRuleTypes[] = LastEntryHasImageConditionRule::class; + $event->conditionRuleTypes[] = MondayMorningSendoutConditionRule::class; + $event->conditionRuleTypes[] = RecentEntriesPublishedConditionRule::class; + } + ); + } +} diff --git a/examples/conditions/segments/ContactIsUserConditionRule.php b/examples/conditions/segments/ContactIsUserConditionRule.php index cf463fab..66144e2d 100644 --- a/examples/conditions/segments/ContactIsUserConditionRule.php +++ b/examples/conditions/segments/ContactIsUserConditionRule.php @@ -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; @@ -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, + ], + ]); } /** diff --git a/examples/conditions/sendouts/LastEntryHasImageConditionRule.php b/examples/conditions/sendouts/LastEntryHasImageConditionRule.php index 25b8f4b2..8e5a2d9d 100644 --- a/examples/conditions/sendouts/LastEntryHasImageConditionRule.php +++ b/examples/conditions/sendouts/LastEntryHasImageConditionRule.php @@ -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; @@ -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'; } /** @@ -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; } diff --git a/examples/conditions/sendouts/MondayMorningSendoutConditionRule.php b/examples/conditions/sendouts/MondayMorningSendoutConditionRule.php new file mode 100644 index 00000000..2c1293e1 --- /dev/null +++ b/examples/conditions/sendouts/MondayMorningSendoutConditionRule.php @@ -0,0 +1,49 @@ +format('H') === '10' && $now->format('N') === '1'; + } +} diff --git a/examples/conditions/sendouts/RecentEntriesPublishedConditionRule.php b/examples/conditions/sendouts/RecentEntriesPublishedConditionRule.php index 8b320297..85afa3dc 100644 --- a/examples/conditions/sendouts/RecentEntriesPublishedConditionRule.php +++ b/examples/conditions/sendouts/RecentEntriesPublishedConditionRule.php @@ -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; @@ -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) diff --git a/lib/craft/behaviors/CustomFieldBehavior.php b/lib/craft/behaviors/CustomFieldBehavior.php new file mode 100644 index 00000000..d63176ce --- /dev/null +++ b/lib/craft/behaviors/CustomFieldBehavior.php @@ -0,0 +1,41 @@ +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);