Skip to content

Commit a98ca55

Browse files
authored
Merge pull request #153 from Laravel-Backpack/cmd-new-filter
Artisan command to generate new filter
2 parents f93957e + 8bde368 commit a98ca55

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Backpack\CRUD\ViewNamespaces;
6+
use Illuminate\Console\GeneratorCommand;
7+
use Illuminate\Support\Str;
8+
9+
class FilterBackpackCommand extends GeneratorCommand
10+
{
11+
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $name = 'backpack:filter';
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'backpack:filter {name} {--from=}';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Generate a Backpack filter';
32+
33+
/**
34+
* The type of class being generated.
35+
*
36+
* @var string
37+
*/
38+
protected $type = 'Filter';
39+
40+
/**
41+
* Get the stub file for the generator.
42+
*
43+
* @return string
44+
*/
45+
protected function getStub()
46+
{
47+
return __DIR__.'/../stubs/filter.stub';
48+
}
49+
50+
/**
51+
* Alias for the fire method.
52+
*
53+
* In Laravel 5.5 the fire() method has been renamed to handle().
54+
* This alias provides support for both Laravel 5.4 and 5.5.
55+
*/
56+
public function handle()
57+
{
58+
$this->fire();
59+
}
60+
61+
/**
62+
* Execute the console command.
63+
*
64+
* @return bool|null
65+
*/
66+
public function fire()
67+
{
68+
$name = Str::of($this->getNameInput());
69+
$path = $this->getPath($name);
70+
71+
if ($this->alreadyExists($this->getNameInput())) {
72+
$this->error("Error : $this->type $name already existed!");
73+
74+
return false;
75+
}
76+
77+
$src = null;
78+
if ($this->option('from')) {
79+
$filter = Str::of($this->option('from'));
80+
$arr = ViewNamespaces::getFor('filters');
81+
foreach ($arr as $key => $value) {
82+
$viewPath = $value.'.'.$filter;
83+
if (view()->exists($viewPath)) {
84+
$src = view($viewPath)->getPath();
85+
break;
86+
}
87+
}
88+
if ($src == null) {
89+
$this->error("Error : $this->type $filter does not exist!");
90+
91+
return false;
92+
}
93+
}
94+
95+
$this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}");
96+
$this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/crud/filters/{$name->snake('_')}.blade.php</>");
97+
98+
$this->makeDirectory($path);
99+
if ($src != null) {
100+
$this->files->copy($src, $path);
101+
} else {
102+
$this->files->put($path, $this->buildClass($name));
103+
}
104+
105+
$this->closeProgressBlock();
106+
$this->newLine();
107+
$this->info($this->type.' created successfully.');
108+
}
109+
110+
/**
111+
* Determine if the class already exists.
112+
*
113+
* @param string $name
114+
* @return bool
115+
*/
116+
protected function alreadyExists($name)
117+
{
118+
return $this->files->exists($this->getPath($name));
119+
}
120+
121+
/**
122+
* Get the destination class path.
123+
*
124+
* @param string $name
125+
* @return string
126+
*/
127+
protected function getPath($name)
128+
{
129+
$file = Str::of($name)->snake('_');
130+
131+
return resource_path("views/vendor/backpack/crud/filters/$file.blade.php");
132+
}
133+
134+
/**
135+
* Build the class with the given name.
136+
*
137+
* @param string $name
138+
* @return string
139+
*/
140+
protected function buildClass($name)
141+
{
142+
$stub = $this->files->get($this->getStub());
143+
144+
return $stub;
145+
}
146+
147+
/**
148+
* Get the console command options.
149+
*
150+
* @return array
151+
*/
152+
protected function getOptions()
153+
{
154+
return [];
155+
}
156+
}

src/Console/stubs/filter.stub

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{{-- Simple Backpack CRUD filter --}}
2+
<li filter-name="{{ $filter->name }}"
3+
filter-type="{{ $filter->type }}"
4+
filter-key="{{ $filter->key }}"
5+
class="nav-item {{ Request::get($filter->name)?'active':'' }}">
6+
<a class="nav-link" href=""
7+
parameter="{{ $filter->name }}"
8+
>{{ $filter->label }}</a>
9+
</li>
10+
11+
12+
{{-- ########################################### --}}
13+
{{-- Extra CSS and JS for this particular filter --}}
14+
15+
{{-- FILTERS EXTRA CSS --}}
16+
{{-- push things in the after_styles section --}}
17+
18+
{{-- @push('crud_list_styles')
19+
no css
20+
@endpush --}}
21+
22+
23+
{{-- FILTERS EXTRA JS --}}
24+
{{-- push things in the after_scripts section --}}
25+
26+
@push('crud_list_scripts')
27+
<script>
28+
jQuery(document).ready(function($) {
29+
$("li[filter-key={{ $filter->key }}] a").click(function(e) {
30+
e.preventDefault();
31+
32+
var parameter = $(this).attr('parameter');
33+
34+
// behaviour for ajax table
35+
var ajax_table = $("#crudTable").DataTable();
36+
var current_url = ajax_table.ajax.url();
37+
38+
if (URI(current_url).hasQuery(parameter)) {
39+
var new_url = URI(current_url).removeQuery(parameter, true);
40+
} else {
41+
var new_url = URI(current_url).addQuery(parameter, true);
42+
}
43+
44+
new_url = normalizeAmpersand(new_url.toString());
45+
46+
// replace the datatables ajax url with new_url and reload it
47+
ajax_table.ajax.url(new_url).load();
48+
49+
// add filter to URL
50+
crud.updateUrl(new_url);
51+
52+
// mark this filter as active in the navbar-filters
53+
if (URI(new_url).hasQuery('{{ $filter->name }}', true)) {
54+
$("li[filter-key={{ $filter->key }}]").removeClass('active').addClass('active');
55+
$('#remove_filters_button').removeClass('invisible');
56+
}
57+
else
58+
{
59+
$("li[filter-key={{ $filter->key }}]").trigger("filter:clear");
60+
}
61+
});
62+
63+
// clear filter event (used here and by the Remove all filters button)
64+
$("li[filter-key={{ $filter->key }}]").on('filter:clear', function(e) {
65+
66+
$("li[filter-key={{ $filter->key }}]").removeClass('active');
67+
});
68+
});
69+
</script>
70+
@endpush
71+
{{-- End of Extra CSS and JS --}}
72+
{{-- ########################################## --}}

src/GeneratorsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Backpack\Generators\Console\Commands\CrudOperationBackpackCommand;
1515
use Backpack\Generators\Console\Commands\CrudRequestBackpackCommand;
1616
use Backpack\Generators\Console\Commands\FieldBackpackCommand;
17+
use Backpack\Generators\Console\Commands\FilterBackpackCommand;
1718
use Backpack\Generators\Console\Commands\ModelBackpackCommand;
1819
use Backpack\Generators\Console\Commands\PageBackpackCommand;
1920
use Backpack\Generators\Console\Commands\PageControllerBackpackCommand;
@@ -36,6 +37,7 @@ class GeneratorsServiceProvider extends ServiceProvider
3637
CrudBackpackCommand::class,
3738
ChartBackpackCommand::class,
3839
FieldBackpackCommand::class,
40+
FilterBackpackCommand::class,
3941
ModelBackpackCommand::class,
4042
PageBackpackCommand::class,
4143
PageControllerBackpackCommand::class,

0 commit comments

Comments
 (0)