Skip to content

Instantly share code, notes, and snippets.

@hissy
Created August 4, 2014 01:31
Show Gist options
  • Save hissy/aace4f9ce59f46115971 to your computer and use it in GitHub Desktop.
Save hissy/aace4f9ce59f46115971 to your computer and use it in GitHub Desktop.
<?php
defined('C5_EXECUTE') or die("Access Denied.");
class SearchBlockController extends Concrete5_Controller_Block_Search {
protected $hColor = array('#EFE795','#D1E095','#95F0C8','#95DAF0','#9E95F0');
public function highlightedMarkup($fulltext, $highlight) {
if(!$highlight){
return $fulltext;
}
$highlight = str_replace(" "," ",$highlight);
if(strpos($highlight," ") !== false){
$highlights = explode(" ",$highlight);
}else{
$highlights = array($highlight);
}
$this->hText = $fulltext;
foreach($highlights as $hkey=>$highlight){
$this->hHighlight = str_replace(array('"',"'","&quot;"),'',$highlight); // strip the quotes as they mess the regex
$this->hText = @preg_replace( "#$this->hHighlight#i", '<span style="background-color:'. $this->hColor[$hkey] .';">$0</span>', $this->hText );
}
return $this->hText;
}
public function highlightedExtendedMarkup($fulltext, $highlight) {
$text = @preg_replace("#\n|\r#", ' ', $fulltext);
$matches = array();
if(strpos($highlight," ") !== false){
$highlights = explode(" ",$highlight);
}else{
$highlights = array($highlight);
}
foreach($highlights as $key=>$highlight){
$fulltext = str_replace($highlight,'<span style="background-color:'. $this->hColor[0] .';">'.$highlight."</span>",$fulltext);
}
$fulltext = explode('<span style="background-color:'. $this->hColor[0] .';">',$fulltext);
foreach($fulltext as $key=>$text){
if($key == 0){
$result = mb_substr($text,strlen($text)-40,40,"UTF-8");
continue;
}
$texts = explode("</span>",$text);
if(strlen($texts[1]) > 47){
$result .= '<span style="background-color:'. $this->hColor[$key] .';">'.$texts[0].'</span>'.mb_substr($texts[1], 0 , 47 , "UTF-8")."…";
}else{
$result .= '<span style="background-color:'. $this->hColor[$key] .';">'.$text;
}
}
return $result;
}
public function setHighlightColor($color) {
$this->hColor = array($color);
}
function do_search() {
$q = $_REQUEST['query'];
// i have NO idea why we added this in rev 2000. I think I was being stupid. - andrew
// $_q = trim(preg_replace('/[^A-Za-z0-9\s\']/i', ' ', $_REQUEST['query']));
$_q = $q;
Loader::library('database_indexed_search');
$ipl = new IndexedPageList();
$ipl->setItemsPerPage(10); // 検索結果に表示するページ数を10件に設定
$aksearch = false;
$ipl->ignoreAliases();
if (is_array($_REQUEST['akID'])) {
Loader::model('attribute/categories/collection');
foreach($_REQUEST['akID'] as $akID => $req) {
$fak = CollectionAttributeKey::getByID($akID);
if (is_object($fak)) {
$type = $fak->getAttributeType();
$cnt = $type->getController();
$cnt->setAttributeKey($fak);
$cnt->searchForm($ipl);
$aksearch = true;
}
}
}
if (isset($_REQUEST['month']) && isset($_REQUEST['year'])) {
$month = strtotime($_REQUEST['year'] . '-' . $_REQUEST['month'] . '-01');
$month = date('Y-m-', $month);
$ipl->filterByPublicDate($month . '%', 'like');
$aksearch = true;
}
if (empty($_REQUEST['query']) && $aksearch == false) {
return false;
}
$ipl->setSimpleIndexMode(true);
if (isset($_REQUEST['query'])) {
$ipl->filterByKeywords($_q);
}
if( is_array($_REQUEST['search_paths']) ){
foreach($_REQUEST['search_paths'] as $path) {
if(!strlen($path)) continue;
$ipl->filterByPath($path);
}
} else if ($this->baseSearchPath != '') {
$ipl->filterByPath($this->baseSearchPath);
}
$ipl->filter(false, '(ak_exclude_search_index = 0 or ak_exclude_search_index is null)');
$res = $ipl->getPage();
foreach($res as $r) {
$results[] = new IndexedSearchResult($r['cID'], $r['cName'], $r['cDescription'], $r['score'], $r['cPath'], $r['content'], $r['cDatePublic']);
}
$this->set('query', $q);
$this->set('paginator', $ipl->getPagination());
$this->set('results', $results);
$this->set('do_search', true);
$this->set('searchList', $ipl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment