From 1b4f9c80d4d27f5692c7c6e19aefb4a1b217f239 Mon Sep 17 00:00:00 2001 From: Ramakant Gangwar Date: Fri, 1 Sep 2023 14:27:05 +0530 Subject: [PATCH] Filters --- composer.json | 6 +- config/voyager-datatable.php | 1 + .../views/components/datatable.blade.php | 18 ++++++ .../filterformfields/media_picker.blade.php | 34 +++++------ .../multiple_checkbox.blade.php | 29 ++++----- .../filterformfields/radio_btn.blade.php | 29 ++++----- src/Http/helpers.php | 6 +- src/Services/Column.php | 14 +++-- src/Services/Filter.php | 61 +++++++++++++++---- src/VoyagerDatatableServiceProvider.php | 2 +- 10 files changed, 124 insertions(+), 76 deletions(-) diff --git a/composer.json b/composer.json index 58ffa0d..63de3f5 100644 --- a/composer.json +++ b/composer.json @@ -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" @@ -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": [ diff --git a/config/voyager-datatable.php b/config/voyager-datatable.php index 28bb032..6ea2d93 100644 --- a/config/voyager-datatable.php +++ b/config/voyager-datatable.php @@ -46,6 +46,7 @@ 'filters' => [ 'type_hidden' => [ 'password', + 'coordinates', ], 'hidden' => [ 'deleted_at', diff --git a/resources/views/components/datatable.blade.php b/resources/views/components/datatable.blade.php index f3a2eb2..908ba5b 100644 --- a/resources/views/components/datatable.blade.php +++ b/resources/views/components/datatable.blade.php @@ -139,6 +139,7 @@ case 'markdown_editor': case 'rich_text_box': case 'text_area': + case 'color': case 'text': $('input', cell) .off('keyup change') @@ -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) @@ -260,6 +266,7 @@ case 'markdown_editor': case 'rich_text_box': case 'text_area': + case 'color': case 'text': $('input', cell) .off('keyup change'); @@ -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); @@ -327,6 +339,7 @@ case 'markdown_editor': case 'rich_text_box': case 'text_area': + case 'color': case 'text': $('input', cell).val(null); break; @@ -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')) { diff --git a/resources/views/filterformfields/media_picker.blade.php b/resources/views/filterformfields/media_picker.blade.php index fbcc821..00f4364 100644 --- a/resources/views/filterformfields/media_picker.blade.php +++ b/resources/views/filterformfields/media_picker.blade.php @@ -1,19 +1,15 @@ -
- -@if(isset($dataTypeContent->{$row->field})) - {$row->field}; ?> -@else - checked) && - filter_var($options->checked, FILTER_VALIDATE_BOOLEAN) ? true: false; ?> -@endif - -class ?? "toggleswitch"; ?> - -@if(isset($options->on) && isset($options->off)) - -@else - -@endif +@php $options->options = [ + '1' => 'Yes', + '0' => 'No', +]; @endphp +{$row->field}) && !is_null($dataTypeContent->{$row->field})) ? $dataTypeContent->{$row->field} : null; ?> + \ No newline at end of file diff --git a/resources/views/filterformfields/multiple_checkbox.blade.php b/resources/views/filterformfields/multiple_checkbox.blade.php index c532de0..5a176c7 100644 --- a/resources/views/filterformfields/multiple_checkbox.blade.php +++ b/resources/views/filterformfields/multiple_checkbox.blade.php @@ -1,18 +1,11 @@ -
- -@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 - checked) && $options->checked ? true : false; ?> - @endif - - - - @endforeach -@endif +{$row->field}) && !is_null($dataTypeContent->{$row->field})) ? $dataTypeContent->{$row->field} : null; ?> + \ No newline at end of file diff --git a/resources/views/filterformfields/radio_btn.blade.php b/resources/views/filterformfields/radio_btn.blade.php index c532de0..5a176c7 100644 --- a/resources/views/filterformfields/radio_btn.blade.php +++ b/resources/views/filterformfields/radio_btn.blade.php @@ -1,18 +1,11 @@ -
- -@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 - checked) && $options->checked ? true : false; ?> - @endif - - - - @endforeach -@endif +{$row->field}) && !is_null($dataTypeContent->{$row->field})) ? $dataTypeContent->{$row->field} : null; ?> + \ No newline at end of file diff --git a/src/Http/helpers.php b/src/Http/helpers.php index 4786d9d..af6484b 100644 --- a/src/Http/helpers.php +++ b/src/Http/helpers.php @@ -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; } @@ -116,7 +120,7 @@ function dataRowsMayHaveHtml(Model $model, DataRow $row): bool } if ($row->type == 'checkbox') { - return false; + return true; } if ($row->type == 'color') { diff --git a/src/Services/Column.php b/src/Services/Column.php index 3c1cead..dda508c 100644 --- a/src/Services/Column.php +++ b/src/Services/Column.php @@ -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 ? ', ' : ''); @@ -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 ? ', ' : ''); @@ -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 @@ -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]); } /** diff --git a/src/Services/Filter.php b/src/Services/Filter.php index baad08f..0cb1381 100644 --- a/src/Services/Filter.php +++ b/src/Services/Filter.php @@ -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; @@ -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); + }); + } + }); } /** @@ -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); + }); + } + }); } /** @@ -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); }); } @@ -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; } @@ -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); + }); }); } @@ -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); + }); } /** @@ -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); + }); + }); } /** diff --git a/src/VoyagerDatatableServiceProvider.php b/src/VoyagerDatatableServiceProvider.php index b8576bd..639b86f 100644 --- a/src/VoyagerDatatableServiceProvider.php +++ b/src/VoyagerDatatableServiceProvider.php @@ -221,7 +221,7 @@ protected function registerFilterFormFields() 'time', 'timestamp', // 'hidden', - 'coordinates', + // 'coordinates', ]; foreach ($filterFilterFormFields as $filterFilterFormField) {