Skip to content

Commit 25e3e7e

Browse files
committed
init
0 parents  commit 25e3e7e

File tree

9 files changed

+261
-0
lines changed

9 files changed

+261
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
phpunit.phar
3+
/vendor
4+
composer.phar
5+
composer.lock
6+
*.project
7+
.idea/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Jens Segers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
laravel-admin extension
2+
======
3+
4+

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "huo-zi/laravel-admin-ext-filter",
3+
"description": "Extend the laravel-admin Filter to group by rows.",
4+
"type": "library",
5+
"keywords": ["laravel-admin", "extension", "filter"],
6+
"homepage": "https://github.com/huozi1024/laravel-admin-ext-filter",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "huozi",
11+
"email": "[email protected]",
12+
"homepage": "https://huozi1024.github.io"
13+
}
14+
],
15+
"require": {
16+
"php": ">=7.0.0",
17+
"encore/laravel-admin": "^1.6"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "~6.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Huozi\\Admin\\Filter\\": "src/"
25+
}
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"Huozi\\Admin\\Filter\\RowFilterServiceProvider"
31+
]
32+
}
33+
}
34+
}

resources/views/row.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="group-row">
2+
@if($filters)
3+
@foreach($filters as $index => $filter)
4+
{!! $filter->render() !!}
5+
@endforeach
6+
@endif
7+
</div>

src/Row.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Huozi\Admin\Filter;
4+
5+
use Encore\Admin\Admin;
6+
use Encore\Admin\Grid\Filter;
7+
use Encore\Admin\Grid\Filter\AbstractFilter;
8+
9+
class Row extends AbstractFilter
10+
{
11+
12+
/**
13+
* @var Filter
14+
*/
15+
private $filter;
16+
17+
private $callback;
18+
19+
private $values;
20+
21+
/**
22+
* Where constructor.
23+
*
24+
* @param string $label
25+
* @param \Closure $query
26+
*/
27+
public function __construct($label, \Closure $callback)
28+
{
29+
$this->label = $label;
30+
$this->callback = $callback;
31+
32+
$this->setPresenter(new RowPresenter);
33+
}
34+
35+
public function setParent(Filter $filter)
36+
{
37+
parent::setParent($filter);
38+
39+
$this->filter = new Filter((function() {
40+
return $this->model;
41+
})->call($this->parent));
42+
$this->filter->removeFilterByID('id');
43+
44+
call_user_func($this->callback, $this->filter);
45+
}
46+
47+
private function setupScript()
48+
{
49+
Admin::style(<<<STYLE
50+
.group-row {
51+
display: flex;
52+
flex-wrap:wrap;
53+
}
54+
.group-row .form-group {
55+
flex: 1;
56+
margin-right: 6px;
57+
margin-bottom: 0;
58+
}
59+
.group-row .form-group label {
60+
display: none;
61+
}
62+
.group-row .form-group .col-sm-8 {
63+
width:100% !important;
64+
padding-right: 0 !important;
65+
}
66+
STYLE
67+
);
68+
}
69+
70+
public function condition($inputs)
71+
{
72+
$this->values = array_reduce($this->filter->filters(), function($values, $filter) use ($inputs) {
73+
if ($condition = $filter->condition($inputs)) {
74+
foreach($condition as $key => $value) {
75+
$values[$key][] = $value;
76+
}
77+
}
78+
return $values;
79+
});
80+
81+
if(!$this->values) {
82+
return;
83+
}
84+
85+
return [
86+
'where' => [
87+
function ($query) {
88+
array_map(function ($method, $values) use ($query) {
89+
foreach ($values as $value) {
90+
call_user_func_array([$query, $method], $value);
91+
}
92+
}, array_keys($this->values ?? []), $this->values ?? []);
93+
}
94+
]
95+
];
96+
}
97+
98+
protected function variables()
99+
{
100+
$this->id = 'distpicker-filter-' . uniqid();
101+
102+
$this->setupScript();
103+
return array_merge([
104+
'id' => $this->id,
105+
'name' => $this->formatName($this->label),
106+
'label' => $this->label,
107+
'filters' => $this->filter->filters(),
108+
'presenter' => $this->presenter(),
109+
], $this->presenter()->variables());
110+
}
111+
112+
/**
113+
* {@inheritdoc}
114+
*/
115+
public function getColumn()
116+
{
117+
$columns = [];
118+
119+
$parentName = $this->parent->getName();
120+
121+
foreach ($this->filter->filters() as $filter) {
122+
$column = $filter->getColumn();
123+
$columns[] = $parentName ? "{$parentName}_{$column}" : $column;
124+
}
125+
126+
return $columns;
127+
}
128+
}

src/RowFilter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Huozi\Admin\Filter;
4+
5+
use Encore\Admin\Extension;
6+
7+
class RowFilter extends Extension
8+
{
9+
public $name = 'laravel-admin-ext-filter';
10+
11+
public $views = __DIR__.'/../resources/views';
12+
13+
}

src/RowFilterServiceProvider.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Huozi\Admin\Filter;
4+
5+
use Encore\Admin\Facades\Admin;
6+
use Encore\Admin\Grid\Filter;
7+
use Illuminate\Support\ServiceProvider;
8+
9+
class RowFilterServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function boot(RowFilter $extension)
15+
{
16+
if (! RowFilter::boot()) {
17+
return ;
18+
}
19+
20+
if ($views = $extension->views()) {
21+
$this->loadViewsFrom($views, 'laravel-admin-ext-filter');
22+
}
23+
24+
if ($this->app->runningInConsole() && $assets = $extension->assets()) {
25+
$this->publishes(
26+
[$assets => public_path('vendor/huo-zi/laravel-admin-ext-filter')],
27+
'laravel-admin-ext-filter'
28+
);
29+
}
30+
31+
Admin::booting(function () {
32+
Filter::extend('row', Row::class);
33+
});
34+
}
35+
}

src/RowPresenter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Huozi\Admin\Filter;
4+
5+
use Encore\Admin\Grid\Filter\Presenter\Presenter;
6+
7+
class RowPresenter extends Presenter
8+
{
9+
public function view() : string
10+
{
11+
return 'laravel-admin-ext-filter::row';
12+
}
13+
}

0 commit comments

Comments
 (0)