/** * @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; }
/** * Create/update lead from form submit * * @param $form * @param array $leadFieldMatches * * @return Lead */ protected function createLeadFromSubmit($form, array $leadFieldMatches) { /** @var \Mautic\LeadBundle\Model\LeadModel $model */ $model = $this->factory->getModel('lead'); $em = $this->factory->getEntityManager(); //set the mapped data $leadFields = $this->factory->getModel('lead.field')->getRepository()->getAliases(null, true, false); $inKioskMode = $form->isInKioskMode(); if (!$inKioskMode) { // Default to currently tracked lead $lead = $model->getCurrentLead(); $leadId = $lead->getId(); $currentFields = $model->flattenFields($lead->getFields()); } else { // Default to a new lead in kiosk mode $lead = new Lead(); $lead->setNewlyCreated(true); $currentFields = $leadFieldMatches; $leadId = null; } $uniqueLeadFields = $this->factory->getModel('lead.field')->getUniqueIdentiferFields(); // Closure to get data and unique fields $getData = function ($currentFields, $uniqueOnly = false) use($leadFields, $uniqueLeadFields) { $uniqueFieldsWithData = $data = array(); foreach ($leadFields as $alias) { $data[$alias] = ''; if (isset($currentFields[$alias])) { $value = $currentFields[$alias]; $data[$alias] = $value; // make sure the value is actually there and the field is one of our uniques if (!empty($value) && array_key_exists($alias, $uniqueLeadFields)) { $uniqueFieldsWithData[$alias] = $value; } } } return $uniqueOnly ? $uniqueFieldsWithData : array($data, $uniqueFieldsWithData); }; // Closure to help search for a conflict $checkForIdentifierConflict = function ($fieldSet1, $fieldSet2) { // Find conflicts $diff = array_diff($fieldSet1, $fieldSet2); // Remove empty values $diff = array_filter($diff); return count($diff); }; // Get data for the form submission list($data, $uniqueFieldsWithData) = $getData($leadFieldMatches); // Check for duplicate lead /** @var \Mautic\LeadBundle\Entity\LeadRepository $leads */ $leads = !empty($uniqueFieldsWithData) ? $em->getRepository('MauticLeadBundle:Lead')->getLeadsByUniqueFields($uniqueFieldsWithData, $leadId) : array(); $uniqueFieldsCurrent = $getData($currentFields, true); if (count($leads)) { /** @var \Mautic\LeadBundle\Entity\Lead $foundLead */ $foundLead = $leads[0]; // Check for a conflict with the currently tracked lead $foundLeadFields = $model->flattenFields($foundLead->getFields()); // Get unique identifier fields for the found lead then compare with the lead currently tracked $uniqueFieldsFound = $getData($foundLeadFields, true); $hasConflict = $checkForIdentifierConflict($uniqueFieldsFound, $uniqueFieldsCurrent); if ($inKioskMode || $hasConflict) { // Use the found lead without merging because there is some sort of conflict with unique identifiers or in kiosk mode and thus should not merge $lead = $foundLead; } else { // Merge the found lead with currently tracked lead $lead = $model->mergeLeads($lead, $foundLead); } // Update unique fields data for comparison with submitted data $currentFields = $model->flattenFields($lead->getFields()); $uniqueFieldsCurrent = $getData($currentFields, true); } if (!$inKioskMode) { // Check for conflicts with the submitted data and the currently tracked lead $hasConflict = $checkForIdentifierConflict($uniqueFieldsWithData, $uniqueFieldsCurrent); if ($hasConflict) { // There's a conflict so create a new lead $lead = new Lead(); $lead->setNewlyCreated(true); } } //check for existing IP address $ipAddress = $this->factory->getIpAddress(); //no lead was found by a mapped email field so create a new one if ($lead->isNewlyCreated()) { if (!$inKioskMode) { $lead->addIpAddress($ipAddress); } // last active time $lead->setLastActive(new \DateTime()); } elseif (!$inKioskMode) { $leadIpAddresses = $lead->getIpAddresses(); if (!$leadIpAddresses->contains($ipAddress)) { $lead->addIpAddress($ipAddress); } } //set the mapped fields $model->setFieldValues($lead, $data, false); if (!empty($event)) { $event->setIpAddress($ipAddress); $lead->addPointsChangeLog($event); } //create a new lead $model->saveEntity($lead, false); if (!$inKioskMode) { // Set the current lead which will generate tracking cookies $model->setCurrentLead($lead); } else { // Set system current lead which will still allow execution of events without generating tracking cookies $model->setSystemCurrentLead($lead); } return $lead; }
/** * @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 setLastActive($lastActive) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setLastActive', array($lastActive)); return parent::setLastActive($lastActive); }
/** * Create/update lead from form submit * * @param $form * @param array $leadFieldMatches * * @return Lead */ protected function createLeadFromSubmit($form, array $leadFieldMatches) { /** @var \Mautic\LeadBundle\Model\LeadModel $model */ $model = $this->factory->getModel('lead'); $em = $this->factory->getEntityManager(); //set the mapped data $leadFields = $this->factory->getModel('lead.field')->getRepository()->getAliases(null, true, false); $data = array(); $inKioskMode = $form->isInKioskMode(); if (!$inKioskMode) { $lead = $model->getCurrentLead(); $leadId = $lead->getId(); $currentFields = $lead->getFields(); } else { $lead = new Lead(); $lead->setNewlyCreated(true); $leadId = null; } $uniqueLeadFields = $this->factory->getModel('lead.field')->getUniqueIdentiferFields(); $uniqueFieldsWithData = array(); foreach ($leadFields as $alias) { $data[$alias] = ''; if (isset($leadFieldMatches[$alias])) { $value = $leadFieldMatches[$alias]; $data[$alias] = $value; // make sure the value is actually there and the field is one of our uniques if (!empty($value) && array_key_exists($alias, $uniqueLeadFields)) { $uniqueFieldsWithData[$alias] = $value; } } } //update the lead rather than creating a new one if there is for sure identifier match ($leadId is to exclude lead from getCurrentLead()) /** @var \Mautic\LeadBundle\Entity\LeadRepository $leads */ $leads = !empty($uniqueFieldsWithData) ? $em->getRepository('MauticLeadBundle:Lead')->getLeadsByUniqueFields($uniqueFieldsWithData, $leadId) : array(); if (count($leads)) { //merge with current lead if not in kiosk mode $lead = $inKioskMode ? $leads[0] : $model->mergeLeads($lead, $leads[0]); } elseif (!$inKioskMode) { // Flatten current fields $currentFields = $model->flattenFields($currentFields); // Create a new lead if unique identifiers differ from getCurrentLead() and submitted data foreach ($uniqueLeadFields as $alias => $value) { //create a new lead if details differ $currentValue = $currentFields[$alias]; if (!empty($currentValue) && strtolower($currentValue) != strtolower($value)) { //for sure a different lead so create a new one $lead = new Lead(); $lead->setNewlyCreated(true); break; } } } //check for existing IP address $ipAddress = $this->factory->getIpAddress(); //no lead was found by a mapped email field so create a new one if ($lead->isNewlyCreated()) { if (!$inKioskMode) { $lead->addIpAddress($ipAddress); } // last active time $lead->setLastActive(new \DateTime()); } elseif (!$inKioskMode) { $leadIpAddresses = $lead->getIpAddresses(); if (!$leadIpAddresses->contains($ipAddress)) { $lead->addIpAddress($ipAddress); } } //set the mapped fields $model->setFieldValues($lead, $data, false); if (!empty($event)) { $event->setIpAddress($ipAddress); $lead->addPointsChangeLog($event); } //create a new lead $model->saveEntity($lead, false); if (!$inKioskMode) { // Set the current lead which will generate tracking cookies $model->setCurrentLead($lead); } else { // Set system current lead which will still allow execution of events without generating tracking cookies $model->setSystemCurrentLead($lead); } return $lead; }