| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Api\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Models\Dashboard; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Models\UsersWidgets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Dingo\Api\Routing\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class DashboardController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use Helpers; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 2 |  |     public function __construct() { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Display a listing of all authorized devices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 2 |  |     public function index(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  |         $dashboards = Dashboard::AllAvailable($request->user()->user_id)->get(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  |         return $dashboards; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Show the form for creating a new resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function create(Request $request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Store a newly created resource in storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param  \Illuminate\Http\Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function store(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $validation = Validator::make($request->all(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'name' => 'required|max:255', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'access' => 'required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if($validation->passes()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $dashboard = new dashboard; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $dashboard->dashboard_name = $request->name; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $dashboard->access         = $request->access; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if ($request->user()->dashboards()->save($dashboard)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 if (is_numeric($request->copy_from)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     $duplicate_widgets = Dashboard::find($request->copy_from)->widgets()->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     foreach ($duplicate_widgets as $tmp_widget) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                         $new_widget               = $tmp_widget->replicate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         $new_widget->user_id      = $request->user()->user_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         $new_widget->dashboard_id = $dashboard->dashboard_id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         unset($new_widget->user_widget_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         $new_widget->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 return $this->response->array(array('statusText' => 'OK', 'dashboard_id' => $dashboard->dashboard_id)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 return $this->response->errorInternal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $errors = $validation->errors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return response()->json($errors,422); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Display the specified resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param  int  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |     public function show(Request $request, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |         $dashboard = Dashboard::find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |         $widgets   = $dashboard->widgets()->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // morph the data as required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |         if ($request->query('displayFormat') == 'human') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |         return array('dashboard' => $dashboard, 'widgets' => $widgets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Show the form for editing the specified resource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param  int  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function edit($id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Update the specified resource in storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param  \Illuminate\Http\Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param  int  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function update(Request $request, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $validation = Validator::make($request->all(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             'name' => 'required|max:255', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             'access' => 'required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if($validation->passes()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $dashboard = Dashboard::find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $dashboard->dashboard_name = $request->name; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $dashboard->access         = $request->access; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             if ($dashboard->save()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 return $this->response->array(array('statusText' => 'OK')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 return $this->response->errorInternal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $errors = $validation->errors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return response()->json($errors,422); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Remove the specified resource from storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param  int  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     public function destroy(Request $request, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         if (Dashboard::where('user_id', $request->user()->user_id)->where('dashboard_id', $id)->delete()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             if (UsersWidgets::where('dashboard_id', $id)->delete() >= 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 return $this->response->array(array('statusText' => 'OK')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 return $this->response->errorInternal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             return $this->response->errorInternal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function clear($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if (Dashboard::find($id)->widgets()->delete() >= 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             return $this->response->array(array('statusText' => 'OK')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             return $this->response->errorInternal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 179 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |  | 
            
                        
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.