|
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\NovaRequest; |
|
14
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
15
|
|
|
use Maatwebsite\LaravelNovaExcel\Concerns\WithDisk; |
|
16
|
|
|
use Maatwebsite\LaravelNovaExcel\Interactions\AskForFilename; |
|
17
|
|
|
use Maatwebsite\LaravelNovaExcel\Interactions\AskForWriterType; |
|
18
|
|
|
use NovaExportConfiguration\Export\CustomExport; |
|
19
|
|
|
use NovaExportConfiguration\Models\ExportStoredFile; |
|
20
|
|
|
use NovaExportConfiguration\NovaExportConfig; |
|
21
|
|
|
|
|
22
|
|
|
class CustomFileExports extends Action |
|
23
|
|
|
{ |
|
24
|
|
|
use InteractsWithQueue, Queueable; |
|
25
|
|
|
use AskForFilename, |
|
26
|
|
|
AskForWriterType, |
|
27
|
|
|
WithDisk, |
|
28
|
|
|
WithQueue; |
|
29
|
|
|
|
|
30
|
|
|
public $standalone = true; |
|
31
|
|
|
|
|
32
|
|
|
public $showOnIndex = true; |
|
33
|
|
|
|
|
34
|
|
|
public $showInline = false; |
|
35
|
|
|
|
|
36
|
|
|
public $showOnDetail = false; |
|
37
|
|
|
|
|
38
|
|
|
protected $actionFields = []; |
|
39
|
|
|
|
|
40
|
|
|
protected array $exportsList = []; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(array $exportsList = []) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->exportsList = $exportsList; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function exportsList(array $exportsList = []): static |
|
48
|
|
|
{ |
|
49
|
|
|
$this->exportsList = $exportsList; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public function name() |
|
56
|
|
|
{ |
|
57
|
|
|
return __('Custom Exports'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function handle(ActionFields $fields, Collection $models) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
/** @var CustomExport $exportable */ |
|
63
|
|
|
$exportable = NovaExportConfig::customExportsByKey($fields->get('export')); |
|
64
|
|
|
if (!$exportable) { |
|
|
|
|
|
|
65
|
|
|
return Action::danger(__('Exportable config not found')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$writerType = $fields->get('writer_type'); |
|
69
|
|
|
$type = 'custom-export'; |
|
70
|
|
|
$name = $fields->get('filename', $exportable::name()) . '.' . Str::lower($writerType ?: 'xlsx'); |
|
71
|
|
|
$filename = date('Y/m/d/') . Str::uuid() . '.' . Str::lower($writerType ?: 'xlsx'); |
|
72
|
|
|
$disk = $this->getDisk()?: $exportable::diskName(); |
|
73
|
|
|
|
|
74
|
|
|
$response = Excel::store( |
|
75
|
|
|
$exportable, |
|
76
|
|
|
$filename, |
|
77
|
|
|
$disk, |
|
78
|
|
|
$writerType |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
if (false === $response) { |
|
82
|
|
|
return Action::danger(__('Resource could not be exported.')); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$dbExport = new ExportStoredFile(); |
|
86
|
|
|
$dbExport->type = $type; |
|
|
|
|
|
|
87
|
|
|
$dbExport->disk = $disk; |
|
|
|
|
|
|
88
|
|
|
$dbExport->path = $filename; |
|
|
|
|
|
|
89
|
|
|
$dbExport->name = $name; |
|
|
|
|
|
|
90
|
|
|
if ($user = Auth::user()) { |
|
91
|
|
|
$dbExport->meta->toMorph('author', $user); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$exportable->setFileModelData(serialize($dbExport)); |
|
95
|
|
|
|
|
96
|
|
|
if ($queueName = $this->getQueue($exportable::queueName())) { |
|
97
|
|
|
$exportable->queue( |
|
98
|
|
|
$filename, |
|
99
|
|
|
$disk, |
|
100
|
|
|
$writerType |
|
101
|
|
|
)->allOnQueue($queueName); |
|
102
|
|
|
} else { |
|
103
|
|
|
$exportable->store( |
|
104
|
|
|
$filename, |
|
105
|
|
|
$disk, |
|
106
|
|
|
$writerType |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return Action::message(__('Data exported to file.')); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function fields(NovaRequest $request) |
|
114
|
|
|
{ |
|
115
|
|
|
|
|
116
|
|
|
return array_merge([ |
|
117
|
|
|
Select::make('Export', 'export') |
|
118
|
|
|
->options($this->exportsList) |
|
119
|
|
|
->required() |
|
120
|
|
|
->displayUsingLabels(), |
|
121
|
|
|
], $this->actionFields); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.