/**
  * {@inheritDoc}
  */
 public function setPoints($points)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setPoints', array($points));
     return parent::setPoints($points);
 }
Пример #2
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;
 }
Пример #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->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;
 }