/** * * @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; }
public function extractResult(Result $result) { $headings = $this->exportHeadings; $lead = $result->getLead(); $entityManager = $this->getEntityManager(); $hydrator = new DoctrineHydrator($entityManager); $leadArray = $hydrator->extract($lead); $output = array_combine($headings, array_pad([], count($headings), "")); foreach ($headings as $heading) { switch ($heading) { case "Score": $score = $result->getScore(); $output[$heading] = $score ? $score : "N/A"; break; case "Account": $account = $lead->getAccount(); $output[$heading] = $account ? $account->getName() : "N/A"; break; case "Last Submitted": $time = $lead->getLastsubmitted(); if ($time instanceof \DateTime) { $time = date_format($time, 'Y-m-d H:i:s'); } $output[$heading] = $time; break; case "Time Created": $time = $lead->getTimecreated(); if ($time instanceof \DateTime) { $time = date_format($time, 'Y-m-d H:i:s'); } $output[$heading] = $time; break; case "Referrer": $output[$heading] = $lead->getReferrer(); break; case "IP Address": $output[$heading] = $lead->getIpaddress(); break; default: $attribute = $lead->findAttribute($heading); if (!$attribute) { $attributes = $lead->getAttributes(true)->filter(function ($attribute) use($heading) { $real_attribute = false; $attribute_desc = false; if ($attribute) { $real_attribute = $attribute->getAttribute(); } if ($real_attribute) { $attribute_desc = $real_attribute->getAttributeDesc(); } return $attribute_desc == $heading; }); if ($attributes->count() > 0) { $attribute = $attributes->first(); } } if ($attribute) { $output[$heading] = $attribute->getValue(); } break; } } return $output; }