| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Requests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Foundation\Http\FormRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class EditDevice extends FormRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Determine if the user is authorized to make this request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public function authorize() | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         return Auth::user()->isUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Get the validation rules that apply to the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function rules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         //TODO figure out way for unique location names for each specific site | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'name' => 'sometimes|nullable|min:2|max:190|name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'open_time' => 'sometimes|nullable|date_format:H:i', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             'close_time' => 'sometimes|nullable|date_format:H:i', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             'update_rate' => 'sometimes|nullable|integer|min:2|digits_between:1,7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'image_rate' => 'sometimes|nullable|integer|min:3|digits_between:1,7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'sensor_rate' => 'sometimes|nullable|integer|min:10|digits_between:1,7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'command' => 'sometimes|nullable|alpha|max:6|in:open,close,lock,unlock', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Get the validation rules that apply to the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return \Illuminate\Contracts\Validation\Validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected function getValidatorInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $validator = parent::getValidatorInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $validator->sometimes('site_id', 'bail|integer|digits_between:1,10|exists:sites,id', function($input) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             return !$input->new_site_name && $input->site_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $validator->sometimes('location_id', 'bail|integer|digits_between:1,10|exists:locations,id', function($input) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return !$input->new_location_name && $input->location_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $validator->sometimes('new_site_name', 'bail|min:2|max:190|name|unique:sites,name', function($input) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return !$input->site_id && $input->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $validator->sometimes('new_location_name', 'bail|min:2|max:190|name|unique:locations,name', function($input) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return !$input->location_id && $input->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Get the error messages for the defined validation rules. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function messages() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'name.required' => 'A device name is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'site_id.required' => 'A site is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'new_site_name.required' => 'A site is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'new_site_name.min'  => 'The new site must be at least 2 characters', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'location_id.required' => 'A location is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'location_id.min'  => 'The new location must be at least 2 characters', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'new_location_name.required' => 'A location is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'open_time.required'  => 'A open time for the device is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'open_time.date_format:H:i'  => 'A open time must be in the format hour:minutes', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'close_time.required'  => 'A close time for the device is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'close_time.date_format:H:i'  => 'A close time must be in the format hour:minutes', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'update_rate.required'  => 'A update rate for the device is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'image_rate.required'  => 'A image rate for the device is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'sensor_rate.required'  => 'A sensor rate for the device is required', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |