Skip to content

Commit 8d8fd5c

Browse files
Merge pull request #77 from ACT-Training/76-add-select-column-for-enum-conditions
updated report to query on enum
2 parents 7e748ab + 85fc56b commit 8d8fd5c

4 files changed

Lines changed: 121 additions & 10 deletions

File tree

resources/views/editor.blade.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,32 @@ class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:
5555
</div>
5656
@endif
5757

58-
@if($criterion['inputType'] === 'number')
58+
@if($criterion['inputType'] === 'number')
59+
<div class="p-1 col-span-1">
60+
<label for="value-{{ $loop->index }}"
61+
class="sr-only block mb-2 text-sm font-medium text-gray-900 dark:text-white">Number</label>
62+
<input wire:model.live.debounce.1000ms="criteria.{{ $loop->index }}.value"
63+
type="number"
64+
id="value-{{ $loop->index }}"
65+
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
66+
</div>
67+
@endif
68+
69+
@if($criterion['inputType'] === 'enum')
5970
<div class="p-1 col-span-1">
6071
<label for="value-{{ $loop->index }}"
61-
class="sr-only block mb-2 text-sm font-medium text-gray-900 dark:text-white">Number</label>
62-
<input wire:model.live.debounce.1000ms="criteria.{{ $loop->index }}.value"
63-
type="number"
64-
id="value-{{ $loop->index }}"
65-
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
72+
class="sr-only block mb-2 text-sm font-medium text-gray-900 dark:text-white">Select</label>
73+
<select
74+
wire:model.live.debounce.1000ms="criteria.{{ $loop->index }}.value"
75+
id="value-{{ $loop->index }}"
76+
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
77+
>
78+
@foreach($criterion['options'] as $key => $label)
79+
<option value="{{ $key }}">{{ $label }}</option>
80+
@endforeach
81+
</select>
6682
</div>
67-
@endif
83+
@endif
6884

6985
@if($criterion['inputType'] === 'date')
7086
@include('query-builder::components.date-input')

src/Support/Concerns/WithQueryBuilder.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function addCriteria(): void
3838
'displayValue' => $condition->displayValue(),
3939
'inputType' => $condition->inputType,
4040
'factor' => $condition->factor,
41+
'options' => $condition->options ?? [],
4142
];
4243
}
4344

@@ -65,6 +66,7 @@ public function updatedCriteria($value, $indexOrKeyName): void
6566
'displayValue' => $this->displayValueForOperation($firstOperationKey),
6667
'inputType' => $this->inputTypeForCondition($value),
6768
'factor' => $this->factorForCondition($value),
69+
'options' => $this->optionsForCondition($value),
6870
];
6971
}
7072

@@ -103,12 +105,20 @@ public function inputTypeForCondition($key): string
103105
return $condition->inputType;
104106
}
105107

106-
public function factorForCondition($key): string
108+
public function factorForCondition($key): int
107109
{
108110
$conditions = $this->resolveConditions();
109111
$condition = $conditions->firstWhere('key', $key);
110112

111-
return $condition->factor;
113+
return $condition->factor ?? 1;
114+
}
115+
116+
public function optionsForCondition($key): array
117+
{
118+
$conditions = $this->resolveConditions();
119+
$condition = $conditions->firstWhere('key', $key);
120+
121+
return $condition->options ?? [];
112122
}
113123

114124
public function displayExtraValueForOperation($value): bool
@@ -177,7 +187,7 @@ private function getCriteriaClass($criteria
177187
return null;
178188
}
179189

180-
if ($factor) {
190+
if ($factor && is_numeric($value) && $factor !== 1) {
181191
$value = $value * $factor;
182192
}
183193

src/Support/Concerns/WithReportBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ACTTraining\QueryBuilder\Support\Columns\DateColumn;
88
use ACTTraining\QueryBuilder\Support\Conditions\BooleanCondition;
99
use ACTTraining\QueryBuilder\Support\Conditions\DateCondition;
10+
use ACTTraining\QueryBuilder\Support\Conditions\EnumCondition;
1011
use ACTTraining\QueryBuilder\Support\Conditions\FloatCondition;
1112
use ACTTraining\QueryBuilder\Support\Conditions\NumberCondition;
1213
use ACTTraining\QueryBuilder\Support\Conditions\TextCondition;
@@ -98,8 +99,10 @@ public function buildConditions(): array
9899
if ($column['skipCondition'] ?? false) {
99100
continue;
100101
}
102+
101103
$conditions[] = match ($column['type'] ?? null) {
102104
'number' => NumberCondition::make($column['label'], $column['key']),
105+
'enum' => EnumCondition::make($column['label'], $column['key'], $column['options'] ?? []),
103106
'float' => FloatCondition::make($column['label'], $column['key']),
104107
'boolean' => BooleanCondition::make($column['label'], $column['key']),
105108
'date' => DateCondition::make($column['label'], $column['key']),
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace ACTTraining\QueryBuilder\Support\Conditions;
4+
5+
use Illuminate\Support\Str;
6+
7+
/**
8+
* Extends the BaseCondition class and adds an extra $options property
9+
*/
10+
class EnumCondition extends BaseCondition
11+
{
12+
public string $inputType = 'enum';
13+
14+
/**
15+
* The options available for the enum condition.
16+
*
17+
* @var array
18+
*/
19+
public array $options;
20+
21+
/**
22+
* Constructor for EnumCondition
23+
* It initializes the $options property along with the properties inherited from BaseCondition.
24+
*
25+
* @param string $key
26+
* @param string $label
27+
* @param array $options
28+
*/
29+
public function __construct($key, $label, array $options = [])
30+
{
31+
parent::__construct($key, $label);
32+
$this->options = $options;
33+
}
34+
35+
/**
36+
* Static make method to create an instance of EnumCondition.
37+
*
38+
* @param string $label
39+
* @param string|null $key
40+
* @param array $options
41+
* @return static
42+
*/
43+
public static function make($label, $key = null, array $options = []): static
44+
{
45+
if (is_null($key)) {
46+
$key = Str::snake($label);
47+
}
48+
49+
return new static($key, $label, $options);
50+
}
51+
52+
public function operations(): array
53+
{
54+
return [
55+
'equals' => 'is',
56+
'not_equals' => 'is not',
57+
];
58+
}
59+
60+
/**
61+
* Get the option array.
62+
*
63+
* @return array
64+
*/
65+
public function options(): array
66+
{
67+
return $this->options;
68+
}
69+
70+
/**
71+
* Convert the EnumCondition instance to an array.
72+
* This overrides the toArray method to include options in the array.
73+
*
74+
* @return array
75+
*/
76+
public function toArray(): array
77+
{
78+
return array_merge(parent::toArray(), [
79+
'options' => $this->options,
80+
]);
81+
}
82+
}

0 commit comments

Comments
 (0)