-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LastActivity and CampaignActivity rules
- Loading branch information
Showing
9 changed files
with
215 additions
and
4 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
138 changes: 138 additions & 0 deletions
138
src/elements/conditions/contacts/CampaignActivityConditionRule.php
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,138 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) PutYourLightsOn | ||
*/ | ||
|
||
namespace putyourlightson\campaign\elements\conditions\contacts; | ||
|
||
use Craft; | ||
use craft\base\conditions\BaseElementSelectConditionRule; | ||
use craft\base\ElementInterface; | ||
use craft\elements\conditions\ElementConditionRuleInterface; | ||
use craft\elements\db\ElementQueryInterface; | ||
use putyourlightson\campaign\elements\CampaignElement; | ||
use putyourlightson\campaign\elements\db\ContactElementQuery; | ||
use putyourlightson\campaign\records\ContactCampaignRecord; | ||
|
||
/** | ||
* @since 2.0.0 | ||
*/ | ||
class CampaignActivityConditionRule extends BaseElementSelectConditionRule implements ElementConditionRuleInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public string $operator = 'opened'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected bool $reloadOnOperatorChange = true; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function elementType(): string | ||
{ | ||
return CampaignElement::class; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLabel(): string | ||
{ | ||
return Craft::t('campaign', 'Campaign Activity'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getExclusiveQueryParams(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function modifyQuery(ElementQueryInterface $query): void | ||
{ | ||
/** @var ContactElementQuery $query */ | ||
$query->innerJoin(ContactCampaignRecord::tableName(), '[[campaign_contacts.id]] = [[contactId]]'); | ||
|
||
$query->andWhere(['not', [ | ||
$this->_operatorColumn($this->operator) => null, | ||
]]); | ||
|
||
$elementId = $this->getElementId(); | ||
if ($elementId !== null) { | ||
$query->andWhere(['campaignId' => $elementId]); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function inputHtml(): string | ||
{ | ||
return match ($this->operator) { | ||
'openedCampaign', 'clickedCampaign' => parent::inputHtml(), | ||
default => '', | ||
}; | ||
} | ||
|
||
protected function operators(): array | ||
{ | ||
return [ | ||
'opened', | ||
'clicked', | ||
'openedCampaign', | ||
'clickedCampaign', | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function operatorLabel(string $operator): string | ||
{ | ||
return match ($operator) { | ||
'opened' => Craft::t('campaign', 'opened any campaign'), | ||
'clicked' => Craft::t('campaign', 'clicked a link in any campaign'), | ||
'openedCampaign' => Craft::t('campaign', 'opened the campaign'), | ||
'clickedCampaign' => Craft::t('campaign', 'clicked a link in the campaign'), | ||
default => parent::operatorLabel($operator), | ||
}; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function matchElement(ElementInterface $element): bool | ||
{ | ||
$query = ContactCampaignRecord::find() | ||
->where([ | ||
'contactId' => $element->id, | ||
]) | ||
->andWhere(['not', [ | ||
$this->_operatorColumn($this->operator) => null, | ||
] | ||
]); | ||
|
||
$elementId = $this->getElementId(); | ||
if ($elementId !== null) { | ||
$query->andWhere(['campaignId' => $elementId]); | ||
} | ||
|
||
return $query->exists(); | ||
} | ||
|
||
/** | ||
* Returns the column to query on based on the operator. | ||
*/ | ||
private function _operatorColumn(string $operator): string | ||
{ | ||
return match ($operator) { | ||
'opened', 'openedCampaign' => 'opened', | ||
'clicked', 'clickedCampaign' => 'clicked', | ||
}; | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/elements/conditions/contacts/LastActivityConditionRule.php
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,54 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) PutYourLightsOn | ||
*/ | ||
|
||
namespace putyourlightson\campaign\elements\conditions\contacts; | ||
|
||
use Craft; | ||
use craft\base\conditions\BaseDateRangeConditionRule; | ||
use craft\base\ElementInterface; | ||
use craft\elements\conditions\ElementConditionRuleInterface; | ||
use craft\elements\db\ElementQueryInterface; | ||
use putyourlightson\campaign\elements\ContactElement; | ||
use putyourlightson\campaign\elements\db\ContactElementQuery; | ||
|
||
/** | ||
* @since 2.0.0 | ||
*/ | ||
class LastActivityConditionRule extends BaseDateRangeConditionRule implements ElementConditionRuleInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLabel(): string | ||
{ | ||
return Craft::t('campaign', 'Last Activity'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getExclusiveQueryParams(): array | ||
{ | ||
return ['lastActivity']; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function modifyQuery(ElementQueryInterface $query): void | ||
{ | ||
/** @var ContactElementQuery $query */ | ||
$query->lastActivity($this->queryParamValue()); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function matchElement(ElementInterface $element): bool | ||
{ | ||
/** @var ContactElement $element */ | ||
return $this->matchValue($element->lastActivity); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
{% import '_includes/forms' as forms %} | ||
{% import 'campaign/_macros' as macros %} | ||
|
||
{% set input %} | ||
{{ segment.contactCondition.getBuilderHtml()|raw }} | ||
{% endset %} | ||
|
||
{{ forms.field({ | ||
label: 'Contact Condition'|t('campaign'), | ||
instructions: 'One or more condition rules to apply to this segment.'|t('campaign'), | ||
instructions: 'One or more condition rules to apply to this segment.'|t('campaign') ~ macros.info('Condition rules can be created via a custom plugin/module.'), | ||
errors: segment.getErrors('contactCondition') | ||
}, input) }} |