| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\DataTables\DevicesDataTable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Device; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Location; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class DeviceController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * Create a new controller instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->middleware('auth'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         // TODO: Setup logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         // $this->middleware('log')->only('index'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Display index page and process dataTable ajax request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param \App\DataTables\DevicesDataTable $dataTable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function index(DevicesDataTable $dataTable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $dataTable->render('device.index'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Show create device page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return view('device.create'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Show the given device. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param  Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param  string  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function show(Request $request, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $device = Device::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return view('device.show', [ 'device' => $device ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * View the edit device page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param  Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param  string  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function edit(Request $request, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $device = Device::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $location = Location::where('id', $device->location_id)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($location) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $sites = Site::query()->select([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                                         'id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                                         'name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                     ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                                     ->orderByRaw(DB::raw("(id = " . $location->site_id . ") DESC")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                                     ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $locations = Location::query()->select([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                                                 'id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                                                 'name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                                             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                                             ->where('site_id', '=', $location->site_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                                             ->orderByRaw(DB::raw("(id = " . $device->location_id . ") DESC")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                                             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $locations = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $sites = Site::all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return view('device.edit', [ 'device' => $device, 'locations' => $locations, 'sites' => $sites ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * View the edit device page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param  string  $site_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     public function locations($site_id) | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $locations = Location::where('site_id', $site_id)->get(); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         return $locations; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Update the given device. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param  Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param  string  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function update(Request $request, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         // TODO: Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         // to add a hidden  _method field to spoof these HTTP verbs. The | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         // method_field helper can create this field for you: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // {{ method_field('PUT') }} | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $device = Device::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $oldLocationID = $device->location_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $location = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $site = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // TODO figure out way for unique location names for each specific site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $validator = Validator::make($request->all(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             'name' => 'required|string|max:255', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             'site' => 'required_without:new_site_name|int|max:255|nullable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             'new_site_name' => 'required_without:site|unique:sites,name|nullable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             'location' => 'required_without:new_location_name|int|max:255|nullable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             'new_location_name' => 'required_without:location|unique:locations,name|string|max:255|nullable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if ($validator->fails()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             return redirect('device/'.$id.'/edit')->withErrors($validator)->withInput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $siteNew = !empty($request->input('new_site_name')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         //Get the site id of the old or newly created site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if ($siteNew) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             //Create a new site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $site = new Site; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $siteName = $request->input('new_site_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             $site->name = $siteName; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             $site->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $site_id = $site->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             $site_id = $request->input('site'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $locationNew = !empty($request->input('new_location_name')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         //Get the location id of the old or newly created location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if ($locationNew) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             //Create a new location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $location = new Location; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             $locationName = $request->input('new_location_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $location->name = $locationName; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             $location->site_id = $site_id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $location->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $location_id = $location->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $location_id = $request->input('location'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         //Update the devices name and location_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $location = Location::where('id', $location_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                                     ->where('site_id', $site_id)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $device->location_id = $location->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $device->name = $request->input('name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $device->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         //If the old site isn't connected to a device then remove it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $this->removeUnusedSite($oldLocationID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         return redirect('device'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * Deletes a device. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @param  Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @param  string  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @return \Illuminate\Http\RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     public function destroy(Request $request, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $device = Device::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         if ($device->trashed()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             // if the user was already deleted then permananetly delete it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             Device::destroy($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         } else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             //Remove the location from the device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |             $oldLocation_id = $device->location_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |             $device->location_id = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             $device->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |             //Remove unused location and site if applicable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |             $this->removeUnusedSite($oldLocation_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             // soft delete the user the first time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |             $device->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         return redirect('device'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |      * Confirms deletion of a device. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |      * @param  Request  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * @param  string  $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      * @return Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |     public function remove(Request $request, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |         $device = Device::findOrFail($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         return view('device.remove', [ 'device' => $device ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * If a site is not connected to a device then delete the site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @param  int $oldLocationID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     private function removeUnusedSite($oldLocationID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |         //Cleanup left over sites and locations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         $deviceExist = Device::where('location_id', '=', $oldLocationID)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         if (!$deviceExist && $oldLocationID != null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |             $site_id = Location::where('id', '=', $oldLocationID)->firstOrFail()->site_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             $locations = Location::where('site_id', '=', $site_id)->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |             if (sizeof($locations) == 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |                 //Get the site connected to the location and delete it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |                 Site::where('id', '=', $site_id)->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |             else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |                 //Delete the location that isn't used anymore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |                 Location::where('id', '=', $oldLocationID)->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 259 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 260 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.