Skip to content

Commit

Permalink
Add searchQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel authored and ifox committed Jul 10, 2024
1 parent 76aaf0e commit 4fa57e7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/content/1_docs/3_modules/5_controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Below is a list of the methods and their purpose:
- **setModuleName**('`yourModuleName`'): Set the name of the module you are working with.
- **setFeatureField**('`fieldname`'): Set the field to use for featuring content.
- **setSearchColumns**(`['title', 'year']`): Set the columns to search in.
- **setSearchQuery**(`
fn (Builder $q, string $search) => $q->orWhereHas('profile', fn (Builder $q) => $q->where('first_name', 'like', "$search%")->orWhere('last_name', 'like', "$search%"))`): For finer controller over the search
- **setPermalinkBase**('`example`'): The static permalink base to your module. Defaults to `setModuleName` when empty.
- **setTitleColumnKey**('`title`'): Sets the field to use as title, defaults to `title`.
- **setModelName**('`Project`'): Usually not required, but in case customization is needed you can use this method to set
Expand Down Expand Up @@ -240,4 +242,3 @@ protected function formData($request)
return [];
}
```

18 changes: 18 additions & 0 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,14 @@ abstract class ModuleController extends Controller
*/
protected ?array $searchColumns = null;


/**
* If you need more fine control over the search query
*
* Do not modify this directly but use the method setSearchQuery().
*/
protected mixed $searchQuery = null;

/**
* Default label translation keys that can be overridden in the labels array.
*
Expand Down Expand Up @@ -627,6 +635,15 @@ protected function setSearchColumns(array $searchColumns): void
$this->searchColumns = $searchColumns;
}

/**
* If you need finer control over the search query, you may provide a callback
* @param callable $query With the following signature: fn (Builder $query, string $searchString, array $translatedAttributes): void => $query
*/
protected function setSearchQuery(callable $query): void
{
$this->searchQuery = $query;
}

/**
* Set the name of the module you are working with.
*/
Expand Down Expand Up @@ -1841,6 +1858,7 @@ protected function getIndexItems(array $scopes = [], bool $forcePagination = fal
} elseif ($filterKey === 'search') {
$appliedFilters[] = FreeTextSearch::make()
->searchFor($filterValue)
->searchQuery($this->searchQuery)
->searchColumns($this->searchColumns);
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/Services/Listings/Filters/FreeTextSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
class FreeTextSearch extends TwillBaseFilter
{
protected array $searchColumns = [];
protected mixed $searchQuery = null;
protected ?string $searchString = null;

public function applyFilter(Builder $builder): Builder
{
if (!empty($this->searchString) && $this->searchColumns !== []) {
if (!empty($this->searchString) && ($this->searchColumns !== [] || isset($this->searchQuery))) {
/** @var \A17\Twill\Models\Model $builderModel */
$translatedAttributes = $builder->getModel()->getTranslatedAttributes();
$builder->where(function (Builder $builder) use ($translatedAttributes) {
if (isset($this->searchQuery)) {
call_user_func($this->searchQuery, $builder, $this->searchString, $translatedAttributes);
}
foreach ($this->searchColumns as $column) {
if (in_array($column, $translatedAttributes, true)) {
$builder->orWhereTranslationLike($column, "%$this->searchString%");
Expand All @@ -31,6 +35,13 @@ public function applyFilter(Builder $builder): Builder
return $builder;
}

public function searchQuery(?callable $query): static
{
$this->searchQuery = $query;

return $this;
}

public function searchFor(string $searchString): static
{
$this->searchString = $searchString;
Expand All @@ -41,6 +52,7 @@ public function searchFor(string $searchString): static
public function searchColumns(array $columns): static
{
$this->searchColumns = $columns;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function setSearchColumnsTest(...$args): void
$this->setSearchColumns(...$args);
}

public function setSearchQueryTest(...$args): void
{
$this->setSearchQuery(...$args);
}

public function quickFilters(): QuickFilters
{
if ($this->testQuickFilters !== []) {
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/Controllers/Tables/Filters/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace A17\Twill\Tests\Integration\Controllers\Tables\Filters;

use Illuminate\Contracts\Database\Eloquent\Builder;

class SearchTest extends FilterTestBase
{
public function setUp(): void
Expand Down Expand Up @@ -41,7 +43,8 @@ public function testSearchColumnSetter(): void {

// Set the search columns.
$controller = $this->controllerWithFiltersAndQuickFilters(active: ['search' => '2022']);
$controller->setSearchColumnsTest(['name', 'year']);
$controller->setSearchColumnsTest(['name']);
$controller->setSearchQueryTest(fn (Builder $q, string $search) => $q->orWhere('year', $search));

$data = $controller->index()->getData();
$this->assertCount(1, $data['tableData']);
Expand Down

0 comments on commit 4fa57e7

Please sign in to comment.