diff --git a/src/DataSource/Builder.php b/src/DataSource/Builder.php index 09714521..adfd0a61 100644 --- a/src/DataSource/Builder.php +++ b/src/DataSource/Builder.php @@ -39,7 +39,12 @@ public static function make( public function filter(): EloquentBuilder|QueryBuilder { - $filters = collect($this->component->filters()); + // To make it work on export, we need to use ->filters instead of filters() + $filters = collect( + app()->runningInConsole() && !app()->runningUnitTests() + ? $this->component->filters + : $this->component->filters() + ); if ($filters->isEmpty()) { return $this->query; @@ -223,7 +228,7 @@ private function filterNestedRelation(string $table, array $columns, string $sea }); } } catch (RelationNotFoundException $e) { - $query->leftJoin($nestedTable, "$table.$nestedTable" . "_id", '=', "$nestedTable.id") + $query->leftJoin($nestedTable, "$table.$nestedTable" . '_id', '=', "$nestedTable.id") ->orWhere(function (EloquentBuilder $query) use ($nestedTable, $nestedColumns, $search) { foreach ($nestedColumns as $nestedColumn) { $search = $this->getBeforeSearchMethod($nestedColumn, $search); diff --git a/src/DataSource/Processors/DataSourceBase.php b/src/DataSource/Processors/DataSourceBase.php index f37113b2..d6bbcc11 100644 --- a/src/DataSource/Processors/DataSourceBase.php +++ b/src/DataSource/Processors/DataSourceBase.php @@ -133,7 +133,7 @@ private static function processRows(BaseCollection $results, PowerGridComponent $row = (object) $row; $data = $fields->map(fn ($field) => $field($row, $index)); - $rowId = data_get($row, $component->realPrimaryKey); + $rowId = data_get($row, $component->primaryKeyAlias ?? $component->primaryKey); if ($renderActions) { try { diff --git a/src/Jobs/ExportJob.php b/src/Jobs/ExportJob.php index 40d6ba10..9c999f6f 100644 --- a/src/Jobs/ExportJob.php +++ b/src/Jobs/ExportJob.php @@ -15,28 +15,24 @@ class ExportJob implements ShouldQueue { use Batchable; use Dispatchable; + use ExportableJob; use InteractsWithQueue; use Queueable; use SerializesModels; - use ExportableJob; private array $properties; - /** - * @param string $componentTable - * @param array $columns - * @param array $params - */ public function __construct( string $componentTable, - array $columns, - array $params + array $columns, + array $params ) { $this->columns = $columns; $this->exportableClass = $params['exportableClass']; $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']); @@ -46,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')]); @@ -58,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/PowerGridComponent.php b/src/PowerGridComponent.php index 8ff215f5..86184a54 100644 --- a/src/PowerGridComponent.php +++ b/src/PowerGridComponent.php @@ -80,7 +80,7 @@ public function updatedPage(): void { $this->checkboxAll = false; - if ($this->hasLazyEnabled) { + if (!app()->runningInConsole() && $this->hasLazyEnabled) { $this->additionalCacheKey = uniqid(); data_set($this->setUp, 'lazy.items', 0); @@ -95,7 +95,7 @@ public function updatedSearch(): void { $this->gotoPage(1, data_get($this->setUp, 'footer.pageName')); - if ($this->hasLazyEnabled) { + if (!app()->runningInConsole() && $this->hasLazyEnabled) { $this->additionalCacheKey = uniqid(); data_set($this->setUp, 'lazy.items', 0); diff --git a/src/Traits/ExportableJob.php b/src/Traits/ExportableJob.php index 604b067d..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,28 +37,31 @@ private function getFilename(): Stringable private function prepareToExport(array $properties = []): Eloquent\Collection|Collection { - /** @phpstan-ignore-next-line */ - $processDataSource = tap(ProcessDataSource::make($this->componentTable, $properties), fn ($datasource) => $datasource->get()); + $this->componentTable->filters = $this->filters ?? []; + $this->componentTable->filtered = $this->filtered ?? []; - $inClause = $processDataSource->component->filtered ?? []; + $processDataSource = tap( + ProcessDataSource::make($this->componentTable, $properties), + fn ($datasource) => $datasource->get() + ); - /** @phpstan-ignore-next-line */ - $this->componentTable->filters = $this->filters ?? []; + $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; - $results = $processDataSource->prepareDataSource() // @phpstan-ignore-line + $results = $this->componentTable->datasource($this->properties ?? []) // @phpstan-ignore-line ->where( fn ($query) => Builder::make($query, $this->componentTable) ->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) ->orderBy($sortField, $processDataSource->component->sortDirection) ->get(); diff --git a/src/Traits/WithExport.php b/src/Traits/WithExport.php index e55801b2..72388bb2 100644 --- a/src/Traits/WithExport.php +++ b/src/Traits/WithExport.php @@ -54,17 +54,19 @@ public function getExportBatchProperty(): ?Batch public function updateExportProgress(): void { - if (!is_null($this->exportBatch)) { - $this->batchFinished = $this->exportBatch->finished(); - $this->batchProgress = $this->exportBatch->progress(); - $this->batchErrors = $this->exportBatch->hasFailures(); + if (is_null($this->exportBatch)) { + return; + } - if ($this->batchFinished) { - $this->batchExporting = false; - } + $this->batchFinished = $this->exportBatch->finished(); + $this->batchProgress = $this->exportBatch->progress(); + $this->batchErrors = $this->exportBatch->hasFailures(); - $this->onBatchExecuting($this->exportBatch); + if ($this->batchFinished) { + $this->batchExporting = false; } + + $this->onBatchExecuting($this->exportBatch); } public function downloadExport(string $file): BinaryFileResponse @@ -104,20 +106,22 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension): $this->exportedFiles = []; $filters = $processDataSource?->component?->filters ?? []; + $filtered = $processDataSource?->component?->filtered ?? []; $queues = collect([]); - $countQueue = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1; - $perPage = $this->total > $countQueue ? ($this->total / $countQueue) : 1; + $queueCount = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1; + $perPage = $this->total > $queueCount ? ($this->total / $queueCount) : 1; $offset = 0; $limit = $perPage; - for ($i = 1; $i < ($countQueue + 1); $i++) { - $fileName = 'powergrid-' . Str::kebab(strval(data_get($this->setUp, 'exportable.fileName'))) . + for ($i = 1; $i < ($queueCount + 1); $i++) { + $fileName = Str::kebab(strval(data_get($this->setUp, 'exportable.fileName'))) . '-' . round(($offset + 1), 2) . '-' . round($limit, 2) . '-' . $this->getId() . '.' . $fileExtension; $params = [ + 'filtered' => $filtered, 'exportableClass' => $exportableClass, 'fileName' => $fileName, 'offset' => $offset, @@ -164,15 +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) { - $results = $processDataSource->get(isExport: true)->whereIn($this->primaryKey, $inClause); + if ($filtered) { + $results = $processDataSource->get(isExport: true) + ->whereIn($this->primaryKey, $filtered); return DataSourceBase::transform($results, $this); } @@ -191,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(); @@ -241,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'] );