/** * * @param Report $report * @param number $limit * @param string $sort * @param string $order * @param boolean $lazy * @param boolean $active * @param boolean $silent * * @return ArrayCollection $data */ public function generateResults(Report $report, $limit = 0, $sort = '_score', $order = 'desc', $lazy = true, $active = true, $silent = false) { $data = new ArrayCollection(); $request = $this->getServiceLocator()->get('Request'); $silent = $request->getQuery('debug') ? false : $silent; if ($report instanceof Report) { $agent = $report->getAgent(); $lead_query = new \Agent\Elastica\Query\BoolQuery(); if ($active) { $lead_query->addMust(new Elastica\Query\Match('active', 1)); } $client = $this->getElasticaClient(); if ($agent && $client) { $filters = $agent->getFilter(); if ($filters) { $lead_query = $this->applyFilters($lead_query, $filters); } $criteria = $agent->getCriteria(false); if ($criteria) { try { $query = new Elastica\Query(); /* @var $criterion \Agent\Entity\AgentCriterion */ foreach ($criteria as $i => $criterion) { $lead_query = $this->buildQuery($lead_query, $criterion); } $query->setQuery($lead_query); $size = $limit ? $limit : 1000; $query->setParam('track_scores', true); $query->addSort([$sort => ['order' => $order]]); $query->setSize($size); $results = $this->runQuery($query, $lazy, $silent); $total = isset($results['total']) ? $results['total'] : false; if ($total && $results['results']) { foreach ($results['results'] as $result) { $data->add($result); } } if ($total > $size) { $limit = $limit ?: $total; for ($page = 1; $page < ceil($limit / $size); $page++) { $query->setFrom($page * $size); $results = $this->runQuery($query, $silent); if ($results['results']) { foreach ($results as $result) { if (is_array($result)) { foreach ($result as $r) { if ($r instanceof Result) { $data->add($r); } } } elseif ($result instanceof Result) { $data->add($result); } } } } } } catch (\Exception $e) { if (!$silent) { $this->getFlashMessenger()->addErrorMessage($e->getMessage()); } } } } } return $data; }
/** * * @param Report $report * @param array $post */ protected function setRelationships(Report $report, $post) { if (isset($post['agent']['criteria'])) { $criteria = $post['agent']['criteria']; foreach ($criteria as $i => $criterion) { $_criterion = $report->getAgent()->getCriteria(true)->get($i); if ($_criterion && isset($criterion['relationship'])) { $relationship_id = $criterion['relationship']; $_relationship = $this->getEntityManager()->getRepository("Agent\\Entity\\Relationship")->findOneBy(['id' => $relationship_id]); if ($_relationship) { $_relationship->setId($relationship_id); $_criterion->setRelationship($_relationship); } $report->getAgent()->getCriteria(true)->set($i, $_criterion); } } } return $report; }