/** * @ORM\PostLoad * * @param Lead $lead * @param LifecycleEventArgs $event */ public function postLoad(Lead $lead, LifecycleEventArgs $event) { // Get the values for the ArrayCollection and sort it using the function $attributes = $lead->getAttributes(); // Filter by active LeadAttribute if ($attributes) { $attributes = array_filter($attributes, function ($leadAttributeValue) { $attribute = $leadAttributeValue->getAttribute(); return $attribute ? $attribute->getActive() : false; }); } // sort as you like usort($attributes, function ($a, $b) { $aOrder = $bOrder = 0; $aAttribute = $a->getAttribute(); if ($aAttribute) { $aOrder = $aAttribute->getAttributeOrder(); } $bAttribute = $b->getAttribute(); if ($bAttribute) { $bOrder = $bAttribute->getAttributeOrder(); } return $aOrder - $bOrder; }); // Clear the current collection values and reintroduce in new order. $lead->setAttributes(new ArrayCollection($attributes)); $default_ip = "0.0.0.0"; $ip = $lead->getIpaddress(); $ip_filtered = Helper::validate_ipv4($ip, false) ? $ip : $default_ip; $lead->setIpv4address($ip_filtered); $referrer = $lead->getReferrer(); $referrer_filtered = $referrer ? Helper::add_protocol(strtolower($referrer)) : null; $lead->setReferrer($referrer_filtered); }
/** * * @param Lead $proxy * * @return \Lead\Entity\Lead */ private function _hydrate(Lead $proxy) { $lead = null; $reports = $this->getReports(); $objectManager = $this->getObjectManager(); if ($objectManager) { $leadRepo = $objectManager->getRepository('Lead\\Entity\\Lead'); if ($proxy && $proxy->getProxy() && ($id = $proxy->getId()) == true) { $lead = $leadRepo->findLead($id); if ($lead) { $lead->setProxy(false); } } elseif (!$proxy->getProxy()) { $lead = $proxy; } } return $lead; }
/** * * @param AbstractQuery $query * @param boolean $lazy * @param boolean $silent * * @return array $results */ protected function runQuery($query, $lazy = false, $silent = false) { $results = ['results' => [], 'total' => 0]; try { $request = $this->getServiceLocator()->get('Request'); $debug = $request->getQuery('debug'); if ($debug) { echo "<script>console.log('" . addSlashes(json_encode($query->toArray())) . "');</script>\n"; } $client = $this->getElasticaClient(); $response = $client->request('reports/lead/_search?query_cache=true', Request::GET, $query->toArray()); if ($response && $response->isOk()) { $objRepository = $this->getObjectManager()->getRepository("Lead\\Entity\\Lead"); $data = $response->getData(); if ($data && isset($data['hits']['hits'])) { $hits = $data['hits']['hits']; $max_score = $data['hits']['max_score'] ?: 1; $results['total'] = $data['hits']['total']; foreach ($hits as $hit) { $_score = isset($hit['_score']) ? $hit['_score'] : 1; $score = round($_score / $max_score * 100); $result = new Result($this->getServiceLocator()); $result->setScore($score); if ($lazy) { $lead = new Lead(); $lead->setId($hit['_id']); $lead->setProxy(true); } else { $lead = $objRepository->findOneBy(['id' => $hit['_id']]); } if ($lead && $lead instanceof Lead) { $result->setLead($lead); $results['results'][] = $result; } } } } } catch (\Exception $e) { if (!$silent) { $this->getFlashMessenger()->addErrorMessage($e->getMessage()); } } return $results; }
protected function getLeadAttributeValue(Lead $lead, $key, $default = "", $desc = false, $exact = false) { $attribute = $lead->findAttribute($key, $desc, $exact); return $attribute ? $attribute->getValue() : $default; }
protected function setReferrerLead($referrer, Lead $lead, $flush = false) { $em = $this->getEntityManager(); $this->createServiceEvent()->setEntityId($lead->getId())->setEntityClass($this->getEntityClass())->setDescription("Lead Edited"); try { $lead->setReferrer($referrer); $em->merge($lead); if ($flush) { $em->flush(); $em->detach($lead); } $this->getServiceEvent()->setMessage("Referrer for Lead #{$lead->getId()} was edited."); $this->logEvent("EditAction.post"); } catch (\Exception $e) { $this->logError($e); return false; } return true; }
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; }
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; }
/** * Set lead * * @param \Lead\Entity\Lead $lead * * @return LeadAttributeValue */ public function setLead(\Lead\Entity\Lead $lead = null) { $this->lead = $lead; if ($this->lead instanceof Lead) { $this->setParent($this->lead->getId()); } return $this; }
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; }