diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4175d25eb..fdceca359 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -1,9 +1,8 @@ 'email'); - $messages = array( - 'alert_email.*'=>trans('validation.email_array') - ); - - $validator = Validator::make($email_to_validate, $rules, $messages); - - return $validator->passes(); - - }); - - // Unique only if undeleted - // This works around the use case where multiple deleted items have the same unique attribute. - // (I think this is a bug in Laravel's validator?) - Validator::extend('unique_undeleted', function ($attribute, $value, $parameters, $validator) { - - if (count($parameters)) { - $count = DB::table($parameters[0])->select('id')->where($attribute, '=', $value)->whereNull('deleted_at')->where('id', '!=', $parameters[1])->count(); - return $count < 1; - } - - }); - - - // Yo dawg. I heard you like validators. - // This validates the custom validator regex in custom fields. - // We're just checking that the regex won't throw an exception, not - // that it's actually correct for what the user intended. - - Validator::extend('valid_regex', function ($attribute, $value, $parameters, $validator) { - - // Make sure it's not just an ANY format - if ($value!='') { - - // Check that the string starts with regex: - if (strpos($value, 'regex:') === FALSE) { - return false; - } - - $test_string = 'My hovercraft is full of eels'; - - // We have to stip out the regex: part here to check with preg_match - $test_pattern = str_replace('regex:','', $value); - - try { - preg_match($test_pattern, $test_string, $matches); - return true; - } catch (\Exception $e) { - return false; - } - - } - return true; - - }); - - - - // Share common setting variables with all views. - view()->composer('*', function ($view) { - $view->with('snipeSettings', \App\Models\Setting::getSettings()); - }); - - // Set the monetary locale to the configured locale to make helper::parseFloat work. - setlocale(LC_MONETARY, config('app.locale')); - setlocale(LC_NUMERIC, config('app.locale')); } diff --git a/app/Providers/SettingsServiceProvider.php b/app/Providers/SettingsServiceProvider.php new file mode 100644 index 000000000..4f5b36311 --- /dev/null +++ b/app/Providers/SettingsServiceProvider.php @@ -0,0 +1,121 @@ +] + * @since [v3.0] + * @return void + */ + public function boot() + { + + + // Share common setting variables with all views. + view()->composer('*', function ($view) { + $view->with('snipeSettings', \App\Models\Setting::getSettings()); + }); + + + /** + * Set some common variables so that they're globally available. + * The paths should always be public (versus private uploads) + */ + // Model paths and URLs + \App::singleton('models_upload_path', function(){ + return public_path('/uploads/models/'); + }); + + \App::singleton('models_upload_url', function(){ + return url('/'.'uploads/models/'); + }); + + // Categories + \App::singleton('categories_upload_path', function(){ + return public_path('/uploads/categories/'); + }); + + \App::singleton('categories_upload_url', function(){ + return url('/'.'uploads/categories/'); + }); + + // Locations + \App::singleton('locations_upload_path', function(){ + return public_path('/uploads/locations/'); + }); + + \App::singleton('locations_upload_url', function(){ + return url('/'.'uploads/locations/'); + }); + + // Users + \App::singleton('users_upload_path', function(){ + return public_path('/uploads/users/'); + }); + + \App::singleton('users_upload_url', function(){ + return url('/'.'uploads/users/'); + }); + + // Manufacturers + \App::singleton('manufacturers_upload_path', function(){ + return public_path('/uploads/manufacturers/'); + }); + + \App::singleton('manufacturers_upload_url', function(){ + return url('/'.'uploads/manufacturers/'); + }); + + // Suppliers + \App::singleton('suppliers_upload_path', function(){ + return public_path('/uploads/suppliers/'); + }); + + \App::singleton('suppliers_upload_url', function(){ + return url('/'.'uploads/suppliers/'); + }); + + // Departments + \App::singleton('departments_upload_path', function(){ + return public_path('/uploads/departments/'); + }); + + \App::singleton('departments_upload_url', function(){ + return url('/'.'uploads/departments/'); + }); + + + // Set the monetary locale to the configured locale to make helper::parseFloat work. + setlocale(LC_MONETARY, config('app.locale')); + setlocale(LC_NUMERIC, config('app.locale')); + + } + + /** + * Register any application services. + * + * @return void + */ + public function register() + { + + } +} diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php new file mode 100644 index 000000000..dac557b7b --- /dev/null +++ b/app/Providers/ValidationServiceProvider.php @@ -0,0 +1,117 @@ +] + * @since [v3.0] + * @return void + */ + public function boot() + { + + // Email array validator + Validator::extend('email_array', function ($attribute, $value, $parameters, $validator) { + $value = str_replace(' ', '', $value); + $array = explode(',', $value); + + foreach ($array as $email) { //loop over values + $email_to_validate['alert_email'][]=$email; + } + + $rules = array('alert_email.*'=>'email'); + $messages = array( + 'alert_email.*'=>trans('validation.email_array') + ); + + $validator = Validator::make($email_to_validate, $rules, $messages); + + return $validator->passes(); + + }); + + + // Unique only if undeleted + // This works around the use case where multiple deleted items have the same unique attribute. + // (I think this is a bug in Laravel's validator?) + Validator::extend('unique_undeleted', function ($attribute, $value, $parameters, $validator) { + + if (count($parameters)) { + $count = DB::table($parameters[0])->select('id')->where($attribute, '=', $value)->whereNull('deleted_at')->where('id', '!=', $parameters[1])->count(); + return $count < 1; + } + + }); + + + // Yo dawg. I heard you like validators. + // This validates the custom validator regex in custom fields. + // We're just checking that the regex won't throw an exception, not + // that it's actually correct for what the user intended. + + Validator::extend('valid_regex', function ($attribute, $value, $parameters, $validator) { + + // Make sure it's not just an ANY format + if ($value!='') { + + // Check that the string starts with regex: + if (strpos($value, 'regex:') === FALSE) { + return false; + } + + $test_string = 'My hovercraft is full of eels'; + + // We have to stip out the regex: part here to check with preg_match + $test_pattern = str_replace('regex:','', $value); + + try { + preg_match($test_pattern, $test_string, $matches); + return true; + } catch (\Exception $e) { + return false; + } + + } + return true; + + }); + + } + + /** + * Register any application services. + * + * @return void + */ + public function register() + { + + } +} diff --git a/config/app.php b/config/app.php index 08befc236..66e002453 100755 --- a/config/app.php +++ b/config/app.php @@ -297,6 +297,8 @@ return [ App\Providers\AuthServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, + App\Providers\SettingsServiceProvider::class, + App\Providers\ValidationServiceProvider::class, /*