Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 24db1f6

Browse files
authored
Merge pull request #12 from lk77/master
Adding model-namespace, columns and action options to Generator
2 parents 93a786e + 4f54ae2 commit 24db1f6

File tree

3 files changed

+96
-13
lines changed

3 files changed

+96
-13
lines changed

‎src/Generators/DataTablesMakeCommand.php‎

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
class DataTablesMakeCommand extends GeneratorCommand
1616
{
1717
/**
18-
* The console command name.
18+
* The name and signature of the console command.
1919
*
2020
* @var string
2121
*/
22-
protected $name = 'datatables:make';
22+
protected $signature = 'datatables:make
23+
{name : The name of the datatable.}
24+
{--model : The name of the model to be used.}
25+
{--model-namespace= : The namespace of the model to be used.}
26+
{--action= : The path of the action view.}
27+
{--columns= : The columns of the datatable.}';
2328

2429
/**
2530
* The console command description.
@@ -47,14 +52,16 @@ protected function buildClass($name)
4752

4853
return $this->replaceModelImport($stub)
4954
->replaceModel($stub)
55+
->replaceColumns($stub)
56+
->replaceAction($stub)
5057
->replaceFilename($stub);
5158
}
5259

5360
/**
5461
* Replace model name.
5562
*
5663
* @param string $stub
57-
* @return mixed
64+
* @return this
5865
*/
5966
protected function replaceModel(&$stub)
6067
{
@@ -72,13 +79,82 @@ protected function getModel()
7279
{
7380
$name = $this->getNameInput();
7481
$rootNamespace = $this->laravel->getNamespace();
75-
$modelNamespace = $this->laravel['config']->get('datatables-buttons.namespace.model');
82+
$model = $this->option('model') || $this->option('model-namespace');
83+
$modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model');
7684

77-
return $this->option('model')
78-
? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") . str_singular($name)
85+
return $model
86+
? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") . str_singular($name)
7987
: $rootNamespace . "\\User";
8088
}
8189

90+
/**
91+
* Replace columns.
92+
*
93+
* @param string $stub
94+
* @return $this
95+
*/
96+
protected function replaceColumns(&$stub)
97+
{
98+
$stub = str_replace(
99+
'DummyColumns', $this->getColumns(), $stub
100+
);
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* Get the columns to be used.
107+
*
108+
* @return string
109+
*/
110+
protected function getColumns()
111+
{
112+
if($this->option('columns') != ''){
113+
return $this->parseArray($this->option('columns'));
114+
}else{
115+
return $this->parseArray('id,add your columns,created_at,updated_at');
116+
}
117+
}
118+
119+
/**
120+
* Parse array from definition
121+
*
122+
* @param string $definition
123+
* @param string $delimiter
124+
* @param int $indentation
125+
* @return string
126+
*/
127+
protected function parseArray($definition, $delimiter = ',', $indentation = 12)
128+
{
129+
return str_replace($delimiter, "',\n" . str_repeat('', $indentation) . "'", $definition);
130+
}
131+
132+
/**
133+
* Replace the action.
134+
*
135+
* @param string $stub
136+
* @return this
137+
*/
138+
protected function replaceAction(&$stub)
139+
{
140+
$stub = str_replace(
141+
'DummyAction', $this->getAction(), $stub
142+
);
143+
144+
return $this;
145+
}
146+
147+
/**
148+
* Set the action view to be used.
149+
*
150+
* @return string
151+
*/
152+
protected function getAction()
153+
{
154+
return $this->option('action') ? $this->option('action') : Str::lower($this->getNameInput()) . '.action';
155+
156+
}
157+
82158
/**
83159
* Replace model import.
84160
*
@@ -101,7 +177,10 @@ protected function replaceModelImport(&$stub)
101177
*/
102178
protected function getStub()
103179
{
104-
return __DIR__ . '/stubs/datatables.stub';
180+
$config = $this->laravel['config'];
181+
return $config->get('datatables-buttons.stub')
182+
? base_path() . $config->get('datatables-buttons.stub') . '/datatables.stub'
183+
: __DIR__ . '/stubs/datatables.stub';
105184
}
106185

107186
/**
@@ -128,6 +207,8 @@ protected function getOptions()
128207
{
129208
return [
130209
['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null],
210+
['action', null, InputOption::VALUE_OPTIONAL, 'Path to action column template.', null],
211+
['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null],
131212
];
132213
}
133214

‎src/Generators/stubs/datatables.stub‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class DummyClass extends DataTable
1616
{
1717
return $this->datatables
1818
->eloquent($this->query())
19-
->addColumn('action', 'path.to.action.view');
19+
->addColumn('action', 'DummyAction');
2020
}
2121

2222
/**
@@ -26,7 +26,7 @@ class DummyClass extends DataTable
2626
*/
2727
public function query()
2828
{
29-
$query = ModelName::query();
29+
$query = ModelName::query()->select($this->getColumns());
3030

3131
return $this->applyScopes($query);
3232
}
@@ -53,10 +53,7 @@ class DummyClass extends DataTable
5353
protected function getColumns()
5454
{
5555
return [
56-
'id',
57-
// add your columns
58-
'created_at',
59-
'updated_at',
56+
'DummyColumns'
6057
];
6158
}
6259

‎src/config/config.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
'model' => '',
2727
],
2828

29+
/**
30+
* Set Custom stub folder
31+
*/
32+
//'stub' => '/resources/custom_stub',
33+
2934
/**
3035
* PDF generator to be used when converting the table to pdf.
3136
* Available generators: excel, snappy

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /