Skip to content

Commit

Permalink
Merge pull request #7567 from kuzmany/campaign-action-add-remove-dnc
Browse files Browse the repository at this point in the history
New Campaign actions:  Add/Remove Do Not Contact
  • Loading branch information
dennisameling committed Jan 28, 2020
2 parents 9cdb5a6 + eadf3d5 commit da42338
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/bundles/LeadBundle/Config/config.php
Expand Up @@ -407,6 +407,13 @@
'mautic.campaign.helper.removed_contact_tracker',
],
],
'mautic.lead.campaignbundle.action_dnc.subscriber' => [
'class' => \Mautic\LeadBundle\EventListener\CampaignActionDNCSubscriber::class,
'arguments' => [
'mautic.lead.model.dnc',
'mautic.lead.model.lead',
],
],
'mautic.lead.reportbundle.subscriber' => [
'class' => \Mautic\LeadBundle\EventListener\ReportSubscriber::class,
'arguments' => [
Expand Down Expand Up @@ -775,6 +782,12 @@
'class' => Mautic\LeadBundle\Form\Type\ConfigType::class,
'alias' => 'leadconfig',
],
'mautic.form.type.preference.channels' => [
'class' => \Mautic\LeadBundle\Form\Type\PreferenceChannelsType::class,
'arguments' => [
'mautic.lead.model.lead',
],
],
],
'other' => [
'mautic.lead.doctrine.subscriber' => [
Expand Down
139 changes: 139 additions & 0 deletions app/bundles/LeadBundle/EventListener/CampaignActionDNCSubscriber.php
@@ -0,0 +1,139 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
*
* @link https://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\LeadBundle\EventListener;

use Mautic\CampaignBundle\CampaignEvents;
use Mautic\CampaignBundle\Event\CampaignBuilderEvent;
use Mautic\CampaignBundle\Event\PendingEvent;
use Mautic\CoreBundle\Helper\ArrayHelper;
use Mautic\LeadBundle\Form\Type\CampaignActionAddDNCType;
use Mautic\LeadBundle\Form\Type\CampaignActionRemoveDNCType;
use Mautic\LeadBundle\LeadEvents;
use Mautic\LeadBundle\Model\DoNotContact;
use Mautic\LeadBundle\Model\LeadModel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CampaignActionDNCSubscriber implements EventSubscriberInterface
{
/**
* @var DoNotContact
*/
private $doNotContact;

/**
* @var LeadModel
*/
private $leadModel;

/**
* CampaignActionDNCSubscriber constructor.
*
* @param DoNotContact $doNotContact
* @param LeadModel $leadModel
*/
public function __construct(DoNotContact $doNotContact, LeadModel $leadModel)
{
$this->doNotContact = $doNotContact;
$this->leadModel = $leadModel;
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
CampaignEvents::CAMPAIGN_ON_BUILD => ['configureAction', 0],
LeadEvents::ON_CAMPAIGN_ACTION_ADD_DONOTCONTACT => ['addDoNotContact', 0],
LeadEvents::ON_CAMPAIGN_ACTION_REMOVE_DONOTCONTACT => ['removeDoNotContact', 0],
];
}

/**
* @param CampaignBuilderEvent $event
*/
public function configureAction(CampaignBuilderEvent $event)
{
$event->addAction(
'lead.adddnc',
[
'label' => 'mautic.lead.lead.events.add_donotcontact',
'description' => 'mautic.lead.lead.events.add_donotcontact_desc',
'batchEventName' => LeadEvents::ON_CAMPAIGN_ACTION_ADD_DONOTCONTACT,
'formType' => CampaignActionAddDNCType::class,
]
);

$event->addAction(
'lead.removednc',
[
'label' => 'mautic.lead.lead.events.remove_donotcontact',
'description' => 'mautic.lead.lead.events.remove_donotcontact_desc',
'batchEventName' => LeadEvents::ON_CAMPAIGN_ACTION_REMOVE_DONOTCONTACT,
'formType' => CampaignActionRemoveDNCType::class,
]
);
}

/**
* @param PendingEvent $event
*/
public function addDoNotContact(PendingEvent $event)
{
$config = $event->getEvent()->getProperties();
$channels = ArrayHelper::getValue('channels', $config, []);
$reason = ArrayHelper::getValue('reason', $config, '');
$persistEntities = [];
$contacts = $event->getContactsKeyedById();
foreach ($contacts as $contactId=>$contact) {
foreach ($channels as $channel) {
$this->doNotContact->addDncForContact(
$contactId,
$channel,
\Mautic\LeadBundle\Entity\DoNotContact::MANUAL,
$reason,
false
);
}
$persistEntities[] = $contact;
}

$this->leadModel->saveEntities($persistEntities);

$event->passAll();
}

/**
* @param PendingEvent $event
*/
public function removeDoNotContact(PendingEvent $event)
{
$config = $event->getEvent()->getProperties();
$channels = ArrayHelper::getValue('channels', $config, []);
$persistEntities = [];
$contacts = $event->getContactsKeyedById();
foreach ($contacts as $contactId=>$contact) {
foreach ($channels as $channel) {
$this->doNotContact->removeDncForContact(
$contactId,
$channel,
true
);
}
$persistEntities[] = $contact;
}

$this->leadModel->saveEntities($persistEntities);

$event->passAll();
}
}
50 changes: 50 additions & 0 deletions app/bundles/LeadBundle/Form/Type/CampaignActionAddDNCType.php
@@ -0,0 +1,50 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\LeadBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

class CampaignActionAddDNCType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'channels',
PreferenceChannelsType::class,
[
'label' => 'mautic.lead.contact.channels',
'multiple' => true,
'required' => true,
'constraints' => [
new NotBlank(),
],
]
);

$builder->add(
'reason',
'textarea',
[
'label' => 'mautic.lead.batch.dnc_reason',
'required' => false,
'label_attr' => ['class' => 'control-label'],
'attr' => ['class' => 'form-control'],
]
);
}
}
39 changes: 39 additions & 0 deletions app/bundles/LeadBundle/Form/Type/CampaignActionRemoveDNCType.php
@@ -0,0 +1,39 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\LeadBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

class CampaignActionRemoveDNCType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'channels',
PreferenceChannelsType::class,
[
'label' => 'mautic.lead.contact.channels',
'multiple' => true,
'required' => true,
'constraints' => [
new NotBlank(),
],
]
);
}
}
65 changes: 65 additions & 0 deletions app/bundles/LeadBundle/Form/Type/PreferenceChannelsType.php
@@ -0,0 +1,65 @@
<?php

/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\LeadBundle\Form\Type;

use Mautic\LeadBundle\Model\LeadModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PreferenceChannelsType extends AbstractType
{
/**
* @var LeadModel
*/
private $leadModel;

/**
* ModifyDNCActionType constructor.
*
* @param LeadModel $leadModel
*/
public function __construct(LeadModel $leadModel)
{
$this->leadModel = $leadModel;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$model = $this->leadModel;

$resolver->setDefaults(
[
'choices' => function (Options $options) use ($model) {
return array_flip($model->getPreferenceChannels());
},
'empty_value' => '',
'attr' => ['class' => 'form-control'],
'label_attr' => ['class' => 'control-label'],
'multiple' => false,
'expanded' => false,
'required' => false,
]
);
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'choice';
}
}
18 changes: 18 additions & 0 deletions app/bundles/LeadBundle/LeadEvents.php
Expand Up @@ -467,6 +467,24 @@ final class LeadEvents
*/
const ON_CAMPAIGN_ACTION_DELETE_CONTACT = 'mautic.lead.on_campaign_action_delete_contact';

/**
* The mautic.lead.on_campaign_action_add_donotcontact event is dispatched when the campaign action to add a donotcontact is executed.
*
* The event listener receives a Mautic\CampaignBundle\Event\PendingEvent
*
* @var string
*/
const ON_CAMPAIGN_ACTION_ADD_DONOTCONTACT = 'mautic.lead.on_campaign_action_add_donotcontact';

/**
* The mautic.lead.on_campaign_action_remove_donotcontact event is dispatched when the campaign action to remove a donotcontact is executed.
*
* The event listener receives a Mautic\CampaignBundle\Event\PendingEvent
*
* @var string
*/
const ON_CAMPAIGN_ACTION_REMOVE_DONOTCONTACT = 'mautic.lead.on_campaign_action_remove_donotcontact';

/**
* The mautic.lead.on_campaign_trigger_condition event is fired when the campaign condition triggers.
*
Expand Down
4 changes: 4 additions & 0 deletions app/bundles/LeadBundle/Translations/en_US/messages.ini
Expand Up @@ -233,6 +233,10 @@ mautic.lead.lead.events.updatecompany="Update contact's primary company"
mautic.lead.lead.events.updatecompany_descr="Update the contact's primary company fields with the defined values from this action"
mautic.lead.lead.events.delete="Delete contact"
mautic.lead.lead.events.delete_descr="<span class='text-danger'>Permanently deletes the contact as well as all associated statistical data. <strong>Warning: this is irreversible!</strong></span>"
mautic.lead.lead.events.add_donotcontact="Add Do Not Contact"
mautic.lead.lead.events.add_donotcontact_desc="Add DoNotContact flag to the contact"
mautic.lead.lead.events.remove_donotcontact="Remove Do Not Contact"
mautic.lead.lead.events.remove_donotcontact_desc="Remove Do Not Contact flag from contact"
mautic.lead.lead.events.field_value="Contact field value"
mautic.lead.lead.events.field_value_descr="Condition based on a contact field value."
mautic.lead.lead.events.device="Contact device"
Expand Down

0 comments on commit da42338

Please sign in to comment.