| Total Complexity | 68 |
| Total Lines | 319 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CBController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CBController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace crocodicstudio\crudbooster\controllers; |
||
| 14 | class CBController extends Controller |
||
| 15 | { |
||
| 16 | use ColumnsRegister, Join, ControllerSetting; |
||
| 17 | |||
| 18 | private $assignmentData; |
||
|
|
|||
| 19 | |||
| 20 | public function __construct() |
||
| 25 | } |
||
| 26 | |||
| 27 | public function __call($method, $parameters) |
||
| 28 | { |
||
| 29 | if($method == "getData") { |
||
| 30 | $key = $parameters[0]; |
||
| 31 | if(isset($this->data[$key])) { |
||
| 32 | return $this->data[$key]; |
||
| 33 | }else{ |
||
| 34 | return null; |
||
| 35 | } |
||
| 36 | }else{ |
||
| 37 | return null; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | private function repository() |
||
| 42 | { |
||
| 43 | $joins = columnSingleton()->getJoin(); |
||
| 44 | $columns = columnSingleton()->getColumns(); |
||
| 45 | |||
| 46 | $query = DB::table($this->data['table']); |
||
| 47 | |||
| 48 | |||
| 49 | $query->addSelect($this->data['table'].'.'.cb()->pk($this->data['table']).' as primary_key'); |
||
| 50 | |||
| 51 | if(isset($this->data['hook_query_index']) && is_callable($this->data['hook_query_index'])) |
||
| 52 | { |
||
| 53 | $query = call_user_func($this->data['hook_query_index'], $query); |
||
| 54 | } |
||
| 55 | |||
| 56 | $softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
||
| 57 | if($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { |
||
| 58 | $query->whereNull($this->data['table'].'.deleted_at'); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | if(isset($joins)) { |
||
| 63 | foreach($joins as $join) |
||
| 64 | { |
||
| 65 | $query->join($join['target_table'], |
||
| 66 | $join['target_table_primary'], |
||
| 67 | $join['operator'], |
||
| 68 | $join['source_table_foreign'], |
||
| 69 | $join['type']); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | foreach($columns as $column) { |
||
| 74 | /** @var ColumnModel $column */ |
||
| 75 | if(strpos($column->getField(),".") === false) { |
||
| 76 | $query->addSelect($this->data['table'].'.'.$column->getField()); |
||
| 77 | }else{ |
||
| 78 | $query->addSelect($column->getField()); |
||
| 79 | } |
||
| 80 | |||
| 81 | $query = getTypeHook($column->getType())->query($query, $column); |
||
| 82 | } |
||
| 83 | |||
| 84 | if(request()->has('q')) |
||
| 85 | { |
||
| 86 | if(isset($this->data['hook_search_query'])) { |
||
| 87 | $query = call_user_func($this->data['hook_search_query'], $query); |
||
| 88 | }else{ |
||
| 89 | $query->where(function ($where) use ($columns) { |
||
| 90 | /** |
||
| 91 | * @var $where Builder |
||
| 92 | */ |
||
| 93 | foreach($columns as $column) |
||
| 94 | { |
||
| 95 | if(strpos($column->getField(),".") === false) { |
||
| 96 | $field = $this->data['table'].'.'.$column->getField(); |
||
| 97 | }else{ |
||
| 98 | $field = $column->getField(); |
||
| 99 | } |
||
| 100 | $where->orWhere($field, 'like', '%'.request('q').'%'); |
||
| 101 | } |
||
| 102 | }); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | if(isset($this->data['hook_query_index']) && is_callable($this->data['hook_query_index'])) { |
||
| 107 | $query = call_user_func($this->data['hook_query_index'], $query); |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | if(request()->has(['order_by','order_sort'])) |
||
| 112 | { |
||
| 113 | if(in_array(request('order_by'),columnSingleton()->getColumnNameOnly())) { |
||
| 114 | $query->orderBy(request('order_by'), request('order_sort')); |
||
| 115 | } |
||
| 116 | }else{ |
||
| 117 | $query->orderBy($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), "desc"); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $query; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getIndex() |
||
| 124 | { |
||
| 125 | if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
||
| 126 | |||
| 127 | $query = $this->repository(); |
||
| 128 | $result = $query->paginate(20); |
||
| 129 | $data['result'] = $result; |
||
| 130 | |||
| 131 | return view("crudbooster::module.index.index", array_merge($data, $this->data)); |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @throws CBValidationException |
||
| 137 | */ |
||
| 138 | private function validation() |
||
| 139 | { |
||
| 140 | if(isset($this->data['validation'])) { |
||
| 141 | $validator = Validator::make(request()->all(), @$this->data['validation'], @$this->data['validation_messages']); |
||
| 142 | if ($validator->fails()) { |
||
| 143 | $message = $validator->messages(); |
||
| 144 | $message_all = $message->all(); |
||
| 145 | throw new CBValidationException(implode(', ',$message_all)); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getAdd() |
||
| 158 | } |
||
| 159 | |||
| 160 | public function postAddSave() |
||
| 161 | { |
||
| 162 | if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
||
| 163 | |||
| 164 | try { |
||
| 165 | $this->validation(); |
||
| 166 | columnSingleton()->valueAssignment(); |
||
| 167 | $data = columnSingleton()->getAssignmentData(); |
||
| 168 | |||
| 169 | if(Schema::hasColumn($this->data['table'], 'created_at')) { |
||
| 170 | $data['created_at'] = date('Y-m-d H:i:s'); |
||
| 171 | } |
||
| 172 | |||
| 173 | $id = DB::table($this->data['table'])->insertGetId($data); |
||
| 174 | |||
| 175 | if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { |
||
| 176 | call_user_func($this->data['hook_after_insert'], $id); |
||
| 177 | } |
||
| 178 | |||
| 179 | } catch (CBValidationException $e) { |
||
| 180 | Log::debug($e); |
||
| 181 | return cb()->redirectBack($e->getMessage(),'info'); |
||
| 182 | } catch (\Exception $e) { |
||
| 183 | Log::error($e); |
||
| 184 | return cb()->redirectBack($e->getMessage(),'warning'); |
||
| 185 | } |
||
| 186 | |||
| 187 | if (request('submit') == trans('crudbooster.button_save_more')) { |
||
| 188 | return cb()->redirect(module()->addURL(), trans("crudbooster.alert_add_data_success"), 'success'); |
||
| 189 | } else { |
||
| 190 | return cb()->redirect(module()->url(), trans("crudbooster.alert_add_data_success"), 'success'); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getEdit($id) |
||
| 195 | { |
||
| 196 | if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have a privilege access to this area"); |
||
| 197 | |||
| 198 | $data = []; |
||
| 199 | $data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
||
| 200 | $data['page_title'] = $this->data['page_title'].' : Edit'; |
||
| 201 | $data['action_url'] = module()->editSaveURL($id); |
||
| 202 | return view('crudbooster::module.form.form', array_merge($data, $this->data)); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function postEditSave($id) |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | public function getDelete($id) |
||
| 270 | } |
||
| 271 | |||
| 272 | public function getDetail($id) |
||
| 280 | } |
||
| 281 | |||
| 282 | public function postUploadImage() |
||
| 283 | { |
||
| 284 | if(auth()->guest()) return redirect(cb()->getLoginUrl()); |
||
| 285 | |||
| 286 | $file = null; |
||
| 287 | try { |
||
| 288 | |||
| 289 | cb()->validation([ |
||
| 290 | 'userfile' => 'required|mimes:' . implode(",",config('crudbooster.UPLOAD_IMAGE_EXTENSION_ALLOWED')) |
||
| 291 | ]); |
||
| 292 | |||
| 293 | $file = cb()->uploadFile('userfile', true); |
||
| 294 | |||
| 295 | } catch (CBValidationException $e) { |
||
| 296 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 297 | } catch (\Exception $e) { |
||
| 298 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 299 | } |
||
| 300 | |||
| 301 | return response()->json([ |
||
| 302 | 'status'=>true, |
||
| 303 | 'filename'=>basename($file), |
||
| 304 | 'full_url'=>asset($file), |
||
| 305 | 'url'=>$file |
||
| 306 | ]); |
||
| 307 | } |
||
| 308 | |||
| 309 | public function postUploadFile() |
||
| 333 | ]); |
||
| 334 | } |
||
| 335 | } |
||
| 336 |