Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Commands/DataTablesPurgeExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Yajra\DataTables\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class DataTablesPurgeExportCommand extends Command
{
Expand All @@ -28,10 +31,14 @@ class DataTablesPurgeExportCommand extends Command
*/
public function handle()
{
collect(Storage::listContents('exports'))
->each(function ($file) {
if ($file['timestamp'] < now()->subDay(config('datatables-export.purge.days'))->getTimestamp()) {
Storage::delete($file['path']);
$disk = config('datatables-export.disk', 'local');
$timestamp = now()->subDay(config('datatables-export.purge.days'))->getTimestamp();

collect(Storage::disk($disk)->files())
->each(function ($file) use ($timestamp, $disk) {
$path = Storage::disk($disk)->path($file);
if (File::lastModified($path) < $timestamp && Str::endsWith(strtolower($file), ['xlsx', 'csv'])) {
File::delete($path);
}
});

Expand Down
27 changes: 22 additions & 5 deletions src/Jobs/DataTableExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Yajra\DataTables\Exceptions\Exception;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

Expand Down Expand Up @@ -53,6 +56,9 @@ public function __construct(array $dataTable, array $request, $user = null)
* Execute the job.
*
* @return void
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
*/
public function handle()
{
Expand All @@ -71,19 +77,30 @@ public function handle()
$dataTable->skipPaging();

$type = Str::startsWith(request('exportType'), Type::CSV) ? Type::CSV : Type::XLSX;
$disk = config('datatables-export.disk', 'local');
$filename = $this->batchId.'.'.$type;

$path = Storage::disk($disk)->path($filename);

$writer = WriterEntityFactory::createWriter($type);
$writer->openToFile(storage_path('app/exports/' . $this->batchId . '.' . $type));
$writer->openToFile($path);

$columns = $oTable->html()->getColumns()->filter->exportable;
$writer->addRow(
WriterEntityFactory::createRowFromArray(
$columns->map(fn($column) => strip_tags($column['title']))->toArray()
$columns->map(fn ($column) => strip_tags($column['title']))->toArray()
)
);

foreach ($dataTable->getFilteredQuery()->lazy() as $row) {
if (config('datatables-export.method', 'lazy') === 'lazy') {
$query = $dataTable->getFilteredQuery()->lazy(config('datatables-export.chunk', 1000));
} else {
$query = $dataTable->getFilteredQuery()->cursor();
}

foreach ($query as $row) {
$cells = collect();
$columns->map(function (Column $column, $index) use ($row, $cells) {
$columns->map(function (Column $column) use ($row, $cells) {
$property = $column['data'];
$value = Arr::get($row, $property, '');

Expand Down Expand Up @@ -117,7 +134,7 @@ public function handle()
*/
protected function wantsDateFormat(Column $column): bool
{
if (!isset($column['exportFormat'])) {
if (! isset($column['exportFormat'])) {
return false;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Livewire/ExportButtonComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function updateExportProgress()

public function downloadExport()
{
return Storage::download('exports/'.$this->batchJobId.'.'.$this->getType(), $this->getFilename());
$disk = config('datatables-export.disk', 'local');

return Storage::disk($disk)->download($this->batchJobId.'.'.$this->getType(), $this->getFilename());
}

public function render()
Expand Down
67 changes: 58 additions & 9 deletions src/config/datatables-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,58 @@

return [

/**
* Default export format for date.
*/
/*
|--------------------------------------------------------------------------
| Method
|--------------------------------------------------------------------------
|
| Method to use to iterate with the query results.
| Options: lazy, cursor
|
| @link https://laravel.com/docs/eloquent#cursors
| @link https://laravel.com/docs/eloquent#chunking-using-lazy-collections
|
*/
'method' => 'lazy',

/*
|--------------------------------------------------------------------------
| Chunk Size
|--------------------------------------------------------------------------
|
| Chunk size to be used when using lazy method.
|
*/
'chunk' => 1000,

/*
|--------------------------------------------------------------------------
| Export filesystem disk
|--------------------------------------------------------------------------
|
| Export filesystem disk where generated files will be stored.
|
*/
'disk' => 'local',

/*
|--------------------------------------------------------------------------
| Default Date Format
|--------------------------------------------------------------------------
|
| Default export format for date.
|
*/
'default_date_format' => 'yyyy-mm-dd',

/**
* List of valid date formats to be used for auto-detection.
*/
/*
|--------------------------------------------------------------------------
| Valid Date Formats
|--------------------------------------------------------------------------
|
| List of valid date formats to be used for auto-detection.
|
*/
'date_formats' => [
'mm/dd/yyyy',
NumberFormat::FORMAT_DATE_DATETIME,
Expand All @@ -37,9 +81,14 @@
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
],

/**
* Purge all exported by purge.days old files.
*/
/*
|--------------------------------------------------------------------------
| Purge Options
|--------------------------------------------------------------------------
|
| Purge all exported by purge.days old files.
|
*/
'purge' => [
'days' => 1,
],
Expand Down