diff --git a/app/Http/Controllers/Admin/Settings.php b/app/Http/Controllers/Admin/Settings.php index cb935bd1..b6b30f19 100644 --- a/app/Http/Controllers/Admin/Settings.php +++ b/app/Http/Controllers/Admin/Settings.php @@ -4,21 +4,23 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Http\Requests; +use App\Models\Settings as SettingsModel; use Illuminate\Support\Facades\App; +use Mcamara\LaravelLocalization\Facades\LaravelLocalization; class Settings extends Controller { /** - * @var \App\Models\Settings + * @var SettingsModel */ protected $settings; /** * Settings constructor. * - * @param \App\Models\Settings $settings + * @param SettingsModel $settings */ - public function __construct(\App\Models\Settings $settings) + public function __construct(SettingsModel $settings) { $this->settings = $settings; } @@ -27,7 +29,7 @@ class Settings extends Controller { $sections = App::make('registerSettingsExtension') ->get(); - $settings = $this->settings->getAll(); + $settings = $this->settings->getAllWithLanguage(); return view('admin.settings.overview', [ 'formSections' => $sections, @@ -38,32 +40,27 @@ class Settings extends Controller public function saveSettings(Requests\SaveSettingsRequest $request) { $data = $request->all(); + $commonSettings = isset($data['commonSettings']) ? $data['commonSettings'] : []; + // remove CSRF token field - unset($data['_token']); + unset($data['_token'], $data['commonSettings']); - $settings = $this->settings->getAll(); - $updated = 0; foreach ($data as $key => $value) { - if (isset($settings->$key)) { - if ($settings->$key != $value) { - $updated++; - \App\Models\Settings::where('key', $key) - ->update(['value' => $value]); - } - } else { - \App\Models\Settings::create(['key' => $key, 'value' => $value]); - $updated++; + // select language for this setting key + $language = isset($commonSettings[$key]) ? null : LaravelLocalization::getCurrentLocale(); + $setting = SettingsModel::firstOrNew(['language' => $language, 'key' => $key]); + + if ($setting->value == $value) { + continue; } + + // set value and save or delete + $setting->value = $value; + empty($value) ? $setting->delete() : $setting->save(); } - 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')); - } + return redirect() + ->route('settings.overview') + ->with('success', trans('admin/settings.successfullySaved')); } } diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 0b346d9e..0f56118a 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -3,12 +3,26 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Mcamara\LaravelLocalization\Facades\LaravelLocalization; class Settings extends Model { protected $table = 'settings'; public $timestamps = false; - protected $fillable = ['key', 'value']; + protected $fillable = ['key', 'value', 'language']; + + /** + * Prepares common query for selecting settings + * + * @return mixed + */ + protected function getSettingsQuery() + { + return Settings::select(['key', 'value', 'language']) + ->where('language', LaravelLocalization::getCurrentLocale()) + ->orWhere('language', null) + ->orderBy('language', 'DESC'); + } /** * Load all settings @@ -17,12 +31,33 @@ class Settings extends Model */ public function getAll() { - $settings = Settings::select(['key', 'value']) - ->get(); + $settings = $this->getSettingsQuery() + ->get(); return $this->returnPairs($settings); } + /** + * Get all settings with language info + * + * @return \stdClass + */ + public function getAllWithLanguage() + { + $settings = $this->getSettingsQuery() + ->get(); + + $data = new \stdClass(); + foreach ($settings as $conf) { + $data->{$conf->key} = [ + 'val' => $conf->value, + 'lang' => $conf->language + ]; + } + + return $data; + } + /** * Load only config for views * @@ -30,9 +65,9 @@ class Settings extends Model */ public function getForView() { - $settings = Settings::select(['key', 'value']) - ->where('load_into_views', 1) - ->get(); + $settings = $this->getSettingsQuery() + ->where('load_into_views', 1) + ->get(); return $this->returnPairs($settings); } diff --git a/database/migrations/2016_07_22_145402_AddLanguageIntoSettings.php b/database/migrations/2016_07_22_145402_AddLanguageIntoSettings.php new file mode 100644 index 00000000..9bd2a55b --- /dev/null +++ b/database/migrations/2016_07_22_145402_AddLanguageIntoSettings.php @@ -0,0 +1,53 @@ +string('language', 8) + ->after('id') + ->nullable(true) + ->default(null); + + $table->dropUnique('settings_key_unique'); + $table->unique(['key', 'language']); + + $table->foreign('language') + ->references('code') + ->on('languages') + ->onDelete('set null') + ->onUpdate('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('settings', function (Blueprint $table) { + $table->dropForeign('settings_language_foreign'); + $table->dropColumn('language'); + $table->dropUnique('settings_key_language_unique'); + $table->unique('key'); + }); + } +} diff --git a/modules/ContactForm/Resources/views/admin/settings/form.blade.php b/modules/ContactForm/Resources/views/admin/settings/form.blade.php index d1f0ffb7..86cce457 100644 --- a/modules/ContactForm/Resources/views/admin/settings/form.blade.php +++ b/modules/ContactForm/Resources/views/admin/settings/form.blade.php @@ -1,13 +1,13 @@ reCAPTCHA \ No newline at end of file diff --git a/resources/lang/en/admin/settings.php b/resources/lang/en/admin/settings.php index ed86914c..42d87f27 100644 --- a/resources/lang/en/admin/settings.php +++ b/resources/lang/en/admin/settings.php @@ -11,8 +11,9 @@ return [ 'successfullySaved' => 'Settings successfully saved', 'nothingToSave' => 'You have submitted unchanged form. Try to change some attribute first.', 'meta' => 'Meta information', - 'socialSites' => 'Social networks', - 'social' => [ + 'commonSettings' => 'Use for all languages?', + 'socialSites' => 'Social networks', + 'social' => [ 'facebook' => 'Facebook profile', 'instagram' => 'Instagram profile', 'twitter' => 'Twitter profile', diff --git a/resources/views/admin/layout.blade.php b/resources/views/admin/layout.blade.php index 365c70e5..8e6c8aba 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')) | {{$pageName}} + @yield('pageName', @trans('admin/common.admin')) | {{isset($pageName) ? $pageName : ''}} @@ -62,7 +62,7 @@ diff --git a/resources/views/admin/settings/parts/basic.blade.php b/resources/views/admin/settings/parts/basic.blade.php index f2e51392..24304ff9 100644 --- a/resources/views/admin/settings/parts/basic.blade.php +++ b/resources/views/admin/settings/parts/basic.blade.php @@ -1,17 +1,27 @@ \ 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 index 02b11190..c47fcf8d 100644 --- a/resources/views/admin/settings/parts/meta.blade.php +++ b/resources/views/admin/settings/parts/meta.blade.php @@ -1,11 +1,13 @@ \ No newline at end of file diff --git a/resources/views/admin/settings/parts/social.blade.php b/resources/views/admin/settings/parts/social.blade.php index de02aa6a..360a15fd 100644 --- a/resources/views/admin/settings/parts/social.blade.php +++ b/resources/views/admin/settings/parts/social.blade.php @@ -1,29 +1,29 @@ diff --git a/resources/views/admin/settings/snippets/commonSettings.blade.php b/resources/views/admin/settings/snippets/commonSettings.blade.php new file mode 100644 index 00000000..5a0e8c01 --- /dev/null +++ b/resources/views/admin/settings/snippets/commonSettings.blade.php @@ -0,0 +1,5 @@ +{$field}) || $settings->{$field}['lang'] == null) + checked + @endif /> +@lang('admin/settings.commonSettings') \ No newline at end of file diff --git a/resources/views/vendor/.gitkeep b/resources/views/vendor/.gitkeep deleted file mode 100644 index e69de29b..00000000