-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithReportBuilder.php
More file actions
181 lines (147 loc) · 5.51 KB
/
WithReportBuilder.php
File metadata and controls
181 lines (147 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace ACTTraining\QueryBuilder\Support\Concerns;
use ACTTraining\QueryBuilder\Support\Columns\BooleanColumn;
use ACTTraining\QueryBuilder\Support\Columns\Column;
use ACTTraining\QueryBuilder\Support\Columns\DateColumn;
use ACTTraining\QueryBuilder\Support\Columns\ViewColumn;
use ACTTraining\QueryBuilder\Support\Conditions\BooleanCondition;
use ACTTraining\QueryBuilder\Support\Conditions\DateCondition;
use ACTTraining\QueryBuilder\Support\Conditions\EnumCondition;
use ACTTraining\QueryBuilder\Support\Conditions\FloatCondition;
use ACTTraining\QueryBuilder\Support\Conditions\NullCondition;
use ACTTraining\QueryBuilder\Support\Conditions\NumberCondition;
use ACTTraining\QueryBuilder\Support\Conditions\TextCondition;
use Illuminate\Http\Response;
use Livewire\Attributes\Validate;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
trait WithReportBuilder
{
#[Validate('required|array')]
public array $selectedColumns = [];
private function findElementByKey(array $array, $targetValue): ?array
{
foreach ($array as $value) {
// Check if the current item is an array
if (is_array($value)) {
// Check if it contains the 'key' element with the target value
if (isset($value['key']) && $value['key'] === $targetValue) {
return $value; // Return the found item
}
// Recursively search the sub-array
$result = $this->findElementByKey($value, $targetValue);
if ($result !== null) {
return $result;
}
}
}
return null; // Return null if no match is found
}
public function updatedSelectedColumns(): void
{
$this->resetPage();
}
public function availableColumns(): array
{
return [];
}
public function configuredColumns(): array
{
$columns = [];
foreach ($this->selectedColumns as $column) {
$columns[] = $this->findElementByKey($this->availableColumns(), $column);
}
return $columns;
}
public function buildColumns(): array
{
$columns = [];
$counter = 0;
foreach (($this->configuredColumns() ?? []) as $column) {
// Skip nulls/invalid items early
if (! is_array($column) || ! isset($column['label'], $column['key'])) {
continue;
}
$type = $column['type'] ?? null;
$columnToAdd = match ($type) {
'number' => Column::make($column['label'], $column['key'])->justify('right'),
'boolean' => BooleanColumn::make($column['label'], $column['key'])->justify('center')->hideIf(false),
'date' => DateColumn::make($column['label'], $column['key'])
->format(config('settings.date.short-format'))
->justify('right'),
'view' => ViewColumn::make($column['label']),
default => Column::make($column['label'], $column['key']),
};
if (! empty($column['view'])) {
$columnToAdd->component($column['view']);
} elseif ($counter === 0) {
$columnToAdd->component('columns.common.title');
}
if (! empty($column['sortable'])) {
$columnToAdd->sortable();
}
if (! empty($column['justify'])) {
$columnToAdd->justify($column['justify']);
}
$columns[] = $columnToAdd;
$counter++;
}
return $columns;
}
public function buildConditions(): array
{
$conditions = [];
foreach (($this->configuredColumns() ?? []) as $column) {
// Skip nulls/garbage early
if (! is_array($column) || ! isset($column['label'], $column['key'])) {
continue;
}
if (! empty($column['skipCondition'])) {
continue;
}
$type = $column['type'] ?? null;
// Normalise enum options
$options = $column['options'] ?? [];
if (! is_array($options)) {
$options = [];
}
$conditions[] = match ($type) {
'number' => NumberCondition::make($column['label'], $column['key']),
'enum' => EnumCondition::make($column['label'], $column['key'], $options),
'float' => FloatCondition::make($column['label'], $column['key']),
'boolean' => BooleanCondition::make($column['label'], $column['key']),
'date' => DateCondition::make($column['label'], $column['key']),
'null' => NullCondition::make($column['label'], $column['key']),
default => TextCondition::make($column['label'], $column['key']),
};
}
return $conditions;
}
public function removeCriteria($index): void
{
unset($this->criteria[$index]);
$this->criteria = array_values($this->criteria);
$this->saveToSession();
}
public function resetReportBuilder(): void
{
$this->criteria = [];
$this->selectedColumns = [];
$this->saveToSession();
}
public function exportReportBuilder(): Response|BinaryFileResponse|null
{
return null;
}
public function saveReportBuilder(): void
{
//
}
public function loadReportBuilder(): void
{
//
}
public function saveToSession(): void
{
//
}
}