Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filters on export queues #1205

Merged
merged 15 commits into from
Oct 14, 2023
21 changes: 12 additions & 9 deletions src/Jobs/ExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,25 +34,29 @@ 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();
}

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([]);
}
}
42 changes: 31 additions & 11 deletions src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,23 +22,42 @@ trait ExportableJob

private int $limit;

private array $filters;

private function getFilename(): Stringable
{
return Str::of($this->fileName)
->replace('.xlsx', '')
->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);
}
}
8 changes: 6 additions & 2 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -117,6 +120,7 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension):
'fileName' => $fileName,
'offset' => $offset,
'limit' => $limit,
'filters' => $filters,
];

$queues->push(new $this->exportableJobClass(
Expand Down
Loading