ExportConfiguration::actions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
1
<?php
2
3
namespace NovaExportConfiguration\Nova\Resources;
4
5
use Laravel\Nova\Fields\ID;
6
use Laravel\Nova\Fields\Select;
7
use Laravel\Nova\Fields\Text;
8
use Laravel\Nova\Fields\Textarea;
9
use Laravel\Nova\Http\Requests\NovaRequest;
10
use Laravel\Nova\Panel;
11
use Laravel\Nova\Resource;
12
use NovaExportConfiguration\Nova\Actions\ConfiguredExportToExcelAction;
13
use NovaExportConfiguration\Nova\Actions\RegenerateExportResultAction;
14
use NovaExportConfiguration\Nova\Filters\TypeFilter;
15
use NovaExportConfiguration\NovaExportConfig;
16
use NovaExportConfiguration\Repositories\ExportRepository;
17
18
/**
19
 * @extends Resource<\NovaExportConfiguration\Models\ExportConfig>
20
 */
21
class ExportConfiguration extends Resource
22
{
23
    /**
24
     * The model the resource corresponds to.
25
     * Override using service provider.
26
     *
27
     * @var class-string<\NovaExportConfiguration\Models\ExportConfig>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\NovaExport...on\Models\ExportConfig> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\NovaExportConfiguration\Models\ExportConfig>.
Loading history...
28
     */
29
    public static $model;
30
31
    public static $group = 'Export';
32
33
    public static $title = 'name';
34
35
    public static $search = [
36
        'name',
37
    ];
38
39
    public function fields(NovaRequest $request)
40
    {
41
        $fields = [
42
            ID::make(__('ID'), 'id')->sortable(),
43
            Select::make(__('Type'), 'type')
44
                ->onlyOnForms()
45
                ->hideWhenUpdating()
46
                ->required()
47
                ->searchable()
48
                ->options(NovaExportConfig::typeOptions()),
49
            Text::make(__('Type'), 'type')
50
                ->displayUsing(fn ($val, $model) => NovaExportConfig::getRepositories()->getByName($model?->type)?->label())
51
                ->hideWhenUpdating()
52
                ->hideWhenCreating()
53
                ->sortable(),
54
            Text::make(__('Name'), 'name')
55
                ->sortable()
56
                ->rules([
57
                    'required',
58
                    'max:255',
59
                ]),
60
            Textarea::make(__('Description'), 'description')
61
                ->hideFromIndex()
62
                ->alwaysShow()
63
                ->rules([
64
                    'nullable',
65
                    'max:3000',
66
                ]),
67
            Text::make(__('Description'), 'description')
68
                ->displayUsing(fn ($val) => nl2br($val))
69
                ->asHtml()
70
                ->onlyOnIndex(),
71
            new Panel(__('Filters'), $this->filterFields($request)),
72
        ];
73
74
        return $fields;
75
    }
76
77
    protected function filterFields(NovaRequest $request): array
78
    {
79
        return ($repo = $this->getRepo($request)) ? $repo->novaResourceConfig()->filterFields($request, $this) : [];
80
    }
81
82
    protected function getRepo(NovaRequest $request): ?ExportRepository
83
    {
84
        $type = $this->model()?->type;
85
        if (!$type) {
86
            if ($request->viaResource()) {
87
                $type = $request->viaResource()::find($request->viaResourceId)?->type;
88
            } else {
89
                $type = $request->findModel()?->type;
90
            }
91
        }
92
93
        return NovaExportConfig::getRepositories()->getByName($type);
94
    }
95
96
    public function filters(NovaRequest $request)
97
    {
98
        return [
99
            new TypeFilter(),
100
        ];
101
    }
102
103
    public function actions(NovaRequest $request)
104
    {
105
        return array_merge([
106
            (new ConfiguredExportToExcelAction())->askForFilename()
107
                ->askForWriterType(),
108
            new RegenerateExportResultAction,
109
        ], NovaExportConfig::configurationActions($request));
110
    }
111
}
112