Skip to content

Commit

Permalink
Merge branch 'mqueme-point-actions' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhartless committed Aug 24, 2016
2 parents 65308a0 + c35e69f commit bdeb1ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
11 changes: 7 additions & 4 deletions app/bundles/PageBundle/Entity/HitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,26 +238,29 @@ public function countVisitors($seconds = 60, $notLeft = false)
public function getLatestHit($options)
{
$sq = $this->_em->getConnection()->createQueryBuilder();
$sq->select('MAX(h.date_hit) latest_hit')
$sq->select('h.date_hit latest_hit')
->from(MAUTIC_TABLE_PREFIX.'page_hits', 'h');

if (isset($options['leadId'])) {
$sq->andWhere(
$sq->expr()->eq('h.lead_id', $options['leadId'])
);
}

if (isset($options['urls']) && $options['urls']) {
$inUrls = (!is_array($options['urls'])) ? array($options['urls']) : $options['urls'];
foreach ($inUrls as $k => $u) {
$sq->andWhere($sq->expr()->like('h.url', ':url_'.$k))
->setParameter('url_'.$k, $u);
}
}

if (isset($options['second_to_last'])) {
$sq->andWhere($sq->expr()->neq('h.id', $options['second_to_last']));
} else {
$sq->orderBy('h.date_hit','DESC limit 1');
}
$result = $sq->execute()->fetch();

return new \DateTime($result['latest_hit']);
return new \DateTime($result['latest_hit'], new \DateTimeZone('UTC'));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions app/bundles/PageBundle/Form/Type/PointActionUrlHitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label_attr' => array('class' => 'control-label'),
'attr' => array(
'class' => 'form-control',
'tooltip' => 'mautic.page.point.action.form.returns.within.descr'
'tooltip' => 'mautic.page.point.action.form.returns.within.descr',
'onBlur' => 'Mautic.EnablesOption(this.id)'
),
'auto_initialize' => false
))
Expand All @@ -114,7 +115,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label_attr' => array('class' => 'control-label'),
'attr' => array(
'class' => 'form-control',
'tooltip' => 'mautic.page.point.action.form.returns.after.descr'
'tooltip' => 'mautic.page.point.action.form.returns.after.descr',
'onBlur' => 'Mautic.EnablesOption(this.id)'
),
'auto_initialize' => false
))
Expand Down
30 changes: 12 additions & 18 deletions app/bundles/PageBundle/Helper/PointActionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,49 +79,43 @@ public static function validateUrlHit($factory, $eventDetails, $action)
$changePoints['first_time'] = true;
}
}
$now = new \DateTime();
$latestHit = $hitRepository->getLatestHit(['leadId' => $lead->getId(), $urlWithSqlWC, 'second_to_last' => $eventDetails->getId()]);

if ($action['properties']['accumulative_time']) {
if (!isset($hitStats)) {
$hitStats = $hitRepository->getDwellTimesForUrl($urlWithSqlWC, ['leadId' => $lead->getId()]);
}
if (isset($hitStats['sum']) && $hitStats['sum'] >= $action['properties']['accumulative_time']) {
$changePoints['accumulative_time'] = true;

if (isset($hitStats['sum'])) {
if($now->getTimestamp() - $latestHit->getTimestamp() == $hitStats['sum']) {
$changePoints['accumulative_time'] = true;
} else {
$changePoints['accumulative_time'] = false;
}
} else {
$changePoints['accumulative_time'] = false;
}
}

if ($action['properties']['page_hits']) {
if (!isset($hitStats)) {
$hitStats = $hitRepository->getDwellTimesForUrl($urlWithSqlWC, ['leadId' => $lead->getId()]);
}
if (isset($hitStats['count']) && $hitStats['count'] >= $action['properties']['page_hits']) {
if (isset($hitStats['count']) && $hitStats['count'] === $action['properties']['page_hits']) {
$changePoints['page_hits'] = true;
} else {
$changePoints['page_hits'] = false;
}
}

if ($action['properties']['returns_within']) {
$latestHit = $hitRepository->getLatestHit(['leadId' => $lead->getId(), $urlWithSqlWC]);
$latestPlus = clone $latestHit;
$latestPlus->add(new \DateInterval('PT' . $action['properties']['returns_within'] . 'S'));
$now = new \dateTime();
if ($latestPlus >= $now) {
if ($now->getTimestamp() - $latestHit->getTimestamp() <= $action['properties']['returns_within']) {
$changePoints['returns_within'] = true;
} else {
$changePoints['returns_within'] = false;
}
}

if ($action['properties']['returns_after']) {
if (!isset($latestHit)) {
$latestHit = $hitRepository->getLatestHit(['leadId' => $lead->getId(), $urlWithSqlWC]);
}
$latestPlus = clone $latestHit;
$latestPlus->add(new \DateInterval('PT' . $action['properties']['returns_after'] . 'S'));
$now = new \dateTime();
if ($latestPlus >= $now) {
if ($now->getTimestamp() - $latestHit->getTimestamp() >= $action['properties']['returns_after']) {
$changePoints['returns_after'] = true;
} else {
$changePoints['returns_after'] = false;
Expand Down
9 changes: 9 additions & 0 deletions app/bundles/PointBundle/Assets/js/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ Mautic.getPointActionPropertiesForm = function(actionType) {
Mautic.removeLabelLoadingIndicator();
}
});
};
Mautic.EnablesOption = function (urlActionProperty) {
if (urlActionProperty === 'point_properties_returns_within' && mQuery('#point_properties_returns_within').val() > 0) {
mQuery('#point_properties_returns_after').val(0);
} else {
if (urlActionProperty === 'point_properties_returns_after' && mQuery('#point_properties_returns_after').val() > 0) {
mQuery('#point_properties_returns_within').val(0);
}
}
};
6 changes: 3 additions & 3 deletions app/bundles/PointBundle/Model/PointModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PointModel extends CommonFormModel
* @var Session
*/
protected $session;

/**
* @var IpLookupHelper
*/
Expand All @@ -56,7 +56,7 @@ class PointModel extends CommonFormModel

/**
* PointModel constructor.
*
*
* @param Session $session
* @param IpLookupHelper $ipLookupHelper
* @param LeadModel $leadModel
Expand Down Expand Up @@ -287,7 +287,7 @@ public function triggerAction($type, $eventDetails = null, $typeId = null, Lead
$delta,
$ipAddress
);

$event = new PointActionEvent($action, $lead);
$this->dispatcher->dispatch(PointEvents::POINT_ON_ACTION, $event);

Expand Down

0 comments on commit bdeb1ba

Please sign in to comment.