Skip to content

Commit

Permalink
Merge pull request #187 from ploi-deploy/improve-relevant-search
Browse files Browse the repository at this point in the history
Improve relevant search
  • Loading branch information
Cannonb4ll authored Nov 1, 2022
2 parents 43c3153 + a490c94 commit ab9e732
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 286 deletions.
6 changes: 6 additions & 0 deletions app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ protected function getFormSchema(): array
->label('Custom header script')
->helperText('This allows you to add your own custom widget, or tracking tool. Code inside here will always be placed inside the head section.')
->columnSpan(2),
]),
Tabs\Tab::make('Excluded search words')
->schema([
TagsInput::make('excluded_matching_search_words')
->placeholder('New excluded word')
->helperText('Define any words here that should be excluded when users create a new item, you can also add words in your own language here to be excluded. Defining words here will increase the search results when a user starts creating an item, to prevent duplicates.')
])
])
->columns()
Expand Down
29 changes: 18 additions & 11 deletions app/Http/Livewire/Modals/Item/CreateItemModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class CreateItemModal extends ModalComponent implements HasForms
{
use InteractsWithForms, CanNotify;

public $similarItems = [];
public $similarItems;

public function mount()
{
$this->form->fill([]);
$this->similarItems = [];
$this->similarItems = collect([]);
}

public function hydrate()
Expand Down Expand Up @@ -68,8 +68,8 @@ protected function getFormSchema(): array
if (app(GeneralSettings::class)->select_board_when_creating_item) {
$inputs[] = Select::make('board_id')
->label(trans('table.board'))
->visible(fn ($get) => $get('project_id'))
->options(fn ($get) => Project::find($get('project_id'))->boards()->pluck('title', 'id'))
->visible(fn($get) => $get('project_id'))
->options(fn($get) => Project::find($get('project_id'))->boards()->pluck('title', 'id'))
->required(app(GeneralSettings::class)->board_required_when_creating_item);
}

Expand Down Expand Up @@ -137,15 +137,22 @@ public function setSimilarItems($state): void
//
// Common words example: the, it, that, when, how, this, true, false, is, not, well, with, use, enable, of, for
// ^ These are words you don't want to search on in your database and exclude from the array.
$words = array_filter(explode(' ', $state));
$words = collect(explode(' ', $state))->filter(function ($item) {
$excludedWords = app(GeneralSettings::class)->excluded_matching_search_words;

$this->similarItems = $state ? Item::query()->visibleForCurrentUser()->where(function ($query) use ($words) {
foreach ($words as $word) {
$query->orWhere('title', 'like', '%' . $word . '%');
}
return !in_array($item, $excludedWords);
});

$this->similarItems = $state ? Item::query()
->visibleForCurrentUser()
->where(function ($query) use ($words) {
foreach ($words as $word) {
$query->orWhere('title', 'like', '%' . $word . '%');
}

return $query;
})->get(['title', 'slug']) : collect([]);

return $query;
})->get(['title', 'slug']) : [];
}

public function render()
Expand Down
1 change: 1 addition & 0 deletions app/Settings/GeneralSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GeneralSettings extends Settings
public bool $show_changelog_author;
public bool $show_changelog_related_items;
public bool $disable_file_uploads;
public array $excluded_matching_search_words;

public function getInboxWorkflow(): InboxWorkflow
{
Expand Down
Loading

0 comments on commit ab9e732

Please sign in to comment.