Skip to content

Commit

Permalink
Merge pull request #2 from Tschucki/bugfixes/slow-searches
Browse files Browse the repository at this point in the history
Max Search Results & own field for searching
  • Loading branch information
Tschucki authored Feb 25, 2024
2 parents c5b5558 + af8cdda commit f4e38bb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@
This plugin lets you add workflows to your filament app. You can attach triggers and dispatchable actions to your
workflows. The plugin will automatically execute the actions when the trigger conditions are met.

## Table of Contents

- [Images](#images)
- [Installation](#installation)
- [Usage](#usage)
- [Basics](#basics)
- [Add the trait to your model](#add-the-trait-to-your-model)
- [Create an Action](#create-an-action)
- [Configuration](#configuration)
- [Define searchable field](#define-searchable-field)
- [Max Search Results](#max-search-results)
- [Testing](#testing)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Security Vulnerabilities](#security-vulnerabilities)
- [Credits](#credits)
- [License](#license)

## Images

![Screenshot 1](.github/images/Basic-Form.png)
Expand Down Expand Up @@ -58,7 +76,7 @@ class User extends Model {
}
```

### Create a Action
### Create an Action
In order to attach an action to your workflows, you will have to create a class within the `App\Jobs\Actions` folder. The class must extend the `BaseAction` class. This requires you to implement the `handle` method. This method will be called when the workflow is executed.

The action class is very similar to a job.
Expand Down Expand Up @@ -88,11 +106,58 @@ class TestAction extends BaseAction
{
\Log::info($this->user->name . ' was created at ' . $this->user->created_at);
}

// Will be later used in the Logs (coming soon)
public function getActionName(): string
{
return 'Der Hackfleisch hassender Zerhacker';
}

public function getActionDescription(): string
{
return 'Schneidet Kopfsalat. Und das nachts :)';
}

public function getActionCategory(): string
{
return 'Default-Category';
}

public function getActionIcon(): string
{
return 'heroicon-o-adjustments';
}
}
```

That's it. Now you can create and attach actions to your workflows.

## Configuration

### Define searchable field

If you don't just want to search for the `id`, you can use the function `getTitleColumnForWorkflowSearch` within your model to search in another field as well.

```php
public function getTitleColumnForWorkflowSearch(): ?string
{
return 'name';
}
```

### Max Search Results
In case you want to change the max search results for the models, you can publish the config file and change the `workflows.search.max_results` value (defaults to 100).
This can come in handy when you have a lot of models and the search is slow.

```php
<?php

return [
'search' => [
'max_results' => 100,
]
];
```

## Testing

Expand Down
4 changes: 3 additions & 1 deletion config/workflows.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

return [

'search' => [
'max_results' => 100,
],
];
19 changes: 16 additions & 3 deletions src/Concerns/CanSetupWorkflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Tschucki\FilamentWorkflows\Concerns;

use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Tschucki\FilamentWorkflows\WorkflowActions\BaseAction;

trait CanSetupWorkflows
Expand Down Expand Up @@ -96,14 +98,25 @@ public static function getActionOptions()
});
}

public static function getModelOptions(?Model $model)
public static function getModelOptions(?Model $model, string $search = ''): array
{
if (! $model) {
return [];
}

if (self::modelUsesTrait($model, InteractsWithWorkflow::class)) {
return $model::all()->mapWithKeys(function ($model, $key) {
$iMaxResults = config('workflows.search.results', 100);
$searchableColumn = $model->getTitleColumnForWorkflowSearch();

if (! empty($search)) {
return $model::where('id', 'LIKE', '%' . $search . '%')->when($searchableColumn !== null, fn (Builder $query) => $query->orWhere($searchableColumn, 'LIKE', '%' . $search . '%'))->take($iMaxResults)->get()->mapWithKeys(function ($model, $key) {
return [
$model->getKey() => $model->getTitelAttributeForWorkflow(),
];
});
}

return $model::take($iMaxResults)->get()->mapWithKeys(function ($model) {
return [
$model->getKey() => $model->getTitelAttributeForWorkflow(),
];
Expand All @@ -122,7 +135,7 @@ public static function getModelFields(?Model $model): array
if (self::modelUsesTrait($model, InteractsWithWorkflow::class)) {
$table = $model->getTable();

return collect(\Schema::getColumnListing($table))->mapWithKeys(function ($column) {
return collect(Schema::getColumnListing($table))->mapWithKeys(function ($column) {
return [
$column => $column,
];
Expand Down
5 changes: 5 additions & 0 deletions src/Concerns/InteractsWithWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public function workflowHasBeenSetup(): bool

return $workflowExists && $workflowIsEnabled && $workflowModelId;
}

public function getTitleColumnForWorkflowSearch(): ?string
{
return null;
}
}
2 changes: 1 addition & 1 deletion src/Resources/FilamentWorkflowResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function form(Form $form): Form
Forms\Components\Tabs::make()->schema([
Forms\Components\Tabs\Tab::make('Basic')->schema([
Forms\Components\Select::make('model_type')->searchable()->reactive()->options(self::getModelTypeOptions())->required(),
Forms\Components\Select::make('model_id')->searchable()->options(fn (Forms\Get $get) => $get('model_type') ? self::getModelOptions(app($get('model_type'))) : [])->nullable(),
Forms\Components\Select::make('model_id')->searchable()->getSearchResultsUsing(fn (Forms\Get $get, ?string $search) => $get('model_type') ? self::getModelOptions(app($get('model_type')), $search) : [])->nullable(),
Forms\Components\TextInput::make('title')->autofocus()->required(),
Forms\Components\Toggle::make('enabled')->inline(false)->default(true)->required(),
Forms\Components\Textarea::make('description')->nullable(),
Expand Down

0 comments on commit f4e38bb

Please sign in to comment.