Skip to content

Commit

Permalink
Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
rxcod9 committed Sep 1, 2023
1 parent 7189857 commit 1b4f9c8
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 76 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}],
"require": {
"php": "^7.3|^8.0|^9.0",
"illuminate/support": "^7|^8|^9",
"illuminate/support": "^7|^8|^9|^10",
"joy/voyager-core": "^1.0",
"tcg/voyager": "^1.4",
"yajra/laravel-datatables-oracle": "~9.0|~10.0"
Expand All @@ -29,8 +29,8 @@
"friendsofphp/php-cs-fixer": "^3.0",
"guzzlehttp/guzzle": "^6.3|^7.0",
"mockery/mockery": "^1.3",
"orchestra/testbench": "^5.0|^6.0|^7.0",
"phpunit/phpunit": "^9.1"
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0",
"phpunit/phpunit": "^9.1|^10.0"
},
"autoload": {
"files": [
Expand Down
1 change: 1 addition & 0 deletions config/voyager-datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'filters' => [
'type_hidden' => [
'password',
'coordinates',
],
'hidden' => [
'deleted_at',
Expand Down
18 changes: 18 additions & 0 deletions resources/views/components/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
case 'markdown_editor':
case 'rich_text_box':
case 'text_area':
case 'color':
case 'text':
$('input', cell)
.off('keyup change')
Expand Down Expand Up @@ -185,8 +186,13 @@
}, 500));
break;
case 'image':
case 'multiple_images':
case 'media_picker':
case 'file':
case 'multiple_checkbox':
case 'checkbox':
case 'radio_btn':
case 'select_multiple':
case 'select_dropdown':
$('select', cell)
.off('change.col-filter-' + filterType)
Expand Down Expand Up @@ -260,6 +266,7 @@
case 'markdown_editor':
case 'rich_text_box':
case 'text_area':
case 'color':
case 'text':
$('input', cell)
.off('keyup change');
Expand All @@ -277,8 +284,13 @@
.off('change');
break;
case 'image':
case 'multiple_images':
case 'media_picker':
case 'file':
case 'multiple_checkbox':
case 'checkbox':
case 'radio_btn':
case 'select_multiple':
case 'select_dropdown':
$('select', cell)
.off('change.col-filter-' + filterType);
Expand Down Expand Up @@ -327,6 +339,7 @@
case 'markdown_editor':
case 'rich_text_box':
case 'text_area':
case 'color':
case 'text':
$('input', cell).val(null);
break;
Expand All @@ -340,8 +353,13 @@
$('input[type="date"]', cell).val(null);
break;
case 'image':
case 'multiple_images':
case 'media_picker':
case 'file':
case 'multiple_checkbox':
case 'checkbox':
case 'radio_btn':
case 'select_multiple':
case 'select_dropdown':
const selectEl = $('select', cell);
if(selectEl.prop('multiple')) {
Expand Down
34 changes: 15 additions & 19 deletions resources/views/filterformfields/media_picker.blade.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<br>
<?php $checked = false; ?>
@if(isset($dataTypeContent->{$row->field}))
<?php $checked = $dataTypeContent->{$row->field}; ?>
@else
<?php $checked = isset($options->checked) &&
filter_var($options->checked, FILTER_VALIDATE_BOOLEAN) ? true: false; ?>
@endif

<?php $class = $options->class ?? "toggleswitch"; ?>

@if(isset($options->on) && isset($options->off))
<input type="checkbox" name="{{ $row->field }}" class="{{ $class }}"
data-on="{{ $options->on }}" {!! $checked ? 'checked="checked"' : '' !!}
data-off="{{ $options->off }}">
@else
<input type="checkbox" name="{{ $row->field }}" class="{{ $class }}"
@if($checked) checked @endif>
@endif
@php $options->options = [
'1' => 'Yes',
'0' => 'No',
]; @endphp
<?php $selected_value = (isset($dataTypeContent->{$row->field}) && !is_null($dataTypeContent->{$row->field})) ? $dataTypeContent->{$row->field} : null; ?>
<select class="form-control input-sm select2" name="{{ $row->field }}">
<option value="">{{__('voyager::generic.none')}}</option>
<?php $default = (isset($options->filter_default) && !isset($dataTypeContent->{$row->field})) ? $options->filter_default : null; ?>
@if(isset($options->options))
@foreach($options->options as $key => $option)
@continue($key === '')
<option value="{{ $key }}" @if($default === $key && $selected_value === NULL) selected="selected" @endif @if($selected_value === $key) selected="selected" @endif>{{ $option }}</option>
@endforeach
@endif
</select>
29 changes: 11 additions & 18 deletions resources/views/filterformfields/multiple_checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<br>
<?php $checked = false; ?>
@if(isset($options->options))
@foreach($options->options as $key => $label)
@if(isset($dataTypeContent->{$row->field}))
@php
$checkedData = $dataTypeContent->{$row->field};
$checkedData = is_array($checkedData) ? $checkedData : json_decode($checkedData, true);
$checked = in_array($key, $checkedData);
@endphp
@else
<?php $checked = isset($options->checked) && $options->checked ? true : false; ?>
@endif

<input type="checkbox" name="{{ $row->field }}[{{$key}}]" {!! $checked ? 'checked="checked"' : '' !!} value="{{$key}}" id="{{$key}}"/>
<label for="{{$key}}">{{$label}}</label>
@endforeach
@endif
<?php $selected_value = (isset($dataTypeContent->{$row->field}) && !is_null($dataTypeContent->{$row->field})) ? $dataTypeContent->{$row->field} : null; ?>
<select multiple class="form-control input-sm select2" name="{{ $row->field }}[]">
<option value="">{{__('voyager::generic.none')}}</option>
<?php $default = (isset($options->filter_default) && !isset($dataTypeContent->{$row->field})) ? $options->filter_default : null; ?>
@if(isset($options->options))
@foreach($options->options as $key => $option)
@continue($key === '')
<option value="{{ $key }}" @if($default === $key && $selected_value === NULL) selected="selected" @endif @if($selected_value === $key) selected="selected" @endif>{{ $option }}</option>
@endforeach
@endif
</select>
29 changes: 11 additions & 18 deletions resources/views/filterformfields/radio_btn.blade.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<br>
<?php $checked = false; ?>
@if(isset($options->options))
@foreach($options->options as $key => $label)
@if(isset($dataTypeContent->{$row->field}))
@php
$checkedData = $dataTypeContent->{$row->field};
$checkedData = is_array($checkedData) ? $checkedData : json_decode($checkedData, true);
$checked = in_array($key, $checkedData);
@endphp
@else
<?php $checked = isset($options->checked) && $options->checked ? true : false; ?>
@endif

<input type="checkbox" name="{{ $row->field }}[{{$key}}]" {!! $checked ? 'checked="checked"' : '' !!} value="{{$key}}" id="{{$key}}"/>
<label for="{{$key}}">{{$label}}</label>
@endforeach
@endif
<?php $selected_value = (isset($dataTypeContent->{$row->field}) && !is_null($dataTypeContent->{$row->field})) ? $dataTypeContent->{$row->field} : null; ?>
<select multiple class="form-control input-sm select2" name="{{ $row->field }}[]">
<option value="">{{__('voyager::generic.none')}}</option>
<?php $default = (isset($options->filter_default) && !isset($dataTypeContent->{$row->field})) ? $options->filter_default : null; ?>
@if(isset($options->options))
@foreach($options->options as $key => $option)
@continue($key === '')
<option value="{{ $key }}" @if($default === $key && $selected_value === NULL) selected="selected" @endif @if($selected_value === $key) selected="selected" @endif>{{ $option }}</option>
@endforeach
@endif
</select>
6 changes: 5 additions & 1 deletion src/Http/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function dataRowsMayHaveHtml(Model $model, DataRow $row): bool
return true;
}

if ($row->type == 'color') {
return true;
}

if ($row->type == 'relationship') {
return true;
}
Expand Down Expand Up @@ -116,7 +120,7 @@ function dataRowsMayHaveHtml(Model $model, DataRow $row): bool
}

if ($row->type == 'checkbox') {
return false;
return true;
}

if ($row->type == 'color') {
Expand Down
14 changes: 10 additions & 4 deletions src/Services/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ protected function columnSelectMultiple(

if (property_exists($row->details, 'options')) {
if (!empty(json_decode($content))) {
$lastKey = end(array_keys(json_decode($content)));
$keys = array_keys(json_decode($content, true));
$lastKey = end($keys);
foreach (json_decode($content) as $key => $item) {
if (@$row->details->options->{$item}) {
$view .= $row->details->options->{$item} . ($key !== $lastKey ? ', ' : '');
Expand All @@ -287,8 +288,9 @@ protected function columnMultipleCheckbox(
$content = null
): string {
$view = '';
if (@count(json_decode($content)) > 0) {
$lastKey = end(array_keys(json_decode($content)));
if ($content && @count(json_decode($content, true)) > 0) {
$keys = array_keys(json_decode($content, true));
$lastKey = end($keys);
foreach (json_decode($content) as $key => $item) {
if (@$row->details->options->{$item}) {
$view .= $row->details->options->{$item} . ($key !== $lastKey ? ', ' : '');
Expand Down Expand Up @@ -431,6 +433,10 @@ protected function columnFile(
DataType $dataType,
$content = null
): string {
if (!$content || json_decode($content) === null) {
return '';
}

$view = (string) view('voyager::multilingual.input-hidden-bread-browse', [
'data' => $data,
'row' => $row
Expand Down Expand Up @@ -484,7 +490,7 @@ protected function columnCoordinates(
DataType $dataType,
$content = null
): string {
return (string) view('voyager::partials.coordinates-static-image');
return (string) view('voyager::partials.coordinates-static-image', ['data' => $data, 'row' => $row]);
}

/**
Expand Down
61 changes: 49 additions & 12 deletions src/Services/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ protected function filterRelationshipBelongsToMany(
DataType $dataType,
Request $request
): void {
// @TODO Not implemented yet.
$keywords = explode(',', $keyword);
$model = $query->getModel();
$options = $row->details;
Expand Down Expand Up @@ -280,7 +279,14 @@ protected function filterSelectMultiple(
DataType $dataType,
Request $request
): void {
// @TODO Not implemented yet.
$keywords = explode(',', $keyword);
$query->where(function ($query) use ($row, $keywords) {
foreach ($keywords as $keyword) {
$query->orWhere(function ($query) use ($row, $keyword) {
$query->whereJsonContains($row->field . '->' . $keyword, $keyword);
});
}
});
}

/**
Expand All @@ -296,7 +302,14 @@ protected function filterMultipleCheckbox(
DataType $dataType,
Request $request
): void {
// @TODO Not implemented yet.
$keywords = explode(',', $keyword);
$query->where(function ($query) use ($row, $keywords) {
foreach ($keywords as $keyword) {
$query->orWhere(function ($query) use ($row, $keyword) {
$query->whereJsonContains($row->field . '->' . $keyword, $keyword);
});
}
});
}

/**
Expand Down Expand Up @@ -389,9 +402,9 @@ protected function filterCheckbox(
): void {
$options = $row->details;
$query->when($keyword === '1' || $keyword === 'Yes', function ($query) use ($row, $options) {
$query->where($row->field, $options->on ?? '1')->whereNotNull($row->field);
$query->where($row->field, '1')->whereNotNull($row->field);
})->when($keyword === '0' || $keyword === 'No', function ($query) use ($row, $options) {
$query->where($row->field, $options->on ?? '0')->orWhereNull($row->field);
$query->where($row->field, '0')->orWhereNull($row->field);
});
}

Expand Down Expand Up @@ -451,7 +464,7 @@ protected function filterNumber(
$to = $keywords[1] ?? null;

if (count($keywords) === 1 && $from) {
$query->whereDate($row->field, $from);
$query->where($row->field, $from);
return;
}

Expand Down Expand Up @@ -504,10 +517,18 @@ protected function filterFile(
DataType $dataType,
Request $request
): void {
$query->when($keyword === '1' || $keyword === 'Yes', function ($query) use ($row, $keyword) {
$query->whereNotNull($row->field);
})->when($keyword === '0' || $keyword === 'No', function ($query) use ($row, $keyword) {
$query->whereNull($row->field);
$query->when($keyword === '1' || $keyword === 'Yes', function ($query) use ($row) {
$query->where(function ($query) use ($row) {
$query
->whereNotNull($row->field)
->whereJsonLength($row->field, '<>', 0);
});
})->when($keyword === '0' || $keyword === 'No', function ($query) use ($row) {
$query->where(function ($query) use ($row) {
$query
->whereNull($row->field)
->orWhereJsonLength($row->field, 0);
});
});
}

Expand Down Expand Up @@ -562,7 +583,11 @@ protected function filterMultipleImages(
DataType $dataType,
Request $request
): void {
// @TODO Not implemented yet.
$query->when($keyword === '1' || $keyword === 'Yes', function ($query) use ($row) {
$query->whereNotNull($row->field);
})->when($keyword === '0' || $keyword === 'No', function ($query) use ($row) {
$query->whereNull($row->field);
});
}

/**
Expand All @@ -578,7 +603,19 @@ protected function filterMediaPicker(
DataType $dataType,
Request $request
): void {
// @TODO Not implemented yet.
$query->when($keyword === '1' || $keyword === 'Yes', function ($query) use ($row, $keyword) {
$query->where(function ($query) use ($row, $keyword) {
$query
->whereNotNull($row->field)
->whereJsonLength($row->field, '<>', 0);
});
})->when($keyword === '0' || $keyword === 'No', function ($query) use ($row, $keyword) {
$query->where(function ($query) use ($row, $keyword) {
$query
->whereNull($row->field)
->orWhereJsonLength($row->field, 0);
});
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/VoyagerDatatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected function registerFilterFormFields()
'time',
'timestamp',
// 'hidden',
'coordinates',
// 'coordinates',
];

foreach ($filterFilterFormFields as $filterFilterFormField) {
Expand Down

0 comments on commit 1b4f9c8

Please sign in to comment.