private function formatResults($results, $terms) { $highlightOptions = array('format' => '<b>\\1</b>', 'html' => false, 'regex' => "|%s|iu"); $snippetFields = array('description', 'fulltext', 'abstract'); // Format the results for the view foreach ($results as &$result) { // Event for special formatting $override = Event::trigger('search.onFormatResult', array($result['hubtype'], &$result, $terms, $highlightOptions)); // Only allow one override per result if (count($override) == 1) { $override = $override[0]; } if (empty($override)) { //@FIXME: SOLR-specific $result['title'] = $result['title'][0]; // Appends http(s):// if (isset($result['url'])) { $result['url'] = rtrim(Request::base(), "/") . $result['url']; } else { $result['url'] = ''; } $snippet = ''; foreach ($result as $field => &$r) { // Only work on strings if (is_string($r)) { $r = strip_tags($r); } // Highlight everything except the URL if ($field != 'url') { $r = \Hubzero\Utility\String::highlight($r, $terms, $highlightOptions); } /** * Generate the snippet * A snippet is the search result text which is displayed **/ if (in_array($field, $snippetFields)) { $snippet .= $r . " "; } } // Do some filtering $snippet = str_replace("\n", '', $snippet); $snippet = str_replace("\r", '', $snippet); $snippet = str_replace("<br/>", '', $snippet); $snippet = str_replace("<br>", '', $snippet); $snippet = \Hubzero\Utility\String::excerpt($snippet, $terms, $radius = 200, $ellipsis = '…'); $result['snippet'] = $snippet; if (isset($result['author'])) { $authorCnt = 1; $authorString = ''; foreach ($result['author'] as $author) { if ($authorCnt < count($result['author'])) { $authorString .= $author; $authorString .= ','; } else { $authorString .= $author; } $authorCnt++; } $result['authorString'] = $authorString; } } else { $result = $override; } } // End foreach results return $results; }
public function listTask() { $config = Component::params('com_search'); $query = new \Hubzero\Search\Query($config); $terms = Request::getVar('terms', '*:*'); $limit = Request::getInt('limit', 10); $start = Request::getInt('start', 0); $sortBy = Request::getVar('sortBy', ''); $sortDir = Request::getVar('sortDir', ''); $type = Request::getVar('type', ''); $filters = Request::getVar('filters', array()); // Apply the sorting if ($sortBy != '' && $sortDir != '') { $query = $query->sortBy($sortBy, $sortDir); } if ($type != '') { $query->addFilter('Type', array('hubtype', '=', $type)); } // Administrators can see all records $isAdmin = User::authorise('core.admin', 'com_users'); if ($isAdmin) { $query = $query->query($terms)->limit($limit)->start($start); } else { $query = $query->query($terms)->limit($limit)->start($start)->restrictAccess(); } // Perform the query $query = $query->run(); $results = $query->getResults(); $numFound = $query->getNumFound(); $highlightOptions = array('format' => '<span class="highlight">\\1</span>', 'html' => false, 'regex' => "|%s|iu"); foreach ($results as &$result) { $snippet = ''; foreach ($result as $field => &$r) { if (is_string($r)) { $r = strip_tags($r); } if ($field != 'url') { $r = \Hubzero\Utility\String::highlight($r, $terms, $highlightOptions); } if ($field == 'description' || $field == 'fulltext' || $field == 'abstract') { if (isset($result['description']) && $result['description'] != $result['fulltext']) { $snippet .= $r; } } } $snippet = str_replace("\n", "", $snippet); $snippet = str_replace("\r", "", $snippet); $snippet = \Hubzero\Utility\String::excerpt($snippet, $terms, $radius = 200, $ellipsis = '…'); $result['snippet'] = $snippet; } $response = new stdClass(); $response->results = $results; $response->total = $numFound; $response->showing = count($results); $response->success = true; $this->send($response); }