/**
  * @param Schema $schema
  */
 public function postUp(Schema $schema)
 {
     // Rebuild the lists
     /** @var \Mautic\LeadBundle\Model\ListModel $listModel */
     $listModel = $this->factory->getModel('lead.list');
     $lists = $listModel->getEntities(array('ignore_paginator' => true));
     foreach ($lists as $l) {
         $listModel->rebuildListLeads($l);
     }
     $leads = $listModel->getLeadsByList($lists, true);
     $persist = array();
     $em = $this->factory->getEntityManager();
     foreach ($this->includedLeads as $l) {
         if (!in_array($l['lead_id'], $leads[$l['leadlist_id']])) {
             $listLead = new ListLead();
             $listLead->setList($lists[$l['leadlist_id']]);
             $listLead->setLead($em->getReference('MauticLeadBundle:Lead', $l['lead_id']));
             $listLead->setManuallyAdded(true);
             $listLead->setDateAdded(new \DateTime());
             $lists[$l['leadlist_id']]->addLead($l['lead_id'], $listLead);
             $persist[$l['leadlist_id']] = $lists[$l['leadlist_id']];
         }
     }
     $listLeadRepository = $listModel->getListLeadRepository();
     foreach ($this->excludedLeads as $l) {
         if (in_array($l['lead_id'], $leads[$l['leadlist_id']])) {
             $listLead = $listLeadRepository->findOneBy(array('lead' => $l['lead_id'], 'list' => $l['leadlist_id']));
             $listLead->setManuallyRemoved(true);
             $lists[$l['leadlist_id']]->addLead($l['lead_id'], $listLead);
             $persist[$l['leadlist_id']] = $lists[$l['leadlist_id']];
         }
     }
     $listLeadRepository->saveEntities($persist);
 }
Example #2
0
 /**
  * Add lead to lists
  *
  * @param array|Lead        $lead
  * @param array|LeadList    $lists
  * @param bool              $manuallyAdded
  * @param bool              $batchProcess
  * @param int               $searchListLead 0 = reference, 1 = yes, -1 = known to not exist
  * @param \DateTime         $dateManipulated
  *
  * @throws \Doctrine\ORM\ORMException
  */
 public function addLead($lead, $lists, $manuallyAdded = false, $batchProcess = false, $searchListLead = 1, $dateManipulated = null)
 {
     if ($dateManipulated == null) {
         $dateManipulated = new \DateTime();
     }
     if (!$lead instanceof Lead) {
         $leadId = is_array($lead) && isset($lead['id']) ? $lead['id'] : $lead;
         $lead = $this->em->getReference('MauticLeadBundle:Lead', $leadId);
     } else {
         $leadId = $lead->getId();
     }
     if (!$lists instanceof LeadList) {
         //make sure they are ints
         $searchForLists = array();
         foreach ($lists as $k => &$l) {
             $l = (int) $l;
             if (!isset($this->leadChangeLists[$l])) {
                 $searchForLists[] = $l;
             }
         }
         if (!empty($searchForLists)) {
             $listEntities = $this->getEntities(array('filter' => array('force' => array(array('column' => 'l.id', 'expr' => 'in', 'value' => $searchForLists)))));
             foreach ($listEntities as $list) {
                 $this->leadChangeLists[$list->getId()] = $list;
             }
         }
         unset($listEntities, $searchForLists);
     } else {
         $this->leadChangeLists[$lists->getId()] = $lists;
         $lists = array($lists->getId());
     }
     if (!is_array($lists)) {
         $lists = array($lists);
     }
     $persistLists = array();
     $dispatchEvents = array();
     foreach ($lists as $listId) {
         if (!isset($this->leadChangeLists[$listId])) {
             // List no longer exists in the DB so continue to the next
             continue;
         }
         if ($searchListLead == -1) {
             $listLead = null;
         } elseif ($searchListLead) {
             $listLead = $this->getListLeadRepository()->findOneBy(array('lead' => $lead, 'list' => $this->leadChangeLists[$listId]));
         } else {
             $listLead = $this->em->getReference('MauticLeadBundle:ListLead', array('lead' => $leadId, 'list' => $listId));
         }
         if ($listLead != null) {
             if ($manuallyAdded && $listLead->wasManuallyRemoved()) {
                 $listLead->setManuallyRemoved(false);
                 $listLead->setManuallyAdded($manuallyAdded);
                 $persistLists[] = $listLead;
                 $dispatchEvents[] = $listId;
             } else {
                 // Detach from Doctrine
                 $this->em->detach($listLead);
                 continue;
             }
         } else {
             $listLead = new ListLead();
             $listLead->setList($this->leadChangeLists[$listId]);
             $listLead->setLead($lead);
             $listLead->setManuallyAdded($manuallyAdded);
             $listLead->setDateAdded($dateManipulated);
             $persistLists[] = $listLead;
             $dispatchEvents[] = $listId;
         }
     }
     if (!empty($persistLists)) {
         $this->getRepository()->saveEntities($persistLists);
     }
     // Clear ListLead entities from Doctrine memory
     $this->em->clear('Mautic\\LeadBundle\\Entity\\ListLead');
     if ($batchProcess) {
         // Detach for batch processing to preserve memory
         $this->em->detach($lead);
     } elseif (!empty($dispatchEvents) && $this->dispatcher->hasListeners(LeadEvents::LEAD_LIST_CHANGE)) {
         foreach ($dispatchEvents as $listId) {
             $event = new ListChangeEvent($lead, $this->leadChangeLists[$listId]);
             $this->dispatcher->dispatch(LeadEvents::LEAD_LIST_CHANGE, $event);
             unset($event);
         }
     }
     unset($lead, $persistLists, $lists);
 }
 /**
  * {@inheritDoc}
  */
 public function setManuallyAdded($manuallyAdded)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setManuallyAdded', array($manuallyAdded));
     return parent::setManuallyAdded($manuallyAdded);
 }