Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENHANCEMENT overload getResults() with extension point to change $booleanSearch param #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/CMS/Search/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

use BadMethodCallException;
use SilverStripe\Assets\File;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Search\SearchForm as SS_SearchForm;
use SilverStripe\Control\Controller;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\SS_List;
use SilverStripers\ElementalSearch\Model\SearchDocument;

class SearchForm extends SS_SearchForm
Expand Down Expand Up @@ -40,4 +44,83 @@ public function classesToSearch($classes)
$this->classesToSearch = $legalClasses;
}

/**
* Return dataObjectSet of the results using current request to get info from form.
* Wraps around {@link searchEngine()}.
*
* @return SS_List
*/
public function getResults()
{
// Get request data from request handler
$request = $this->getRequestHandler()->getRequest();

// set language (if present)
$locale = null;
$origLocale = null;
if (class_exists('Translatable')) {
$locale = $request->requestVar('searchlocale');
if (SiteTree::singleton()->hasExtension('Translatable') && $locale) {
if ($locale === "ALL") {
Translatable::disable_locale_filter();
} else {
$origLocale = Translatable::get_current_locale();

Translatable::set_current_locale($locale);
}
}
}

$keywords = $request->requestVar('Search');

$andProcessor = function ($matches) {
return ' +' . $matches[2] . ' +' . $matches[4] . ' ';
};
$notProcessor = function ($matches) {
return ' -' . $matches[3];
};

$keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords);
$keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords);
$keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords);
$keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords);

$keywords = $this->addStarsToKeywords($keywords);

$pageLength = $this->getPageLength();
$start = $request->requestVar('start') ?: 0;

$booleanSearch =
strpos($keywords, '"') !== false ||
strpos($keywords, '+') !== false ||
strpos($keywords, '-') !== false ||
strpos($keywords, '*') !== false;

$this->extend('updateBooleanSearchParam', $booleanSearch);

$results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch);

// filter by permission
if ($results) {
foreach ($results as $result) {
if (!$result->canView()) {
$results->remove($result);
}
}
}

// reset locale
if (class_exists('Translatable')) {
if (SiteTree::singleton()->hasExtension('Translatable') && $locale) {
if ($locale == "ALL") {
Translatable::enable_locale_filter();
} else {
Translatable::set_current_locale($origLocale);
}
}
}

return $results;
}

}