Skip to content

Commit fe146d8

Browse files
authored
feat(stisla): Stila jquery datatables related code added (#921)
docs: Stila jquery data tables related code added (#921)
1 parent 6854795 commit fe146d8

File tree

11 files changed

+368
-3
lines changed

11 files changed

+368
-3
lines changed

src/Commands/BaseCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use InfyOm\Generator\Generators\RepositoryGenerator;
1616
use InfyOm\Generator\Generators\RepositoryTestGenerator;
1717
use InfyOm\Generator\Generators\Scaffold\ControllerGenerator;
18+
use InfyOm\Generator\Generators\Scaffold\JQueryDatatableAssetsGenerator;
1819
use InfyOm\Generator\Generators\Scaffold\MenuGenerator;
1920
use InfyOm\Generator\Generators\Scaffold\RequestGenerator;
2021
use InfyOm\Generator\Generators\Scaffold\RoutesGenerator;
@@ -140,6 +141,11 @@ public function generateScaffoldItems()
140141
$menuGenerator = new MenuGenerator($this->commandData);
141142
$menuGenerator->generate();
142143
}
144+
145+
if ($this->commandData->jqueryDT()) {
146+
$assetsGenerator = new JQueryDatatableAssetsGenerator($this->commandData);
147+
$assetsGenerator->generate();
148+
}
143149
}
144150

145151
public function performPostActions($runMigration = false)
@@ -301,6 +307,7 @@ public function getOptions()
301307
['localized', null, InputOption::VALUE_NONE, 'Localize files.'],
302308
['repositoryPattern', null, InputOption::VALUE_REQUIRED, 'Repository Pattern'],
303309
['connection', null, InputOption::VALUE_REQUIRED, 'Specify connection name'],
310+
['jqueryDT', null, InputOption::VALUE_NONE, 'Generate listing screen into JQuery Datatables'],
304311
];
305312
}
306313

src/Common/CommandData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public function addDynamicVariable($name, $val)
125125
$this->dynamicVars[$name] = $val;
126126
}
127127

128+
public function jqueryDT()
129+
{
130+
return $this->getOption('jqueryDT') ? true : false;
131+
}
132+
128133
public function getFields()
129134
{
130135
$this->fields = [];

src/Common/GeneratorConfig.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class GeneratorConfig
4747
public $pathRequest;
4848
public $pathRoutes;
4949
public $pathViews;
50+
public $pathAssets;
5051
public $modelJsPath;
5152

5253
/* Model Names */
@@ -97,6 +98,7 @@ class GeneratorConfig
9798
'repositoryPattern',
9899
'localized',
99100
'connection',
101+
'jqueryDT',
100102
];
101103

102104
public $tableName;
@@ -221,6 +223,11 @@ public function loadPaths()
221223
resource_path('views/')
222224
).$viewPrefix.$this->mSnakePlural.'/';
223225

226+
$this->pathAssets = config(
227+
'infyom.laravel_generator.path.assets',
228+
resource_path('assets/')
229+
);
230+
224231
$this->pathSeeder = config('infyom.laravel_generator.path.seeder', database_path('seeders/'));
225232
$this->pathDatabaseSeeder = config('infyom.laravel_generator.path.database_seeder', database_path('seeders/DatabaseSeeder.php'));
226233
$this->pathViewProvider = config(

src/Generators/Scaffold/ControllerGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function generate()
4343

4444
$templateData = get_template("scaffold.controller.$templateName", 'laravel-generator');
4545

46+
$this->generateDataTable();
47+
} elseif ($this->commandData->jqueryDT()) {
48+
$templateName = 'jquery_datatable_controller';
49+
$templateData = get_template("scaffold.controller.$templateName", 'laravel-generator');
50+
4651
$this->generateDataTable();
4752
} else {
4853
if ($this->commandData->getOption('repositoryPattern')) {
@@ -75,7 +80,7 @@ public function generate()
7580

7681
private function generateDataTable()
7782
{
78-
$templateName = 'datatable';
83+
$templateName = ($this->commandData->jqueryDT()) ? 'jquery_datatable' : 'datatable';
7984
if ($this->commandData->isLocalizedTemplates()) {
8085
$templateName .= '_locale';
8186
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace InfyOm\Generator\Generators\Scaffold;
4+
5+
use InfyOm\Generator\Common\CommandData;
6+
use InfyOm\Generator\Generators\BaseGenerator;
7+
use InfyOm\Generator\Utils\FileUtil;
8+
9+
/**
10+
* Class JQueryDatatableAssetsGenerator.
11+
*/
12+
class JQueryDatatableAssetsGenerator extends BaseGenerator
13+
{
14+
/** @var CommandData */
15+
private $commandData;
16+
17+
/** @var string */
18+
private $path;
19+
20+
/** @var string */
21+
private $fileName;
22+
23+
private $config;
24+
25+
public function __construct(CommandData $commandData)
26+
{
27+
$this->commandData = $commandData;
28+
$this->path = $commandData->config->pathAssets.'js/';
29+
$this->config = $this->commandData->config;
30+
$this->fileName = $this->config->tableName.'.js';
31+
}
32+
33+
public function generate()
34+
{
35+
$this->generateJquery();
36+
}
37+
38+
public function generateJquery()
39+
{
40+
$templateName = 'jquery';
41+
42+
if ($this->commandData->isLocalizedTemplates()) {
43+
$templateName .= '_locale';
44+
}
45+
46+
$columnsCount = 0;
47+
48+
$fields = '';
49+
foreach ($this->commandData->fields as $field) {
50+
if (in_array($field->name, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
51+
continue;
52+
}
53+
54+
$fields .= "{
55+
data: '$field->name',
56+
name: '$field->name'
57+
},";
58+
59+
$columnsCount++;
60+
}
61+
62+
// Publish Datatable JS file
63+
$templateData = get_template('scaffold.'.$templateName, 'laravel-generator');
64+
$templateData = fill_template($this->commandData->dynamicVars, $templateData);
65+
$templateData = str_replace('$ACTION_COLUMN_COUNT$', $columnsCount, $templateData);
66+
$templateData = str_replace('$JQUERY_FIELDS$', $fields, $templateData);
67+
68+
$path = $this->path.$this->config->tableName.'/';
69+
if (!file_exists($path)) {
70+
FileUtil::createDirectoryIfNotExist($path);
71+
}
72+
file_put_contents($path.$this->fileName, $templateData);
73+
$this->commandData->commandComment("\n".$this->config->tableName.' assets added.');
74+
75+
// Publish JS Rendere Template
76+
$templateName = 'js_renderer_template';
77+
$templateData = get_template('scaffold.'.$templateName, 'laravel-generator');
78+
$templateData = fill_template($this->commandData->dynamicVars, $templateData);
79+
80+
$path = $this->config->pathViews.'templates/';
81+
if (!file_exists($path)) {
82+
FileUtil::createDirectoryIfNotExist($path);
83+
}
84+
85+
file_put_contents($path.'templates.php', $templateData);
86+
$this->commandData->commandComment("\n".'JS Render Templates added.');
87+
88+
// Publish Webpack mix lines
89+
$webpackMixContents = file_get_contents(base_path('webpack.mix.js'));
90+
$templateName = 'webpack_mix_js';
91+
$templateData = get_template('scaffold.'.$templateName, 'laravel-generator');
92+
$templateData = fill_template($this->commandData->dynamicVars, $templateData);
93+
$webpackMixContents .= "\n\n".$templateData;
94+
95+
file_put_contents(base_path('webpack.mix.js'), $webpackMixContents);
96+
$this->commandData->commandComment("\n".$this->commandData->config->mCamelPlural.' webpack.mix.js updated.');
97+
}
98+
}

src/Generators/Scaffold/ViewGenerator.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ private function generateBladeTableBody()
123123
{
124124
$templateName = 'blade_table_body';
125125

126+
$tableFields = $this->generateTableHeaderFields();
127+
if ($this->commandData->jqueryDT()) {
128+
$templateName = 'js_table';
129+
$tableFields = $this->generateJSTableHeaderFields();
130+
}
131+
126132
if ($this->commandData->isLocalizedTemplates()) {
127133
$templateName .= '_locale';
128134
}
@@ -131,7 +137,7 @@ private function generateBladeTableBody()
131137

132138
$templateData = fill_template($this->commandData->dynamicVars, $templateData);
133139

134-
$templateData = str_replace('$FIELD_HEADERS$', $this->generateTableHeaderFields(), $templateData);
140+
$templateData = str_replace('$FIELD_HEADERS$', $tableFields, $templateData);
135141

136142
$cellFieldTemplate = get_template('scaffold.views.table_cell', $this->templateType);
137143

@@ -155,6 +161,20 @@ private function generateBladeTableBody()
155161
return str_replace('$FIELD_BODY$', $tableBodyFields, $templateData);
156162
}
157163

164+
private function generateJSTableHeaderFields()
165+
{
166+
$fields = '';
167+
foreach ($this->commandData->fields as $field) {
168+
if (in_array($field->name, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
169+
continue;
170+
}
171+
172+
$fields .= '<th scope="col">'.str_replace("'", '', $field->name).'</th>';
173+
}
174+
175+
return $fields;
176+
}
177+
158178
private function generateTableHeaderFields()
159179
{
160180
$templateName = 'table_header';
@@ -207,7 +227,7 @@ private function generateTableHeaderFields()
207227

208228
private function generateIndex()
209229
{
210-
$templateName = 'index';
230+
$templateName = ($this->commandData->jqueryDT()) ? 'js_index' : 'index';
211231

212232
if ($this->commandData->isLocalizedTemplates()) {
213233
$templateName .= '_locale';
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace $NAMESPACE_CONTROLLER$;
4+
5+
use $NAMESPACE_DATATABLES$\$MODEL_NAME$DataTable;
6+
use $NAMESPACE_REQUEST$;
7+
use Illuminate\Http\Request;
8+
use $NAMESPACE_REQUEST$\Create$MODEL_NAME$Request;
9+
use $NAMESPACE_REQUEST$\Update$MODEL_NAME$Request;
10+
use $NAMESPACE_REPOSITORY$\$MODEL_NAME$Repository;
11+
use Flash;
12+
use $NAMESPACE_APP$\Http\Controllers\AppBaseController;
13+
use Response;
14+
use Datatables;
15+
16+
class $MODEL_NAME$Controller extends AppBaseController
17+
{
18+
/** @var $MODEL_NAME$Repository */
19+
private $$MODEL_NAME_CAMEL$Repository;
20+
21+
public function __construct($MODEL_NAME$Repository $$MODEL_NAME_CAMEL$Repo)
22+
{
23+
$this->$MODEL_NAME_CAMEL$Repository = $$MODEL_NAME_CAMEL$Repo;
24+
}
25+
26+
/**
27+
* Display a listing of the $MODEL_NAME$.
28+
*
29+
* @param Request $request
30+
* @return Response
31+
*/
32+
public function index(Request $request)
33+
{
34+
if ($request->ajax()) {
35+
return Datatables::of((new $MODEL_NAME$DataTable())->get())->make(true);
36+
}
37+
38+
return view('$VIEW_PREFIX$$MODEL_NAME_PLURAL_SNAKE$.index');
39+
}
40+
41+
/**
42+
* Show the form for creating a new $MODEL_NAME$.
43+
*
44+
* @return Response
45+
*/
46+
public function create()
47+
{
48+
return view('$VIEW_PREFIX$$MODEL_NAME_PLURAL_SNAKE$.create');
49+
}
50+
51+
/**
52+
* Store a newly created $MODEL_NAME$ in storage.
53+
*
54+
* @param Create$MODEL_NAME$Request $request
55+
*
56+
* @return Response
57+
*/
58+
public function store(Create$MODEL_NAME$Request $request)
59+
{
60+
$input = $request->all();
61+
62+
$$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->create($input);
63+
64+
Flash::success('$MODEL_NAME_HUMAN$ saved successfully.');
65+
66+
return redirect(route('$ROUTE_NAMED_PREFIX$$MODEL_NAME_PLURAL_CAMEL$.index'));
67+
}
68+
69+
/**
70+
* Display the specified $MODEL_NAME$.
71+
*
72+
* @param int $id
73+
*
74+
* @return Response
75+
*/
76+
public function show($id)
77+
{
78+
$$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id);
79+
80+
if (empty($$MODEL_NAME_CAMEL$)) {
81+
Flash::error('$MODEL_NAME_HUMAN$ not found');
82+
83+
return redirect(route('$ROUTE_NAMED_PREFIX$$MODEL_NAME_PLURAL_CAMEL$.index'));
84+
}
85+
86+
return view('$VIEW_PREFIX$$MODEL_NAME_PLURAL_SNAKE$.show')->with('$MODEL_NAME_CAMEL$', $$MODEL_NAME_CAMEL$);
87+
}
88+
89+
/**
90+
* Show the form for editing the specified $MODEL_NAME$.
91+
*
92+
* @param int $id
93+
*
94+
* @return Response
95+
*/
96+
public function edit($id)
97+
{
98+
$$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id);
99+
100+
if (empty($$MODEL_NAME_CAMEL$)) {
101+
Flash::error('$MODEL_NAME_HUMAN$ not found');
102+
103+
return redirect(route('$ROUTE_NAMED_PREFIX$$MODEL_NAME_PLURAL_CAMEL$.index'));
104+
}
105+
106+
return view('$VIEW_PREFIX$$MODEL_NAME_PLURAL_SNAKE$.edit')->with('$MODEL_NAME_CAMEL$', $$MODEL_NAME_CAMEL$);
107+
}
108+
109+
/**
110+
* Update the specified $MODEL_NAME$ in storage.
111+
*
112+
* @param int $id
113+
* @param Update$MODEL_NAME$Request $request
114+
*
115+
* @return Response
116+
*/
117+
public function update($id, Update$MODEL_NAME$Request $request)
118+
{
119+
$$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id);
120+
121+
if (empty($$MODEL_NAME_CAMEL$)) {
122+
Flash::error('$MODEL_NAME_HUMAN$ not found');
123+
124+
return redirect(route('$ROUTE_NAMED_PREFIX$$MODEL_NAME_PLURAL_CAMEL$.index'));
125+
}
126+
127+
$$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->update($request->all(), $id);
128+
129+
Flash::success('$MODEL_NAME_HUMAN$ updated successfully.');
130+
131+
return redirect(route('$ROUTE_NAMED_PREFIX$$MODEL_NAME_PLURAL_CAMEL$.index'));
132+
}
133+
134+
/**
135+
* Remove the specified $MODEL_NAME$ from storage.
136+
*
137+
* @param int $id
138+
*
139+
* @return Response
140+
*/
141+
public function destroy($id)
142+
{
143+
$$MODEL_NAME_CAMEL$ = $this->$MODEL_NAME_CAMEL$Repository->find($id);
144+
145+
$$MODEL_NAME_CAMEL$->delete();
146+
147+
return $this->sendSuccess('$MODEL_NAME_HUMAN$ deleted successfully.');
148+
}
149+
}

0 commit comments

Comments
 (0)