/** * Highlight some text * * @param string $text Text to find phrases in * @param integer $phrase Phrase to highlight * @param array $options Options for highlighting * @return string * @throws \InvalidArgumentException If no text was passed */ public function __invoke($text = null, $phrase = null, $options = array()) { if (null === $text) { throw new \InvalidArgumentException(__METHOD__ . '(); No text passed.'); } return String::highlight($text, $phrase, $options); }
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); }
<?php echo String::highlight($record->task->name, $this->filters['search'], array('html' => true)); ?> </div> </div> <div class="td last" title="<?php echo $record->description; ?> "> <div class="small-label"><?php echo Lang::txt('COM_TIME_RECORDS_DESCRIPTION'); ?> :</div> <div class="small-content"> <?php echo String::highlight(String::truncate($record->description, 25), $this->filters['search'], array('html' => true)); ?> </div> </div> </div> <?php } ?> <?php if (!$this->records->count()) { ?> <div class="tr"> <div colspan="7" class="td no_records"><?php echo Lang::txt('COM_TIME_RECORDS_NONE_TO_DISPLAY'); ?> </div>
public function formatted($config = array('format' => 'apa'), $highlight = NULL) { //get hub specific details $hub_name = \Config::get('sitename'); $hub_url = rtrim(\Request::base(), '/'); //get scope specific details $coins_only = isset($config['coins_only']) ? $config['coins_only'] : "no"; $include_coins = isset($config['include_coins']) ? $config['include_coins'] : "no"; $c_type = 'journal'; $type = $this->relatedType->type; switch (strtolower($type)) { case 'book': case 'inbook': case 'conference': case 'proceedings': case 'inproceedings': $c_type = "book"; break; case 'journal': case 'article': case 'journal article': $c_type = "journal"; break; default: break; } //var to hold COinS data $coins_data = array("ctx_ver=Z39.88-2004", "rft_val_fmt=info:ofi/fmt:kev:mtx:{$c_type}", "rfr_id=info:sid/{$hub_url}:{$hub_name}"); //array to hold replace vals $replace_values = array(); // get the template // default to IEEE try { $format = \Components\Citations\Models\Format::oneOrFail($config['citationFormat']); } catch (\Exception $e) { $format = \Components\Citations\Models\Format::all()->where('style', 'LIKE', '%IEEE%')->row()->toObject(); } //get the template keys $template_keys = array("type" => "{TYPE}", "cite" => "{CITE KEY}", "ref_type" => "{REF TYPE}", "date_submit" => "{DATE SUBMITTED}", "date_accept" => "{DATE ACCEPTED}", "date_publish" => "{DATE PUBLISHED}", "author" => "{AUTHORS}", "editor" => "{EDITORS}", "title" => "{TITLE/CHAPTER}", "booktitle" => "{BOOK TITLE}", "chapter" => "{CHAPTER}", "journal" => "{JOURNAL}", "journaltitle" => "{JOURNAL TITLE}", "volume" => "{VOLUME}", "number" => "{ISSUE/NUMBER}", "pages" => "{PAGES}", "isbn" => "{ISBN/ISSN}", "issn" => "{ISSN}", "doi" => "{DOI}", "series" => "{SERIES}", "edition" => "{EDITION}", "school" => "{SCHOOL}", "publisher" => "{PUBLISHER}", "institution" => "{INSTITUTION}", "address" => "{ADDRESS}", "location" => "{LOCATION}", "howpublished" => "{HOW PUBLISHED}", "url" => "{URL}", "eprint" => "{E-PRINT}", "note" => "{TEXT SNIPPET/NOTES}", "organization" => "{ORGANIZATION}", "abstract" => "{ABSTRACT}", "year" => "{YEAR}", "month" => "{MONTH}", "search_string" => "{SECONDARY LINK}", "sec_cnt" => "{SECONDARY COUNT}"); /** * Values used by COINs * * @var array */ $coins_keys = array('title' => 'rft.atitle', 'journaltitle' => 'rft.jtitle', 'date_publish' => 'rft.date', 'volume' => 'rft.volume', 'number' => 'rft.issue', 'pages' => 'rft.pages', 'issn' => 'rft.issn', 'isbn' => 'rft.isbn', 'type' => 'rft.genre', 'author' => 'rft.au', 'url' => 'rft_id', 'doi' => 'rft_id=info:doi/', 'author' => 'rft.au'); // form the formatted citation foreach ($template_keys as $k => $v) { if (!$this->keyExistsOrIsNotEmpty($k, $this) && $k != 'author') { $replace_values[$v] = ''; } else { $replace_values[$v] = $this->{$k}; //add to coins data if we can but not authors as that will get processed below if (in_array($k, array_keys($coins_keys)) && $k != 'author') { //key specific switch ($k) { case 'title': break; case 'doi': $coins_data[] = $this->_coins_keys[$k] . $this->{$k}; break; case 'url': $coins_data[] = $this->_coins_keys[$k] . '=' . htmlentities($this->{$k}); break; case 'journaltitle': $jt = html_entity_decode($this->{$k}); $jt = !preg_match('!\\S!u', $jt) ? utf8_encode($jt) : $jt; $coins_data[] = $this->_coins_keys[$k] . '=' . $jt; break; default: $coins_data[] = $this->_coins_keys[$k] . '=' . $this->{$k}; } } if ($k == 'author') { $a = array(); $auth = html_entity_decode($this->{$k}); $auth = !preg_match('!\\S!u', $auth) ? utf8_encode($auth) : $auth; // prefer the use of the relational table if ($this->relatedAuthors->count() > 0) { $authors = $this->relatedAuthors; $authorCount = $this->relatedAuthors->count(); } elseif ($auth != '' && $this->relatedAuthors->count() == 0) { $author_string = $auth; $authors = explode(';', $author_string); $authorCount = count($authors); } else { $authorCount = 0; $replace_values[$v] = ''; } if ($authorCount > 0) { foreach ($authors as $author) { // for legacy profile handling if (is_string($author)) { preg_match('/{{(.*?)}}/s', $author, $matches); if (!empty($matches)) { $id = trim($matches[1]); if (is_numeric($id)) { $user = \User::getInstance($id); if (is_object($user)) { $a[] = '<a rel="external" href="' . \Route::url('index.php?option=com_members&id=' . $matches[1]) . '">' . str_replace($matches[0], '', $author) . '</a>'; } else { $a[] = $author; } } } else { $a[] = $author; } // add author coins $coins_data[] = 'rft.au=' . trim(preg_replace('/\\{\\{\\d+\\}\\}/', '', trim($author))); } elseif (is_object($author)) { if ($author->uidNumber > 0) { $a[] = '<a rel="external" href="' . \Route::url('index.php?option=com_members&id=' . $author->uidNumber) . '">' . $author->author . '</a>'; } else { $a[] = $author->author; } } else { $a[] = $author; } } $replace_values[$v] = implode(", ", $a); } } if ($k == 'title') { $url_format = isset($config['citation_url']) ? $config['citation_url'] : 'url'; $custom_url = isset($config['citation_custom_url']) ? $config['citation_custom_url'] : ''; $url = $this->url; if ($url_format == 'custom' && $custom_url != '') { //parse custom url to make sure we are not using any vars preg_match_all('/\\{(\\w+)\\}/', $custom_url, $matches, PREG_SET_ORDER); if ($matches) { foreach ($matches as $match) { $field = strtolower($match[1]); $replace = $match[0]; $replaceWith = ''; if (property_exists($this, $field)) { if (strstr($this->{$field}, 'http')) { $custom_url = $this->{$field}; } else { $replaceWith = urlencode($this->{$field}); $custom_url = str_replace($replace, $replaceWith, $custom_url); } } } //set the citation url to be the new custom url parsed $url = $custom_url; } } //prepare url if (strstr($url, "\r\n")) { $url = array_filter(array_values(explode("\r\n", $url))); $url = $url[0]; } elseif (strstr($url, " ")) { $url = array_filter(array_values(explode(" ", $url))); $url = $url[0]; } $t = html_entity_decode($this->{$k}); $t = !preg_match('!\\S!u', $t) ? utf8_encode($t) : $t; $title = $url != '' && preg_match('/http:|https:/', $url) ? '<a rel="external" class="citation-title" href="' . $url . '">' . $t . '</a>' : '<span class="citation-title">' . $t . '</span>'; //do we want to display single citation //$singleCitationView = $config('citation_single_view', 0); $singleCitationView = isset($config['citation_single_view']) ? $config['citation_single_view'] : 0; if ($singleCitationView && isset($this->id)) { $title = '<a href="' . \Route::url('index.php?option=com_citations&task=view&id=' . $this->id) . '">' . $t . '</a>'; } //send back title to replace title placeholder ({TITLE}) $replace_values[$v] = '"' . $title . '"'; //add title to coin data but fixing bad chars first $coins_data[] = 'rft.atitle=' . $t; } if ($k == 'pages') { $replace_values[$v] = "pg: " . $this->{$k}; } } } // Add more to coins $template = $format->format; $tmpl = isset($template) ? $template : $default_template; $cite = strtr($tmpl, $replace_values); // Strip empty tags $pattern = "/<[^\\/>]*>([\\s]?)*<\\/[^>]*>/"; $cite = preg_replace($pattern, '', $cite); //reformat dates $pattern = "/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/"; $cite = preg_replace($pattern, "\$2-\$3-\$1", $cite); // Reduce multiple spaces to one $pattern = "/\\s/s"; $cite = preg_replace($pattern, ' ', $cite); // Strip empty punctuation inside $b = array("''" => '', '""' => '', '()' => '', '{}' => '', '[]' => '', '??' => '', '!!' => '', '..' => '.', ',,' => ',', ' ,' => '', ' .' => '', ',.' => '.', '","' => '', 'doi:.' => '', '(DOI:).' => ''); foreach ($b as $k => $i) { $cite = str_replace($k, $i, $cite); } // Strip empty punctuation from the start $c = array("' ", '" ', '(', ') ', ', ', '. ', '? ', '! ', ': ', '; '); foreach ($c as $k) { if (substr($cite, 0, 2) == $k) { $cite = substr($cite, 2); } } // remove trailing commas $cite = trim($cite); if (substr($cite, -1) == ',') { $cite = substr($cite, 0, strlen($cite) - 1); } // percent encode chars $chars = array('%', ' ', '/', ':', '"', '\'', '&'); $replace = array("%20", "%20", "%2F", "%3A", "%22", "%27", "%26"); $coins_data = str_replace($chars, $replace, implode('&', $coins_data)); $cite = preg_replace('/, :/', ':', $cite); // highlight citation data // do before appendnind coins as we dont want that data accidentily highlighted (causes style issues) $cite = $highlight ? String::highlight($cite, $highlight) : $cite; // if we want coins add them if ($include_coins == "yes" || $coins_only == "yes") { $coins = '<span class="Z3988" title="' . $coins_data . '"></span>'; if ($coins_only == "yes") { return $coins; } $cite .= $coins; } // output the citation return $cite; }
/** * Formatting Resources * * @param object &$row Record to format * @param string $link Parameter description (if any) ... * @param string $highlight String to highlight * @return string */ public static function formatReference(&$row, $link = 'none', $highlight = '') { $html = "\t" . '<p>'; if (self::keyExistsOrIsNotEmpty('author', $row)) { $auths = explode(';', $row->author); $a = array(); foreach ($auths as $auth) { preg_match('/{{(.*?)}}/s', $auth, $matches); if (isset($matches[0]) && $matches[0] != '') { $matches[0] = preg_replace('/{{(.*?)}}/s', '\\1', $matches[0]); $aid = 0; if (is_numeric($matches[0])) { $aid = $matches[0]; } else { $zuser = \User::getInstance(trim($matches[0])); if (is_object($zuser)) { $aid = $zuser->get('id'); } } $auth = preg_replace('/{{(.*?)}}/s', '', $auth); if ($aid) { $a[] = '<a href="' . \Route::url('index.php?option=com_members&id=' . $aid) . '">' . trim($auth) . '</a>'; } else { $a[] = trim($auth); } } else { $a[] = trim($auth); } } $row->author = implode('; ', $a); $html .= stripslashes($row->author); } elseif (self::keyExistsOrIsNotEmpty('editor', $row)) { $html .= stripslashes($row->editor); } if (self::keyExistsOrIsNotEmpty('year', $row)) { $html .= ' (' . $row->year . ')'; } if (self::keyExistsOrIsNotEmpty('title', $row)) { if (!$row->url) { $html .= ', "' . stripslashes($row->title); } else { $html .= ', "<a href="' . self::cleanUrl($row->url) . '">' . String::highlight(stripslashes($row->title), $highlight) . '</a>'; } } if (self::keyExistsOrIsNotEmpty('journal', $row) || self::keyExistsOrIsNotEmpty('edition', $row) || self::keyExistsOrIsNotEmpty('booktitle', $row)) { $html .= ','; } $html .= '"'; if (self::keyExistsOrIsNotEmpty('journal', $row)) { $html .= ' <i>' . String::highlight(stripslashes($row->journal), $highlight) . '</i>'; } elseif (self::keyExistsOrIsNotEmpty('booktitle', $row)) { $html .= ' <i>' . stripslashes($row->booktitle) . '</i>'; } if ($row->type) { switch ($row->type) { case 'phdthesis': $html .= ' (' . \Lang::txt('PhD Thesis') . ')'; break; case 'mastersthesis': $html .= ' (' . \Lang::txt('Masters Thesis') . ')'; break; default: break; } } if (self::keyExistsOrIsNotEmpty('edition', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . $row->edition; } if (self::keyExistsOrIsNotEmpty('chapter', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->chapter); } if (self::keyExistsOrIsNotEmpty('series', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->series); } if (self::keyExistsOrIsNotEmpty('publisher', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->publisher); } if (self::keyExistsOrIsNotEmpty('address', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->address); } if (self::keyExistsOrIsNotEmpty('volume', $row)) { $html = self::grammarCheck($html, ','); $html .= ' <b>' . $row->volume . '</b>'; } if (self::keyExistsOrIsNotEmpty('number', $row)) { $html = self::grammarCheck($html, ','); $html .= ' <b>' . $row->number . '</b>'; } if (self::keyExistsOrIsNotEmpty('pages', $row)) { $html .= ': pg. ' . $row->pages; } if (self::keyExistsOrIsNotEmpty('organization', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->organization); } if (self::keyExistsOrIsNotEmpty('institution', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->institution); } if (self::keyExistsOrIsNotEmpty('school', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->school); } if (self::keyExistsOrIsNotEmpty('location', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . stripslashes($row->location); } if (self::keyExistsOrIsNotEmpty('month', $row)) { $html = self::grammarCheck($html, ','); $html .= ' ' . $row->month; } if (self::keyExistsOrIsNotEmpty('isbn', $row)) { $html = self::grammarCheck($html, '.'); $html .= ' ' . $row->isbn; } if (self::keyExistsOrIsNotEmpty('doi', $row)) { $html = self::grammarCheck($html, '.'); $html .= ' (' . \Lang::txt('DOI') . ': ' . $row->doi . ')'; } $html = self::grammarCheck($html, '.'); $html .= '</p>' . "\n"; return $html; }
echo Route::url($this->base . '&task=toggleactive&id=' . $task->id); ?> "></a> </div> <div class="td"> <div class="small-label"><?php echo Lang::txt('COM_TIME_TASKS_NAME'); ?> :</div> <div class="small-content"> <a href="<?php echo Route::url($this->base . '&task=edit&id=' . $task->id); ?> "> <?php echo String::highlight($task->name, $this->filters['search'], array('html' => true)); ?> </a> </div> </div> <div class="td"> <div class="small-label"><?php echo Lang::txt('COM_TIME_TASKS_HUB_NAME'); ?> :</div> <div class="small-content"> <?php echo $task->hub->name; ?> </div> </div>
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; }