-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7567 from kuzmany/campaign-action-add-remove-dnc
New Campaign actions: Add/Remove Do Not Contact
- Loading branch information
Showing
7 changed files
with
328 additions
and
0 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
139 changes: 139 additions & 0 deletions
139
app/bundles/LeadBundle/EventListener/CampaignActionDNCSubscriber.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,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
50
app/bundles/LeadBundle/Form/Type/CampaignActionAddDNCType.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,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
39
app/bundles/LeadBundle/Form/Type/CampaignActionRemoveDNCType.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,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
65
app/bundles/LeadBundle/Form/Type/PreferenceChannelsType.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,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'; | ||
} | ||
} |
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