-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from TheDMSGroup/ENG-335
[ENG-335] Import leads from file
- Loading branch information
Showing
5 changed files
with
417 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
/* | ||
* @copyright 2018 Mautic Contributors. All rights reserved | ||
* @author Mautic, Inc | ||
* | ||
* @link http://mautic.org | ||
* | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
namespace MauticPlugin\MauticContactSourceBundle\EventListener; | ||
|
||
use Mautic\CampaignBundle\Model\CampaignModel; | ||
use Mautic\CoreBundle\EventListener\CommonSubscriber; | ||
use Mautic\LeadBundle\Event\LeadEvent; | ||
use Mautic\LeadBundle\LeadEvents; | ||
use Mautic\LeadBundle\Model\ImportModel; | ||
use MauticPlugin\MauticContactSourceBundle\Model\Api as ApiModel; | ||
use MauticPlugin\MauticContactSourceBundle\Model\ContactSourceModel; | ||
|
||
/** | ||
* Class BatchSubscriber. | ||
*/ | ||
class BatchSubscriber extends CommonSubscriber | ||
{ | ||
/** | ||
* @var ContactSourceModel | ||
*/ | ||
protected $sourceModel; | ||
|
||
/** | ||
* @var CampaignModel | ||
*/ | ||
protected $campaignModel; | ||
|
||
/** | ||
* @var ApiModel | ||
*/ | ||
protected $apiModel; | ||
|
||
/** | ||
* @var ImportModel | ||
*/ | ||
protected $importModel; | ||
|
||
/** | ||
* FormSubscriber constructor. | ||
* | ||
* @param ContactSourceModel $sourceModel | ||
* @param CampaignModel $campaignModel | ||
* @param ApiModel $apiModel | ||
* @param ImportModel $importModel | ||
*/ | ||
public function __construct( | ||
ContactSourceModel $sourceModel, | ||
CampaignModel $campaignModel, | ||
ApiModel $apiModel, | ||
ImportModel $importModel | ||
) { | ||
$this->sourceModel = $sourceModel; | ||
$this->campaignModel = $campaignModel; | ||
$this->apiModel = $apiModel; | ||
$this->importModel = $importModel; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [LeadEvents::LEAD_PRE_SAVE => 'onLeadPreSave', | ||
LeadEvents::LEAD_POST_SAVE => 'onLeadPostSave', ]; | ||
} | ||
|
||
public function onLeadPreSave(LeadEvent $leadEvent) | ||
{ | ||
if (true == $leadEvent->getLead()->imported | ||
&& $this->em->getUnitOfWork()->getIdentityMap()['Mautic\LeadBundle\Entity\Import']) { | ||
$this->apiModel->setImported(true); | ||
|
||
$import = array_shift($this->em->getUnitOfWork()->getIdentityMap()['Mautic\LeadBundle\Entity\Import']); | ||
$importProperties = $import->getProperties(); | ||
|
||
$campaignId = $importProperties['parser']['campaign']; | ||
$sourceId = $importProperties['parser']['source']; | ||
|
||
$this->apiModel->contact = $leadEvent->getLead(); | ||
$this->apiModel->setCampaignId($campaignId); | ||
$this->apiModel->setCampaign($this->campaignModel->getEntity($campaignId)); | ||
$this->apiModel->setSourceId($sourceId); | ||
$this->apiModel->setContactSource($this->apiModel->getContactSourceModel()->getEntity($sourceId)); | ||
|
||
$this->apiModel->parseSourceCampaignSettings(); | ||
$this->apiModel->processOffline(); | ||
$this->apiModel->applyAttribution(); | ||
|
||
$leadEvent->setLead($this->apiModel->contact); | ||
} | ||
} | ||
|
||
public function onLeadPostSave(LeadEvent $leadEvent) | ||
{ | ||
if (true == $leadEvent->getLead()->imported | ||
&& $this->em->getUnitOfWork()->getIdentityMap()['Mautic\LeadBundle\Entity\Import']) { | ||
$this->apiModel->setImported(true); | ||
|
||
$import = array_shift($this->em->getUnitOfWork()->getIdentityMap()['Mautic\LeadBundle\Entity\Import']); | ||
$importProperties = $import->getProperties(); | ||
|
||
$campaignId = $importProperties['parser']['campaign']; | ||
$sourceId = $importProperties['parser']['source']; | ||
|
||
$this->apiModel->setContact($leadEvent->getLead()); | ||
$this->apiModel->setCampaignId($campaignId); | ||
$this->apiModel->setCampaign($this->campaignModel->getEntity($campaignId)); | ||
$this->apiModel->setSourceId($sourceId); | ||
$this->apiModel->setContactSource($this->apiModel->getContactSourceModel()->getEntity($sourceId)); | ||
$this->apiModel->parseSourceCampaignSettings(); | ||
|
||
$this->apiModel->addContactsToCampaign($this->apiModel->getCampaign(), [$leadEvent->getLead()], true); | ||
$this->apiModel->logResults(); | ||
|
||
$leadEvent->setLead($this->apiModel->getContact()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?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 MauticPlugin\MauticContactSourceBundle\Form\Extension; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Mautic\CampaignBundle\Entity\Campaign; | ||
use Mautic\LeadBundle\Form\Type\LeadImportType; | ||
use MauticPlugin\MauticContactSourceBundle\Entity\ContactSource; | ||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\CallbackTransformer; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class LeadImportExtension extends AbstractTypeExtension | ||
{ | ||
/** @var EntityManager */ | ||
protected $em; | ||
|
||
public function __construct(EntityManager $em) | ||
{ | ||
/* @var EntityManager $em */ | ||
$this->em = $em; | ||
} | ||
|
||
/** | ||
* Returns the name of the type being extended. | ||
* | ||
* @return string The name of the type being extended | ||
*/ | ||
public function getExtendedType() | ||
{ | ||
return LeadImportType::class; | ||
} | ||
|
||
/** | ||
* Add a custom 'object' type to write to a corresponding table for that new custom value. | ||
* | ||
* @param FormBuilderInterface $builder | ||
* @param array $options | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->add( | ||
'source', | ||
EntityType::class, | ||
[ | ||
'class' => 'MauticContactSourceBundle:ContactSource', | ||
'empty_value' => 'Select A Source', | ||
'choice_label' => function ($source) { | ||
return $source->getName(); | ||
}, | ||
] | ||
); | ||
|
||
$builder->get('source') | ||
->addModelTransformer(new CallbackTransformer( | ||
function ($source) { | ||
// transform the ID back to an Object | ||
return $source ? $this->em->getRepository('MauticCampaignBundle:Event')->find($source) : null; | ||
}, | ||
function ($source) { | ||
// transform the object to a ID | ||
return $source ? $source->getID() : null; | ||
} | ||
)); | ||
|
||
$builder->add( | ||
'campaign', | ||
EntityType::class, | ||
[ | ||
'class' => 'MauticCampaignBundle:Campaign', | ||
'empty_value' => 'Select A Campaign', | ||
'choice_label' => function ($campaign) { | ||
return $campaign->getName(); | ||
}, | ||
] | ||
); | ||
|
||
$builder->get('campaign') | ||
->addModelTransformer(new CallbackTransformer( | ||
function ($campaign) { | ||
// transform the ID back to an Object | ||
return $campaign ? $this->em->getRepository('MauticCampaignBundle:Campaign')->find($campaign) : null; | ||
}, | ||
function ($campaign) { | ||
// transform the object to a ID | ||
return $campaign ? $campaign->getID() : null; | ||
} | ||
)); | ||
} | ||
} |
Oops, something went wrong.