|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaListCard; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Laravel\Nova\Card; |
|
8
|
|
|
use Laravel\Nova\Nova; |
|
9
|
|
|
use Laravel\Nova\Resource; |
|
10
|
|
|
use NovaListCard\Http\Requests\CardRequest; |
|
11
|
|
|
|
|
12
|
|
|
abstract class ListCard extends Card |
|
13
|
|
|
{ |
|
14
|
|
|
public ?string $name = null; |
|
15
|
|
|
|
|
16
|
|
|
public $width = '1/3'; |
|
17
|
|
|
|
|
18
|
|
|
public ?string $resource = null; |
|
19
|
|
|
|
|
20
|
|
|
public ?string $relationship = null; |
|
21
|
|
|
|
|
22
|
|
|
public ?string $aggregate = null; |
|
23
|
|
|
|
|
24
|
|
|
public ?string $aggregateColumn = null; |
|
25
|
|
|
|
|
26
|
|
|
public ?int $limit = 5; |
|
27
|
|
|
|
|
28
|
|
|
public ?string $orderColumn = null; |
|
29
|
|
|
|
|
30
|
|
|
public ?string $orderDirection = null; |
|
31
|
|
|
|
|
32
|
|
|
public ?\Closure $queryCallback = null; |
|
33
|
|
|
|
|
34
|
|
|
public array $heading = []; |
|
35
|
|
|
|
|
36
|
|
|
public ?string $valueColumn = null; |
|
37
|
|
|
|
|
38
|
|
|
public ?string $valueFormat = null; |
|
39
|
|
|
|
|
40
|
|
|
public ?string $valueFormatter = null; |
|
41
|
|
|
|
|
42
|
|
|
public ?string $timestampColumn = null; |
|
43
|
|
|
|
|
44
|
|
|
public ?string $timestampFormat = null; |
|
45
|
|
|
|
|
46
|
|
|
public bool $noMaxHeight = false; |
|
47
|
|
|
|
|
48
|
|
|
public string $classes = ''; |
|
49
|
|
|
|
|
50
|
2 |
|
public function component() |
|
51
|
|
|
{ |
|
52
|
2 |
|
return 'nova-list-card'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function cacheFor(): int|Carbon |
|
56
|
|
|
{ |
|
57
|
2 |
|
return 0; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function getCacheKey(CardRequest $request): string |
|
61
|
|
|
{ |
|
62
|
1 |
|
return sprintf( |
|
63
|
1 |
|
'nova.metric.%s.%s.%s.%s.%s.%s', |
|
64
|
1 |
|
$this->uriKey(), |
|
65
|
1 |
|
$request->input('range', 'no-range'), |
|
66
|
1 |
|
$request->input('timezone', 'no-timezone'), |
|
67
|
1 |
|
$request->input('twelveHourTime', 'no-12-hour-time'), |
|
68
|
1 |
|
$this->onlyOnDetail ? $request->findModelOrFail()->getKey() : 'no-resource-id', |
|
69
|
1 |
|
md5($this->resource) |
|
|
|
|
|
|
70
|
1 |
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
public function name(): string |
|
74
|
|
|
{ |
|
75
|
6 |
|
return $this->name ?: Nova::humanize($this); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
public function uriKey(): string |
|
79
|
|
|
{ |
|
80
|
5 |
|
return Str::slug($this->name(), '-', null); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param class-string<Resource> $resource |
|
|
|
|
|
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
6 |
|
public function resource(string $resource): static |
|
88
|
|
|
{ |
|
89
|
6 |
|
$this->resource = $resource; |
|
90
|
6 |
|
$this->classes($resource::uriKey()); |
|
91
|
|
|
|
|
92
|
6 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
public function withAggregate(string $aggregate, string $relationship, ?string $column = null): static |
|
96
|
|
|
{ |
|
97
|
6 |
|
$this->aggregate = $aggregate; |
|
98
|
6 |
|
$this->relationship = $relationship; |
|
99
|
6 |
|
$this->aggregateColumn = $column; |
|
100
|
|
|
|
|
101
|
6 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
6 |
|
public function withCount(string $relationship): static |
|
105
|
|
|
{ |
|
106
|
6 |
|
return $this->withAggregate('count', $relationship); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
6 |
|
public function withSum(string $relationship, string $column): static |
|
110
|
|
|
{ |
|
111
|
6 |
|
return $this->withAggregate('sum', $relationship, $column); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
6 |
|
public function orderBy(string $column, string $direction = 'desc'): static |
|
115
|
|
|
{ |
|
116
|
6 |
|
$this->orderColumn = $column; |
|
117
|
|
|
|
|
118
|
6 |
|
$direction = Str::lower($direction); |
|
119
|
6 |
|
if (!in_array($direction, ['asc', 'desc'])) { |
|
120
|
6 |
|
$direction = 'desc'; |
|
121
|
|
|
} |
|
122
|
6 |
|
$this->orderDirection = $direction; |
|
123
|
|
|
|
|
124
|
6 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
6 |
|
public function limit(?int $limit = 5): static |
|
128
|
|
|
{ |
|
129
|
6 |
|
$this->limit = $limit; |
|
130
|
|
|
|
|
131
|
6 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
6 |
|
public function queryCallback(?\Closure $queryCallback = null): static |
|
135
|
|
|
{ |
|
136
|
6 |
|
$this->queryCallback = $queryCallback; |
|
137
|
|
|
|
|
138
|
6 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
6 |
|
public function heading(string $left, ?string $right = null): static |
|
142
|
|
|
{ |
|
143
|
6 |
|
$this->heading = ['left' => $left, 'right' => $right]; |
|
144
|
6 |
|
$this->classes(Str::slug($left)); |
|
145
|
|
|
|
|
146
|
6 |
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
6 |
|
public function value(string $column, ?string $formatter = 'integer', ?string $format = null): static |
|
150
|
|
|
{ |
|
151
|
6 |
|
$this->valueColumn = $column; |
|
152
|
6 |
|
$this->valueFormat = $format; |
|
153
|
6 |
|
$this->valueFormatter = $formatter; |
|
154
|
|
|
|
|
155
|
6 |
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
6 |
|
public function timestamp(string $column = 'created_at', string $format = 'd/m/Y'): static |
|
159
|
|
|
{ |
|
160
|
6 |
|
$this->timestampColumn = $column; |
|
161
|
6 |
|
$this->timestampFormat = $format; |
|
162
|
|
|
|
|
163
|
6 |
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
6 |
|
public function noMaxHeight(bool $noMaxHeight = true): static |
|
167
|
|
|
{ |
|
168
|
6 |
|
$this->noMaxHeight = $noMaxHeight; |
|
169
|
|
|
|
|
170
|
6 |
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
6 |
|
public function classes(string $classes): static |
|
174
|
|
|
{ |
|
175
|
6 |
|
$this->classes = "{$this->classes} {$classes}"; |
|
176
|
|
|
|
|
177
|
6 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @inheritDoc |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function jsonSerialize(): array |
|
184
|
|
|
{ |
|
185
|
1 |
|
return array_merge([ |
|
186
|
1 |
|
'uriKey' => $this->uriKey(), |
|
187
|
1 |
|
'classes' => trim($this->classes), |
|
188
|
1 |
|
'heading' => $this->heading, |
|
189
|
1 |
|
'noMaxHeight' => $this->noMaxHeight, |
|
190
|
1 |
|
'url' => route('nova-list-card.data', ['key' => $this->uriKey()]), |
|
191
|
1 |
|
], parent::jsonSerialize()); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|