Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking lead manipulator #5379

Merged
merged 24 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9d921e3
LeadManipulator + event log
Hadamcik Nov 27, 2017
7643b19
LeadModel +setManipulator
Hadamcik Nov 27, 2017
e88ec04
Fix manipulators
Hadamcik Nov 27, 2017
f504a28
Page hits fix
Hadamcik Nov 27, 2017
62c6882
Merge event log
Hadamcik Nov 28, 2017
c6add31
Pull staging
Hadamcik Nov 28, 2017
8143b25
PHPDoc
Hadamcik Mar 27, 2018
7de7a6b
Pull upstream staging
Hadamcik Mar 27, 2018
47ea3c0
Log import ID
Hadamcik Mar 28, 2018
aab73cf
Merge branch 'staging' of github.com:mautic/mautic into staging.track…
Hadamcik Mar 28, 2018
c16a7f0
Force reformat
Hadamcik Mar 28, 2018
0ae20af
Fixed issue with tracking pixel where $page is null and also only man…
alanhartless Mar 29, 2018
cd52227
Don't store a create/identified event everytime the contact is saved
alanhartless Mar 29, 2018
3c522bd
Don't store empty logs for deleted contacts
alanhartless Mar 29, 2018
cfc0aec
Added action index
alanhartless Mar 30, 2018
22778b6
Do not create visitors for certain requests (javascript endpoints tha…
alanhartless Mar 30, 2018
2f03a0b
Display entries in the timeline
alanhartless Mar 30, 2018
ef4ba89
Store a description to display in the timeline
alanhartless Mar 30, 2018
b5423e0
Fixed tests
alanhartless Apr 2, 2018
b5ef6f8
LandingPage tests
Hadamcik Apr 5, 2018
8b5b79c
Fix tests + skip second visit test
Hadamcik Apr 5, 2018
855863b
Merge branch 'staging' of github.com:Dreiser/mautic into staging.trac…
Hadamcik Apr 5, 2018
f9bf1ed
Pull staging
Hadamcik Apr 5, 2018
e3d2e1f
Set current lead missing - fix
Hadamcik Apr 5, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/bundles/FormBundle/Model/SubmissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Mautic\FormBundle\Helper\FormFieldHelper;
use Mautic\FormBundle\Helper\FormUploader;
use Mautic\FormBundle\Validator\UploadFieldValidator;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\CompanyChangeLog;
use Mautic\LeadBundle\Entity\Lead;
Expand Down Expand Up @@ -1004,6 +1005,11 @@ protected function createLeadFromSubmit(Form $form, array $leadFieldMatches, $le
$lead->setLastActive(new \DateTime());

//create a new lead
$lead->setManipulator(new LeadManipulator(
'form',
'submission',
$form->getId()
));
$this->leadModel->saveEntity($lead, false);

if (!$inKioskMode) {
Expand Down
10 changes: 10 additions & 0 deletions app/bundles/LeadBundle/Controller/LeadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Mautic\CoreBundle\Controller\FormController;
use Mautic\CoreBundle\Helper\EmojiHelper;
use Mautic\CoreBundle\Model\IteratorExportDataModel;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
Expand Down Expand Up @@ -441,6 +442,10 @@ public function newAction()
$model->setFieldValues($lead, $data, true);

//form is valid so process the data
$lead->setManipulator(new LeadManipulator(
'lead',
'lead'
));
$model->saveEntity($lead);

if (!empty($companies)) {
Expand Down Expand Up @@ -622,6 +627,11 @@ public function editAction($objectId, $ignorePost = false)
$model->setFieldValues($lead, $data, true);

//form is valid so process the data
$lead->setManipulator(new LeadManipulator(
'lead',
'lead',
$objectId
));
$model->saveEntity($lead, $form->get('buttons')->get('save')->isClicked());
$model->modifyCompanies($lead, $companies);

Expand Down
65 changes: 65 additions & 0 deletions app/bundles/LeadBundle/DataObject/LeadManipulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 Mautic\LeadBundle\DataObject;

/**
* Class LeadManipulator.
*/
class LeadManipulator
{
/** @var string|null */
private $bundleName;

/** @var string|null */
private $objectName;

/** @var int|null */
private $objectId;

/**
* LeadManipulator constructor.
*
* @param string|null $bundleName
* @param string|null $objectName
* @param int|null $objectId
*/
public function __construct($bundleName = null, $objectName = null, $objectId = null)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason why the first 2 params are nullable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in database, they can be nullable. I thought maybe there is use case when it's possible

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I was thinking we should write the code as strict as possible.

{
$this->bundleName = $bundleName;
$this->objectName = $objectName;
$this->objectId = $objectId;
}

/**
* @return string
*/
public function getBundleName()
{
return $this->bundleName;
}

/**
* @return string
*/
public function getObjectName()
{
return $this->objectName;
}

/**
* @return int
*/
public function getObjectId()
{
return $this->objectId;
}
}
24 changes: 24 additions & 0 deletions app/bundles/LeadBundle/Entity/Lead.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;
use Mautic\CoreBundle\Entity\IpAddress;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Model\FieldModel;
use Mautic\NotificationBundle\Entity\PushID;
use Mautic\StageBundle\Entity\Stage;
Expand Down Expand Up @@ -199,6 +200,9 @@ class Lead extends FormEntity implements CustomFieldEntityInterface
*/
private $color;

/** @var LeadManipulator */
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this annotation use the new lines as all of the annotations above and bellow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, it's just so long..

private $manipulator = null;

/**
* Sets if the IP was just created by LeadModel::getCurrentLead().
*
Expand Down Expand Up @@ -1286,6 +1290,26 @@ protected function getFirstSocialIdentity()
return false;
}

/**
* @param LeadManipulator|null $manipulator
*
* @return self
*/
public function setManipulator(LeadManipulator $manipulator = null)
{
$this->manipulator = $manipulator;

return $this;
}

/**
* @return LeadManipulator|null
*/
public function getManipulator()
{
return $this->manipulator;
}

/**
* @return bool
*/
Expand Down
15 changes: 15 additions & 0 deletions app/bundles/LeadBundle/Entity/LeadEventLogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ public function getEventsByLead($bundle, $object, Lead $lead = null, array $opti
return $this->getTimelineResults($qb, $options, $alias.'.original_file', $alias.'.date_added', ['query'], ['date_added']);
}

/**
* Updates lead ID (e.g. after a lead merge).
*
* @param int $fromLeadId
* @param int $toLeadId
*/
public function updateLead($fromLeadId, $toLeadId)
{
$q = $this->_em->getConnection()->createQueryBuilder();
$q->update(MAUTIC_TABLE_PREFIX.'lead_event_log')
->set('lead_id', (int) $toLeadId)
->where('lead_id = '.(int) $fromLeadId)
->execute();
}

/**
* Defines default table alias for lead_event_log table.
*
Expand Down
12 changes: 12 additions & 0 deletions app/bundles/LeadBundle/EventListener/LeadSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static function getSubscribedEvents()
return [
LeadEvents::LEAD_POST_SAVE => ['onLeadPostSave', 0],
LeadEvents::LEAD_POST_DELETE => ['onLeadDelete', 0],
LeadEvents::LEAD_PRE_MERGE => ['preLeadMerge', 0],
LeadEvents::LEAD_POST_MERGE => ['onLeadMerge', 0],
LeadEvents::FIELD_POST_SAVE => ['onFieldPostSave', 0],
LeadEvents::FIELD_POST_DELETE => ['onFieldDelete', 0],
Expand Down Expand Up @@ -247,6 +248,17 @@ public function onNoteDelete(Events\LeadNoteEvent $event)
$this->auditLogModel->writeToLog($log);
}

/**
* @param Events\LeadMergeEvent $event
*/
public function preLeadMerge(Events\LeadMergeEvent $event)
{
$this->em->getRepository('MauticLeadBundle:LeadEventLog')->updateLead(
$event->getLoser()->getId(),
$event->getVictor()->getId()
);
}

/**
* @param Events\LeadMergeEvent $event
*/
Expand Down
24 changes: 24 additions & 0 deletions app/bundles/LeadBundle/Model/LeadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\EmailBundle\Helper\EmailValidator;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\CompanyChangeLog;
use Mautic\LeadBundle\Entity\DoNotContact;
Expand Down Expand Up @@ -497,6 +498,21 @@ public function saveEntity($entity, $unlock = true)
$entity->addCompanyChangeLogEntry('form', 'Identify Company', 'Lead added to the company, '.$company['companyname'], $company['id']);
}
}
$manipulator = $entity->getManipulator();
if ($manipulator !== null) {
$manipulationLog = new LeadEventLog();
$manipulationLog->setLead($entity);
$manipulationLog->setBundle($manipulator->getBundleName());
$manipulationLog->setObject($manipulator->getObjectName());
$manipulationLog->setObjectId($manipulator->getObjectId());
if ($entity->isNewlyCreated()) {
$manipulationLog->setAction('create');
} else {
$manipulationLog->setAction('identified');
}
$entity->addEventLog($manipulationLog);
$entity->setManipulator(null);
}

$this->setEntityDefaultValues($entity);

Expand Down Expand Up @@ -1906,6 +1922,10 @@ public function import($fields, $data, $owner = null, $list = null, $tags = null
}

if ($persist) {
$lead->setManipulator(new LeadManipulator(
'lead',
'lead'
));
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm not understanding the task right, but how can you tell if the source of the lead is import over manual creation over UI? This code is called 4 times on 4 different places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already long time, so I did not fully recalled what is bundle name and what is object name. Where do you see problem? It's manipulated from lead bundle, lead model so it's lead-lead if I'm right.

I'm not sure if it says something about UI

$this->saveEntity($lead);

if ($list !== null) {
Expand Down Expand Up @@ -2911,6 +2931,10 @@ protected function createNewContact(IpAddress $ip, $persist = true)
if ($persist) {
// Set to prevent loops
$this->currentLead = $lead;
$lead->setManipulator(new LeadManipulator(
'lead',
'lead'
));
$this->saveEntity($lead, false);

$fields = $this->getLeadDetails($lead);
Expand Down
6 changes: 6 additions & 0 deletions app/bundles/PageBundle/Model/PageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Mautic\CoreBundle\Model\FormModel;
use Mautic\CoreBundle\Model\TranslationModelTrait;
use Mautic\CoreBundle\Model\VariantModelTrait;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadDevice;
use Mautic\LeadBundle\Entity\UtmTag;
Expand Down Expand Up @@ -469,6 +470,11 @@ public function hitPage($page, Request $request, $code = '200', Lead $lead = nul
// Lead came from a non-trackable IP so ignore
return;
}
$lead->setManipulator(new LeadManipulator(
'page',
$page instanceof Page ? 'page' : 'redirect',
$page->getId()
));
$this->leadModel->saveEntity($lead);

$ipAddress = $this->ipLookupHelper->getIpAddress();
Expand Down
5 changes: 5 additions & 0 deletions app/bundles/PluginBundle/Integration/AbstractIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Mautic\CoreBundle\Helper\EncryptionHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\CompanyModel;
Expand Down Expand Up @@ -2065,6 +2066,10 @@ public function getMauticLead($data, $persist = true, $socialCache = null, $iden
if ($persist && !empty($lead->getChanges(true))) {
// Only persist if instructed to do so as it could be that calling code needs to manipulate the lead prior to executing event listeners
try {
$lead->setManipulator(new LeadManipulator(
'plugin',
$this->getName()
));
$leadModel->saveEntity($lead, false);
} catch (\Exception $exception) {
$this->logger->addWarning($exception->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace MauticPlugin\MauticCrmBundle\Integration;

use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Helper\IdentifyCompanyHelper;
Expand Down Expand Up @@ -460,6 +461,10 @@ public function getMauticLead($data, $persist = true, $socialCache = null, $iden

if ($persist && !empty($lead->getChanges(true))) {
// Only persist if instructed to do so as it could be that calling code needs to manipulate the lead prior to executing event listeners
$lead->setManipulator(new LeadManipulator(
'plugin',
$this->getName()
));
$leadModel->saveEntity($lead, false);
}

Expand Down
5 changes: 5 additions & 0 deletions plugins/MauticCrmBundle/Integration/HubspotIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace MauticPlugin\MauticCrmBundle\Integration;

use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\LeadBundle\DataObject\LeadManipulator;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\StagesChangeLog;
use Mautic\PluginBundle\Entity\IntegrationEntityRepository;
Expand Down Expand Up @@ -483,6 +484,10 @@ public function getMauticLead($data, $persist = true, $socialCache = null, $iden
if ($persist && !empty($lead->getChanges(true))) {
// Only persist if instructed to do so as it could be that calling code needs to manipulate the lead prior to executing event listeners
try {
$lead->setManipulator(new LeadManipulator(
'plugin',
$this->getName()
));
$this->leadModel->saveEntity($lead, false);
if (isset($company)) {
$this->leadModel->addToCompany($lead, $company);
Expand Down