Exemplo n.º 1
0
 /**
  * @param               $lead
  * @param MauticFactory $factory
  * @param               $action
  * @param               $config
  * @param               $form
  */
 public static function changePoints($lead, MauticFactory $factory, $action, $config, $form)
 {
     $model = $factory->getModel('lead');
     //create a new points change event
     $event = new PointsChangeLog();
     $event->setType('form');
     $event->setEventName($form->getId() . ":" . $form->getName());
     $event->setActionName($action->getName());
     $event->setIpAddress($factory->getIpAddress());
     $event->setDateAdded(new \DateTime());
     $event->setDelta($config['points']);
     $event->setLead($lead);
     $lead->addPointsChangeLog($event);
     $lead->addToPoints($config['points']);
     $model->saveEntity($lead, false);
 }
Exemplo n.º 2
0
 /**
  * @param $event
  * @param $lead
  *
  * @return bool
  */
 public static function changePoints($event, $lead, MauticFactory $factory)
 {
     $points = $event['properties']['points'];
     $somethingHappened = false;
     if ($lead != null && !empty($points)) {
         $lead->addToPoints($points);
         //add a lead point change log
         $log = new PointsChangeLog();
         $log->setDelta($points);
         $log->setLead($lead);
         $log->setType('campaign');
         $log->setEventName("{$event['campaign']['id']}: {$event['campaign']['name']}");
         $log->setActionName("{$event['id']}: {$event['name']}");
         $log->setIpAddress($factory->getIpAddress());
         $log->setDateAdded(new \DateTime());
         $lead->addPointsChangeLog($log);
         $somethingHappened = true;
     }
     return $somethingHappened;
 }
Exemplo n.º 3
0
 /**
  * @param      $fields
  * @param      $data
  * @param null $owner
  * @param null $list
  * @param null $tags
  * @param bool $persist Persist to the database; otherwise return entity
  *
  * @return bool
  * @throws \Doctrine\ORM\ORMException
  * @throws \Swift_RfcComplianceException
  */
 public function importLead($fields, $data, $owner = null, $list = null, $tags = null, $persist = true)
 {
     // Let's check for an existing lead by email
     $hasEmail = !empty($fields['email']) && !empty($data[$fields['email']]);
     if ($hasEmail) {
         // Validate the email
         MailHelper::validateEmail($data[$fields['email']]);
         $leadFound = $this->getRepository()->getLeadByEmail($data[$fields['email']]);
         $lead = $leadFound ? $this->em->getReference('MauticLeadBundle:Lead', $leadFound['id']) : new Lead();
         $merged = $leadFound;
     } else {
         $lead = new Lead();
         $merged = false;
     }
     if (!empty($fields['dateAdded']) && !empty($data[$fields['dateAdded']])) {
         $dateAdded = new DateTimeHelper($data[$fields['dateAdded']]);
         $lead->setDateAdded($dateAdded->getUtcDateTime());
     }
     unset($fields['dateAdded']);
     if (!empty($fields['dateModified']) && !empty($data[$fields['dateModified']])) {
         $dateModified = new DateTimeHelper($data[$fields['dateModified']]);
         $lead->setDateModified($dateModified->getUtcDateTime());
     }
     unset($fields['dateModified']);
     if (!empty($fields['lastActive']) && !empty($data[$fields['lastActive']])) {
         $lastActive = new DateTimeHelper($data[$fields['lastActive']]);
         $lead->setLastActive($lastActive->getUtcDateTime());
     }
     unset($fields['lastActive']);
     if (!empty($fields['dateIdentified']) && !empty($data[$fields['dateIdentified']])) {
         $dateIdentified = new DateTimeHelper($data[$fields['dateIdentified']]);
         $lead->setDateIdentified($dateIdentified->getUtcDateTime());
     }
     unset($fields['dateIdentified']);
     if (!empty($fields['createdByUser']) && !empty($data[$fields['createdByUser']])) {
         $userRepo = $this->em->getRepository('MauticUserBundle:User');
         $createdByUser = $userRepo->findByIdentifier($data[$fields['createdByUser']]);
         if ($createdByUser !== null) {
             $lead->setCreatedBy($createdByUser);
         }
     }
     unset($fields['createdByUser']);
     if (!empty($fields['modifiedByUser']) && !empty($data[$fields['modifiedByUser']])) {
         $userRepo = $this->em->getRepository('MauticUserBundle:User');
         $modifiedByUser = $userRepo->findByIdentifier($data[$fields['modifiedByUser']]);
         if ($modifiedByUser !== null) {
             $lead->setModifiedBy($modifiedByUser);
         }
     }
     unset($fields['modifiedByUser']);
     if (!empty($fields['ip']) && !empty($data[$fields['ip']])) {
         $addresses = explode(',', $data[$fields['ip']]);
         foreach ($addresses as $address) {
             $ipAddress = new IpAddress();
             $ipAddress->setIpAddress(trim($address));
             $lead->addIpAddress($ipAddress);
         }
     }
     unset($fields['ip']);
     if (!empty($fields['points']) && !empty($data[$fields['points']]) && $lead->getId() === null) {
         // Add points only for new leads
         $lead->setPoints($data[$fields['points']]);
         //add a lead point change log
         $log = new PointsChangeLog();
         $log->setDelta($data[$fields['points']]);
         $log->setLead($lead);
         $log->setType('lead');
         $log->setEventName($this->factory->getTranslator()->trans('mautic.lead.import.event.name'));
         $log->setActionName($this->factory->getTranslator()->trans('mautic.lead.import.action.name', array('%name%' => $this->factory->getUser()->getUsername())));
         $log->setIpAddress($this->factory->getIpAddress());
         $log->setDateAdded(new \DateTime());
         $lead->addPointsChangeLog($log);
     }
     unset($fields['points']);
     // Set unsubscribe status
     if (!empty($fields['doNotEmail']) && !empty($data[$fields['doNotEmail']]) && $hasEmail) {
         $doNotEmail = filter_var($data[$fields['doNotEmail']], FILTER_VALIDATE_BOOLEAN);
         if ($doNotEmail) {
             $reason = $this->factory->getTranslator()->trans('mautic.lead.import.by.user', array("%user%" => $this->factory->getUser()->getUsername()));
             // The email must be set for successful unsubscribtion
             $lead->addUpdatedField('email', $data[$fields['email']]);
             $this->unsubscribeLead($lead, $reason, false);
         }
     }
     unset($fields['doNotEmail']);
     if ($owner !== null) {
         $lead->setOwner($this->em->getReference('MauticUserBundle:User', $owner));
     }
     if ($tags !== null) {
         $this->modifyTags($lead, $tags, null, false);
     }
     // Set profile data
     foreach ($fields as $leadField => $importField) {
         // Prevent overwriting existing data with empty data
         if (array_key_exists($importField, $data) && !is_null($data[$importField]) && $data[$importField] != '') {
             $lead->addUpdatedField($leadField, $data[$importField]);
         }
     }
     $lead->imported = true;
     if ($persist) {
         $this->saveEntity($lead);
         if ($list !== null) {
             $this->addToLists($lead, array($list));
         }
     }
     return $merged;
 }
Exemplo n.º 4
0
 /**
  * @param CampaignExecutionEvent $event
  */
 public function onCampaignTriggerActionChangePoints(CampaignExecutionEvent $event)
 {
     if (!$event->checkContext('lead.changepoints')) {
         return;
     }
     $lead = $event->getLead();
     $points = $event->getConfig()['points'];
     $somethingHappened = false;
     if ($lead !== null && !empty($points)) {
         $lead->adjustPoints($points);
         //add a lead point change log
         $log = new PointsChangeLog();
         $log->setDelta($points);
         $log->setLead($lead);
         $log->setType('campaign');
         $log->setEventName("{$event->getEvent()['campaign']['id']}: {$event->getEvent()['campaign']['name']}");
         $log->setActionName("{$event->getEvent()['id']}: {$event->getEvent()['name']}");
         $log->setIpAddress($this->ipLookupHelper->getIpAddress());
         $log->setDateAdded(new \DateTime());
         $lead->addPointsChangeLog($log);
         $this->leadModel->saveEntity($lead);
         $somethingHappened = true;
     }
     return $event->setResult($somethingHappened);
 }
Exemplo n.º 5
0
 /**
  * Creates a points change entry.
  *
  * @param           $type
  * @param           $name
  * @param           $action
  * @param           $pointsDelta
  * @param IpAddress $ip
  */
 public function addPointsChangeLogEntry($type, $name, $action, $pointsDelta, IpAddress $ip)
 {
     if ($pointsDelta === 0) {
         // No need to record a null delta
         return;
     }
     // Create a new points change event
     $event = new PointsChangeLog();
     $event->setType($type);
     $event->setEventName($name);
     $event->setActionName($action);
     $event->setDateAdded(new \DateTime());
     $event->setDelta($pointsDelta);
     $event->setIpAddress($ip);
     $event->setLead($this);
     $this->addPointsChangeLog($event);
 }
Exemplo n.º 6
0
 /**
  * @param      $fields
  * @param      $data
  * @param null $owner
  * @param null $list
  * @param null $tags
  * @param bool $persist Persist to the database; otherwise return entity
  *
  * @return bool
  * @throws \Doctrine\ORM\ORMException
  * @throws \Swift_RfcComplianceException
  */
 public function importLead($fields, $data, $owner = null, $list = null, $tags = null, $persist = true)
 {
     // Let's check for an existing lead by email
     $hasEmail = !empty($fields['email']) && !empty($data[$fields['email']]);
     if ($hasEmail) {
         // Validate the email
         MailHelper::validateEmail($data[$fields['email']]);
         $leadFound = $this->getRepository()->getLeadByEmail($data[$fields['email']]);
         $lead = $leadFound ? $this->em->getReference('MauticLeadBundle:Lead', $leadFound['id']) : new Lead();
         $merged = $leadFound;
     } else {
         $lead = new Lead();
         $merged = false;
     }
     if (!empty($fields['dateAdded']) && !empty($data[$fields['dateAdded']])) {
         $dateAdded = new DateTimeHelper($data[$fields['dateAdded']]);
         $lead->setDateAdded($dateAdded->getUtcDateTime());
     }
     unset($fields['dateAdded']);
     if (!empty($fields['dateModified']) && !empty($data[$fields['dateModified']])) {
         $dateModified = new DateTimeHelper($data[$fields['dateModified']]);
         $lead->setDateModified($dateModified->getUtcDateTime());
     }
     unset($fields['dateModified']);
     if (!empty($fields['lastActive']) && !empty($data[$fields['lastActive']])) {
         $lastActive = new DateTimeHelper($data[$fields['lastActive']]);
         $lead->setLastActive($lastActive->getUtcDateTime());
     }
     unset($fields['lastActive']);
     if (!empty($fields['dateIdentified']) && !empty($data[$fields['dateIdentified']])) {
         $dateIdentified = new DateTimeHelper($data[$fields['dateIdentified']]);
         $lead->setDateIdentified($dateIdentified->getUtcDateTime());
     }
     unset($fields['dateIdentified']);
     if (!empty($fields['createdByUser']) && !empty($data[$fields['createdByUser']])) {
         $userRepo = $this->em->getRepository('MauticUserBundle:User');
         $createdByUser = $userRepo->findByIdentifier($data[$fields['createdByUser']]);
         if ($createdByUser !== null) {
             $lead->setCreatedBy($createdByUser);
         }
     }
     unset($fields['createdByUser']);
     if (!empty($fields['modifiedByUser']) && !empty($data[$fields['modifiedByUser']])) {
         $userRepo = $this->em->getRepository('MauticUserBundle:User');
         $modifiedByUser = $userRepo->findByIdentifier($data[$fields['modifiedByUser']]);
         if ($modifiedByUser !== null) {
             $lead->setModifiedBy($modifiedByUser);
         }
     }
     unset($fields['modifiedByUser']);
     if (!empty($fields['ip']) && !empty($data[$fields['ip']])) {
         $addresses = explode(',', $data[$fields['ip']]);
         foreach ($addresses as $address) {
             $ipAddress = new IpAddress();
             $ipAddress->setIpAddress(trim($address));
             $lead->addIpAddress($ipAddress);
         }
     }
     unset($fields['ip']);
     if (!empty($fields['points']) && !empty($data[$fields['points']]) && $lead->getId() === null) {
         // Add points only for new leads
         $lead->setPoints($data[$fields['points']]);
         //add a lead point change log
         $log = new PointsChangeLog();
         $log->setDelta($data[$fields['points']]);
         $log->setLead($lead);
         $log->setType('lead');
         $log->setEventName($this->translator->trans('mautic.lead.import.event.name'));
         $log->setActionName($this->translator->trans('mautic.lead.import.action.name', array('%name%' => $this->user->getUsername())));
         $log->setIpAddress($this->ipLookupHelper->getIpAddress());
         $log->setDateAdded(new \DateTime());
         $lead->addPointsChangeLog($log);
     }
     if (!empty($fields['stage']) && !empty($data[$fields['stage']])) {
         static $stages = [];
         $stageName = $data[$fields['stage']];
         if (!array_key_exists($stageName, $stages)) {
             // Set stage for contact
             $stage = $this->em->getRepository('MauticStageBundle:Stage')->getStageByName($stageName);
             if (empty($stage)) {
                 $stage = new Stage();
                 $stage->setName($stageName);
                 $stages[$stageName] = $stage;
             }
         } else {
             $stage = $stages[$stageName];
         }
         $lead->setStage($stage);
         //add a contact stage change log
         $log = new StagesChangeLog();
         $log->setEventName($stage->getId() . ":" . $stage->getName());
         $log->setLead($lead);
         $log->setActionName($this->translator->trans('mautic.lead.import.action.name', ['%name%' => $this->user->getUsername()]));
         $log->setDateAdded(new \DateTime());
         $lead->stageChangeLog($log);
     }
     unset($fields['stage']);
     // Set unsubscribe status
     if (!empty($fields['doNotEmail']) && !empty($data[$fields['doNotEmail']]) && $hasEmail) {
         $doNotEmail = filter_var($data[$fields['doNotEmail']], FILTER_VALIDATE_BOOLEAN);
         if ($doNotEmail) {
             $reason = $this->translator->trans('mautic.lead.import.by.user', array("%user%" => $this->user->getUsername()));
             // The email must be set for successful unsubscribtion
             $lead->addUpdatedField('email', $data[$fields['email']]);
             $this->addDncForLead($lead, 'email', $reason, DoNotContact::MANUAL);
         }
     }
     unset($fields['doNotEmail']);
     if ($owner !== null) {
         $lead->setOwner($this->em->getReference('MauticUserBundle:User', $owner));
     }
     if ($tags !== null) {
         $this->modifyTags($lead, $tags, null, false);
     }
     // Set profile data using the form so that values are validated
     $fieldData = [];
     foreach ($fields as $leadField => $importField) {
         // Prevent overwriting existing data with empty data
         if (array_key_exists($importField, $data) && !is_null($data[$importField]) && $data[$importField] != '') {
             $fieldData[$leadField] = $data[$importField];
         }
     }
     static $leadFields;
     if (null === $leadFields) {
         $leadFields = $this->leadFieldModel->getEntities(array('force' => array(array('column' => 'f.isPublished', 'expr' => 'eq', 'value' => true)), 'hydration_mode' => 'HYDRATE_ARRAY'));
     }
     $form = $this->createForm($lead, $this->formFactory, null, ['fields' => $leadFields, 'csrf_protection' => false]);
     // Unset stage and owner from the form because it's already been handled
     unset($form['stage'], $form['owner']);
     $form->submit($fieldData);
     if (!$form->isValid()) {
         $fieldErrors = [];
         foreach ($form as $formField) {
             $errors = $formField->getErrors(true);
             if (count($errors)) {
                 $errorString = $formField->getConfig()->getOption('label') . ": ";
                 foreach ($errors as $error) {
                     $errorString .= " {$error->getMessage()}";
                 }
                 $fieldErrors[] = $errorString;
             }
         }
         $fieldErrors = implode("\n", $fieldErrors);
         throw new \Exception($fieldErrors);
     } else {
         // All clear
         foreach ($fieldData as $field => $value) {
             $lead->addUpdatedField($field, $value);
         }
     }
     $lead->imported = true;
     if ($persist) {
         $this->saveEntity($lead);
         if ($list !== null) {
             $this->addToLists($lead, array($list));
         }
     }
     return $merged;
 }
 /**
  * {@inheritDoc}
  */
 public function setDelta($delta)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setDelta', array($delta));
     return parent::setDelta($delta);
 }