Skip to content

Commit 3d27ba5

Browse files
committed
Updated RecordController and splited Record Helper methods
1 parent 3739d95 commit 3d27ba5

File tree

2 files changed

+165
-160
lines changed

2 files changed

+165
-160
lines changed

src/Http/Controllers/RecordController.php

+4-160
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
use CodexShaper\DBM\Database\Drivers\MongoDB\Type;
66
use CodexShaper\DBM\Database\Schema\Table;
77
use CodexShaper\DBM\Facades\Driver;
8-
use CodexShaper\DBM\Models\DBM_Collection;
8+
use CodexShaper\DBM\Traits\RecordHelper;
99
use DBM;
1010
use Illuminate\Http\Request;
11-
use Illuminate\Support\Facades\DB;
12-
use Illuminate\Support\Facades\Validator;
1311
use Illuminate\Support\Str;
1412

1513
class RecordController extends Controller
1614
{
15+
use RecordHelper;
1716

1817
public function index()
1918
{
@@ -340,7 +339,6 @@ public function delete(Request $request)
340339
// $originalColumns = Table::getColumnsName($tableName);
341340
$columns = json_decode($request->columns);
342341
$fields = $request->fields;
343-
344342
$object = DBM::Object()->where('name', $tableName)->first();
345343
$model = $object->model;
346344
$details = $object->details;
@@ -351,18 +349,14 @@ public function delete(Request $request)
351349
}
352350

353351
try {
354-
355352
$table = DBM::model($model, $tableName)->where($key, $columns->{$key})->first();
356-
357353
if ($table) {
358-
354+
// Remove Relationship data
359355
foreach ($fields as $field) {
360-
361356
$field = json_decode($field);
362-
363357
$this->removeRelationshipData($field, $object, $table);
364358
}
365-
359+
// Check Table deleted successfully
366360
if ($table->delete()) {
367361
return response()->json(['success' => true]);
368362
}
@@ -376,65 +370,6 @@ public function delete(Request $request)
376370
return response()->json(['success' => false]);
377371
}
378372

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-
438373
public function getTableDetails(Request $request)
439374
{
440375
if ($request->ajax()) {
@@ -643,95 +578,4 @@ public function getTableDetails(Request $request)
643578

644579
return response()->json(['success' => false]);
645580
}
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-
}
737581
}

src/Traits/RecordHelper.php

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace CodexShaper\DBM\Traits;
4+
5+
use CodexShaper\DBM\Facades\Manager as DBM;
6+
use CodexShaper\DBM\Models\DBM_Collection;
7+
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Validator;
9+
10+
trait RecordHelper
11+
{
12+
public function removeRelationshipData($field, $object, $table)
13+
{
14+
if ($field->type == 'relationship') {
15+
16+
$relationship = $field->settings;
17+
18+
$localModel = $relationship->localModel;
19+
$foreignModel = $relationship->foreignModel;
20+
21+
$findColumn = $object->details['findColumn'];
22+
23+
$localObject = $localModel::where($findColumn, $table->{$findColumn})->first();
24+
25+
if ($relationship->relationType == 'belongsToMany') {
26+
27+
$pivotTable = $relationship->pivotTable;
28+
$parentPivotKey = $relationship->parentPivotKey;
29+
$relatedPivotKey = $relationship->relatedPivotKey;
30+
31+
DBM::Object()
32+
->setManyToManyRelation(
33+
$localObject,
34+
$foreignModel,
35+
$pivotTable,
36+
$parentPivotKey,
37+
$relatedPivotKey
38+
)
39+
->belongs_to_many()
40+
->detach();
41+
} else if ($relationship->relationType == 'hasMany') {
42+
43+
$foreignKey = $relationship->foreignKey;
44+
$localKey = $relationship->localKey;
45+
46+
DBM::Object()
47+
->setCommonRelation(
48+
$localObject,
49+
$foreignModel,
50+
$foreignKey,
51+
$localKey)
52+
->has_many()
53+
->delete();
54+
}
55+
56+
}
57+
}
58+
59+
public function getSettingOptions($field)
60+
{
61+
$options = $field->settings['options'];
62+
if (isset($options['controller'])) {
63+
$partials = explode('@', $options['controller']);
64+
$controllerName = $partials[0];
65+
$methodName = $partials[1];
66+
67+
return app($controllerName)->{$methodName}();
68+
}
69+
}
70+
71+
public function removeRelationshipKeyForBelongsTo($fields, $foreignKey)
72+
{
73+
$results = [];
74+
75+
foreach ($fields as $key => $field) {
76+
if ($field->name == $foreignKey) {
77+
unset($fields[$key]);
78+
continue;
79+
}
80+
$results[] = $field;
81+
}
82+
83+
return $results;
84+
}
85+
86+
public function validation($fields, $columns, $action = "create")
87+
{
88+
$errors = [];
89+
foreach ($fields as $field) {
90+
$name = $field->name;
91+
92+
if (is_object($field->settings) && property_exists($field->settings, 'validation') !== false) {
93+
94+
$validationSettings = $field->settings->validation;
95+
$rules = $this->prepareRules($columns, $action, $validationSettings);
96+
$data = [$name => $columns->{$name}];
97+
$validator = Validator::make($data, [$name => $rules]);
98+
if ($validator->fails()) {
99+
foreach ($validator->errors()->all() as $error) {
100+
$errors[] = $error;
101+
}
102+
}
103+
}
104+
}
105+
106+
return $errors;
107+
}
108+
109+
public function prepareRules($columns, $action, $settings)
110+
{
111+
$rules = '';
112+
113+
if (is_string($settings)) {
114+
$rules = $settings;
115+
} else if ($action == 'create' && isset($settings->create)) {
116+
$createSettings = $settings->create;
117+
$rules = $createSettings->rules;
118+
} else if ($action == 'update' && isset($settings->update)) {
119+
$updateSettings = $settings->update;
120+
$localKey = $updateSettings->localKey;
121+
$rules = $updateSettings->rules . ',' . $columns->{$localKey};
122+
}
123+
124+
return $rules;
125+
}
126+
127+
public function getFieldType($collectionName, $fieldName)
128+
{
129+
$collection = DBM_Collection::where('name', $collectionName)->first();
130+
131+
return $collection->fields()->where('name', $fieldName)->first()->type;
132+
}
133+
134+
public function generateError($errors)
135+
{
136+
return response()->json([
137+
'success' => false,
138+
'errors' => $errors,
139+
], 400);
140+
}
141+
142+
public function hasFunction($fields, $column)
143+
{
144+
foreach ($fields as $field) {
145+
if ($field->name == $column && ($field->function_name != null || $field->function_name != "")) {
146+
return $field->function_name;
147+
}
148+
}
149+
150+
return false;
151+
}
152+
153+
public function executeFunction($functionName, $value = null)
154+
{
155+
$signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()";
156+
157+
$result = DB::raw("{$signature}");
158+
159+
return $result;
160+
}
161+
}

0 commit comments

Comments
 (0)