Skip to content

Commit

Permalink
Merge pull request #80 from TheDMSGroup/ENG-335
Browse files Browse the repository at this point in the history
[ENG-335] Import leads from file
  • Loading branch information
heathdutton authored Jul 9, 2018
2 parents 0837ce9 + 74022f0 commit ab26570
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 69 deletions.
21 changes: 21 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@
'mautic.contactsource.dashboard.subscriber' => [
'class' => 'MauticPlugin\MauticContactSourceBundle\EventListener\DashboardSubscriber',
],
'mautic.contactsource.subscriber.batch' => [
'class' => 'MauticPlugin\MauticContactSourceBundle\EventListener\BatchSubscriber',
'arguments' => [
'mautic.contactsource.model.contactsource',
'mautic.campaign.model.campaign',
'mautic.contactsource.model.api',
'mautic.lead.model.import',
],
],
],
'forms' => [
'mautic.contactsource.form.type.contactsourceshow_list' => [
Expand Down Expand Up @@ -197,6 +206,18 @@
'class' => 'MauticPlugin\MauticContactSourceBundle\Helper\JSONHelper',
],
],
'extension' => [
'mautic.contactsource.extension.lead_import' => [
'class' => 'MauticPlugin\MauticContactSourceBundle\Form\Extension\LeadImportExtension',
'arguments' => [
'doctrine.orm.entity_manager',
],
'tag' => 'form.type_extension',
'tagArguments' => [
'extended_type' => 'Mautic\LeadBundle\Form\Type\LeadImportType',
],
],
],
],

'menu' => [
Expand Down
127 changes: 127 additions & 0 deletions EventListener/BatchSubscriber.php
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());
}
}
}
100 changes: 100 additions & 0 deletions Form/Extension/LeadImportExtension.php
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;
}
));
}
}
Loading

0 comments on commit ab26570

Please sign in to comment.