5
5
use BalajiDharma \LaravelFormBuilder \Facades \FormBuilder ;
6
6
use Exception ;
7
7
use Illuminate \Database \Eloquent \Builder ;
8
+ use Illuminate \Http \Request ;
8
9
use Illuminate \Support \Facades \Config ;
9
10
use Illuminate \Support \Facades \DB ;
10
11
use Illuminate \Support \Facades \Schema ;
11
- use Illuminate \Http \Request ;
12
12
13
13
class CrudBuilder
14
14
{
@@ -76,12 +76,14 @@ public function setIdentifier()
76
76
public function setAddtional ($ addtional )
77
77
{
78
78
$ this ->addtional = array_merge ($ this ->addtional , $ addtional );
79
+
79
80
return $ this ;
80
81
}
81
82
82
83
public function setTitle ($ title )
83
84
{
84
85
$ this ->title = $ title ;
86
+
85
87
return $ this ;
86
88
}
87
89
@@ -92,18 +94,21 @@ public function setRedirectUrl($redirectUrl = null)
92
94
} else {
93
95
$ this ->redirectUrl = url ()->current ();
94
96
}
97
+
95
98
return $ this ;
96
99
}
97
100
98
101
public function setDisplayFilters (bool $ displayFilters = true )
99
102
{
100
103
$ this ->displayFilters = $ displayFilters ;
104
+
101
105
return $ this ;
102
106
}
103
107
104
108
public function setDisplaySearch (bool $ displaySearch = true )
105
109
{
106
110
$ this ->displaySearch = $ displaySearch ;
111
+
107
112
return $ this ;
108
113
}
109
114
@@ -185,16 +190,15 @@ private function buildRows($columns = [])
185
190
$ tableName = $ model ->getTable ();
186
191
187
192
// Get all column names and their types for the table
188
- $ tableColumns = Schema::getColumnListing ($ tableName );
189
-
193
+ $ tableColumns = Schema::getColumns ($ tableName );
190
194
foreach ($ columns as $ column ) {
191
195
$ attribute = $ column ['attribute ' ] ?? null ;
192
196
$ type = $ column ['type ' ] ?? 'custom ' ;
193
197
$ fillable = false ;
194
198
$ primaryKey = false ;
195
-
196
- if ($ attribute && ! isset ($ column ['type ' ]) && in_array ( $ attribute , $ tableColumns ) ) {
197
- $ type = DB :: getSchemaBuilder ()-> getColumnType ( $ tableName , $ attribute ) ;
199
+ $ tableColumn = collect ( $ tableColumns )-> where ( ' name ' , $ attribute )-> first ();
200
+ if ($ attribute && ! isset ($ column ['type ' ]) && $ tableColumn ) {
201
+ $ type = $ tableColumn [ ' type_name ' ] ;
198
202
$ type = $ this ->crudHelper ->getInputType ($ type );
199
203
$ primaryKey = $ attribute == $ model ->getKeyName ();
200
204
}
@@ -253,7 +257,7 @@ public function applyFilters()
253
257
}
254
258
}
255
259
}
256
- if (!$ hasFilters ) {
260
+ if (! $ hasFilters ) {
257
261
$ this ->displayFilters = false ;
258
262
}
259
263
}
@@ -306,19 +310,29 @@ public function applySorting()
306
310
}
307
311
$ field = collect ($ this ->fields )->where ('attribute ' , $ attribute )->first () ?? null ;
308
312
309
- if ($ field && isset ($ field ['sortable ' ])) {
310
- if (isset ($ field ['relation ' ])) {
311
- $ relation = $ field ['relation ' ];
312
- $ relationField = $ field ['relation_field ' ] ?? $ field ['attribute ' ];
313
- $ this ->dataProvider ->whereHas ($ relation , function ($ q ) use ($ relationField , $ sortOrder ) {
314
- $ table = $ q ->getModel ()->getTable ();
315
- $ qualifiedField = "{$ table }. {$ relationField }" ;
316
- $ q ->orderBy ($ qualifiedField , $ sortOrder );
317
- });
318
- } else {
319
- $ table = $ this ->dataProvider ->getModel ()->getTable ();
320
- $ this ->dataProvider ->orderBy ("{$ table }. {$ attribute }" , $ sortOrder );
321
- }
313
+ $ this ->applyFieldSort ($ field , $ sortOrder , $ attribute );
314
+ } else {
315
+ $ field = collect ($ this ->fields )->whereNotNull ('defaultSort ' )->first () ?? null ;
316
+ if ($ field ) {
317
+ $ this ->applyFieldSort ($ field , $ field ['defaultSort ' ], $ field ['attribute ' ]);
318
+ }
319
+ }
320
+ }
321
+
322
+ public function applyFieldSort ($ field , $ sortOrder , $ attribute )
323
+ {
324
+ if ($ field && isset ($ field ['sortable ' ])) {
325
+ if (isset ($ field ['relation ' ])) {
326
+ $ relation = $ field ['relation ' ];
327
+ $ relationField = $ field ['relation_field ' ] ?? $ field ['attribute ' ];
328
+ $ this ->dataProvider ->whereHas ($ relation , function ($ q ) use ($ relationField , $ sortOrder ) {
329
+ $ table = $ q ->getModel ()->getTable ();
330
+ $ qualifiedField = "{$ table }. {$ relationField }" ;
331
+ $ q ->orderBy ($ qualifiedField , $ sortOrder );
332
+ });
333
+ } else {
334
+ $ table = $ this ->dataProvider ->getModel ()->getTable ();
335
+ $ this ->dataProvider ->orderBy ("{$ table }. {$ attribute }" , $ sortOrder );
322
336
}
323
337
}
324
338
}
@@ -362,23 +376,42 @@ public function buildRoutes($mainRoute = null)
362
376
$ mainRoute = substr ($ routeName , 0 , strrpos ($ routeName , '. ' ));
363
377
}
364
378
365
- return [
366
- 'index ' => $ this ->route ($ mainRoute .'.index ' ),
367
- 'create ' => $ this ->route ($ mainRoute .'.create ' ),
368
- 'store ' => $ this ->route ($ mainRoute .'.store ' ),
369
- 'edit ' => function ($ id ) use ($ mainRoute ) {
370
- return $ this ->route ($ mainRoute .'.edit ' , $ id );
371
- },
372
- 'update ' => function ($ id ) use ($ mainRoute ) {
373
- return $ this ->route ($ mainRoute .'.update ' , $ id );
374
- },
375
- 'show ' => function ($ id ) use ($ mainRoute ) {
376
- return $ this ->route ($ mainRoute .'.show ' , $ id );
377
- },
378
- 'destroy ' => function ($ id ) use ($ mainRoute ) {
379
- return $ this ->route ($ mainRoute .'.destroy ' , $ id );
380
- },
381
- ];
379
+ $ routes = [];
380
+ foreach ($ this ->getActions () as $ action => $ value ) {
381
+ if (!$ value ) {
382
+ continue ;
383
+ }
384
+ switch ($ action ) {
385
+ case 'create ' :
386
+ $ routes ['create ' ] = $ this ->route ($ mainRoute .'.create ' );
387
+ break ;
388
+ case 'store ' :
389
+ $ routes ['store ' ] = $ this ->route ($ mainRoute .'.store ' );
390
+ break ;
391
+ case 'edit ' :
392
+ $ routes ['edit ' ] = function ($ id ) use ($ mainRoute ) {
393
+ return $ this ->route ($ mainRoute .'.edit ' , $ id );
394
+ };
395
+ break ;
396
+ case 'update ' :
397
+ $ routes ['update ' ] = function ($ id ) use ($ mainRoute ) {
398
+ return $ this ->route ($ mainRoute .'.update ' , $ id );
399
+ };
400
+ break ;
401
+ case 'show ' :
402
+ $ routes ['show ' ] = function ($ id ) use ($ mainRoute ) {
403
+ return $ this ->route ($ mainRoute .'.show ' , $ id );
404
+ };
405
+ break ;
406
+ case 'destroy ' :
407
+ $ routes ['destroy ' ] = function ($ id ) use ($ mainRoute ) {
408
+ return $ this ->route ($ mainRoute .'.destroy ' , $ id );
409
+ };
410
+ break ;
411
+ }
412
+ }
413
+
414
+ return $ routes ;
382
415
}
383
416
384
417
public function render ($ view )
@@ -412,7 +445,7 @@ protected function commonRenderData()
412
445
'identifier ' => $ this ->identifier ,
413
446
'redirectUrl ' => $ this ->redirectUrl ,
414
447
'displaySearch ' => $ this ->displaySearch ,
415
- 'displayFilters ' => $ this ->displayFilters
448
+ 'displayFilters ' => $ this ->displayFilters ,
416
449
];
417
450
}
418
451
@@ -454,7 +487,7 @@ public function buildForm()
454
487
}
455
488
}
456
489
457
- if ($ this ->request ->input ('_redirect ' )){
490
+ if ($ this ->request ->input ('_redirect ' )) {
458
491
$ form ->add ('_redirect ' , 'hidden ' , [
459
492
'value ' => $ this ->request ->input ('_redirect ' ),
460
493
]);
@@ -469,8 +502,7 @@ public function buildForm()
469
502
470
503
public function route ($ name , $ parameters = [], $ absolute = true )
471
504
{
472
- if ($ this ->redirectUrl )
473
- {
505
+ if ($ this ->redirectUrl ) {
474
506
return $ this ->mergeQueryParams (route ($ name , $ parameters ), ['_redirect ' => $ this ->redirectUrl ]);
475
507
} else {
476
508
return route ($ name , $ parameters );
@@ -495,18 +527,52 @@ public function mergeQueryParams($url, array $parameters = [])
495
527
496
528
// Remove null/empty parameters
497
529
$ query = array_filter ($ query , function ($ value ) {
498
- return !is_null ($ value ) && $ value !== '' ;
530
+ return ! is_null ($ value ) && $ value !== '' ;
499
531
});
500
532
501
533
// Build base URL without query string
502
534
$ baseUrl = explode ('? ' , $ url )[0 ];
503
535
504
536
// Return URL with merged query parameters
505
537
return $ query
506
- ? $ baseUrl . '? ' . http_build_query ($ query )
538
+ ? $ baseUrl. '? ' . http_build_query ($ query )
507
539
: $ baseUrl ;
508
540
} catch (\Exception $ e ) {
509
541
return $ url ;
510
542
}
511
543
}
544
+
545
+ public function getActions ()
546
+ {
547
+ $ default = [
548
+ 'index ' => true ,
549
+ 'create ' => true ,
550
+ 'store ' => true ,
551
+ 'edit ' => true ,
552
+ 'update ' => true ,
553
+ 'show ' => true ,
554
+ 'destroy ' => true ,
555
+ ];
556
+ return array_merge ($ default , $ this ->actions ?? []);
557
+ }
558
+
559
+
560
+ public function setActions (array $ actions )
561
+ {
562
+ $ this ->actions = $ actions ;
563
+ }
564
+
565
+
566
+ public function getAction ($ action )
567
+ {
568
+ $ actions = $ this ->getActions ();
569
+ return $ actions [$ action ] ?? false ;
570
+ }
571
+
572
+ public function setAction ($ action , $ value )
573
+ {
574
+ $ actions = $ this ->getActions ();
575
+ $ actions [$ action ] = $ value ;
576
+ $ this ->setActions ($ actions );
577
+ }
512
578
}
0 commit comments