/** * @param ListChangeEvent $eventDetails * @param $event * * @return bool */ public static function validateListChange(ListChangeEvent $eventDetails, $event) { $limitAddTo = $event['properties']['addedTo']; $limitRemoveFrom = $event['properties']['removedFrom']; $list = $eventDetails->getList(); if ($eventDetails->wasAdded() && !empty($limitAddTo) && !in_array($list->getId(), $limitAddTo)) { return false; } if ($eventDetails->wasRemoved() && !empty($limitRemoveFrom) && !in_array($list->getId(), $limitRemoveFrom)) { return false; } return true; }
/** * 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); } } }