Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 29 additions & 14 deletions src/Jobs/DataTableExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Yajra\DataTables\Jobs;

use OpenSpout\Common\Helper\CellTypeHelper;
use OpenSpout\Common\Type;
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use Carbon\Carbon;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
Expand All @@ -16,11 +12,13 @@
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 OpenSpout\Common\Helper\CellTypeHelper;
use OpenSpout\Common\Type;
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Yajra\DataTables\Exceptions\Exception;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

Expand All @@ -40,9 +38,9 @@ class DataTableExportJob implements ShouldQueue, ShouldBeUnique
/**
* Create a new job instance.
*
* @param array $dataTable
* @param array $request
* @param null $user
* @param array $dataTable
* @param array $request
* @param null $user
*/
public function __construct(array $dataTable, array $request, $user = null)
{
Expand Down Expand Up @@ -78,7 +76,7 @@ public function handle()

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

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

Expand All @@ -88,7 +86,7 @@ public function handle()
$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()
)
);

Expand All @@ -112,12 +110,16 @@ public function handle()
$cells->push(
WriterEntityFactory::createCell($date, (new StyleBuilder)->setFormat($format)->build())
);
} else if ($this->wantsText($column)) {
$cells->push(
WriterEntityFactory::createCell($value, (new StyleBuilder)->setFormat($column['exportFormat'])->build())
);
} else {
$format = $column['exportFormat']
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
: null;

$value = $this->isNumeric($value) ? (float) $value : $value;
$value = $this->isNumeric($value) ? (float)$value : $value;

$cells->push(WriterEntityFactory::createCell($value, $format));
}
Expand All @@ -129,7 +131,7 @@ public function handle()
}

/**
* @param \Yajra\DataTables\Html\Column $column
* @param \Yajra\DataTables\Html\Column $column
* @return bool
*/
protected function wantsDateFormat(Column $column): bool
Expand All @@ -142,7 +144,7 @@ protected function wantsDateFormat(Column $column): bool
}

/**
* @param mixed $value
* @param mixed $value
* @return bool
*/
protected function isNumeric($value): bool
Expand All @@ -154,4 +156,17 @@ protected function isNumeric($value): bool

return is_numeric($value);
}

/**
* @param \Yajra\DataTables\Html\Column $column
* @return bool
*/
protected function wantsText(Column $column): bool
{
if (! isset($column['exportFormat'])) {
return false;
}

return in_array($column['exportFormat'], config('datatables-export.text_formats', []));
}
}
9 changes: 9 additions & 0 deletions src/config/datatables-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
],

/*
* List of valid text formats to be used.
*/
'text_formats' => [
'@',
NumberFormat::FORMAT_GENERAL,
NumberFormat::FORMAT_TEXT
],

/*
|--------------------------------------------------------------------------
| Purge Options
Expand Down