|
@@ -2,93 +2,40 @@
|
|
|
|
|
|
namespace Dcat\Admin\Grid\Exporters;
|
|
|
|
|
|
-use Illuminate\Contracts\Support\Arrayable;
|
|
|
-use Illuminate\Support\Arr;
|
|
|
-use Illuminate\Support\Str;
|
|
|
+use Dcat\EasyExcel\Excel;
|
|
|
+use Dcat\Admin\Grid;
|
|
|
|
|
|
class CsvExporter extends AbstractExporter
|
|
|
{
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $extension = 'csv';
|
|
|
+
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
public function export()
|
|
|
{
|
|
|
- $filename = $this->getFilename().'.csv';
|
|
|
-
|
|
|
- $headers = [
|
|
|
- 'Content-Encoding' => 'UTF-8',
|
|
|
- 'Content-Type' => 'text/csv;charset=UTF-8',
|
|
|
- 'Content-Disposition' => "attachment; filename=\"$filename\"",
|
|
|
- ];
|
|
|
-
|
|
|
- response()->stream(function () {
|
|
|
- $handle = fopen('php://output', 'w');
|
|
|
-
|
|
|
- $titles = $this->titles;
|
|
|
-
|
|
|
- if ($titles) {
|
|
|
- // Add CSV headers
|
|
|
- fputcsv($handle, $titles);
|
|
|
- }
|
|
|
-
|
|
|
- $records = $this->getData();
|
|
|
-
|
|
|
- if ($this->builder) {
|
|
|
- $records = $this->builder->call($records);
|
|
|
- }
|
|
|
+ if (! class_exists(Excel::class)) {
|
|
|
+ throw new \Exception('To use exporter, please install [dcat/easy-excel] first.');
|
|
|
+ }
|
|
|
|
|
|
- if (empty($titles)) {
|
|
|
- $titles = $this->getHeaderRowFromRecords($records);
|
|
|
+ $filename = $this->getFilename().'.'.$this->extension;
|
|
|
|
|
|
- // Add CSV headers
|
|
|
- fputcsv($handle, $titles);
|
|
|
- }
|
|
|
+ $exporter = Excel::export();
|
|
|
|
|
|
- foreach ($records as $record) {
|
|
|
- fputcsv($handle, $this->getFormattedRecord($titles, $record));
|
|
|
- }
|
|
|
+ if ($this->scope === Grid\Exporter::SCOPE_ALL) {
|
|
|
+ $exporter->chunk(function (int $times) {
|
|
|
+ return $this->buildData($times);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ $exporter->data($this->buildData());
|
|
|
+ }
|
|
|
|
|
|
- // Close the output stream
|
|
|
- fclose($handle);
|
|
|
- }, 200, $headers)->send();
|
|
|
+ $exporter->headings($this->titles)->download($filename);
|
|
|
|
|
|
exit;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param array $records
|
|
|
- *
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function getHeaderRowFromRecords(array $records): array
|
|
|
- {
|
|
|
- $titles = [];
|
|
|
-
|
|
|
- collect(Arr::dot($records[0] ?? []))->keys()->map(
|
|
|
- function ($key) use(&$titles) {
|
|
|
- if (Str::contains($key, '.')) return;
|
|
|
-
|
|
|
- $titles[$key] = Str::ucfirst($key);
|
|
|
- }
|
|
|
- );
|
|
|
-
|
|
|
- return $titles;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param $titles
|
|
|
- * @param $record
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function getFormattedRecord($titles, $record)
|
|
|
- {
|
|
|
- $result = [];
|
|
|
-
|
|
|
- $record = Arr::dot($record);
|
|
|
- foreach ($titles as $k => $label) {
|
|
|
- $result[] = $record[$k] ?? '';
|
|
|
- }
|
|
|
-
|
|
|
- return $result;
|
|
|
- }
|
|
|
}
|