From bff35e5f1af52ba3f0827f2ca66e29315a1b5e4f Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 19 Apr 2016 19:54:22 +1000 Subject: [PATCH] Lists can now define search scope or mode (see docs) --- modules/backend/behaviors/ListController.php | 5 ++ modules/backend/widgets/Lists.php | 48 +++++++++++++++++++- modules/backend/widgets/Search.php | 12 +++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/modules/backend/behaviors/ListController.php b/modules/backend/behaviors/ListController.php index 882ef3a3a..0d23cfc32 100644 --- a/modules/backend/behaviors/ListController.php +++ b/modules/backend/behaviors/ListController.php @@ -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()); } diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index e350c4c6d..6103cb4ba 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -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 // diff --git a/modules/backend/widgets/Search.php b/modules/backend/widgets/Search.php index 0fab52e0c..b94079149 100644 --- a/modules/backend/widgets/Search.php +++ b/modules/backend/widgets/Search.php @@ -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', ]); /*