Codexshaper /
laravel-database-manager
| 1 | <?php |
||
| 2 | |||
| 3 | namespace CodexShaper\DBM\Http\Controllers; |
||
| 4 | |||
| 5 | use CodexShaper\DBM\Database\Schema\Table; |
||
| 6 | use CodexShaper\DBM\Facades\Driver; |
||
| 7 | use CodexShaper\DBM\Facades\Manager as DBM; |
||
| 8 | use Illuminate\Http\Request; |
||
| 9 | use Illuminate\Support\Str; |
||
| 10 | |||
| 11 | class RelationController extends Controller |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Get Relation. |
||
| 15 | * |
||
| 16 | * @return \Illuminate\Http\JsonResponse |
||
| 17 | */ |
||
| 18 | public function get(Request $request) |
||
| 19 | { |
||
| 20 | if ($request->ajax()) { |
||
| 21 | if (($response = DBM::authorize('relationship.update')) !== true) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 22 | return $response; |
||
| 23 | } |
||
| 24 | |||
| 25 | $tableName = $request->table; |
||
| 26 | $object = DBM::Object()->where('name', $tableName)->first(); |
||
| 27 | $fields = $object->fields; |
||
| 28 | $field = $this->prepareRelationshipField($fields, json_decode($request->field)); |
||
| 29 | |||
| 30 | return response()->json(['success' => true, 'field' => $field]); |
||
| 31 | } |
||
| 32 | |||
| 33 | return response()->json(['success' => false]); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Prepare relationship field. |
||
| 38 | * |
||
| 39 | * @param \Illuminate\Support\Collection $fields |
||
| 40 | * @param object $field |
||
| 41 | * |
||
| 42 | * @return object |
||
| 43 | */ |
||
| 44 | public function prepareRelationshipField($fields, $field) |
||
| 45 | { |
||
| 46 | $prefix = (Driver::isMongoDB()) ? '_' : ''; |
||
| 47 | |||
| 48 | foreach ($fields as $fld) { |
||
| 49 | if ($fld->id == $field->{$prefix.'id'}) { |
||
| 50 | $relationship = $fld->settings; |
||
| 51 | $localTable = $relationship['localTable']; |
||
| 52 | $foreignTable = $relationship['foreignTable']; |
||
| 53 | $pivotTable = $relationship['pivotTable']; |
||
| 54 | |||
| 55 | $field->localFields = Table::getTable($localTable); |
||
| 56 | $field->foreignFields = Table::getTable($foreignTable); |
||
| 57 | $field->pivotFields = Table::getTable($pivotTable); |
||
| 58 | $field->relationship = $relationship; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | return $field; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Create Relation. |
||
| 67 | * |
||
| 68 | * @return \Illuminate\Http\JsonResponse |
||
| 69 | */ |
||
| 70 | public function add(Request $request) |
||
| 71 | { |
||
| 72 | if ($request->ajax()) { |
||
| 73 | if (($response = DBM::authorize('relationship.create')) !== true) { |
||
|
0 ignored issues
–
show
|
|||
| 74 | return $response; |
||
| 75 | } |
||
| 76 | |||
| 77 | $relationship = $request->relationship; |
||
| 78 | |||
| 79 | if (($response = $this->checkErrors($relationship)) !== true) { |
||
| 80 | return $response; |
||
| 81 | } |
||
| 82 | |||
| 83 | $fieldName = $this->getFieldName($relationship); |
||
| 84 | $settings = $this->prepareSettings($relationship); |
||
| 85 | |||
| 86 | $object = DBM::Object()->where('name', $relationship['localTable'])->first(); |
||
| 87 | $order = DBM::Field()->where('dbm_object_id', $object->id)->max('order'); |
||
| 88 | |||
| 89 | $field = DBM::Field(); |
||
| 90 | $field->dbm_object_id = $object->id; |
||
| 91 | $field->name = $fieldName; |
||
| 92 | $field->type = 'relationship'; |
||
| 93 | $field->display_name = ucfirst($relationship['foreignTable']); |
||
| 94 | $field->order = $order + 1; |
||
| 95 | $field->settings = $settings; |
||
| 96 | |||
| 97 | if ($field->save()) { |
||
| 98 | return response()->json(['success' => true]); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return response()->json(['success' => false]); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Check Errors. |
||
| 107 | * |
||
| 108 | * @return \Illuminate\Http\JsonResponse|true |
||
| 109 | */ |
||
| 110 | public function checkErrors($relationship) |
||
| 111 | { |
||
| 112 | $localModel = $relationship['localModel']; |
||
| 113 | $foreignModel = $relationship['foreignModel']; |
||
| 114 | |||
| 115 | if (! class_exists($localModel)) { |
||
| 116 | $error = "{$localModel} Model not found. Please create the {$localModel} model first"; |
||
| 117 | |||
| 118 | return $this->generateError([$error]); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (! class_exists($foreignModel)) { |
||
| 122 | $error = "{$foreignModel} Model not found. Please create the {$foreignModel} model first"; |
||
| 123 | |||
| 124 | return $this->generateError([$error]); |
||
| 125 | } |
||
| 126 | |||
| 127 | return true; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get Field Name. |
||
| 132 | * |
||
| 133 | * @param string $relationship |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function getFieldName($relationship) |
||
| 138 | { |
||
| 139 | $localTable = Str::singular($relationship['localTable']); |
||
| 140 | $foreignTable = Str::singular($relationship['foreignTable']); |
||
| 141 | $relationType = $relationship['type']; |
||
| 142 | |||
| 143 | return strtolower("{$localTable}_{$relationType}_{$foreignTable}_relationship"); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Prepare Settings. |
||
| 148 | * |
||
| 149 | * @param array $relationship |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function prepareSettings($relationship) |
||
| 154 | { |
||
| 155 | return [ |
||
| 156 | 'relationType' => $relationship['type'], |
||
| 157 | 'localModel' => $relationship['localModel'], |
||
| 158 | 'localTable' => $relationship['localTable'], |
||
| 159 | 'localKey' => $relationship['localKey'], |
||
| 160 | 'foreignModel' => $relationship['foreignModel'], |
||
| 161 | 'foreignTable' => $relationship['foreignTable'], |
||
| 162 | 'foreignKey' => $relationship['foreignKey'], |
||
| 163 | 'displayLabel' => $relationship['displayLabel'], |
||
| 164 | 'pivotTable' => $relationship['pivotTable'], |
||
| 165 | 'parentPivotKey' => $relationship['parentPivotKey'], |
||
| 166 | 'relatedPivotKey' => $relationship['relatedPivotKey'], |
||
| 167 | ]; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Update Relationship. |
||
| 172 | * |
||
| 173 | * @return \Illuminate\Http\JsonResponse |
||
| 174 | */ |
||
| 175 | public function update(Request $request) |
||
| 176 | { |
||
| 177 | if ($request->ajax()) { |
||
| 178 | if (($response = DBM::authorize('relationship.update')) !== true) { |
||
|
0 ignored issues
–
show
|
|||
| 179 | return $response; |
||
| 180 | } |
||
| 181 | |||
| 182 | $relationship = $request->relationship; |
||
| 183 | $field = $request->field; |
||
| 184 | |||
| 185 | if (($response = $this->checkErrors($relationship)) !== true) { |
||
| 186 | return $response; |
||
| 187 | } |
||
| 188 | |||
| 189 | $settings = $this->prepareSettings($relationship); |
||
| 190 | |||
| 191 | $field = DBM::Field()::find($field['id']); |
||
| 192 | $field->settings = $settings; |
||
| 193 | if ($field->update()) { |
||
| 194 | return response()->json(['success' => true]); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return response()->json(['success' => false]); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Delete Relation. |
||
| 203 | * |
||
| 204 | * @return \Illuminate\Http\JsonResponse |
||
| 205 | */ |
||
| 206 | public function delete(Request $request) |
||
| 207 | { |
||
| 208 | if ($request->ajax()) { |
||
| 209 | if (($response = DBM::authorize('relationship.delete')) !== true) { |
||
|
0 ignored issues
–
show
|
|||
| 210 | return $response; |
||
| 211 | } |
||
| 212 | |||
| 213 | $tableName = $request->table; |
||
|
0 ignored issues
–
show
|
|||
| 214 | $data = json_decode($request->field); |
||
| 215 | |||
| 216 | $field = DBM::Field()::find($data->id); |
||
| 217 | |||
| 218 | if ($field->delete()) { |
||
| 219 | return response()->json(['success' => true]); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | return response()->json(['success' => false]); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get Relation. |
||
| 228 | * |
||
| 229 | * @param array $errors |
||
| 230 | * |
||
| 231 | * @return \Illuminate\Http\JsonResponse |
||
| 232 | */ |
||
| 233 | protected function generateError($errors) |
||
| 234 | { |
||
| 235 | return response()->json([ |
||
| 236 | 'success' => false, |
||
| 237 | 'errors' => $errors, |
||
| 238 | ], 400); |
||
| 239 | } |
||
| 240 | } |
||
| 241 |