|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaListCard\Http\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
|
8
|
|
|
use Laravel\Nova\Nova; |
|
9
|
|
|
use Laravel\Nova\Resource; |
|
10
|
|
|
use NovaListCard\ListCard; |
|
11
|
|
|
|
|
12
|
|
|
class CardRequest extends NovaRequest |
|
13
|
|
|
{ |
|
14
|
|
|
protected ?ListCard $_card = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Determine if the user is authorized to make this request. |
|
18
|
|
|
* |
|
19
|
|
|
* @return bool |
|
20
|
|
|
*/ |
|
21
|
4 |
|
public function authorize() |
|
22
|
|
|
{ |
|
23
|
4 |
|
return true; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the validation rules that apply to the request. |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public function rules() |
|
32
|
|
|
{ |
|
33
|
4 |
|
return []; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
4 |
|
public function card(): ?ListCard |
|
38
|
|
|
{ |
|
39
|
4 |
|
if (!$this->_card) { |
|
40
|
4 |
|
$allCards = new Collection(); |
|
41
|
4 |
|
collect(Nova::$dashboards) |
|
|
|
|
|
|
42
|
4 |
|
->filter(fn ($dashboard) => method_exists($dashboard, 'cards') && is_array($dashboard->cards())) |
|
43
|
4 |
|
->each(fn ($dashboard) => $allCards->push(...$dashboard->cards())); |
|
44
|
|
|
|
|
45
|
4 |
|
$this->_card = $allCards |
|
46
|
4 |
|
->filter(fn ($card) => (method_exists($card, 'uriKey') && $card->uriKey() == $this->route('key'))) |
|
47
|
4 |
|
->first(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
return $this->_card; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return class-string<Resource> |
|
|
|
|
|
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function cardResource(): string |
|
57
|
|
|
{ |
|
58
|
4 |
|
$resource = $this->card()?->resource; |
|
59
|
4 |
|
if (!$resource) { |
|
60
|
1 |
|
throw new \Exception('Resource not found'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
return $resource; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return Builder |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function prepareQuery(): Builder |
|
71
|
|
|
{ |
|
72
|
3 |
|
$resource = $this->cardResource(); |
|
73
|
|
|
|
|
74
|
|
|
/** @var Builder $query */ |
|
75
|
3 |
|
$query = $resource::newModel()->query(); |
|
76
|
|
|
|
|
77
|
3 |
|
$card = $this->card(); |
|
78
|
|
|
|
|
79
|
3 |
|
if ($card->relationship) { |
|
80
|
2 |
|
$query = match ($card->aggregate) { |
|
81
|
2 |
|
'count' => $query->withCount($card->relationship), |
|
82
|
2 |
|
'sum' => $query->withSum($card->relationship, $card->aggregateColumn ?: $card->valueColumn), |
|
83
|
2 |
|
'avg' => $query->withAvg($card->relationship, $card->aggregateColumn ?: $card->valueColumn), |
|
84
|
2 |
|
'min' => $query->withMin($card->relationship, $card->aggregateColumn ?: $card->valueColumn), |
|
85
|
2 |
|
'max' => $query->withMax($card->relationship, $card->aggregateColumn ?: $card->valueColumn), |
|
86
|
2 |
|
default => $query |
|
87
|
2 |
|
}; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
3 |
|
if ($card->orderColumn) { |
|
92
|
2 |
|
$query->orderBy($card->orderColumn, $card->orderDirection); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ( |
|
96
|
3 |
|
($limit = $card->limit) && |
|
97
|
3 |
|
$limit > 0 |
|
98
|
|
|
) { |
|
99
|
3 |
|
$query->take($limit); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
if (is_callable($card->queryCallback)) { |
|
103
|
1 |
|
call_user_func($card->queryCallback, $query); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
return $query; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|