From a93e0b07df493b7038adb25fbf3db8a53a60c5d6 Mon Sep 17 00:00:00 2001 From: Marius Odendaal <81690364+mariusdatakrag@users.noreply.github.com> Date: Sat, 14 Oct 2023 14:46:59 +0200 Subject: [PATCH] Add filters on export queues (#1205) * Fix where clause is ambiguous Fix "where clause is ambiguous" * Fix filtering when no filter input text is not needed When no filter input text is not needed and field has dot notation:. This filtering applies to : - is_empty - is_not_empty - is_null - is_not_null - is_blank - is_not_blank * Fix braces * Add filters on export queues Missing filers on exporting on queues * Remove lines, not needed * Remove and clean code * Remove collection * FIx code * Remove ignore line * Remove unused import * Pin fixes * Add ignore line * fix export count logic * pint --------- Co-authored-by: luanfreitasdev --- src/Jobs/ExportJob.php | 21 ++++++++++-------- src/Traits/ExportableJob.php | 42 ++++++++++++++++++++++++++---------- src/Traits/WithExport.php | 8 +++++-- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index dcfbbea4..c299af4d 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -4,7 +4,6 @@ use Illuminate\Bus\{Batchable, Queueable}; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\{InteractsWithQueue, SerializesModels}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; @@ -35,6 +34,7 @@ public function __construct( $this->fileName = $params['fileName']; $this->offset = $params['offset']; $this->limit = $params['limit']; + $this->filters = $params['filters']; /** @var PowerGridComponent $componentTable */ $this->componentTable = new $componentTable(); @@ -42,18 +42,21 @@ public function __construct( public function handle(): void { - /** @var Builder $query */ - $query = $this->componentTable->datasource(); + $exportable = new $this->exportableClass(); - $query = $query->offset($this->offset) - ->limit($this->limit) - ->get(); + $currentHiddenStates = collect($this->columns) + ->mapWithKeys(fn ($column) => [data_get($column, 'field') => data_get($column, 'hidden')]); - $exportable = new $this->exportableClass(); + $columnsWithHiddenState = array_map(function ($column) use ($currentHiddenStates) { + $column->hidden = $currentHiddenStates[$column->field]; + + return $column; + }, $this->componentTable->columns()); /** @phpstan-ignore-next-line */ - $exportable->fileName($this->getFilename()) - ->setData($this->columns, $this->transform($query)) + $exportable + ->fileName($this->getFilename()) + ->setData($columnsWithHiddenState, $this->prepareToExport()) ->download([]); } } diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index d686189b..c94ebc12 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -2,9 +2,10 @@ namespace PowerComponents\LivewirePowerGrid\Traits; -use Illuminate\Database\Eloquent\Collection; -use Illuminate\Support\{Str, Stringable}; -use PowerComponents\LivewirePowerGrid\PowerGridComponent; +use Illuminate\Database\Eloquent as Eloquent; +use Illuminate\Support\{Collection, Str, Stringable}; +use PowerComponents\LivewirePowerGrid\DataSource\Builder; +use PowerComponents\LivewirePowerGrid\{PowerGridComponent, ProcessDataSource}; /** @codeCoverageIgnore */ trait ExportableJob @@ -21,6 +22,8 @@ trait ExportableJob private int $limit; + private array $filters; + private function getFilename(): Stringable { return Str::of($this->fileName) @@ -28,16 +31,33 @@ private function getFilename(): Stringable ->replace('.csv', ''); } - private function transform(Collection $collection): Collection + private function prepareToExport(): Eloquent\Collection|Collection { - return $collection->transform(function ($row) { - $columns = $this->componentTable->addColumns()->columns; + /** @phpstan-ignore-next-line */ + $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn ($datasource) => $datasource->get()); + + $inClause = $processDataSource->component->filtered ?? []; + + /** @phpstan-ignore-next-line */ + $this->componentTable->filters = $this->filters ?? []; + + /** @phpstan-ignore-next-line */ + $currentTable = $processDataSource->component->currentTable; + + $sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField; - foreach ($columns as $key => $column) { - $row->{$key} = $column($row); - } + $results = $processDataSource->prepareDataSource() + ->where( + fn ($query) => Builder::make($query, $this->componentTable) + ->filterContains() + ->filter() + ) + ->when($inClause, function ($query, $inClause) use ($processDataSource) { + return $query->whereIn($processDataSource->component->primaryKey, $inClause); + }) + ->orderBy($sortField, $processDataSource->component->sortDirection) + ->get(); - return $row; - }); + return $processDataSource->transform($results); } } diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index 27e5bb54..9f6faacf 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -98,10 +98,13 @@ public function runOnQueue(string $exportFileType, string $exportType): bool private function putQueuesToBus(string $exportableClass, string $fileExtension): Collection { + $processDataSource = tap(ProcessDataSource::fillData($this), fn ($datasource) => $datasource->get()); + $this->exportedFiles = []; + $filters = $processDataSource?->component?->filters ?? []; $queues = collect([]); - $countQueue = $this->getQueuesCount(); - $perPage = $this->total / $countQueue; + $countQueue = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1; + $perPage = $this->total > $countQueue ? ($this->total / $countQueue) : 1; $offset = 0; $limit = $perPage; @@ -117,6 +120,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): 'fileName' => $fileName, 'offset' => $offset, 'limit' => $limit, + 'filters' => $filters, ]; $queues->push(new $this->exportableJobClass(