Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

SilverStripe custom search results function. For Page and DataObject

View FulltextSearchableResults.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/**
* Process and render search results.
*
* !! NOTE
* _config.php includes:
*
* FulltextSearchable::enable();
* Object::add_extension('ExtendedPage', "FulltextSearchable('HeadlineText')");
* Object::add_extension('NewsStory', "FulltextSearchable('Name,Content')");
* !!
*
* @param array $data The raw request data submitted by user
* @param Form $form The form instance that was submitted
* @param SS_HTTPRequest $request Request generated for this action
*/
function results($data, $form, $request)
{
$keyword = trim($request->requestVar('Search'));
$keyword = Convert::raw2sql($keyword);
$keywordHTML = htmlentities($keyword, ENT_NOQUOTES, 'UTF-8');
$pages = new ArrayList();
$news = new ArrayList();
$files = new ArrayList();
$mode = ' IN BOOLEAN MODE';
//$mode = ' WITH QUERY EXPANSION';
//$mode = '';
$siteTreeClasses = array('Page', 'ExtendedPage'); //add in an classes that extend Page or SiteTree
$siteTreeMatch = "MATCH( Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords ) AGAINST ('$keyword'$mode)
+ MATCH( Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords ) AGAINST ('$keywordHTML'$mode)";
//custom MATCH statement for any Object that extend Page or SiteTree and have additional fields/columns that need to be searchable
//create one block per Object
//i.e. "MATCH( column1, column2, column3 ) AGAINST ('$keyword'$mode) + MATCH( column1, column2, column3 ) AGAINST ('$keywordHTML'$mode)"
$extendedPageMatch = "MATCH( HeadlineText ) AGAINST ('$keyword'$mode)
+ MATCH( HeadlineText ) AGAINST ('$keywordHTML'$mode)";
//DataObject
$newsItemMatch = "MATCH( Name, Content ) AGAINST ('$keyword'$mode)
+ MATCH( Name, Content ) AGAINST ('$keywordHTML'$mode)";
$fileMatch = "MATCH( Filename, Title, Content ) AGAINST ('$keyword'$mode)
+ MATCH( Filename, Title, Content ) AGAINST ('$keywordHTML'$mode)";
/*
* Standard pages
* SiteTree Classes with the default search MATCH
*/
foreach ( $siteTreeClasses as $c )
{
$query = DataList::create($c)
->filter(array('RootLanguageParentID' => $this->RootLanguageParentID))
->where($siteTreeMatch);
$query = $query->dataQuery()->query();
$query->addSelect(array('Relevance' => $siteTreeMatch));
$records = DB::query($query->sql());
$objects = array();
foreach( $records as $record )
{
if ( in_array($record['ClassName'], $siteTreeClasses) )
$objects[] = new $record['ClassName']($record);
}
$pages->merge($objects);
}
/*
* Pages with additional searchable fields
* copy/edit this block for each Object that extend Page/SiteTree and have other columns searchable
* see above for custom MATCH statement
*/
$query = DataList::create('ExtendedPage')
->where($siteTreeMatch . ' OR ' . $extendedPageMatch);
$query = $query->dataQuery()->query();
$query->addSelect(array('Relevance' => $siteTreeMatch . ' OR ' . $extendedPageMatch));
$records = DB::query($query->sql());
$objects = array();
foreach( $records as $record ) $objects[] = new $record['ClassName']($record);
$pages->merge($objects);
$pages->removeDuplicates();
/*
* news DataObject
*/
$query = DataList::create('NewsStory')->where($newsItemMatch);
$query = $query->dataQuery()->query();
$query->addSelect(array('Relevance' => $newsItemMatch));
$records = DB::query($query->sql());
$objects = array();
foreach( $records as $record ) $objects[] = new $record['ClassName']($record);
$news->merge($objects);
/*
* files
*/
$query = DataList::create('File')->where($fileMatch);
$query = $query->dataQuery()->query();
$query->addSelect(array('Relevance' => $fileMatch));
$records = DB::query($query->sql());
$objects = array();
foreach( $records as $record )
{
$extension = strtolower( pathinfo( $record['Name'], PATHINFO_EXTENSION ) );
//only return some file extensions
if ( in_array($extension, array('xls','xlsx','doc','docx','pdf','rtf','zip','7z','rar')) )
$objects[] = new $record['ClassName']($record);
}
$files->merge($objects);
/* ** */
$pages->sort(array(
'Relevance' => 'DESC',
'Title' => 'ASC'
));
$news->sort(array(
'Relevance' => 'DESC',
'Date' => 'DESC'
));
$files->sort(array(
'Relevance' => 'DESC',
'Name' => 'ASC'
));
$data = array(
'Pages' => $pages,
'News' => $news,
'Files' => $files,
'Query' => $keyword
);
if ( $pages->count() == 0
&& $news->count() == 0
&& $files->count() == 0 )
{
$data['NoResults'] = 1;
}
return $this->customise($data)->renderWith(array('Search','Page'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.