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 ObjectController extends Controller |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * Get all objects. |
||||
| 15 | * |
||||
| 16 | * @return \Illuminate\Http\JsonResponse |
||||
| 17 | */ |
||||
| 18 | public function all(Request $request) |
||||
| 19 | { |
||||
| 20 | if ($request->ajax()) { |
||||
| 21 | if (($response = DBM::authorize('crud.browse')) !== true) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 22 | return $response; |
||||
| 23 | } |
||||
| 24 | |||||
| 25 | $perPage = (int) $request->perPage; |
||||
| 26 | $query = $request->q; |
||||
| 27 | |||||
| 28 | $userPermissions = DBM::userPermissions(); |
||||
| 29 | $tables = Table::paginate($perPage, null, [], $query); |
||||
| 30 | $newTables = $this->filterCrudTables($tables); |
||||
| 31 | |||||
| 32 | return response()->json([ |
||||
| 33 | 'success' => true, |
||||
| 34 | 'tables' => $newTables, |
||||
| 35 | 'userPermissions' => $userPermissions, |
||||
| 36 | 'pagination' => $tables, |
||||
| 37 | ]); |
||||
| 38 | } |
||||
| 39 | |||||
| 40 | return response()->json(['success' => false]); |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * Filter CRUD Tables. |
||||
| 45 | * Check CRUD exists or not. |
||||
| 46 | * |
||||
| 47 | * @param mixed $tables |
||||
| 48 | * |
||||
| 49 | * @return array |
||||
| 50 | */ |
||||
| 51 | public function filterCrudTables($tables) |
||||
| 52 | { |
||||
| 53 | $objects = DBM::Object()->all(); |
||||
| 54 | $newTables = []; |
||||
| 55 | |||||
| 56 | foreach ($tables as $table) { |
||||
| 57 | foreach ($objects as $object) { |
||||
| 58 | if ($table == $object->name) { |
||||
| 59 | $newTables[] = [ |
||||
| 60 | 'name' => $table, |
||||
| 61 | 'isCrud' => true, |
||||
| 62 | ]; |
||||
| 63 | continue 2; |
||||
| 64 | } |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | $newTables[] = [ |
||||
| 68 | 'name' => $table, |
||||
| 69 | 'isCrud' => false, |
||||
| 70 | ]; |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | return $newTables; |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | /** |
||||
| 77 | * Get Object details. |
||||
| 78 | * |
||||
| 79 | * @return \Illuminate\Http\JsonResponse |
||||
| 80 | */ |
||||
| 81 | public function getObjectDetails(Request $request) |
||||
| 82 | { |
||||
| 83 | if ($request->ajax()) { |
||||
| 84 | try { |
||||
| 85 | if (! Table::exists($request->table)) { |
||||
| 86 | throw new \Exception('Sorry! There is no table', 1); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | $tableName = $request->table; |
||||
| 90 | $details = $this->getObject($tableName); |
||||
| 91 | $permission = $details['isCrudExists'] ? 'update' : 'create'; |
||||
| 92 | |||||
| 93 | if (($response = DBM::authorize("crud.{$permission}")) !== true) { |
||||
|
0 ignored issues
–
show
|
|||||
| 94 | return $response; |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | $relationship = $this->getRelationshipDetails($tableName); |
||||
| 98 | |||||
| 99 | return response()->json([ |
||||
| 100 | 'success' => true, |
||||
| 101 | 'relationship_tables' => $relationship['tables'], |
||||
| 102 | 'relationship_details' => $relationship['details'], |
||||
| 103 | 'object' => $details['object'], |
||||
| 104 | 'fields' => $details['fields'], |
||||
| 105 | 'isCrudExists' => $details['isCrudExists'], |
||||
| 106 | 'userPermissions' => DBM::userPermissions(), |
||||
| 107 | 'driver' => Driver::getConnectionName(), |
||||
| 108 | ]); |
||||
| 109 | } catch (\Exception $e) { |
||||
| 110 | return $this->generateError([$e->getMessage()]); |
||||
| 111 | } |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | return response()->json(['success' => false]); |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * Get Object. |
||||
| 119 | * |
||||
| 120 | * @param string $tableName |
||||
| 121 | * |
||||
| 122 | * @return array |
||||
| 123 | */ |
||||
| 124 | public function getObject($tableName) |
||||
| 125 | { |
||||
| 126 | $isCrudExists = false; |
||||
| 127 | $fields = []; |
||||
| 128 | |||||
| 129 | if ($object = DBM::Object()->where('name', $tableName)->first()) { |
||||
| 130 | $isCrudExists = true; |
||||
| 131 | if (! $object->model) { |
||||
| 132 | $object->model = DBM::generateModelName($object->name); |
||||
|
0 ignored issues
–
show
It seems like
$object->name can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $className of CodexShaper\DBM\Manager::generateModelName() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 133 | } |
||||
| 134 | $fields = $object->fields()->orderBy('order', 'ASC')->get(); |
||||
| 135 | } |
||||
| 136 | |||||
| 137 | if (! $object) { |
||||
| 138 | $table = Table::getTable($tableName); |
||||
| 139 | |||||
| 140 | $object = new \stdClass; |
||||
| 141 | $object->name = $table['name']; |
||||
| 142 | $object->slug = Str::slug($table['name']); |
||||
| 143 | $object->display_name = ucfirst($table['name']); |
||||
| 144 | $object->model = DBM::generateModelName($table['name']); |
||||
| 145 | $object->controller = ''; |
||||
| 146 | |||||
| 147 | $fields = $this->prepareFields($table); |
||||
| 148 | } |
||||
| 149 | |||||
| 150 | $object->makeModel = false; |
||||
| 151 | |||||
| 152 | return [ |
||||
| 153 | 'object' => $object, |
||||
| 154 | 'fields' => $fields, |
||||
| 155 | 'isCrudExists' => $isCrudExists, |
||||
| 156 | ]; |
||||
| 157 | } |
||||
| 158 | |||||
| 159 | /** |
||||
| 160 | * Prepare Object Fields. |
||||
| 161 | * |
||||
| 162 | * @param array $table |
||||
| 163 | * |
||||
| 164 | * @return array |
||||
| 165 | */ |
||||
| 166 | public function prepareFields($table) |
||||
| 167 | { |
||||
| 168 | $fields = []; |
||||
| 169 | $order = 1; |
||||
| 170 | |||||
| 171 | foreach ($table['columns'] as $column) { |
||||
| 172 | $fields[] = (object) [ |
||||
| 173 | 'name' => $column->name, |
||||
| 174 | 'display_name' => ucfirst($column->name), |
||||
| 175 | 'type' => DatabaseController::getInputType($column->type['name']), |
||||
| 176 | 'create' => ($column->autoincrement) ? false : true, |
||||
| 177 | 'read' => ($column->autoincrement) ? false : true, |
||||
| 178 | 'edit' => ($column->autoincrement) ? false : true, |
||||
| 179 | 'delete' => ($column->autoincrement) ? false : true, |
||||
| 180 | 'function_name' => '', |
||||
| 181 | 'order' => $order, |
||||
| 182 | 'settings' => '{ }', |
||||
| 183 | ]; |
||||
| 184 | |||||
| 185 | $order++; |
||||
| 186 | } |
||||
| 187 | |||||
| 188 | return $fields; |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | /** |
||||
| 192 | * Get Relationship details. |
||||
| 193 | * |
||||
| 194 | * @param string $tableName |
||||
| 195 | * |
||||
| 196 | * @return array |
||||
| 197 | */ |
||||
| 198 | public function getRelationshipDetails($tableName) |
||||
| 199 | { |
||||
| 200 | $tables = Table::all(); |
||||
| 201 | |||||
| 202 | $relationshipDetails = (object) [ |
||||
| 203 | 'type' => 'hasOne', |
||||
| 204 | 'foreignTableDetails' => Table::getTable($tables[0]), |
||||
| 205 | 'localTableDetails' => Table::getTable($tableName), |
||||
| 206 | ]; |
||||
| 207 | |||||
| 208 | return [ |
||||
| 209 | 'tables' => $tables, |
||||
| 210 | 'details' => $relationshipDetails, |
||||
| 211 | ]; |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | /** |
||||
| 215 | * Get errors. |
||||
| 216 | * |
||||
| 217 | * @param array $errors |
||||
| 218 | * |
||||
| 219 | * @return \Illuminate\Http\JsonResponse |
||||
| 220 | */ |
||||
| 221 | protected function generateError($errors) |
||||
| 222 | { |
||||
| 223 | return response()->json([ |
||||
| 224 | 'success' => false, |
||||
| 225 | 'errors' => $errors, |
||||
| 226 | ], 400); |
||||
| 227 | } |
||||
| 228 | } |
||||
| 229 |