From 9536466e561d257c32108e1473ad44eec845b8bc Mon Sep 17 00:00:00 2001 From: Zdeno Kuzmany Date: Wed, 4 Jul 2018 08:45:04 +0200 Subject: [PATCH] Added support for max repeat restart campaign --- Config/config.php | 1 + EventListener/CampaignSubscriber.php | 35 ++++++++++++++++--- .../CampaignEventRemoveLogsActionType.php | 15 ++++++++ Translations/en_US/messages.ini | 2 ++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Config/config.php b/Config/config.php index 71c84f2..2f2592b 100644 --- a/Config/config.php +++ b/Config/config.php @@ -13,6 +13,7 @@ 'mautic.helper.integration', 'doctrine.dbal.default_connection', 'mautic.campaign.model.campaign', + 'mautic.campaign.model.event', ], ], ], diff --git a/EventListener/CampaignSubscriber.php b/EventListener/CampaignSubscriber.php index 2e8e9a1..8bcfe89 100644 --- a/EventListener/CampaignSubscriber.php +++ b/EventListener/CampaignSubscriber.php @@ -13,9 +13,11 @@ use Doctrine\DBAL\Connection; use Mautic\CampaignBundle\CampaignEvents; +use Mautic\CampaignBundle\Entity\Event; use Mautic\CampaignBundle\Event\CampaignBuilderEvent; use Mautic\CampaignBundle\Event\CampaignExecutionEvent; use Mautic\CampaignBundle\Model\CampaignModel; +use Mautic\CampaignBundle\Model\EventModel; use Mautic\CoreBundle\EventListener\CommonSubscriber; use Mautic\PluginBundle\Helper\IntegrationHelper; use MauticPlugin\MauticRecurringCampaignsBundle\RecurringCampaignsEvents; @@ -37,17 +39,27 @@ class CampaignSubscriber extends CommonSubscriber */ protected $campaignModel; + /** + * @var EventModel + */ + private $eventModel; + /** * ButtonSubscriber constructor. * - * @param IntegrationHelper $helper - * @param Connection $db + * @param IntegrationHelper $integrationHelper + * @param Connection $db + * @param CampaignModel $campaignModel + * @param EventModel $eventModel + * + * @internal param IntegrationHelper $helper */ - public function __construct(IntegrationHelper $integrationHelper, Connection $db, CampaignModel $campaignModel) + public function __construct(IntegrationHelper $integrationHelper, Connection $db, CampaignModel $campaignModel, EventModel $eventModel) { $this->integrationHelper = $integrationHelper; $this->db = $db; $this->campaignModel = $campaignModel; + $this->eventModel = $eventModel; } /** @@ -99,8 +111,23 @@ public function onCampaignTriggerAction(CampaignExecutionEvent $event) $lead = $event->getLead(); $campaigns = $event->getConfig()['campaigns']; - + $limit = $event->getConfig()['limit']; $qb = $this->db; + + $properties = $event->getEvent()['properties']; + /** @var Event $event */ + $eventEntity = $this->eventModel->getEntity($event->getEvent()['id']); + $numberOfRotation = isset($properties['numberOfRotation']) ? $properties['numberOfRotation'] : 0; + + // return true if max execution + if (!empty($limit) && $numberOfRotation >= $limit) { + return $event->setResult(true); + } + + $properties['numberOfRotation'] = $numberOfRotation + 1; + $eventEntity->setProperties($properties); + $this->eventModel->saveEntity($eventEntity); + foreach ($campaigns as $campaignId) { if(!empty($event->getConfig()['action'])){ $qb->delete( diff --git a/Form/Type/CampaignEventRemoveLogsActionType.php b/Form/Type/CampaignEventRemoveLogsActionType.php index 529ab94..2d30699 100644 --- a/Form/Type/CampaignEventRemoveLogsActionType.php +++ b/Form/Type/CampaignEventRemoveLogsActionType.php @@ -12,6 +12,7 @@ namespace MauticPlugin\MauticRecurringCampaignsBundle\Form\Type; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints\NotBlank; /** @@ -43,6 +44,20 @@ public function buildForm(FormBuilderInterface $builder, array $options) ], ]); + $builder->add( + 'limit', + NumberType::class, + [ + 'label' => 'plugin.recurring.campaigns.campaign.execute.limit', + 'label_attr' => ['class' => 'control-label'], + 'attr'=> [ + 'class' => 'form-control', + 'tooltip'=>'plugin.recurring.campaigns.campaign.execute.limit.desc' + ], + 'data' => !empty($options['data']['limit']) ? $options['data']['limit'] : 0, + ] + ); + $builder->add( 'action', 'yesno_button_group', diff --git a/Translations/en_US/messages.ini b/Translations/en_US/messages.ini index e1e7805..005c8a4 100644 --- a/Translations/en_US/messages.ini +++ b/Translations/en_US/messages.ini @@ -6,5 +6,7 @@ plugin.recurring.campaigns.campaign.remove.scheduled="Remove just scheduled cont plugin.recurring.campaigns.campaign.remove.scheduled.desc="If disabled, all contacts logs will remove" plugin.recurring.campaigns.campaign.remove.from="After remove logs remove contact from selected campaigns" plugin.recurring.campaigns.campaign.remove.from.desc="Contact will remove also from selected campaigsn after" +plugin.recurring.campaigns.campaign.execute.limit="Max event execution" +plugin.recurring.campaigns.campaign.execute.limit.desc="Set max time of remove logs campaigns. Unlimited is zero"