Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions src/JotaworksDoiBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,94 @@
'mautic.helper.encryption',
'mautic.email.model.email',
'mautic.lead.model.lead',
'mautic.tracker.contact'
'mautic.tracker.contact',
'jw.doi.leadhelper',
'jw.doi.actionhelper',
'jotaworksdoi.config'
]
],
'jw.mautic.email.report.doi' => [
'class' => \MauticPlugin\JotaworksDoiBundle\EventListener\DoiReportSubscriber::class,
'arguments' => [
'mautic.lead.reportbundle.fields_builder',
'jotaworksdoi.config'
],
],
'jw.mautic.webhook.subscriber' => [
'class' => \MauticPlugin\JotaworksDoiBundle\EventListener\WebhookSubscriber::class,
'arguments' => [
'mautic.webhook.model.webhook',
'jotaworksdoi.config'
],
],
'jw.mautic.queue.subscriber' => [
'class' => \MauticPlugin\JotaworksDoiBundle\EventListener\QueueSubscriber::class,
'arguments' => [
'monolog.logger.mautic','jw.doi.actionhelper','jw.doi.nothumanclickhelper'
'monolog.logger.mautic',
'jw.doi.actionhelper',
'jw.doi.nothumanclickhelper'
],
]
],
'forms' => [
'jw.mautic.form.type.jw_emailsend_list' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Form\Type\EmailSendType::class,
'arguments' => ['mautic.factory','translator']
, ],
'arguments' => [
'mautic.factory',
'translator'
],
]
],
'helpers' => [
'jw.doi.actionhelper' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Helper\DoiActionHelper::class,
'arguments' => ['event_dispatcher', 'mautic.helper.ip_lookup', 'mautic.page.model.page', 'mautic.email.model.email', 'mautic.core.model.auditlog', 'mautic.lead.model.lead', 'request_stack' ]
'arguments' => [
'event_dispatcher',
'mautic.helper.ip_lookup',
'mautic.page.model.page',
'mautic.email.model.email',
'mautic.core.model.auditlog',
'mautic.lead.model.lead',
'request_stack',
'jw.doi.leadhelper',
'jotaworksdoi.config'
]
],
'jw.doi.leadhelper' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Helper\LeadHelper::class,
'arguments' => [
'doctrine.dbal.default_connection'
]
],
'jw.doi.nothumanclickhelper' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Helper\NotHumanClickHelper::class,
'arguments' => ['mautic.helper.paths' ]
'arguments' => [
'mautic.helper.paths'
]
]
],
'integrations' => [
'mautic.integration.jotaworksdoi' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Integration\JotaworksDoiIntegration::class,
'tags' => [
'mautic.integration',
'mautic.basic_integration'
]
],
'jotaworksdoi.integration.configuration' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Integration\Support\ConfigSupport::class,
'tags' => [
'mautic.config_integration'
]
]
],
'others' => [
'jotaworksdoi.config' => [
'class' => \MauticPlugin\JotaworksDoiBundle\Integration\Config::class,
'arguments' => [
'mautic.integrations.helper',
]
]

]
],
'routes' => [
Expand Down
21 changes: 20 additions & 1 deletion src/JotaworksDoiBundle/EventListener/DoiReportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Mautic\ReportBundle\ReportEvents;
use MauticPlugin\MauticCustomReportBundle\Entity\CustomCreatedContactLog;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use MauticPlugin\JotaworksDoiBundle\Integration\Config;

class DoiReportSubscriber implements EventSubscriberInterface
{
Expand All @@ -29,12 +30,18 @@ class DoiReportSubscriber implements EventSubscriberInterface
*/
private $fieldsBuilder;

/**
* @var Config
*/
private $bundleConfig;

/**
* @param FieldsBuilder $fieldsBuilder
*/
public function __construct(FieldsBuilder $fieldsBuilder)
public function __construct(FieldsBuilder $fieldsBuilder, Config $bundleConfig)
{
$this->fieldsBuilder = $fieldsBuilder;
$this->bundleConfig = $bundleConfig;
}

/**
Expand All @@ -56,6 +63,10 @@ public static function getSubscribedEvents()
*/
public function onReportBuilder(ReportBuilderEvent $event)
{
if(!$this->bundleConfig->isPublished()) {
return;
}

if (!$event->checkContext([self::REPORT_NAME])) {
return;
}
Expand Down Expand Up @@ -95,6 +106,10 @@ public function onReportBuilder(ReportBuilderEvent $event)
*/
public function onReportGenerate(ReportGeneratorEvent $event)
{
if(!$this->bundleConfig->isPublished()) {
return;
}

if (!$event->checkContext([self::REPORT_NAME])) {
return;
}
Expand Down Expand Up @@ -123,6 +138,10 @@ public function onReportGenerate(ReportGeneratorEvent $event)

public function onReportDisplay(ReportDataEvent $event)
{
if(!$this->bundleConfig->isPublished()) {
return;
}

if (!$event->checkContext([self::REPORT_NAME])) {
return;
}
Expand Down
127 changes: 99 additions & 28 deletions src/JotaworksDoiBundle/EventListener/FormSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
use MauticPlugin\JotaworksDoiBundle\Entity\DoNotContact as DNC;
use MauticPlugin\JotaworksDoiBundle\Helper\LeadHelper;
use MauticPlugin\JotaworksDoiBundle\Helper\Base64Helper;
use MauticPlugin\JotaworksDoiBundle\Helper\DoiActionHelper;
use MauticPlugin\JotaworksDoiBundle\DoiEvents;
use MauticPlugin\JotaworksDoiBundle\Event\DoiStarted;
use MauticPlugin\JotaworksDoiBundle\Integration\Config;

/**
* Class FormSubscriber.
Expand All @@ -43,19 +45,37 @@ class FormSubscriber implements EventSubscriberInterface

protected $contactTracker;

/**
* @var LeadHelper
*/
private $leadHelper;

/**
* @var DoiActionHelper
*/
private $doiActionHelper;

/**
* @var Config
*/
private $bundleConfig;


/**
* FormSubscriber constructor.
*
*/
public function __construct($router, $eventDispatcher, $encryptionHelper, $emailModel, $leadModel, ContactTracker $contactTracker)
public function __construct($router, $eventDispatcher, $encryptionHelper, $emailModel, $leadModel, ContactTracker $contactTracker, LeadHelper $leadHelper, DoiActionHelper $doiActionHelper, Config $bundleConfig)
{
$this->router = $router;
$this->eventDispatcher = $eventDispatcher;
$this->encryptionHelper = $encryptionHelper;
$this->emailModel = $emailModel;
$this->leadModel = $leadModel;
$this->contactTracker = $contactTracker;
$this->leadHelper = $leadHelper;
$this->doiActionHelper = $doiActionHelper;
$this->bundleConfig = $bundleConfig;
}

/**
Expand All @@ -78,7 +98,10 @@ public static function getSubscribedEvents()
*/
public function onFormBuilder(Events\FormBuilderEvent $event)
{

if(!$this->bundleConfig->isPublished()) {
return;
}

// Send email to lead
$action = [
'group' => 'mautic.email.actions',
Expand All @@ -101,7 +124,7 @@ private function leadFieldUpdate($config, $lead ) {
return;
}

LeadHelper::leadFieldUpdate($config['lead_field_update_before'], $this->leadModel, $lead );
$this->leadHelper->leadFieldUpdate($config['lead_field_update_before'], $this->leadModel, $lead );
}

/**
Expand All @@ -110,33 +133,18 @@ private function leadFieldUpdate($config, $lead ) {
* or
* - if the do not contact is set by the user and not by a bounced mail or manually set
*/
private function shouldEmailBeSended($lead)
private function shouldEmailBeSended($lead): bool
{
$doNotContactStatus = $this->leadHelper->getDoNotContactStatus($lead->getId(), 'email');

foreach ($lead->getDoNotContact() as $dnc)
{
$reason = $dnc->getReason();
$channel = $dnc->getChannel();

//user unsubscribed from email
if( DNC::UNSUBSCRIBE === $reason && $channel=="email" )
{
return true;
}

if( DNC::BOUNCED === $reason && $channel=="email" )
{
switch ($doNotContactStatus) {
case DNC::BOUNCED:
return false;
}

if( DNC::MANUAL === $reason && $channel=="email" )
{
case DNC::MANUAL:
return false;
}

}

return true;
default:
return true;
}
}

private function buildDoiConfirmUrl( $data )
Expand Down Expand Up @@ -196,7 +204,7 @@ private function sendDoiEmail($lead, $config, $doidata, $tokens, $submissionId)
{
return false;
}

$emailId = (int) $config['email'];
$email = $this->emailModel->getEntity($emailId);
if ($email === null || !$email->isPublished()) {
Expand Down Expand Up @@ -247,7 +255,22 @@ protected function shouldDoiProcessStart($lead, $data, $submissionId)
// - email address + form id + time reaches limit
//
// + make limits configurable in plugin settings
// OR: make this a generic anti form spam plugin!
// OR: make this a generic anti form spam plugin!

// Check if feature 'no_email_to_confirmed' is enabled
if (in_array('no_email_to_confirmed', $this->bundleConfig->getSupportedFeatures())) {
// Check DNC status of the lead
$doNotContactStatus = $this->leadHelper->getDoNotContactStatus($lead->getId(), 'email');

if ($doNotContactStatus === DNC::IS_CONTACTABLE) {
// Check optin confirmation status of the lead
if (count($data['treatAsConfirmed']) > 0 && $this->isLeadConfirmed($lead, $data)) {
// Run optin success actions and stop further processing
$this->doiActionHelper->applyDoiActions($data, true);
return false;
}
}
}

return true;
}
Expand All @@ -257,6 +280,10 @@ protected function shouldDoiProcessStart($lead, $data, $submissionId)
*/
public function onFormSubmitActionSendEmail(Events\SubmissionEvent $event)
{
if(!$this->bundleConfig->isPublished()) {
return;
}

//only action if this is our form action
if (!$event->checkContext('jw.email.send.lead')) {
return;
Expand All @@ -274,10 +301,13 @@ public function onFormSubmitActionSendEmail(Events\SubmissionEvent $event)
$data = [
'lead_id' => $lead->getId(),
'url' => $this->preparePostUrl($config['post_url'], $tokens ),
'treatAsConfirmed' => $config['treat_as_confirmed'],
'add_tags' => $config['add_campaign_doi_success_tags'],
'remove_tags' => $config['remove_tags_doi_success_tags'],
'addToLists' => $config['add_campaign_doi_success_lists'],
'removeFromLists' => $config['remove_campaign_doi_success_lists'],
'optinStatusField' => $config['optin_status_field'],
'optinSuccessValue' => $config['optin_success_value'],
'leadFieldUpdate' => $config['lead_field_update'],
'form_id' => $formId,
'hash' => md5(uniqid())
Expand All @@ -300,4 +330,45 @@ public function onFormSubmitActionSendEmail(Events\SubmissionEvent $event)

}

private function isLeadConfirmed($lead, $data): bool
{
$treatAsConfirmed = $data['treatAsConfirmed'];
$optinSuccessTags = $data['add_tags'];
$optinSuccessLists = $data['addToLists'];
$optinStatusField = $data['optinStatusField'];
$optinSuccessValue = $data['optinSuccessValue'];
$confirmedCriterias = 0;

// Get tags assigned to the lead
$leadTags = [];
foreach ($lead->getTags() as $tag) {
$leadTags[] = $tag->getTag();
}

// Get lists (segments) assigned to the lead
$leadLists = $this->leadHelper->getLeadLists($lead->getId());

// Check if lead has all tags assigned
if (in_array('tags', $treatAsConfirmed)) {
$confirmedCriterias += (array_intersect($optinSuccessTags, $leadTags) === $optinSuccessTags) ? 1 : 0;
}

// Check if lead is member of all lists
if (in_array('segments', $treatAsConfirmed)) {
$confirmedCriterias += (array_intersect($optinSuccessLists, $leadLists) === $optinSuccessLists) ? 1 : 0;
}

// Check if opt-in status field of the lead has the correct value
if (in_array('status_field', $treatAsConfirmed)) {
if (!empty($optinStatusField)) {
$confirmedCriterias += ($lead->getProfileFields()[$optinStatusField] === $optinSuccessValue) ? 1 : 0;
} else {
$confirmedCriterias += 1;
}
}

// Check if contact meets all criterias of a successful opt-in
return ($confirmedCriterias === count($treatAsConfirmed)) ? true : false;
}

}
Loading