FEATURE: make settings translatable

This commit is contained in:
Michal Kváček
2016-07-23 21:47:20 +02:00
parent b89941b909
commit ab3d8eab41
11 changed files with 150 additions and 47 deletions
+22 -25
View File
@@ -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'));
}
}
+41 -6
View File
@@ -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);
}
@@ -0,0 +1,53 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
class AddLanguageIntoSettings extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::raw("ALTER TABLE `settings`
CHANGE `language` `language` varchar(8) COLLATE 'utf8_general_ci' NULL AFTER `id`,
CHANGE `key` `key` varchar(64) COLLATE 'utf8_general_ci' NOT NULL COMMENT 'User readable key for accessing value of this config. Config will be available under this key also in views.' AFTER `language`,
CHANGE `value` `value` text COLLATE 'utf8_general_ci' NOT NULL COMMENT 'Value of config.' AFTER `key`,
COLLATE 'utf8_general_ci';");
Schema::table('settings', function (Blueprint $table) {
$table->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');
});
}
}
@@ -1,13 +1,13 @@
<label>
@lang('contactform::admin/settings.recaptchaSiteKey')
<input type="text" name="recaptchaSiteKey" value="{{old('recaptchaSiteKey', isset($settings->recaptchaSiteKey) ? $settings->recaptchaSiteKey : null)}}" />
<input type="text" name="recaptchaSiteKey" value="{{old('recaptchaSiteKey', isset($settings->recaptchaSiteKey) ? $settings->recaptchaSiteKey['val'] : null)}}" />
</label>
<label>
@lang('contactform::admin/settings.recaptchaSecret')
<input type="text" name="recaptchaSecret" value="{{old('recaptchaSecret', isset($settings->recaptchaSecret) ? $settings->recaptchaSecret : null)}}" />
<input type="text" name="recaptchaSecret" value="{{old('recaptchaSecret', isset($settings->recaptchaSecret) ? $settings->recaptchaSecret['val'] : null)}}" />
</label>
<em><a href="https://www.google.com/recaptcha/admin#list" target="_blank">reCAPTCHA</a></em>
+3 -2
View File
@@ -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',
+2 -2
View File
@@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
@yield('head_meta')
<title>@yield('pageName', @trans('admin/common.admin')) | {{$pageName}}</title>
<title>@yield('pageName', @trans('admin/common.admin')) | {{isset($pageName) ? $pageName : ''}}</title>
<link rel="stylesheet" href="{{asset('resources/assets/css/foundation.min.css')}}" />
<link rel="stylesheet" href="{{asset('resources/assets/css/font-awesome.min.css')}}" />
<link rel="stylesheet" href="{{asset('resources/assets/css/admin.css')}}" />
@@ -62,7 +62,7 @@
</div>
<footer class="row text-center">
&copy; {{$pageName}} {{date('Y')}}
&copy; {{isset($pageName) ? $pageName : ''}} {{date('Y')}}
</footer>
</div>
<script src="{{asset('resources/assets/js/vendor/jquery.min.js')}}"></script>
@@ -1,17 +1,27 @@
<label>
@lang('admin/settings.pageName')
<input type="text" name="pageName" value="{{old('pageName', $settings->pageName)}}" />
<input type="text" name="pageName" value="{{old('pageName', isset($settings->pageName) ? $settings->pageName['val'] : '')}}" />
@if (isset($errors) && $errors->has('pageName'))
<div class="callout alert">
{{ $errors->first('pageName') }}
</div>
@endif
</label>
<label>
@lang('admin/settings.author')
<input type="text" name="author" value="{{old('author', $settings->author)}}" />
<input type="text" name="author" value="{{old('author', isset($settings->author) ? $settings->author['val'] : '')}}" />
@include('admin.settings.snippets.commonSettings', ['field' => 'author'])
</label>
<label>
@lang('admin/settings.googleAnalyticsCode')
<input type="text" name="googleAnalyticsCode" value="{{old('googleAnalyticsCode', $settings->googleAnalyticsCode)}}" />
<input type="text" name="googleAnalyticsCode" value="{{old('googleAnalyticsCode', isset($settings->googleAnalyticsCode) ? $settings->googleAnalyticsCode['val'] : '')}}" />
@include('admin.settings.snippets.commonSettings', ['field' => 'googleAnalyticsCode'])
</label>
@@ -1,11 +1,13 @@
<label>
@lang('admin/settings.metaKeywords')
<input type="text" name="metaKeywords" value="{{old('metaKeywords', $settings->metaKeywords)}}" />
<input type="text" name="metaKeywords"
value="{{old('metaKeywords', isset($settings->metaKeywords) ? $settings->metaKeywords['val'] : '')}}" />
</label>
<label>
@lang('admin/settings.metaDescription')
<textarea name="metaDescription" rows="3">{{old('metaDescription', $settings->metaDescription)}}</textarea>
<textarea name="metaDescription"
rows="3">{{old('metaDescription', isset($settings->metaDescription) ? $settings->metaDescription['val'] : '')}}</textarea>
</label>
@@ -1,29 +1,29 @@
<label>
@lang('admin/settings.social.facebook')
<input type="text" name="facebookProfile" value="{{old('facebookProfile', isset($settings->facebookProfile) ? $settings->facebookProfile : '')}}" />
<input type="text" name="facebookProfile" value="{{old('facebookProfile', isset($settings->facebookProfile) ? $settings->facebookProfile['val'] : '')}}" />
</label>
<label>
@lang('admin/settings.social.twitter')
<input type="text" name="twitterProfile" value="{{old('twitterProfile', isset($settings->twitterProfile) ? $settings->twitterProfile : '')}}" />
<input type="text" name="twitterProfile" value="{{old('twitterProfile', isset($settings->twitterProfile) ? $settings->twitterProfile['val'] : '')}}" />
</label>
<label>
@lang('admin/settings.social.pinterest')
<input type="text" name="pinterestProfile" value="{{old('pinterestProfile', isset($settings->pinterestProfile) ? $settings->pinterestProfile : '')}}" />
<input type="text" name="pinterestProfile" value="{{old('pinterestProfile', isset($settings->pinterestProfile) ? $settings->pinterestProfile['val'] : '')}}" />
</label>
<label>
@lang('admin/settings.social.gplus')
<input type="text" name="gplusProfile" value="{{old('gplusProfile', isset($settings->gplusProfile) ? $settings->gplusProfile : '')}}" />
<input type="text" name="gplusProfile" value="{{old('gplusProfile', isset($settings->gplusProfile) ? $settings->gplusProfile['val'] : '')}}" />
</label>
<label>
@lang('admin/settings.social.instagram')
<input type="text" name="instagramProfile" value="{{old('instagramProfile', isset($settings->instagramProfile) ? $settings->instagramProfile : '')}}" />
<input type="text" name="instagramProfile" value="{{old('instagramProfile', isset($settings->instagramProfile) ? $settings->instagramProfile['val'] : '')}}" />
</label>
@@ -0,0 +1,5 @@
<input type="checkbox" name="commonSettings[{{$field}}]" value="1"
@if(!isset($settings->{$field}) || $settings->{$field}['lang'] == null)
checked
@endif />
@lang('admin/settings.commonSettings')
View File