From 9578cf0a952969acf2399b0dd72f70fff5c9119f Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Mon, 2 Oct 2023 13:01:08 +0200 Subject: [PATCH 01/14] Fix where clause is ambiguous Fix "where clause is ambiguous" --- src/DataSource/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataSource/Builder.php b/src/DataSource/Builder.php index 8cce2c8c..270ad0bc 100644 --- a/src/DataSource/Builder.php +++ b/src/DataSource/Builder.php @@ -92,7 +92,7 @@ public function filter(): EloquentBuilder|QueryBuilder foreach ($column as $field => $value) { if (is_array($value) && $filterType === 'input_text') { foreach ($value as $arrayField => $arrayValue) { - $filter($query, $filters, $filterType, $arrayField, $arrayValue); + $filter($query, $filters, $filterType, $field . '.' . $arrayField, $arrayValue); } } else { $filter($query, $filters, $filterType, $field, $value); From 03441235d493788dcaeb413fc7123691f60b65ba Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Sat, 7 Oct 2023 17:45:05 +0200 Subject: [PATCH 02/14] 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 --- src/Components/Filters/FilterInputText.php | 4 ++-- src/Traits/HasFilter.php | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Components/Filters/FilterInputText.php b/src/Components/Filters/FilterInputText.php index d0d27fb9..187f9d0e 100644 --- a/src/Components/Filters/FilterInputText.php +++ b/src/Components/Filters/FilterInputText.php @@ -40,8 +40,8 @@ public static function getWireAttributes(string $field, string $title): array { return collect() ->put('selectAttributes', new ComponentAttributeBag([ - 'wire:model.blur' => 'filters.input_text_options.' . $field, - 'wire:input.blur' => 'filterInputTextOptions(\'' . $field . '\', $event.target.value)', + 'wire:model.live' => 'filters.input_text_options.' . $field, + 'wire:input.live' => 'filterInputTextOptions(\'' . $field . '\', $event.target.value)', ])) ->put('inputAttributes', new ComponentAttributeBag([ 'wire:model.live.debounce.700ms' => 'filters.input_text.' . $field, diff --git a/src/Traits/HasFilter.php b/src/Traits/HasFilter.php index db079b99..63d2cfb0 100644 --- a/src/Traits/HasFilter.php +++ b/src/Traits/HasFilter.php @@ -398,7 +398,13 @@ public function filterInputTextOptions(string $field, string $value): void if (in_array($value, ['is_empty', 'is_not_empty', 'is_null', 'is_not_null', 'is_blank', 'is_not_blank'])) { $this->enabledFilters[$field]['disabled'] = true; - $this->filters['input_text'][$field] = null; + + if (str($field)->contains('.')) { + $this->filters['input_text'][str($field)->before('.')->toString()][str($field)->after('.')->toString()] = null; + } + else { + $this->filters['input_text'][$field] = null; + } } if (blank($value)) { From f890ce9f17018bcda3c1264ccf5d591b948f7a5d Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Sat, 7 Oct 2023 18:03:16 +0200 Subject: [PATCH 03/14] Fix braces --- src/Traits/HasFilter.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Traits/HasFilter.php b/src/Traits/HasFilter.php index 63d2cfb0..fa82e117 100644 --- a/src/Traits/HasFilter.php +++ b/src/Traits/HasFilter.php @@ -401,8 +401,7 @@ public function filterInputTextOptions(string $field, string $value): void if (str($field)->contains('.')) { $this->filters['input_text'][str($field)->before('.')->toString()][str($field)->after('.')->toString()] = null; - } - else { + } else { $this->filters['input_text'][$field] = null; } } From 4b05ea1e06113b2a7104819ab55f4050b22c1638 Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 15:52:47 +0200 Subject: [PATCH 04/14] Add filters on export queues Missing filers on exporting on queues --- src/Jobs/ExportJob.php | 25 +++++++++++------ src/Traits/ExportableJob.php | 52 +++++++++++++++++++++++++++++------- src/Traits/WithExport.php | 4 +++ 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index dcfbbea4..78c0f24c 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -6,8 +6,10 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Support\Facades\DB; use Illuminate\Queue\{InteractsWithQueue, SerializesModels}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; +use PowerComponents\LivewirePowerGrid\ProcessDataSource; use PowerComponents\LivewirePowerGrid\Traits\ExportableJob; /** @codeCoverageIgnore */ @@ -35,6 +37,8 @@ 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 +46,23 @@ public function __construct( public function handle(): void { - /** @var Builder $query */ - $query = $this->componentTable->datasource(); - - $query = $query->offset($this->offset) - ->limit($this->limit) - ->get(); $exportable = new $this->exportableClass(); + $currentHiddenStates = collect($this->columns) + ->mapWithKeys(fn ($column) => [data_get($column, 'field') => data_get($column, 'hidden')]); + + $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()) /** @phpstan-ignore-next-line */ + ->setData($columnsWithHiddenState, $this->prepareToExport()) ->download([]); + } } diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index d686189b..cb03e6d0 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -2,9 +2,12 @@ namespace PowerComponents\LivewirePowerGrid\Traits; -use Illuminate\Database\Eloquent\Collection; -use Illuminate\Support\{Str, Stringable}; +use Illuminate\Database\Eloquent as Eloquent; +use Illuminate\Support\{Collection, Str, Stringable}; +use PowerComponents\LivewirePowerGrid\DataSource\Builder; use PowerComponents\LivewirePowerGrid\PowerGridComponent; +use PowerComponents\LivewirePowerGrid\ProcessDataSource; + /** @codeCoverageIgnore */ trait ExportableJob @@ -21,6 +24,8 @@ trait ExportableJob private int $limit; + private array $filters; + private function getFilename(): Stringable { return Str::of($this->fileName) @@ -28,16 +33,45 @@ private function getFilename(): Stringable ->replace('.csv', ''); } - private function transform(Collection $collection): Collection + /** + * @throws Exception + */ + public function prepareToExport(): Eloquent\Collection|Collection { - return $collection->transform(function ($row) { - $columns = $this->componentTable->addColumns()->columns; + $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn($datasource) => $datasource->get()); + + $inClause = $processDataSource->component->filtered; + + $processDataSource->component->filter = $this->filters; + $this->componentTable->filters = $this->filters ?? []; + + if ($processDataSource->isCollection) { + if ($inClause) { + $results = $processDataSource->get()->whereIn($this->componentTable->primaryKey, $inClause); - foreach ($columns as $key => $column) { - $row->{$key} = $column($row); + return $processDataSource->transform($results); } - return $row; - }); + return $processDataSource->transform($processDataSource->resolveCollection()); + } + + /** @phpstan-ignore-next-line */ + $currentTable = $processDataSource->component->currentTable; + + $sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField; + + $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 $processDataSource->transform($results); } } diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index 27e5bb54..6345960b 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -98,7 +98,10 @@ 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; @@ -117,6 +120,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): 'fileName' => $fileName, 'offset' => $offset, 'limit' => $limit, + 'filters' => $filters ]; $queues->push(new $this->exportableJobClass( From ef6cc050330d2846b0ef9df9df35568345416156 Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 16:38:31 +0200 Subject: [PATCH 05/14] Remove lines, not needed --- src/Jobs/ExportJob.php | 3 --- src/Traits/ExportableJob.php | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index 78c0f24c..3623a0e6 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -39,14 +39,12 @@ public function __construct( $this->limit = $params['limit']; $this->filters = $params['filters']; - /** @var PowerGridComponent $componentTable */ $this->componentTable = new $componentTable(); } public function handle(): void { - $exportable = new $this->exportableClass(); $currentHiddenStates = collect($this->columns) @@ -63,6 +61,5 @@ public function handle(): void ->fileName($this->getFilename()) /** @phpstan-ignore-next-line */ ->setData($columnsWithHiddenState, $this->prepareToExport()) ->download([]); - } } diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index cb03e6d0..51d2e2d6 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -33,9 +33,6 @@ private function getFilename(): Stringable ->replace('.csv', ''); } - /** - * @throws Exception - */ public function prepareToExport(): Eloquent\Collection|Collection { $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn($datasource) => $datasource->get()); From 611f8731aaefe12db6027d360f968b6a851cc68c Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 16:43:11 +0200 Subject: [PATCH 06/14] Remove and clean code --- src/Traits/ExportableJob.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 51d2e2d6..38eb92c7 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -39,7 +39,6 @@ public function prepareToExport(): Eloquent\Collection|Collection $inClause = $processDataSource->component->filtered; - $processDataSource->component->filter = $this->filters; $this->componentTable->filters = $this->filters ?? []; if ($processDataSource->isCollection) { From 0d016a2034158e82b9e99a593aed4fd3bb0e5411 Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 16:51:37 +0200 Subject: [PATCH 07/14] Remove collection --- src/Traits/ExportableJob.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 38eb92c7..1ade2f6d 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -37,20 +37,10 @@ public function prepareToExport(): Eloquent\Collection|Collection { $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn($datasource) => $datasource->get()); - $inClause = $processDataSource->component->filtered; + $inClause = $processDataSource?->component?->filtered ?? []; $this->componentTable->filters = $this->filters ?? []; - if ($processDataSource->isCollection) { - if ($inClause) { - $results = $processDataSource->get()->whereIn($this->componentTable->primaryKey, $inClause); - - return $processDataSource->transform($results); - } - - return $processDataSource->transform($processDataSource->resolveCollection()); - } - /** @phpstan-ignore-next-line */ $currentTable = $processDataSource->component->currentTable; From f129629fea0b2e91722410e27de45fbfab2d4f7f Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 16:54:46 +0200 Subject: [PATCH 08/14] FIx code --- src/Traits/ExportableJob.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 1ade2f6d..8cde528f 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -37,7 +37,7 @@ public function prepareToExport(): Eloquent\Collection|Collection { $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn($datasource) => $datasource->get()); - $inClause = $processDataSource?->component?->filtered ?? []; + $inClause = $processDataSource->component->filtered ?? []; $this->componentTable->filters = $this->filters ?? []; From 9fa37e0844770606b9c27ebb9c68bdb5d4ae7172 Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 17:22:26 +0200 Subject: [PATCH 09/14] Remove ignore line --- src/Jobs/ExportJob.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index 3623a0e6..d0f08a43 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -58,7 +58,7 @@ public function handle(): void /** @phpstan-ignore-next-line */ $exportable - ->fileName($this->getFilename()) /** @phpstan-ignore-next-line */ + ->fileName($this->getFilename()) ->setData($columnsWithHiddenState, $this->prepareToExport()) ->download([]); } From fc65e494bbf9d475b00a8431e1c4707beb8c971f Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 17:29:07 +0200 Subject: [PATCH 10/14] Remove unused import --- src/Jobs/ExportJob.php | 1 - src/Traits/ExportableJob.php | 1 - src/Traits/WithExport.php | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index d0f08a43..2b1eaa7a 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -9,7 +9,6 @@ use Illuminate\Support\Facades\DB; use Illuminate\Queue\{InteractsWithQueue, SerializesModels}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; -use PowerComponents\LivewirePowerGrid\ProcessDataSource; use PowerComponents\LivewirePowerGrid\Traits\ExportableJob; /** @codeCoverageIgnore */ diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 8cde528f..1456dfa4 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -8,7 +8,6 @@ use PowerComponents\LivewirePowerGrid\PowerGridComponent; use PowerComponents\LivewirePowerGrid\ProcessDataSource; - /** @codeCoverageIgnore */ trait ExportableJob { diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index 6345960b..3d732f62 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -120,7 +120,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): 'fileName' => $fileName, 'offset' => $offset, 'limit' => $limit, - 'filters' => $filters + 'filters' => $filters, ]; $queues->push(new $this->exportableJobClass( From 5bc9efc7bc5f8e9895634a40bc095e9a8824b74c Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 17:33:40 +0200 Subject: [PATCH 11/14] Pin fixes --- src/Jobs/ExportJob.php | 1 - src/Traits/ExportableJob.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index 2b1eaa7a..08701a1a 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Bus\Dispatchable; -use Illuminate\Support\Facades\DB; use Illuminate\Queue\{InteractsWithQueue, SerializesModels}; use PowerComponents\LivewirePowerGrid\PowerGridComponent; use PowerComponents\LivewirePowerGrid\Traits\ExportableJob; diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 1456dfa4..483c2863 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -32,7 +32,7 @@ private function getFilename(): Stringable ->replace('.csv', ''); } - public function prepareToExport(): Eloquent\Collection|Collection + private function prepareToExport(): Eloquent\Collection|Collection { $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn($datasource) => $datasource->get()); From fb9bbe50ddc1d4c10a0e3e08903c72539895bdbd Mon Sep 17 00:00:00 2001 From: Marius Odendaal Date: Wed, 11 Oct 2023 17:46:19 +0200 Subject: [PATCH 12/14] Add ignore line --- src/Traits/ExportableJob.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 483c2863..66b19fd8 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -34,10 +34,12 @@ private function getFilename(): Stringable private function prepareToExport(): Eloquent\Collection|Collection { + /** @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 */ From 439a02451ec8824c0574daa00c648942c3dca9c3 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Sat, 14 Oct 2023 09:38:54 -0300 Subject: [PATCH 13/14] fix export count logic --- src/Traits/WithExport.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index 3d732f62..9f6faacf 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -103,8 +103,8 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): $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; From dd762d3b478b66135170c1e3e0f612d4800a15ea Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Sat, 14 Oct 2023 09:39:34 -0300 Subject: [PATCH 14/14] pint --- src/Jobs/ExportJob.php | 1 - src/Traits/ExportableJob.php | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index 08701a1a..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; diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 66b19fd8..c94ebc12 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -5,8 +5,7 @@ use Illuminate\Database\Eloquent as Eloquent; use Illuminate\Support\{Collection, Str, Stringable}; use PowerComponents\LivewirePowerGrid\DataSource\Builder; -use PowerComponents\LivewirePowerGrid\PowerGridComponent; -use PowerComponents\LivewirePowerGrid\ProcessDataSource; +use PowerComponents\LivewirePowerGrid\{PowerGridComponent, ProcessDataSource}; /** @codeCoverageIgnore */ trait ExportableJob @@ -35,7 +34,7 @@ private function getFilename(): Stringable private function prepareToExport(): Eloquent\Collection|Collection { /** @phpstan-ignore-next-line */ - $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn($datasource) => $datasource->get()); + $processDataSource = tap(ProcessDataSource::fillData($this->componentTable), fn ($datasource) => $datasource->get()); $inClause = $processDataSource->component->filtered ?? []; @@ -49,7 +48,7 @@ private function prepareToExport(): Eloquent\Collection|Collection $results = $processDataSource->prepareDataSource() ->where( - fn($query) => Builder::make($query, $this->componentTable) + fn ($query) => Builder::make($query, $this->componentTable) ->filterContains() ->filter() )