ldc-boutique-cnc/app/Providers/AppServiceProvider.php
2022-08-25 12:26:43 +02:00

111 lines
3.7 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Range;
use App\Models\Tag;
use App\Models\Setting;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Pagination\Paginator;
use Illuminate\Database\QueryException;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
\URL::forceScheme('https');
if (config('app.env') !== 'local') {
setlocale(LC_TIME, config('app.locale') . "_" . strtoupper(config('app.locale')) . '.utf8');
}
if (config('app.env') === 'local') {
setlocale(LC_TIME, strtoupper(config('app.locale')) . "_" . config('app.locale') . '.utf8');
}
// bo settings as config
// this is useful when setting up the app whene settings table doesn't exist yet
try {
config([
'bosettings' => Setting::all()
->keyBy('key')
// key every setting by its name
->transform(function ($setting) {
return $setting->value;
// return only the value
})
->toArray()
// make it an array
]);
} catch (QueryException $e) {
// nevermind, you should run php artisan migrate before going any further.
}
// this is useful when setting up the app whene ranges table doesn't exist yet
try {
// on partage les ranges (gammes produit)
$ranges = Range::where('status', '1')->get();
} catch (QueryException $e) {
$ranges = null;
}
View::share('published_ranges', $ranges);
// this is useful when setting up the app when tags table doesn't exist yet
try {
// on partage les tags
$tags = Tag::where('status', '1')->orderBy('order', 'ASC')->get();
} catch (QueryException $e) {
$tags = null;
}
View::share('published_tags', $tags);
// @TODO : Supprimer cet element front_settings
// this is useful when setting up the app whene settings table doesn't exist yet
try {
$front_settings = Setting::where('key', 'LIKE', 'theme_%')->get();
} catch (QueryException $e) {
$front_settings = null;
}
// front theme settings
View::share('front_settings', $front_settings);
Blade::directive('fmt_currency', function ($expression) {
return "<?php
if(is_float($expression)) {
echo sprintf('%s € TTC', number_format($expression, 2, ',', ' '));
} else {
echo $expression;
} ?>";
});
Blade::directive('fmt_sellunit', function ($expression) {
if (config('bosettings.sell_unit_management') == "strict") {
return "<?php if(!empty($expression)) { echo sprintf('/ %s', SellUnit::make($expression)->label); } ?>";
} else {
return "<?php if(!empty($expression)) { echo sprintf('/ %s', $expression); } ?>";
}
});
Blade::directive('excerpt_product_title', function ($expression) {
$limit = config('theme.max_product_title_length');
return "<?php echo Illuminate\Support\Str::limit($expression,$limit) ?>";
});
// default paginator template for back Office
Paginator::useBootstrap();
}
}