Example #1
0
 /**
  * Add/remove leads from campaigns based on lead list changes
  *
  * @param ListChangeEvent $event
  */
 public function onLeadListChange(ListChangeEvent $event)
 {
     $lead = $event->getLead();
     $list = $event->getList();
     $action = $event->wasAdded() ? 'added' : 'removed';
     $repo = $this->campaignModel->getRepository();
     //get campaigns for the list
     $listCampaigns = $repo->getPublishedCampaignsByLeadLists($list->getId());
     $leadLists = $this->leadModel->getLists($lead, true);
     $leadListIds = array_keys($leadLists);
     // If the lead was removed then don't count it
     if ($action == 'removed') {
         $key = array_search($list->getId(), $leadListIds);
         unset($leadListIds[$key]);
     }
     if (!empty($listCampaigns)) {
         foreach ($listCampaigns as $c) {
             $campaign = $this->em->getReference('MauticCampaignBundle:Campaign', $c['id']);
             if (!isset($campaignLists[$c['id']])) {
                 $campaignLists[$c['id']] = array_keys($c['lists']);
             }
             if ($action == 'added') {
                 $this->campaignModel->addLead($campaign, $lead);
             } else {
                 if (array_intersect($leadListIds, $campaignLists[$c['id']])) {
                     continue;
                 }
                 $this->campaignModel->removeLead($campaign, $lead);
             }
             unset($campaign);
         }
     }
 }