private function addContactsToCampaign(
        Campaign $campaign,
        $contacts = [],
        $manuallyAdded = false
    ) {
        foreach ($contacts as $contact) {
            $campaignContact = new CampaignContact();
            $alreadyExists   = false;
            if (!null == $contact->getDateModified(
                )) { // see if New Contact b/c isNew() is unreliable on PostSave Event
                $leadRepository = $this->em->getRepository('MauticCampaignBundle:Lead');
                $alreadyExists  = $leadRepository->checkLeadInCampaigns(
                    $contact,
                    ['campaigns' => [$campaign->getId()]]
                );
            }
            if (!$alreadyExists) {
                $campaignContact->setCampaign($campaign);
                $campaignContact->setDateAdded(new \DateTime());
                $campaignContact->setLead($contact);
                $campaignContact->setManuallyAdded($manuallyAdded);
                $saved = $this->campaignModel->saveCampaignLead($campaignContact);
            }

            // @todo - Support non realtime event firing.
            // if (!$realTime) {
            //     // Only trigger events if not in realtime where events would be followed directly.
            //     if ($saved && $this->campaignModel->hasListeners(CampaignEvents::CAMPAIGN_ON_LEADCHANGE)) {
            //         $event = new CampaignLeadChangeEvent($campaign, $contact, 'added');
            //         $this->dispatcher->dispatch(CampaignEvents::CAMPAIGN_ON_LEADCHANGE, $event);
            //         unset($event);
            //     }
            // }
            $this->em->detach($campaignContact);
        }
        unset($campaign, $campaignContact, $contacts);
    }
private function addContactsToCampaign(
        Campaign $campaign,
        $contacts = [],
        $manuallyAdded = false
    ) {
        foreach ($contacts as $contact) {
            $campaignContact = new CampaignContact();
            $alreadyExists   = false;
            if (!null == $contact->getDateModified(
                )) { // see if New Contact b/c isNew() is unreliable on PostSave Event
                $leadRepository = $this->em->getRepository('MauticCampaignBundle:Lead');
                $alreadyExists  = $leadRepository->checkLeadInCampaigns(
                    $contact,
                    ['campaigns' => [$campaign->getId()]]
                );
            }
            if (!$alreadyExists) {
                $campaignContact->setCampaign($campaign);
                $campaignContact->setDateAdded(new \DateTime());
                $campaignContact->setLead($contact);
                $campaignContact->setManuallyAdded($manuallyAdded);
                $saved = $this->campaignModel->saveCampaignLead($campaignContact);
            }

            // @todo - Support non realtime event firing.
            // if (!$realTime) {
            //     // Only trigger events if not in realtime where events would be followed directly.
            //     if ($saved && $this->campaignModel->hasListeners(CampaignEvents::CAMPAIGN_ON_LEADCHANGE)) {
            //         $event = new CampaignLeadChangeEvent($campaign, $contact, 'added');
            //         $this->dispatcher->dispatch(CampaignEvents::CAMPAIGN_ON_LEADCHANGE, $event);
            //         unset($event);
            //     }
            // }
            $this->em->detach($campaignContact);
        }
        unset($campaign, $campaignContact, $contacts);
    }
private function addContactsToCampaign ( Campaign $ campaign , $ contacts = [ ] , $ manuallyAdded = false ) { foreach ( $ contacts as $ contact ) { $ campaignContact = new CampaignContact ( ) ; $ alreadyExists = false ; if ( ! null == $ contact -> getDateModified ( ) ) { // see if New Contact b/c isNew() is unreliable on PostSave Event $ leadRepository = $ this -> em -> getRepository ( 'MauticCampaignBundle:Lead' ) ; $ alreadyExists = $ leadRepository -> checkLeadInCampaigns ( $ contact , [ 'campaigns' => [ $ campaign -> getId ( ) ] ] ) ; } if ( ! $ alreadyExists ) { $ campaignContact -> setCampaign ( $ campaign ) ; $ campaignContact -> setDateAdded ( new \ DateTime ( ) ) ; $ campaignContact -> setLead ( $ contact ) ; $ campaignContact -> setManuallyAdded ( $ manuallyAdded ) ; $ saved = $ this -> campaignModel -> saveCampaignLead ( $ campaignContact ) ; } // @todo - Support non realtime event firing. // if (!$realTime) { //     // Only trigger events if not in realtime where events would be followed directly. //     if ($saved && $this->campaignModel->hasListeners(CampaignEvents::CAMPAIGN_ON_LEADCHANGE)) { //         $event = new CampaignLeadChangeEvent($campaign, $contact, 'added'); //         $this->dispatcher->dispatch(CampaignEvents::CAMPAIGN_ON_LEADCHANGE, $event); //         unset($event); //     } // } $ this -> em -> detach ( $ campaignContact ) ; } unset ( $ campaign , $ campaignContact , $ contacts ) ; }
Add contact to a campaign, and optionally run in real-time.

@param Campaign $campaign
@param array    $contacts
@param bool     $manuallyAdded

@throws \Exception
Add contact to a campaign and optionally run in real - time .