diff --git a/app/Handlers/Handler.php b/app/Handlers/Handler.php index a6305096..56f78074 100644 --- a/app/Handlers/Handler.php +++ b/app/Handlers/Handler.php @@ -123,7 +123,7 @@ abstract class Handler implements AppHandler $sub->route('customfields.overview', trans('admin/mainMenu.customFieldsOverview'), [], 50, ['icon' => 'fa fa-code-fork']); }, 30, ['icon' => 'fa fa-code']); $menu->route('log.overview', trans('admin/mainMenu.actionLog'), [], 40, ['icon' => 'fa fa-history']); - $menu->url('#', trans('admin/mainMenu.settings'), 50, ['icon' => 'fa fa-gear']); + $menu->route('settings.overview', trans('admin/mainMenu.settings'), [], 50, ['icon' => 'fa fa-gear']); }); // order diff --git a/app/Handlers/Registrators/Items/SettingExtension.php b/app/Handlers/Registrators/Items/SettingExtension.php new file mode 100644 index 00000000..64a65eff --- /dev/null +++ b/app/Handlers/Registrators/Items/SettingExtension.php @@ -0,0 +1,56 @@ +id = $id; + $this->name = $name; + $this->view = $view; + } + + /** + * @return string + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return string + */ + public function getView() + { + return $this->view; + } +} \ No newline at end of file diff --git a/app/Handlers/Registrators/SettingsExtensionRegistrator.php b/app/Handlers/Registrators/SettingsExtensionRegistrator.php new file mode 100644 index 00000000..882d1b75 --- /dev/null +++ b/app/Handlers/Registrators/SettingsExtensionRegistrator.php @@ -0,0 +1,68 @@ +settingsExtensions = []; + } + + /** + * Method for registering new settings extensions/parts + * + * @param $sectionId + * @param $sectionName + * @param $sectionView + * + * @throws EnumerationExistsException + */ + public function register($sectionId, $sectionName, $sectionView) + { + $this->checkIfRegistered($sectionId); + + // register new extension + $this->settingsExtensions[$sectionId] = new SettingExtension($sectionId, $sectionName, $sectionView); + } + + /** + * Registers new setting part - only as SettingExtension directly + * + * @param SettingExtension $extension + */ + public function registerObject(SettingExtension $extension) + { + $this->checkIfRegistered($extension->getId()); + + $this->settingsExtensions[$extension->getId()] = $extension; + } + + /** + * Getter for all registered extensions + * + * @return array + */ + public function get() + { + return $this->settingsExtensions; + } + + /** + * Checks if this section is not already defined + * + * @param $sectionId + * + * @throws EnumerationExistsException + */ + private function checkIfRegistered($sectionId) + { + if (isset($this->settingsExtensions[$sectionId])) { + throw new EnumerationExistsException('Already existing key ' . $sectionId); + } + } +} diff --git a/app/Http/Controllers/Admin/Settings.php b/app/Http/Controllers/Admin/Settings.php new file mode 100644 index 00000000..91033c9e --- /dev/null +++ b/app/Http/Controllers/Admin/Settings.php @@ -0,0 +1,60 @@ +settings = $settings; + } + + public function getOverview() + { + $sections = App::make('registerSettingsExtension') + ->get(); + $settings = $this->settings->getAll(); + + return view('admin.settings.overview', [ + 'formSections' => $sections, + 'settings' => $settings + ]); + } + + public function saveSettings(Requests\SaveSettingsRequest $request) + { + $data = $request->all(); + // remove CSRF token field + unset($data['_token']); + + $settings = $this->settings->getAll(); + $updated = 0; + foreach($data as $key => $value) { + if ($settings->$key != $value) { + $updated ++; + \App\Models\Settings::where('key', $key)->update(['value' => $value]); + } + } + + if ($updated > 0) { + return redirect()->route('settings.overview')->with('success', trans('admin/settings.successfullySaved')); + } else { + return redirect()->route('settings.overview')->with('error', trans('admin/settings.nothingToSave')); + } + + } +} diff --git a/app/Http/Requests/SaveSettingsRequest.php b/app/Http/Requests/SaveSettingsRequest.php new file mode 100644 index 00000000..5f637372 --- /dev/null +++ b/app/Http/Requests/SaveSettingsRequest.php @@ -0,0 +1,18 @@ + 'required', + ]; + } +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 4eb5d6d9..2cd30e2f 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -215,6 +215,13 @@ Route::group([ Route::post('upload/image', 'UploadFile@postImage') ->name('upload.postImage'); + // form with settings + Route::get('settings', 'Settings@getOverview') + ->name('settings.overview'); + + Route::post('settings', 'Settings@saveSettings') + ->name('settings.save'); + // custom fields // ------------------------------ diff --git a/app/Models/Settings.php b/app/Models/Settings.php new file mode 100644 index 00000000..0b346d9e --- /dev/null +++ b/app/Models/Settings.php @@ -0,0 +1,56 @@ +get(); + + return $this->returnPairs($settings); + } + + /** + * Load only config for views + * + * @return \stdClass + */ + public function getForView() + { + $settings = Settings::select(['key', 'value']) + ->where('load_into_views', 1) + ->get(); + + return $this->returnPairs($settings); + } + + /** + * From pair create object key => value + * + * @param $settings + * + * @return \stdClass + */ + private function returnPairs($settings) + { + $data = new \stdClass(); + foreach ($settings as $conf) { + $data->{$conf->key} = $conf->value; + } + + return $data; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index c1795b57..afb59f52 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Handlers\Registrators\SettingsExtensionRegistrator; use App\Handlers\Registrators\TemplateRegistrator; use App\Handlers\Registrators\WidgetRegistrator; use App\Widgets\HtmlCode\HtmlCode; @@ -18,6 +19,7 @@ class AppServiceProvider extends ServiceProvider { $this->registerWidgets(); $this->registerTemplates(); + $this->registerSettings(); } /** @@ -45,6 +47,10 @@ class AppServiceProvider extends ServiceProvider $this->app->singleton('registerTemplateDir', function ($app) { return new TemplateRegistrator($app); }); + + $this->app->singleton('registerSettingsExtension', function ($app) { + return new SettingsExtensionRegistrator($app); + }); } /** @@ -74,4 +80,15 @@ class AppServiceProvider extends ServiceProvider $registrator->register(config('app.theme'), 'admin/templates.core'); } + + /** + * Register setting sections + */ + private function registerSettings() + { + $registrator = $this->app->make('registerSettingsExtension'); + + $registrator->register('basic', 'admin/settings.basic', 'admin.settings.parts.basic'); + $registrator->register('meta', 'admin/settings.meta', 'admin.settings.parts.meta'); + } } diff --git a/app/Providers/GlobalViewDataProvider.php b/app/Providers/GlobalViewDataProvider.php index 290de0ba..11949db6 100644 --- a/app/Providers/GlobalViewDataProvider.php +++ b/app/Providers/GlobalViewDataProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Models\Settings; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; @@ -14,8 +15,42 @@ class GlobalViewDataProvider extends ServiceProvider */ public function boot() { - view()->share('vendorName', 'ja s.r.o.'); + $settings = new Settings(); + foreach ($settings->getForView() as $key => $value) { + view()->share($key, $value); + } + + $this->extendBlade(); + } + + /** + * Creates pattern for matching Blade directives + * + * @param $function + * + * @return string + */ + private function createPattern($function) + { + return '/(?createPattern('extendVisitor'); @@ -48,26 +83,4 @@ class GlobalViewDataProvider extends ServiceProvider return preg_replace($pattern, $code, $view); }); } - - /** - * Creates pattern for matching Blade directives - * - * @param $function - * - * @return string - */ - private function createPattern($function) - { - return '/(?integer('id', true); + $table->string('key', 64) + ->unique() + ->comment('User readable key for accessing value of this config. Config will be available under this key also in views.'); + $table->text('value') + ->comment('Value of config.'); + $table->boolean('load_into_views') + ->default(true) + ->comment('Use in views directly?') + ->index(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('settings'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 9d64c78a..15606e81 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -15,5 +15,6 @@ class DatabaseSeeder extends Seeder $this->call(SeedCustomFieldTypes::class); $this->call(SeedCustomFieldResources::class); $this->call(SeedActionLogTypes::class); + $this->call(SeedSettings::class); } } \ No newline at end of file diff --git a/database/seeds/SeedSettings.php b/database/seeds/SeedSettings.php new file mode 100644 index 00000000..c45099bc --- /dev/null +++ b/database/seeds/SeedSettings.php @@ -0,0 +1,27 @@ + 'pageName', 'value' => ''], + ['key' => 'author', 'value' => ''], + ['key' => 'metaKeywords', 'value' => ''], + ['key' => 'metaDescription', 'value' => ''], + ['key' => 'googleAnalyticsCode', 'value' => ''], + ]; + + foreach ($types as $type) { + \DB::table('settings') + ->insert($type); + } + } +} diff --git a/resources/lang/en/admin/settings.php b/resources/lang/en/admin/settings.php new file mode 100644 index 00000000..4bd27177 --- /dev/null +++ b/resources/lang/en/admin/settings.php @@ -0,0 +1,14 @@ + 'All available settings', + 'basic' => 'Basic information', + 'save' => 'Save', + 'pageName' => 'Page name', + 'metaKeywords' => 'Web keywords', + 'metaDescription' => 'Web description', + 'googleAnalyticsCode' => 'Google Analytics code', + 'author' => 'Page owner', + 'successfullySaved' => 'Settings successfully saved', + 'nothingToSave' => 'You have submitted unchanged form. Try to change some attribute first.', + 'meta' => 'Meta information' +]; \ No newline at end of file diff --git a/resources/views/admin/layout.blade.php b/resources/views/admin/layout.blade.php index ce4a8e84..f151ae21 100644 --- a/resources/views/admin/layout.blade.php +++ b/resources/views/admin/layout.blade.php @@ -5,7 +5,7 @@ @yield('head_meta') - @yield('pageName', @trans('admin/common.admin')) | {{$vendorName}} + @yield('pageName', @trans('admin/common.admin')) | {{$pageName}} @@ -48,7 +48,7 @@ diff --git a/resources/views/admin/loginForm/form.blade.php b/resources/views/admin/loginForm/form.blade.php index d095e85d..b66d361d 100644 --- a/resources/views/admin/loginForm/form.blade.php +++ b/resources/views/admin/loginForm/form.blade.php @@ -4,7 +4,7 @@ - @lang('admin/loginForm.login') | {{$vendorName}} + @lang('admin/loginForm.login') | {{$pageName}} diff --git a/resources/views/admin/settings/overview.blade.php b/resources/views/admin/settings/overview.blade.php new file mode 100644 index 00000000..69aa2c94 --- /dev/null +++ b/resources/views/admin/settings/overview.blade.php @@ -0,0 +1,50 @@ +@extends('admin.layout') + +@section('pageName', @trans('admin/settings.overview')) + +@section('content') + +
+

@lang('admin/settings.overview')

+
+ + @if (session('success')) +
+ {{ session('success') }} +
+ @endif + + @if (session('error')) +
+ {{ session('error') }} +
+ @endif + +
+ + +
+ {!! csrf_field() !!} +
+ @foreach($formSections as $id => $section) +
+ @include($section->getView()) +
+ @endforeach +
+
+
+ +
+
+
+
+@stop \ No newline at end of file diff --git a/resources/views/admin/settings/parts/basic.blade.php b/resources/views/admin/settings/parts/basic.blade.php new file mode 100644 index 00000000..f2e51392 --- /dev/null +++ b/resources/views/admin/settings/parts/basic.blade.php @@ -0,0 +1,17 @@ + + + + + \ No newline at end of file diff --git a/resources/views/admin/settings/parts/meta.blade.php b/resources/views/admin/settings/parts/meta.blade.php new file mode 100644 index 00000000..02b11190 --- /dev/null +++ b/resources/views/admin/settings/parts/meta.blade.php @@ -0,0 +1,11 @@ + + + \ No newline at end of file diff --git a/resources/views/visitor/default_theme/errorLayout.blade.php b/resources/views/visitor/default_theme/errorLayout.blade.php index beb4cb26..59063134 100644 --- a/resources/views/visitor/default_theme/errorLayout.blade.php +++ b/resources/views/visitor/default_theme/errorLayout.blade.php @@ -5,7 +5,7 @@ @yield('head_meta') - @yield('pageName', @trans('visitor/common.welcome')) | {{$vendorName}} + @yield('pageName', @trans('visitor/common.welcome')) | {{$pageName}} diff --git a/resources/views/visitor/default_theme/layout.blade.php b/resources/views/visitor/default_theme/layout.blade.php index 7490b817..914c304e 100755 --- a/resources/views/visitor/default_theme/layout.blade.php +++ b/resources/views/visitor/default_theme/layout.blade.php @@ -5,7 +5,7 @@ @yield('head_meta') - @yield('pageName', @trans('visitor/common.welcome')) | {{$vendorName}} + @yield('pageName', @trans('visitor/common.welcome')) | {{$pageName}} diff --git a/resources/views/visitor/errorLayout.blade.php b/resources/views/visitor/errorLayout.blade.php index beb4cb26..59063134 100644 --- a/resources/views/visitor/errorLayout.blade.php +++ b/resources/views/visitor/errorLayout.blade.php @@ -5,7 +5,7 @@ @yield('head_meta') - @yield('pageName', @trans('visitor/common.welcome')) | {{$vendorName}} + @yield('pageName', @trans('visitor/common.welcome')) | {{$pageName}} diff --git a/resources/views/visitor/layout.blade.php b/resources/views/visitor/layout.blade.php index beb4cb26..59063134 100644 --- a/resources/views/visitor/layout.blade.php +++ b/resources/views/visitor/layout.blade.php @@ -5,7 +5,7 @@ @yield('head_meta') - @yield('pageName', @trans('visitor/common.welcome')) | {{$vendorName}} + @yield('pageName', @trans('visitor/common.welcome')) | {{$pageName}}