/** * (non-PHPdoc) * * @see \Agent\Entity\Relationship::getQuery() */ public function getQuery($values = null, $query = null, $required = null, $boost = null) { $fields = array_flip(['city', 'state', 'zip']); $location = array_intersect_key(array_filter($values), $fields); $criterion = $this->getCriterion(); $method = 'addMust'; // $required ? 'addMust' : 'addShould'; $distance = empty($values['distance']) ? false : $values['distance']; if ($criterion && $location) { if (!$distance) { $query = parent::getQuery($values, $query, $required, $boost); $attribute = $criterion->getAttribute(); if ($attribute) { $attributeDesc = $attribute->getAttributeDesc(); if ($attributeDesc) { $token = strtolower(trim(current(explode(' ', $attributeDesc)))); if ($token && isset($location[$token])) { $value = $location[$token]; if (isset($value)) { if (is_array($value)) { $statesQuery = new \Agent\Elastica\Query\BoolQuery(); foreach ($value as $_value) { $state_full_name = $this->getStateNames($_value); if ($state_full_name) { $statesQuery->addShould(new Elastica\Query\Match('value', $state_full_name)); } $statesQuery->addShould(new Elastica\Query\Match('value', $_value)); } $query->{$method}($statesQuery); } else { $state_full_name = $this->getStateNames($value); if ($state_full_name) { $query->{$method}(new Elastica\Query\Match('value', $state_full_name)); } $query->{$method}(new Elastica\Query\Match('value', $value)); } } } } } } else { try { $localityQuery = new LocalityQuery($criterion->getServiceLocator()); $locality = $localityQuery->request($location); if ($locality) { list($latlon['lat'], $latlon['lon']) = $locality['_source']['latlon']; $query = new Elastica\Query\GeoDistance('locality', $latlon, $distance . "mi"); } } catch (\Exception $e) { // } } } return $query; }
public function geoAction() { $id = $this->getEvent()->getRouteMatch()->getParam('id', 0); $operation = $this->params()->fromQuery('operation', 'info'); $result = ['outcome' => 0]; $em = $this->getEntityManager(); $objRepository = $em->getRepository($this->getEntityClass()); if ($id) { /* @var $lead \Lead\Entity\Lead */ $lead = $objRepository->find($id); if ($lead && $lead instanceof Lead) { $location = []; $location['ipaddress'] = $lead->getIpaddress(); $location['locality'] = $lead->getLocality(); foreach (['city', 'state', 'zip', 'phone'] as $attribute) { $value = $lead->findAttribute($attribute, true); $location[$attribute] = $value ? $value->getValue() : null; } $result['data']['location'] = $location; $missing = array_filter(array_map(function ($x) { return empty($x); }, array_slice($location, 0, -1))); if ($missing) { $localityQuery = new LocalityQuery($this->getServiceLocator()); $locality = $localityQuery->request(array_filter($location)); if ($locality) { switch ($operation) { case 'update': $result['outcome'] = $this->updateLeadGeo($lead, $locality) ? 1 : 0; break; case 'info': $result['outcome'] = 1; $result['data']['geo'] = $locality; $result['data']['lead'] = $lead->toArray(); } } } else { $result['outcome'] = 2; } } } else { $leads = $em->createQuery("SELECT l.id \n\t\t\t\t\tFROM \n\t\t\t\t\t\tLead\\Entity\\Lead l \n\t\t\t\t\t\t\tINNER JOIN l.attributes v1 \n\t\t\t\t\t\t\tINNER JOIN v1.attribute a1 \n\t\t\t\t\tWHERE a1.attributeDesc = 'City' \n\t\t\t\t\t\tAND l.id NOT IN (\n\t\t\t\t\t\tSELECT e.id \n\t\t\t\t\t\t\tFROM Lead\\Entity\\Lead e \n\t\t\t\t\t\t\t\tINNER JOIN e.attributes v \n\t\t\t\t\t\t\t\tINNER JOIN v.attribute a \n\t\t\t\t\t\tWHERE a.attributeDesc = 'State' \n\t\t\t\t\t\t\tAND e.locality IS NOT NULL\n\t\t\t\t\t)")->getScalarResult(); $result['data']['ids'] = array_map('current', $leads); $result['outcome'] = 1; } return new JsonModel($result); }
/** * * @return string */ public function getLocality() { if (!isset($this->locality) || empty($this->locality)) { $result = null; $fields = ['city', 'state', 'zip']; $location = array_combine($fields, array_pad([], count($fields), null)); foreach ($fields as $field) { foreach ([ucwords($field), $field] as $name) { $attribute = $this->findAttribute($name); if ($attribute) { $location[$field] = $attribute->getValue(); break 1; } } } $location = array_filter($location); if (count($location) > 1 || isset($location['zip'])) { try { $localityQuery = new Agent\Elastica\Query\LocalityQuery($this->getServiceLocator()); $locality = $localityQuery->request($location); if ($locality) { $result = $this->locality = implode(",", $locality['_source']['latlon']); } } catch (\Exception $e) { return $result; } } } return $this->locality; }