1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaExportConfiguration\Nova\Actions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Laravel\Nova\Actions\Action; |
11
|
|
|
use Laravel\Nova\Fields\ActionFields; |
12
|
|
|
use Laravel\Nova\Fields\Select; |
13
|
|
|
use Laravel\Nova\Http\Requests\ActionRequest; |
14
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
15
|
|
|
use Maatwebsite\LaravelNovaExcel\Concerns\WithDisk; |
16
|
|
|
use Maatwebsite\LaravelNovaExcel\Concerns\WithFilename; |
17
|
|
|
use Maatwebsite\LaravelNovaExcel\Concerns\WithWriterType; |
18
|
|
|
use Maatwebsite\LaravelNovaExcel\Interactions\AskForFilename; |
19
|
|
|
use Maatwebsite\LaravelNovaExcel\Interactions\AskForWriterType; |
20
|
|
|
use NovaExportConfiguration\Export\CustomExport; |
21
|
|
|
use NovaExportConfiguration\Models\ExportStoredFile; |
22
|
|
|
use NovaExportConfiguration\NovaExportConfig; |
23
|
|
|
|
24
|
|
|
class CustomFileExports extends Action |
25
|
|
|
{ |
26
|
|
|
use InteractsWithQueue, Queueable; |
27
|
|
|
use AskForFilename, |
28
|
|
|
AskForWriterType, |
29
|
|
|
WithDisk, |
30
|
|
|
WithFilename, |
31
|
|
|
WithWriterType, |
32
|
|
|
WithQueue; |
33
|
|
|
|
34
|
|
|
public $standalone = true; |
35
|
|
|
|
36
|
|
|
public $showOnIndex = true; |
37
|
|
|
|
38
|
|
|
public $showInline = false; |
39
|
|
|
|
40
|
|
|
public $showOnDetail = false; |
41
|
|
|
|
42
|
|
|
protected $actionFields = []; |
43
|
|
|
|
44
|
|
|
protected array $exportsList = []; |
45
|
|
|
|
46
|
|
|
public function __construct(array $exportsList = []) |
47
|
|
|
{ |
48
|
|
|
$this->exportsList = $exportsList; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function exportsList(array $exportsList = []): static |
52
|
|
|
{ |
53
|
|
|
$this->exportsList = $exportsList; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
public function name() |
60
|
|
|
{ |
61
|
|
|
return __('Custom Exports'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function handleRequest(ActionRequest $request) |
65
|
|
|
{ |
66
|
|
|
$this->handleWriterType($request); |
67
|
|
|
$this->handleFilename($request); |
68
|
|
|
|
69
|
|
|
return parent::handleRequest($request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function handle(ActionFields $fields, Collection $models) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$exportName = $fields->get('export'); |
75
|
|
|
|
76
|
|
|
if (!$exportName) { |
77
|
|
|
return Action::danger(__('Export not selected.')); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @var CustomExport $exportable */ |
81
|
|
|
$exportable = NovaExportConfig::customExportsByKey($exportName); |
82
|
|
|
if (!$exportable) { |
|
|
|
|
83
|
|
|
return Action::danger(__('Exportable config not found')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$writerType = $fields->get('writer_type'); |
87
|
|
|
|
88
|
|
|
$dbExport = ExportStoredFile::init( |
89
|
|
|
'custom-export', |
90
|
|
|
$this->getDisk() ?: $exportable::diskName(), |
|
|
|
|
91
|
|
|
date('Y/m/d/') . Str::uuid() . '.' . $this->getDefaultExtension(), |
92
|
|
|
$this->getFilename(), |
|
|
|
|
93
|
|
|
function ($file) { |
94
|
|
|
if ($user = Auth::user()) { |
95
|
|
|
$file->meta->toMorph('author', $user); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
$exportable->useStoreFile($dbExport); |
102
|
|
|
|
103
|
|
|
if ($queueName = $this->getQueue($exportable::queueName())) { |
104
|
|
|
$exportable->queue( |
105
|
|
|
$dbExport->path, |
106
|
|
|
$dbExport->disk, |
107
|
|
|
$writerType |
108
|
|
|
)->allOnQueue($queueName); |
109
|
|
|
|
110
|
|
|
return Action::message(__('Request added to queue. Please wait a while to complete it.')); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$exportable->store( |
114
|
|
|
$dbExport->path, |
115
|
|
|
$dbExport->disk, |
116
|
|
|
$writerType |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return Action::message(__('Data exported to file.')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function fields(NovaRequest $request) |
123
|
|
|
{ |
124
|
|
|
|
125
|
|
|
return array_merge([ |
126
|
|
|
Select::make('Export', 'export') |
127
|
|
|
->options($this->exportsList) |
128
|
|
|
->required() |
129
|
|
|
->displayUsingLabels() |
130
|
|
|
->rules(['required']), |
131
|
|
|
], $this->actionFields); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function getDefaultExtension(): string |
135
|
|
|
{ |
136
|
|
|
return $this->getWriterType() ? strtolower($this->getWriterType()) : 'xlsx'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.