|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaExportConfiguration\Nova\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Laravel\Nova\Actions\Action; |
|
8
|
|
|
use Laravel\Nova\Http\Requests\ActionRequest; |
|
9
|
|
|
use NovaExportConfiguration\Models\ExportStoredFile; |
|
10
|
|
|
use NovaExportConfiguration\NovaExportConfig; |
|
11
|
|
|
|
|
12
|
|
|
class ConfiguredExportToExcelAction extends \Maatwebsite\LaravelNovaExcel\Actions\ExportToExcel |
|
13
|
|
|
{ |
|
14
|
|
|
use WithQueue; |
|
15
|
|
|
|
|
16
|
|
|
public $showOnIndex = false; |
|
17
|
|
|
|
|
18
|
|
|
public $showInline = true; |
|
19
|
|
|
|
|
20
|
|
|
public $showOnDetail = true; |
|
21
|
|
|
|
|
22
|
|
|
public function name() |
|
23
|
|
|
{ |
|
24
|
|
|
return __('Export Config'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function withDefaultFilename(ActionRequest $request) |
|
28
|
|
|
{ |
|
29
|
|
|
$model = $request->findModelOrFail($request->resources); |
|
30
|
|
|
$this->withFilename(Str::kebab($model->name).date('-Ymd').'.'.$this->getDefaultExtension()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function handle(ActionRequest $request, Action $exportable): array |
|
35
|
|
|
{ |
|
36
|
|
|
/** @var \NovaExportConfiguration\Models\ExportConfig $model */ |
|
37
|
|
|
$model = $request->findModelOrFail($request->resources); |
|
38
|
|
|
|
|
39
|
|
|
$repo = NovaExportConfig::getRepositories()->getByName($model->type); |
|
|
|
|
|
|
40
|
|
|
if (!$repo) { |
|
41
|
|
|
return Action::danger(__('Export repository not found.')); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
$resource = $request->resource(); |
|
44
|
|
|
$type = $resource::uriKey(); |
|
45
|
|
|
$name = $this->getFilename(); |
|
46
|
|
|
$filename = date('Y/m/d/').Str::uuid().'.'.$this->getDefaultExtension(); |
|
47
|
|
|
$disk = $this->getDisk() ?: $repo->disk(); |
|
48
|
|
|
$filters = $request->filters; |
|
49
|
|
|
$fields = $request->resolveFields(); |
|
50
|
|
|
|
|
51
|
|
|
$dbExport = new ExportStoredFile(); |
|
52
|
|
|
$dbExport->type = $type; |
|
|
|
|
|
|
53
|
|
|
$dbExport->disk = $disk; |
|
|
|
|
|
|
54
|
|
|
$dbExport->path = $filename; |
|
|
|
|
|
|
55
|
|
|
$dbExport->name = $name; |
|
|
|
|
|
|
56
|
|
|
$dbExport->meta->setAttribute('fields', $fields); |
|
|
|
|
|
|
57
|
|
|
$dbExport->meta->setAttribute('filters', $filters); |
|
58
|
|
|
if ($user = Auth::user()) { |
|
59
|
|
|
$dbExport->meta->setAttribute('author.type', $user->getMorphClass()); |
|
60
|
|
|
$dbExport->meta->setAttribute('author.id', $user->getKey()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$export = $repo->export($model, serialize($dbExport)); |
|
64
|
|
|
if ($queueName = $this->getQueue($repo->queue())) { |
|
65
|
|
|
$export->queue( |
|
66
|
|
|
$filename, |
|
67
|
|
|
$disk, |
|
68
|
|
|
$this->getWriterType() |
|
69
|
|
|
)->allOnQueue($queueName); |
|
70
|
|
|
} else { |
|
71
|
|
|
$export->store( |
|
72
|
|
|
$filename, |
|
73
|
|
|
$disk, |
|
74
|
|
|
$this->getWriterType() |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
return Action::message(__('Export started.')); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.