| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Encore\Admin\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Encore\Admin\Auth\Database\Administrator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Encore\Admin\Auth\Database\Permission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Encore\Admin\Auth\Database\Role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Encore\Admin\Facades\Admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Encore\Admin\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Encore\Admin\Grid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Encore\Admin\Layout\Content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Routing\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class UserController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use ModelForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Index interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @return Content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 23 |  | View Code Duplication |     public function index() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         return Admin::content(function (Content $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $content->header(trans('admin.administrator')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $content->description(trans('admin.list')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $content->body($this->grid()->render()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Edit interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return Content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function edit($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return Admin::content(function (Content $content) use ($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $content->header(trans('admin.administrator')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $content->description(trans('admin.edit')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $content->body($this->form()->edit($id)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Create interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return Content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 |  | View Code Duplication |     public function create() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return Admin::content(function (Content $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $content->header(trans('admin.administrator')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $content->description(trans('admin.create')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $content->body($this->form()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Make a grid builder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return Grid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 67 |  | View Code Duplication |     protected function grid() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return Administrator::grid(function (Grid $grid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $grid->id('ID')->sortable(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $grid->username(trans('admin.username')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $grid->name(trans('admin.name')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $grid->roles(trans('admin.roles'))->pluck('name')->label(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $grid->created_at(trans('admin.created_at')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $grid->updated_at(trans('admin.updated_at')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $grid->actions(function (Grid\Displayers\Actions $actions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 if ($actions->getKey() == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     $actions->disableDelete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $grid->tools(function (Grid\Tools $tools) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $tools->batch(function (Grid\Tools\BatchActions $actions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     $actions->disableDelete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Make a form builder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function form() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return Administrator::form(function (Form $form) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $form->display('id', 'ID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $form->text('username', trans('admin.username'))->rules('required'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $form->text('name', trans('admin.name'))->rules('required'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $form->image('avatar', trans('admin.avatar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $form->password('password', trans('admin.password'))->rules('required|confirmed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 ->default(function ($form) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     return $form->model()->password; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $form->ignore(['password_confirmation']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $form->multipleSelect('permissions', trans('admin.permissions'))->options(Permission::all()->pluck('name', 'id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $form->display('created_at', trans('admin.created_at')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $form->display('updated_at', trans('admin.updated_at')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 118 |  | View Code Duplication |             $form->saving(function (Form $form) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 if ($form->password && $form->model()->password != $form->password) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $form->password = bcrypt($form->password); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * Update the specified resource in storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function update($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if ($id == 1 && Admin::user()->id != 1) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return back(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $this->form()->update($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Remove the specified resource from storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |     public function destroy($id) | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |         $ids = explode(',', $id); | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |         if (in_array(1, $ids)) { | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |             return response()->json([ | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |                 'status' => false, | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |                 'message' => trans('admin.delete_failed'), | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |             ]); | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 159 |  | View Code Duplication |         if ($this->form()->destroy($id)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |             return response()->json([ | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |                 'status' => true, | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |                 'message' => trans('admin.delete_succeeded'), | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |             ]); | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |             return response()->json([ | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |                 'status' => false, | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |                 'message' => trans('admin.delete_failed'), | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |             ]); | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 171 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.