Lists can now define search scope or mode (see docs)

This commit is contained in:
Samuel Georges 2016-04-19 19:54:22 +10:00
parent e2566c36a0
commit bff35e5f1a
3 changed files with 63 additions and 2 deletions

View File

@ -190,6 +190,11 @@ class ListController extends ControllerBehavior
return $widget->onRefresh();
});
$widget->setSearchOptions([
'mode' => $searchWidget->mode,
'scope' => $searchWidget->scope,
]);
// Find predefined search term
$widget->setSearchTerm($searchWidget->getActiveTerm());
}

View File

@ -136,6 +136,19 @@ class Lists extends WidgetBase
*/
protected $searchTerm;
/**
* @var string If searching the records, specifies a policy to use.
* - all: result must contain all words
* - any: result can contain any word
* - exact: result must contain the exact phrase
*/
protected $searchMode;
/**
* @var string Use a custom scope method for performing searches.
*/
protected $searchScope;
/**
* @var array Collection of functions to apply to each list query.
*/
@ -378,7 +391,7 @@ class Lists extends WidgetBase
* Search primary columns
*/
if (count($primarySearchable) > 0) {
$innerQuery->orSearchWhere($this->searchTerm, $primarySearchable);
$this->applySearchToQuery($innerQuery, $primarySearchable, 'or');
}
/*
@ -394,7 +407,7 @@ class Lists extends WidgetBase
if (count($columnsToSearch) > 0) {
$innerQuery->orWhereHas($join, function ($_query) use ($columnsToSearch) {
$_query->searchWhere($this->searchTerm, $columnsToSearch);
$this->applySearchToQuery($_query, $columnsToSearch);
});
}
}
@ -1017,6 +1030,21 @@ class Lists extends WidgetBase
$this->searchTerm = $term;
}
/**
* Applies a search options to the list search.
* @param array $options
*/
public function setSearchOptions($options = [])
{
extract(array_merge([
'mode' => null,
'scope' => null
], $options));
$this->searchMode = $mode;
$this->searchScope = $scope;
}
/**
* Returns a collection of columns which can be searched.
* @return array
@ -1037,6 +1065,22 @@ class Lists extends WidgetBase
return $searchable;
}
/**
* Applies the search constraint to a query.
*/
protected function applySearchToQuery($query, $columns, $boolean = 'and')
{
$term = $this->searchTerm;
if ($scopeMethod = $this->searchScope) {
$query->$scopeMethod($term);
}
else {
$searchMethod = $boolean == 'and' ? 'searchWhere' : 'orSearchWhere';
$query->$searchMethod($term, $columns, $this->searchMode);
}
}
//
// Sorting
//

View File

@ -31,6 +31,16 @@ class Search extends WidgetBase
*/
public $partial;
/**
* @var string Defines the search mode. Commonly passed to the searchWhere() query.
*/
public $mode;
/**
* @var string Custom scope method name. Commonly passed to the query.
*/
public $scope;
//
// Object properties
//
@ -59,6 +69,8 @@ class Search extends WidgetBase
'prompt',
'partial',
'growable',
'scope',
'mode',
]);
/*