Codexshaper /
laravel-database-manager
| 1 | <?php |
||
| 2 | |||
| 3 | namespace CodexShaper\DBM\Http\Controllers; |
||
| 4 | |||
| 5 | use CodexShaper\DBM\Facades\Manager as DBM; |
||
| 6 | use Illuminate\Http\Request; |
||
| 7 | use Illuminate\Support\Str; |
||
| 8 | |||
| 9 | class CrudController extends Controller |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Load all crud tables. |
||
| 13 | * |
||
| 14 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
| 15 | */ |
||
| 16 | public function index() |
||
| 17 | { |
||
| 18 | return view('dbm::app'); |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Create|Update CRUD. |
||
| 23 | * |
||
| 24 | * @return \Illuminate\Http\JsonResponse |
||
| 25 | */ |
||
| 26 | public function storeOrUpdate(Request $request) |
||
| 27 | { |
||
| 28 | if ($request->ajax()) { |
||
| 29 | $table = $request->object; |
||
| 30 | $columns = $request->fields; |
||
| 31 | $permission = $request->isCrudExists ? 'update' : 'create'; |
||
| 32 | |||
| 33 | if (($response = DBM::authorize('crud.'.$permission)) !== true) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 34 | return $response; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (($response = $this->makeModel($table)) !== true) { |
||
| 38 | return $response; |
||
| 39 | } |
||
| 40 | |||
| 41 | if (! class_exists($table['controller'])) { |
||
| 42 | DBM::makeController($table['controller']); |
||
| 43 | } |
||
| 44 | |||
| 45 | try { |
||
| 46 | if ($object = $this->addOrUpdateObject($table)) { |
||
| 47 | $this->addMenu($object); |
||
| 48 | foreach ($columns as $column) { |
||
| 49 | $this->addOrUpdateField($column, $object); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | return response()->json([ |
||
| 54 | 'success' => true, |
||
| 55 | 'object' => $request->object, |
||
| 56 | 'fields' => $request->fields, |
||
| 57 | ]); |
||
| 58 | } catch (\Exception $e) { |
||
| 59 | return $this->generateError([$e->getMessage()]); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | return response()->json(['success' => false]); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Create a new model if not exists. |
||
| 68 | * |
||
| 69 | * @param array $table |
||
| 70 | * |
||
| 71 | * @return \Illuminate\Http\JsonResponse|true |
||
| 72 | */ |
||
| 73 | public function makeModel($table) |
||
| 74 | { |
||
| 75 | if (empty($table['model'])) { |
||
| 76 | return $this->generateError(['Model Must be provided']); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($table['makeModel'] && ! class_exists($table['model'])) { |
||
| 80 | DBM::makeModel($table['model'], $table['name']); |
||
| 81 | } |
||
| 82 | |||
| 83 | if (! $table['makeModel'] && ! class_exists($table['model'])) { |
||
| 84 | $error = "Create model {$table['model']} first or checked create model option"; |
||
| 85 | |||
| 86 | return $this->generateError([$error]); |
||
| 87 | } |
||
| 88 | |||
| 89 | return true; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Create|Update Object. |
||
| 94 | * |
||
| 95 | * @param array $table |
||
| 96 | * |
||
| 97 | * @return \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject|false |
||
| 98 | */ |
||
| 99 | public function addOrUpdateObject($table) |
||
| 100 | { |
||
| 101 | $object = DBM::Object()->where('name', $table['name'])->first(); |
||
| 102 | $action = 'update'; |
||
| 103 | if (! $object) { |
||
| 104 | $object = DBM::Object(); |
||
| 105 | $object->name = $table['name']; |
||
| 106 | $action = 'save'; |
||
| 107 | } |
||
| 108 | $object->slug = Str::slug($table['slug']); |
||
| 109 | $object->display_name = ucfirst($table['display_name']); |
||
| 110 | $object->model = $table['model']; |
||
| 111 | $object->controller = $table['controller']; |
||
| 112 | $object->details = [ |
||
| 113 | 'findColumn' => $table['findColumn'], |
||
| 114 | 'searchColumn' => $table['searchColumn'], |
||
| 115 | 'perPage' => $table['perPage'], |
||
| 116 | 'orderColumn' => $table['orderColumn'], |
||
| 117 | 'orderDisplayColumn' => $table['orderDisplayColumn'], |
||
| 118 | 'orderDirection' => $table['orderDirection'], |
||
| 119 | ]; |
||
| 120 | |||
| 121 | if ($object->{$action}()) { |
||
| 122 | return $object; |
||
| 123 | } |
||
| 124 | |||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Create Menu. |
||
| 130 | * |
||
| 131 | * @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
||
| 132 | * |
||
| 133 | * @return true|false |
||
| 134 | */ |
||
| 135 | public function addMenu($object) |
||
| 136 | { |
||
| 137 | $menu = DBM::Menu()::where('slug', 'admin')->first(); |
||
| 138 | |||
| 139 | if (! $menu) { |
||
| 140 | $order = DBM::Menu()::max('order'); |
||
| 141 | $menu = DBM::Menu(); |
||
| 142 | $menu->name = 'Admin'; |
||
| 143 | $menu->slug = Str::slug('Admin'); |
||
| 144 | $menu->url = '/admin'; |
||
| 145 | $menu->order = $order + 1; |
||
| 146 | $menu->save(); |
||
| 147 | } |
||
| 148 | |||
| 149 | if (! DBM::MenuItem()::where('slug', Str::slug($object->name))->first()) { |
||
| 150 | $itemOrder = DBM::MenuItem()::max('order'); |
||
| 151 | |||
| 152 | $menuItem = DBM::MenuItem(); |
||
| 153 | $menuItem->menu_id = $menu->id; |
||
| 154 | $menuItem->title = $object->name; |
||
| 155 | $menuItem->slug = Str::slug($object->name); |
||
| 156 | $menuItem->url = '/database/record/'.$object->name; |
||
| 157 | $menuItem->parent_id = null; |
||
| 158 | $menuItem->order = $itemOrder + 1; |
||
| 159 | $menuItem->route = 'record'; |
||
| 160 | $menuItem->params = '{"tableName":"'.$object->name.'"}'; |
||
| 161 | $menuItem->middleware = null; |
||
| 162 | $menuItem->controller = null; |
||
| 163 | $menuItem->target = '_self'; |
||
| 164 | $menuItem->icon = null; |
||
| 165 | $menuItem->custom_class = null; |
||
| 166 | |||
| 167 | if ($menuItem->save()) { |
||
| 168 | return true; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Remove Menu. |
||
| 177 | * |
||
| 178 | * @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
||
| 179 | * |
||
| 180 | * @return true|false |
||
| 181 | */ |
||
| 182 | public function removeMenu($slug) |
||
| 183 | { |
||
| 184 | $menu = Menu::where('slug', 'admin')->first(); |
||
| 185 | |||
| 186 | if ($menuItem = MenuItem::where(['slug' => $slug, 'menu_id' => $menu->id])->first()) { |
||
|
0 ignored issues
–
show
The type
CodexShaper\DBM\Http\Controllers\MenuItem was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 187 | $menuItem->delete(); |
||
| 188 | |||
| 189 | return true; |
||
| 190 | } |
||
| 191 | |||
| 192 | return false; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Create|Update Object Field. |
||
| 197 | * |
||
| 198 | * @param array $column |
||
| 199 | * @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
||
| 200 | * |
||
| 201 | * @return \CodexShaper\DBM\Models\DBM_Field|\CodexShaper\DBM\Models\DBM_MongoField |
||
| 202 | */ |
||
| 203 | public function addOrUpdateField($column, $object) |
||
| 204 | { |
||
| 205 | $field = DBM::Field()->where([ |
||
| 206 | 'dbm_object_id' => $object->id, |
||
| 207 | 'name' => $column['name'], |
||
| 208 | ])->first(); |
||
| 209 | |||
| 210 | $action = 'update'; |
||
| 211 | |||
| 212 | if (! $field) { |
||
| 213 | $field = DBM::Field(); |
||
| 214 | $field->dbm_object_id = $object->id; |
||
| 215 | $field->name = $column['name']; |
||
| 216 | $action = 'save'; |
||
| 217 | } |
||
| 218 | |||
| 219 | $field->display_name = ucfirst($column['display_name']); |
||
| 220 | $field->type = $column['type']; |
||
| 221 | $field->create = isset($column['create']) ? $column['create'] : false; |
||
| 222 | $field->read = isset($column['read']) ? $column['read'] : false; |
||
| 223 | $field->edit = isset($column['edit']) ? $column['edit'] : false; |
||
| 224 | $field->delete = isset($column['delete']) ? $column['delete'] : false; |
||
| 225 | $field->order = $column['order']; |
||
| 226 | $field->function_name = isset($column['function_name']) ? $column['function_name'] : ''; |
||
| 227 | $field->settings = json_decode($column['settings']); |
||
| 228 | |||
| 229 | $field->{$action}(); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Delete CRUD. |
||
| 234 | * |
||
| 235 | * @return \Illuminate\Http\JsonResponse |
||
| 236 | */ |
||
| 237 | public function delete(Request $request) |
||
| 238 | { |
||
| 239 | if ($request->ajax()) { |
||
| 240 | if (($response = DBM::authorize('crud.delete')) !== true) { |
||
|
0 ignored issues
–
show
|
|||
| 241 | return $response; |
||
| 242 | } |
||
| 243 | |||
| 244 | $object = DBM::Object()->where('name', $request->table)->first(); |
||
| 245 | if ($object) { |
||
| 246 | $this->removeMenu($object->slug); |
||
| 247 | $object->fields()->delete(); |
||
| 248 | $object->delete(); |
||
| 249 | |||
| 250 | return response()->json(['success' => true]); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | return response()->json(['success' => false]); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Generate an error. |
||
| 259 | * |
||
| 260 | * @param array $errors |
||
| 261 | * |
||
| 262 | * @return \Illuminate\Http\JsonResponse |
||
| 263 | */ |
||
| 264 | protected function generateError($errors) |
||
| 265 | { |
||
| 266 | return response()->json([ |
||
| 267 | 'success' => false, |
||
| 268 | 'errors' => $errors, |
||
| 269 | ], 400); |
||
| 270 | } |
||
| 271 | } |
||
| 272 |