From 0d444819e9f23a37f918503543bdb8c2adbc83e5 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Mon, 23 Dec 2024 12:16:37 -0300 Subject: [PATCH] WIP --- src/Jobs/ExportJob.php | 5 ++--- src/Traits/ExportableJob.php | 14 +++++++------- src/Traits/WithExport.php | 17 +++++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index e4e4782f..9c999f6f 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -32,6 +32,7 @@ public function __construct( $this->fileName = $params['fileName']; $this->offset = $params['offset']; $this->limit = $params['limit']; + $this->filtered = $params['filtered']; $this->filters = (array) Crypt::decrypt($params['filters']); $this->properties = (array) Crypt::decrypt($params['parameters']); @@ -41,8 +42,6 @@ public function __construct( public function handle(): void { - $exportable = new $this->exportableClass(); - $currentHiddenStates = collect($this->columns) ->mapWithKeys(fn ($column) => [data_get($column, 'field') => data_get($column, 'hidden')]); @@ -53,7 +52,7 @@ public function handle(): void }, $this->componentTable->columns()); /** @phpstan-ignore-next-line */ - $exportable + (new $this->exportableClass()) ->fileName($this->getFilename()) ->setData($columnsWithHiddenState, $this->prepareToExport($this->properties)) ->download([]); diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index a41af52f..56b09b7d 100644 --- a/src/Traits/ExportableJob.php +++ b/src/Traits/ExportableJob.php @@ -26,6 +26,8 @@ trait ExportableJob private array $filters; + private array $filtered; + private function getFilename(): Stringable { return Str::of($this->fileName) @@ -35,18 +37,16 @@ private function getFilename(): Stringable private function prepareToExport(array $properties = []): Eloquent\Collection|Collection { - /** @phpstan-ignore-next-line */ - $this->componentTable->filters = $this->filters ?? []; + $this->componentTable->filters = $this->filters ?? []; + $this->componentTable->filtered = $this->filtered ?? []; - /** @phpstan-ignore-next-line */ $processDataSource = tap( ProcessDataSource::make($this->componentTable, $properties), fn ($datasource) => $datasource->get() ); - $inClause = $processDataSource->component->filtered ?? []; + $filtered = $processDataSource->component->filtered ?? []; - /** @phpstan-ignore-next-line */ $currentTable = $processDataSource->component->currentTable; $sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField; @@ -57,8 +57,8 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co ->filterContains() ->filter() ) - ->when($inClause, function ($query, $inClause) use ($processDataSource) { - return $query->whereIn($processDataSource->component->primaryKey, $inClause); + ->when($filtered, function ($query, $filtered) use ($processDataSource) { + return $query->whereIn($processDataSource->component->primaryKey, $filtered); }) ->offset($this->offset) ->limit($this->limit) diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index 374c4ffc..72388bb2 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -106,6 +106,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): $this->exportedFiles = []; $filters = $processDataSource?->component?->filters ?? []; + $filtered = $processDataSource?->component?->filtered ?? []; $queues = collect([]); $queueCount = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1; $perPage = $this->total > $queueCount ? ($this->total / $queueCount) : 1; @@ -120,6 +121,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): '.' . $fileExtension; $params = [ + 'filtered' => $filtered, 'exportableClass' => $exportableClass, 'fileName' => $fileName, 'offset' => $offset, @@ -166,16 +168,16 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup { $processDataSource = tap(ProcessDataSource::make($this), fn ($datasource) => $datasource->get()); - $inClause = $processDataSource->component->filtered; + $filtered = $processDataSource->component->filtered; if ($selected && filled($processDataSource->component->checkboxValues)) { - $inClause = $processDataSource->component->checkboxValues; + $filtered = $processDataSource->component->checkboxValues; } if ($processDataSource->component->datasource() instanceof Collection) { - if ($inClause) { + if ($filtered) { $results = $processDataSource->get(isExport: true) - ->whereIn($this->primaryKey, $inClause); + ->whereIn($this->primaryKey, $filtered); return DataSourceBase::transform($results, $this); } @@ -194,8 +196,8 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup ->filterContains() ->filter() ) - ->when($inClause, function ($query, $inClause) use ($processDataSource) { - return $query->whereIn($processDataSource->component->primaryKey, $inClause); + ->when($filtered, function ($query, $filtered) use ($processDataSource) { + return $query->whereIn($processDataSource->component->primaryKey, $filtered); }) ->orderBy($sortField, $processDataSource->component->sortDirection) ->get(); @@ -244,10 +246,9 @@ private function export(string $exportType, bool $selected): BinaryFileResponse| /** @var string $fileName */ $fileName = data_get($this->setUp, 'exportable.fileName'); $exportable - ->fileName($fileName) /** @phpstan-ignore-next-line */ + ->fileName($fileName) ->setData($columnsWithHiddenState, $this->prepareToExport($selected)); - /** @phpstan-ignore-next-line */ return $exportable->download( exportOptions: $this->setUp['exportable'] );