5
5
use CodexShaper \DBM \Database \Drivers \MongoDB \Type ;
6
6
use CodexShaper \DBM \Database \Schema \Table ;
7
7
use CodexShaper \DBM \Facades \Driver ;
8
- use CodexShaper \DBM \Models \ DBM_Collection ;
8
+ use CodexShaper \DBM \Traits \ RecordHelper ;
9
9
use DBM ;
10
10
use Illuminate \Http \Request ;
11
- use Illuminate \Support \Facades \DB ;
12
- use Illuminate \Support \Facades \Validator ;
13
11
use Illuminate \Support \Str ;
14
12
15
13
class RecordController extends Controller
16
14
{
15
+ use RecordHelper;
17
16
18
17
public function index ()
19
18
{
@@ -340,7 +339,6 @@ public function delete(Request $request)
340
339
// $originalColumns = Table::getColumnsName($tableName);
341
340
$ columns = json_decode ($ request ->columns );
342
341
$ fields = $ request ->fields ;
343
-
344
342
$ object = DBM ::Object ()->where ('name ' , $ tableName )->first ();
345
343
$ model = $ object ->model ;
346
344
$ details = $ object ->details ;
@@ -351,18 +349,14 @@ public function delete(Request $request)
351
349
}
352
350
353
351
try {
354
-
355
352
$ table = DBM ::model ($ model , $ tableName )->where ($ key , $ columns ->{$ key })->first ();
356
-
357
353
if ($ table ) {
358
-
354
+ // Remove Relationship data
359
355
foreach ($ fields as $ field ) {
360
-
361
356
$ field = json_decode ($ field );
362
-
363
357
$ this ->removeRelationshipData ($ field , $ object , $ table );
364
358
}
365
-
359
+ // Check Table deleted successfully
366
360
if ($ table ->delete ()) {
367
361
return response ()->json (['success ' => true ]);
368
362
}
@@ -376,65 +370,6 @@ public function delete(Request $request)
376
370
return response ()->json (['success ' => false ]);
377
371
}
378
372
379
- public function removeRelationshipData ($ field , $ object , $ table )
380
- {
381
- if ($ field ->type == 'relationship ' ) {
382
-
383
- $ relationship = $ field ->settings ;
384
-
385
- $ localModel = $ relationship ->localModel ;
386
- $ foreignModel = $ relationship ->foreignModel ;
387
-
388
- $ findColumn = $ object ->details ['findColumn ' ];
389
-
390
- $ localObject = $ localModel ::where ($ findColumn , $ table ->{$ findColumn })->first ();
391
-
392
- if ($ relationship ->relationType == 'belongsToMany ' ) {
393
-
394
- $ pivotTable = $ relationship ->pivotTable ;
395
- $ parentPivotKey = $ relationship ->parentPivotKey ;
396
- $ relatedPivotKey = $ relationship ->relatedPivotKey ;
397
-
398
- DBM ::Object ()
399
- ->setManyToManyRelation (
400
- $ localObject ,
401
- $ foreignModel ,
402
- $ pivotTable ,
403
- $ parentPivotKey ,
404
- $ relatedPivotKey
405
- )
406
- ->belongs_to_many ()
407
- ->detach ();
408
- } else if ($ relationship ->relationType == 'hasMany ' ) {
409
-
410
- $ foreignKey = $ relationship ->foreignKey ;
411
- $ localKey = $ relationship ->localKey ;
412
-
413
- DBM ::Object ()
414
- ->setCommonRelation (
415
- $ localObject ,
416
- $ foreignModel ,
417
- $ foreignKey ,
418
- $ localKey )
419
- ->has_many ()
420
- ->delete ();
421
- }
422
-
423
- }
424
- }
425
-
426
- protected function getSettingOptions ($ field )
427
- {
428
- $ options = $ field ->settings ['options ' ];
429
- if (isset ($ options ['controller ' ])) {
430
- $ partials = explode ('@ ' , $ options ['controller ' ]);
431
- $ controllerName = $ partials [0 ];
432
- $ methodName = $ partials [1 ];
433
-
434
- return app ($ controllerName )->{$ methodName }();
435
- }
436
- }
437
-
438
373
public function getTableDetails (Request $ request )
439
374
{
440
375
if ($ request ->ajax ()) {
@@ -643,95 +578,4 @@ public function getTableDetails(Request $request)
643
578
644
579
return response ()->json (['success ' => false ]);
645
580
}
646
-
647
- public function removeRelationshipKeyForBelongsTo ($ fields , $ foreignKey )
648
- {
649
- $ results = [];
650
-
651
- foreach ($ fields as $ key => $ field ) {
652
- if ($ field ->name == $ foreignKey ) {
653
- unset($ fields [$ key ]);
654
- continue ;
655
- }
656
- $ results [] = $ field ;
657
- }
658
-
659
- return $ results ;
660
- }
661
-
662
- protected function validation ($ fields , $ columns , $ action = "create " )
663
- {
664
- $ errors = [];
665
- foreach ($ fields as $ field ) {
666
- $ name = $ field ->name ;
667
-
668
- if (is_object ($ field ->settings ) && property_exists ($ field ->settings , 'validation ' ) !== false ) {
669
-
670
- $ validationSettings = $ field ->settings ->validation ;
671
- $ rules = $ this ->prepareRules ($ columns , $ action , $ validationSettings );
672
- $ data = [$ name => $ columns ->{$ name }];
673
- $ validator = Validator::make ($ data , [$ name => $ rules ]);
674
- if ($ validator ->fails ()) {
675
- foreach ($ validator ->errors ()->all () as $ error ) {
676
- $ errors [] = $ error ;
677
- }
678
- }
679
- }
680
- }
681
-
682
- return $ errors ;
683
- }
684
-
685
- protected function prepareRules ($ columns , $ action , $ settings )
686
- {
687
- $ rules = '' ;
688
-
689
- if (is_string ($ settings )) {
690
- $ rules = $ settings ;
691
- } else if ($ action == 'create ' && isset ($ settings ->create )) {
692
- $ createSettings = $ settings ->create ;
693
- $ rules = $ createSettings ->rules ;
694
- } else if ($ action == 'update ' && isset ($ settings ->update )) {
695
- $ updateSettings = $ settings ->update ;
696
- $ localKey = $ updateSettings ->localKey ;
697
- $ rules = $ updateSettings ->rules . ', ' . $ columns ->{$ localKey };
698
- }
699
-
700
- return $ rules ;
701
- }
702
-
703
- protected function getFieldType ($ collectionName , $ fieldName )
704
- {
705
- $ collection = DBM_Collection::where ('name ' , $ collectionName )->first ();
706
-
707
- return $ collection ->fields ()->where ('name ' , $ fieldName )->first ()->type ;
708
- }
709
-
710
- protected function generateError ($ errors )
711
- {
712
- return response ()->json ([
713
- 'success ' => false ,
714
- 'errors ' => $ errors ,
715
- ], 400 );
716
- }
717
-
718
- protected function hasFunction ($ fields , $ column )
719
- {
720
- foreach ($ fields as $ field ) {
721
- if ($ field ->name == $ column && ($ field ->function_name != null || $ field ->function_name != "" )) {
722
- return $ field ->function_name ;
723
- }
724
- }
725
-
726
- return false ;
727
- }
728
-
729
- protected function executeFunction ($ functionName , $ value = null )
730
- {
731
- $ signature = ($ value != null ) ? "{$ functionName }(' {$ value }') " : "{$ functionName }() " ;
732
-
733
- $ result = DB ::raw ("{$ signature }" );
734
-
735
- return $ result ;
736
- }
737
581
}
0 commit comments