Skip to content

Commit

Permalink
Merge pull request #4800 from kuzmany/date-added-to-campaign-condition
Browse files Browse the repository at this point in the history
New campaign condition: Contacts campaign
  • Loading branch information
Melissa Leffler committed Mar 30, 2018
2 parents 27dc024 + 84caa52 commit 6f4f244
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 1 deletion.
34 changes: 34 additions & 0 deletions app/bundles/CampaignBundle/Entity/LeadRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,38 @@ public function updateLead($fromLeadId, $toLeadId)
$q->execute();
}
}

/**
* Check Lead in campaign.
*
* @param Lead $lead
* @param array $options
*
* @return bool
*/
public function checkLeadInCampaigns($lead, $options = [])
{
if (empty($options['campaigns'])) {
return false;
}
$q = $this->_em->getConnection()->createQueryBuilder();
$q->select('l.campaign_id')
->from(MAUTIC_TABLE_PREFIX.'campaign_leads', 'l');
$q->where(
$q->expr()->andX(
$q->expr()->eq('l.lead_id', ':leadId'),
$q->expr()->in('l.campaign_id', $options['campaigns'], \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
)
);

if (!empty($options['dataAddedLimit'])) {
$q->andWhere($q->expr()
->{$options['expr']}('l.date_added', ':dateAdded'))
->setParameter('dateAdded', $options['dateAdded']);
}

$q->setParameter('leadId', $lead->getId());

return (bool) $q->execute()->fetchColumn();
}
}
6 changes: 6 additions & 0 deletions app/bundles/LeadBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@
'mautic.lead.model.lead',
'mautic.lead.model.field',
'mautic.lead.model.list',
'mautic.campaign.model.campaign',
],
],
'mautic.lead.reportbundle.subscriber' => [
Expand Down Expand Up @@ -617,6 +618,11 @@
'class' => 'Mautic\LeadBundle\Form\Type\CampaignEventLeadSegmentsType',
'alias' => 'campaignevent_lead_segments',
],
'mautic.form.type.campaignevent_lead_campaigns' => [
'class' => Mautic\LeadBundle\Form\Type\CampaignEventLeadCampaignsType::class,
'alias' => 'campaignevent_lead_campaigns',
'arguments' => ['mautic.lead.model.list'],
],
'mautic.form.type.campaignevent_lead_owner' => [
'class' => 'Mautic\LeadBundle\Form\Type\CampaignEventLeadOwnerType',
'alias' => 'campaignevent_lead_owner',
Expand Down
21 changes: 20 additions & 1 deletion app/bundles/LeadBundle/EventListener/CampaignSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Mautic\CampaignBundle\CampaignEvents;
use Mautic\CampaignBundle\Event\CampaignBuilderEvent;
use Mautic\CampaignBundle\Event\CampaignExecutionEvent;
use Mautic\CampaignBundle\Model\CampaignModel;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\LeadBundle\Entity\Lead;
Expand Down Expand Up @@ -51,19 +52,25 @@ class CampaignSubscriber extends CommonSubscriber
*/
protected $listModel;

/**
* @var CampaignModel
*/
protected $campaignModel;

/**
* CampaignSubscriber constructor.
*
* @param IpLookupHelper $ipLookupHelper
* @param LeadModel $leadModel
* @param FieldModel $leadFieldModel
*/
public function __construct(IpLookupHelper $ipLookupHelper, LeadModel $leadModel, FieldModel $leadFieldModel, ListModel $listModel)
public function __construct(IpLookupHelper $ipLookupHelper, LeadModel $leadModel, FieldModel $leadFieldModel, ListModel $listModel, CampaignModel $campaignModel)
{
$this->ipLookupHelper = $ipLookupHelper;
$this->leadModel = $leadModel;
$this->leadFieldModel = $leadFieldModel;
$this->listModel = $listModel;
$this->campaignModel = $campaignModel;
}

/**
Expand Down Expand Up @@ -209,6 +216,16 @@ public function onCampaignBuild(CampaignBuilderEvent $event)
];

$event->addCondition('lead.owner', $trigger);

$trigger = [
'label' => 'mautic.lead.lead.events.campaigns',
'description' => 'mautic.lead.lead.events.campaigns_descr',
'formType' => 'campaignevent_lead_campaigns',
'formTheme' => 'MauticLeadBundle:FormTheme\ContactCampaignsCondition',
'eventName' => LeadEvents::ON_CAMPAIGN_TRIGGER_CONDITION,
];

$event->addCondition('lead.campaigns', $trigger);
}

/**
Expand Down Expand Up @@ -426,6 +443,8 @@ public function onCampaignTriggerCondition(CampaignExecutionEvent $event)
$result = $listRepo->checkLeadSegmentsByIds($lead, $event->getConfig()['segments']);
} elseif ($event->checkContext('lead.owner')) {
$result = $this->leadModel->getRepository()->checkLeadOwner($lead, $event->getConfig()['owner']);
} elseif ($event->checkContext('lead.campaigns')) {
$result = $this->campaignModel->getCampaignLeadRepository()->checkLeadInCampaigns($lead, $event->getConfig());
} elseif ($event->checkContext('lead.field_value')) {
if ($event->getConfig()['operator'] === 'date') {
// Set the date in system timezone since this is triggered by cron
Expand Down
103 changes: 103 additions & 0 deletions app/bundles/LeadBundle/Form/Type/CampaignEventLeadCampaignsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/*
* @copyright 2014 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\ListModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

/**
* Class CampaignEventLeadCampaignsType.
*/
class CampaignEventLeadCampaignsType extends AbstractType
{
/**
* @var ListModel
*/
protected $listModel;

/**
* CampaignEventLeadCampaignsType constructor.
*
* @param ListModel $listModel
*/
public function __construct(ListModel $listModel)
{
$this->listModel = $listModel;
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('campaigns',
'campaign_list', [
'label' => 'mautic.lead.lead.events.campaigns.membership',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
],
'required' => true,
]);

$builder->add(
'dataAddedLimit',
'yesno_button_group',
[
'label' => 'mautic.lead.lead.events.campaigns.date.added.filter',
'data' => (isset($options['data']['dataAddedLimit'])) ? $options['data']['dataAddedLimit'] : false,
]
);

$builder->add(
'expr',
'choice',
[
'label' => 'mautic.lead.lead.events.campaigns.expression',
'multiple' => false,
'choices' => $this->listModel->getOperatorsForFieldType(
[
'include' => [
'gt',
'lt',
],
]),
'required' => false,
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'data-show-on' => '{"campaignevent_properties_dataAddedLimit_1":"checked"}',
],
]
);

$builder->add('dateAdded', 'text', [
'label' => 'mautic.lead.lead.events.campaigns.date',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'data-toggle' => 'datetime',
'data-show-on' => '{"campaignevent_properties_dataAddedLimit_1":"checked"}',
],
'required' => false,
]);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'campaignevent_lead_campaigns';
}
}
6 changes: 6 additions & 0 deletions app/bundles/LeadBundle/Translations/en_US/messages.ini
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ mautic.lead.lead.events.tags="Contact tags"
mautic.lead.lead.events.tags_descr="Condition based on a contact tags."
mautic.lead.lead.events.segments="Contact segments"
mautic.lead.lead.events.segments_descr="Condition based on a contact segments."
mautic.lead.lead.events.campaigns="Contact campaigns"
mautic.lead.lead.events.campaigns.membership="Campaigns membership"
mautic.lead.lead.events.campaigns_descr="Condition based on a contact campaigns."
mautic.lead.lead.events.campaigns.date.added.filter="Filter by date added to campaign"
mautic.lead.lead.events.campaigns.expression="Expression"
mautic.lead.lead.events.campaigns.date="Date"
mautic.lead.lead.events.owner="Contact owner"
mautic.lead.lead.events.owner_descr="Condition based on a contact owner."
mautic.lead.lead.field.custom_avatar="Custom"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

<div class="row condition-row">
<div class="col-xs-12">
<?php echo $view['form']->row($form['campaigns']); ?>
</div>
</div>

<div class="row condition-row">
<div class="col-xs-5">
<?php echo $view['form']->row($form['dataAddedLimit']); ?>
</div>
<div class="col-xs-3">
<?php echo $view['form']->row($form['expr']); ?>
</div>
<div class="col-xs-4">
<?php echo $view['form']->row($form['dateAdded']); ?>
</div>
</div>

0 comments on commit 6f4f244

Please sign in to comment.