Exemple #1
0
 /**
  * Triggers a specific point change.
  *
  * @param $type
  * @param mixed $eventDetails passthrough from function triggering action to the callback function
  * @param mixed $typeId       Something unique to the triggering event to prevent  unnecessary duplicate calls
  * @param Lead  $lead
  *25
  */
 public function triggerAction($type, $eventDetails = null, $typeId = null, Lead $lead = null)
 {
     //only trigger actions for anonymous users
     if (!$this->security->isAnonymous()) {
         return;
     }
     if ($typeId !== null && MAUTIC_ENV === 'prod') {
         //let's prevent some unnecessary DB calls
         $triggeredEvents = $this->session->get('mautic.triggered.point.actions', []);
         if (in_array($typeId, $triggeredEvents)) {
             return;
         }
         $triggeredEvents[] = $typeId;
         $this->session->set('mautic.triggered.point.actions', $triggeredEvents);
     }
     //find all the actions for published points
     /** @var \Mautic\PointBundle\Entity\PointRepository $repo */
     $repo = $this->getRepository();
     $availablePoints = $repo->getPublishedByType($type);
     $ipAddress = $this->ipLookupHelper->getIpAddress();
     if (null === $lead) {
         $lead = $this->leadModel->getCurrentLead();
         if (null === $lead || !$lead->getId()) {
             return;
         }
     }
     //get available actions
     $availableActions = $this->getPointActions();
     //get a list of actions that has already been performed on this lead
     $completedActions = $repo->getCompletedLeadActions($type, $lead->getId());
     $persist = [];
     foreach ($availablePoints as $action) {
         //if it's already been done, then skip it
         if (isset($completedActions[$action->getId()])) {
             continue;
         }
         //make sure the action still exists
         if (!isset($availableActions['actions'][$action->getType()])) {
             continue;
         }
         $settings = $availableActions['actions'][$action->getType()];
         $args = ['action' => ['id' => $action->getId(), 'type' => $action->getType(), 'name' => $action->getName(), 'properties' => $action->getProperties(), 'points' => $action->getDelta()], 'lead' => $lead, 'factory' => $this->factory, 'eventDetails' => $eventDetails];
         $callback = isset($settings['callback']) ? $settings['callback'] : ['\\Mautic\\PointBundle\\Helper\\EventHelper', 'engagePointAction'];
         if (is_callable($callback)) {
             if (is_array($callback)) {
                 $reflection = new \ReflectionMethod($callback[0], $callback[1]);
             } elseif (strpos($callback, '::') !== false) {
                 $parts = explode('::', $callback);
                 $reflection = new \ReflectionMethod($parts[0], $parts[1]);
             } else {
                 $reflection = new \ReflectionMethod(null, $callback);
             }
             $pass = [];
             foreach ($reflection->getParameters() as $param) {
                 if (isset($args[$param->getName()])) {
                     $pass[] = $args[$param->getName()];
                 } else {
                     $pass[] = null;
                 }
             }
             $pointsChange = $reflection->invokeArgs($this, $pass);
             if ($pointsChange) {
                 $delta = $action->getDelta();
                 $lead->adjustPoints($delta);
                 $parsed = explode('.', $action->getType());
                 $lead->addPointsChangeLogEntry($parsed[0], $action->getId() . ': ' . $action->getName(), $parsed[1], $delta, $ipAddress);
                 $event = new PointActionEvent($action, $lead);
                 $this->dispatcher->dispatch(PointEvents::POINT_ON_ACTION, $event);
                 $log = new LeadPointLog();
                 $log->setIpAddress($ipAddress);
                 $log->setPoint($action);
                 $log->setLead($lead);
                 $log->setDateFired(new \DateTime());
                 $persist[] = $log;
             }
         }
     }
     if (!empty($persist)) {
         $this->leadModel->saveEntity($lead);
         $this->getRepository()->saveEntities($persist);
         // Detach logs to reserve memory
         $this->em->clear('Mautic\\PointBundle\\Entity\\LeadPointLog');
     }
 }
Exemple #2
0
 /**
  * Triggers a specific point change
  *
  * @param $type
  * @param mixed $eventDetails passthrough from function triggering action to the callback function
  * @param mixed $typeId Something unique to the triggering event to prevent  unnecessary duplicate calls
  *
  * @return void
  */
 public function triggerAction($type, $eventDetails = null, $typeId = null)
 {
     //only trigger actions for anonymous users
     if (!$this->security->isAnonymous()) {
         return;
     }
     if ($typeId !== null && $this->factory->getEnvironment() == 'prod') {
         //let's prevent some unnecessary DB calls
         $session = $this->factory->getSession();
         $triggeredEvents = $session->get('mautic.triggered.point.actions', array());
         if (in_array($typeId, $triggeredEvents)) {
             return;
         }
         $triggeredEvents[] = $typeId;
         $session->set('mautic.triggered.point.actions', $triggeredEvents);
     }
     //find all the actions for published points
     /** @var \Mautic\PointBundle\Entity\PointRepository $repo */
     $repo = $this->getRepository();
     $availablePoints = $repo->getPublishedByType($type);
     $leadModel = $this->factory->getModel('lead');
     $lead = $leadModel->getCurrentLead();
     $ipAddress = $this->factory->getIpAddress();
     //get available actions
     $availableActions = $this->getPointActions();
     //get a list of actions that has already been performed on this lead
     $completedActions = $repo->getCompletedLeadActions($type, $lead->getId());
     $persist = array();
     $persistLead = false;
     foreach ($availablePoints as $action) {
         //if it's already been done, then skip it
         if (isset($completedActions[$action->getId()])) {
             continue;
         }
         //make sure the action still exists
         if (!isset($availableActions['actions'][$action->getType()])) {
             continue;
         }
         $settings = $availableActions['actions'][$action->getType()];
         $args = array('action' => array('id' => $action->getId(), 'type' => $action->getType(), 'name' => $action->getName(), 'properties' => $action->getProperties(), 'points' => $action->getDelta()), 'lead' => $lead, 'factory' => $this->factory, 'eventDetails' => $eventDetails);
         $callback = isset($settings['callback']) ? $settings['callback'] : array('\\Mautic\\PointBundle\\Helper\\EventHelper', 'engagePointAction');
         if (is_callable($callback)) {
             if (is_array($callback)) {
                 $reflection = new \ReflectionMethod($callback[0], $callback[1]);
             } elseif (strpos($callback, '::') !== false) {
                 $parts = explode('::', $callback);
                 $reflection = new \ReflectionMethod($parts[0], $parts[1]);
             } else {
                 $reflection = new \ReflectionMethod(null, $callback);
             }
             $pass = array();
             foreach ($reflection->getParameters() as $param) {
                 if (isset($args[$param->getName()])) {
                     $pass[] = $args[$param->getName()];
                 } else {
                     $pass[] = null;
                 }
             }
             $pointsChange = $reflection->invokeArgs($this, $pass);
             if ($pointsChange) {
                 $delta = $action->getDelta();
                 $lead->addToPoints($delta);
                 $parsed = explode('.', $action->getType());
                 $lead->addPointsChangeLogEntry($parsed[0], $action->getId() . ": " . $action->getName(), $parsed[1], $delta, $ipAddress);
                 $log = new LeadPointLog();
                 $log->setIpAddress($ipAddress);
                 $log->setPoint($action);
                 $log->setLead($lead);
                 $log->setDateFired(new \DateTime());
                 $action->addLog($log);
                 $persist[] = $action;
                 $persistLead = true;
             }
         }
     }
     //save the lead
     if ($persistLead) {
         $leadModel->saveEntity($lead);
     }
     //persist the action xref
     if (!empty($persist)) {
         $this->getRepository()->saveEntities($persist);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function setPoint($point)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setPoint', array($point));
     return parent::setPoint($point);
 }