protected function hydrate(Lead $lead, $data = []) { $em = $this->getEntityManager(); $leadAttributeRepository = $em->getRepository("Lead\\Entity\\LeadAttribute"); $accountRepository = $em->getRepository("Account\\Entity\\Account"); if (isset($data['attributes'], $data['lead'])) { $attribute_count = $leadAttributeRepository->getCount(); foreach ($data as $section => $values) { switch ($section) { case 'attributes': foreach ($values as $attributeName => $attributeValue) { $leadAttribute = $leadAttributeRepository->findOneBy(['attributeName' => $attributeName, 'active' => 1]); if (!$leadAttribute) { $leadAttribute = $leadAttributeRepository->findOneBy(['attributeDesc' => $attributeName, 'attributeName' => 'Question', 'active' => 1]); } if (!$leadAttribute) { $leadAttribute = new LeadAttribute(); $leadAttribute->setAttributeName('Question'); $leadAttribute->setAttributeDesc($attributeName); $leadAttribute->setAttributeOrder($attribute_count++); } if ($leadAttribute) { $leadAttributeValue = new LeadAttributeValue(); $leadAttributeValue->setValue($attributeValue); $leadAttributeValue->setAttribute($leadAttribute); $lead->addAttribute($leadAttributeValue); } } break; case 'lead': foreach ($values as $property => $value) { switch ($property) { case 'timecreated': case 'referrer': case 'ipaddress': if ($property == 'timecreated') { // $isvalid = // $this->validateDate($value); $value = new \DateTime("now"); } if (method_exists($lead, 'set' . ucfirst($property))) { $lead->{'set' . ucfirst($property)}($value); } break; case 'company': case 'companyid': if (!$lead->getAccount()) { switch ($property) { case 'company': $criteria = ['name' => $value]; break; case 'companyid': $criteria = ['id' => $value]; break; } $account = $accountRepository->findOneBy($criteria); if ($account) { $lead->setAccount($account); } } break; } } break; } } return $lead; } return false; }
protected function hydrate(Lead $lead, $data = []) { $em = $this->getEntityManager(); $leadAttributeRepository = $em->getRepository("Lead\\Entity\\LeadAttribute"); foreach ($data as $attributeId => $csvRowItem) { if (is_array($csvRowItem)) { $csvHeading = key($csvRowItem); $csvValue = current($csvRowItem); if (is_numeric($attributeId)) { $leadAttribute = $leadAttributeRepository->findOneBy(['id' => $attributeId, 'active' => 1]); if ($leadAttribute) { $leadAttributeValue = new LeadAttributeValue(); $leadAttributeValue->setValue($csvValue); $leadAttributeValue->setAttribute($leadAttribute); $lead->addAttribute($leadAttributeValue); } } elseif ($attributeId == "Question") { if (is_array($csvValue)) { foreach ($csvRowItem as $csvHeading => $arrayValue) { $csvValue = current($arrayValue); $leadAttribute = $leadAttributeRepository->findOneBy(['attributeDesc' => $csvHeading, 'active' => 1]); if (!$leadAttribute) { $leadAttribute = new LeadAttribute(); $leadAttribute->setAttributeName($attributeId); $leadAttribute->setAttributeDesc($csvHeading); } $leadAttributeValue = new LeadAttributeValue(); $leadAttributeValue->setValue($csvValue); $leadAttributeValue->setAttribute($leadAttribute); $lead->addAttribute($leadAttributeValue); } } else { $leadAttribute = $leadAttributeRepository->findOneBy(['attributeDesc' => $csvHeading, 'active' => 1]); if (!$leadAttribute) { $leadAttribute = new LeadAttribute(); $leadAttribute->setAttributeName($attributeId); $leadAttribute->setAttributeDesc($csvHeading); } $leadAttributeValue = new LeadAttributeValue(); $leadAttributeValue->setValue($csvValue); $leadAttributeValue->setAttribute($leadAttribute); $lead->addAttribute($leadAttributeValue); } } elseif (in_array($attributeId, ['lastsubmitted', 'timecreated', 'referrer', 'ipaddress'])) { if (in_array($attributeId, ['lastsubmitted', 'timecreated'])) { $isvalid = $this->validateDate($csvValue); $csvValue = $isvalid ? new \DateTime($csvValue) : new \DateTime("now"); } $lead->{'set' . ucfirst($attributeId)}($csvValue); } } } return $lead; }
public function updateLeadGeo(Lead $lead, $location = []) { $result = false; $update = false; if ($location && isset($location['_source'])) { $locality = $location['_source']; $em = $this->getEntityManager(); $attributeRepo = $em->getRepository('Lead\\Entity\\LeadAttribute'); // Find missing fields, if any $existing = []; $existing['locality'] = $lead->getLocality(); foreach (['city', 'state', 'zip'] as $attribute) { $value = $lead->findAttribute($attribute, true); $existing[$attribute] = $value ? $value->getValue() : null; } foreach ($existing as $field => $value) { if (empty($value)) { switch ($field) { case 'locality': $lead->setLocality(implode(",", $locality['latlon'])); $update = true; break; default: $updated = false; switch ($field) { case 'state': $updated = $locality[$field]['abbrev']; break; default: $updated = $locality[$field]; break; } if ($updated) { $attribute_id = $attributeRepo->getIDFromDesc(ucwords($field), $field == 'zip'); if ($attribute_id) { $attribute = $attributeRepo->find($attribute_id); if ($attribute) { $leadAttributeValue = new LeadAttributeValue(); $leadAttributeValue->setValue($updated); $leadAttributeValue->setAttribute($attribute); $lead->addAttribute($leadAttributeValue); $update = true; } } } break; } } } if ($update) { try { $em->merge($lead); $em->flush(); $result = true; } catch (\Exception $e) { $result = false; } } } return $result; }