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
+19 -22
View File
@@ -4,21 +4,23 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests; use App\Http\Requests;
use App\Models\Settings as SettingsModel;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class Settings extends Controller class Settings extends Controller
{ {
/** /**
* @var \App\Models\Settings * @var SettingsModel
*/ */
protected $settings; protected $settings;
/** /**
* Settings constructor. * 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; $this->settings = $settings;
} }
@@ -27,7 +29,7 @@ class Settings extends Controller
{ {
$sections = App::make('registerSettingsExtension') $sections = App::make('registerSettingsExtension')
->get(); ->get();
$settings = $this->settings->getAll(); $settings = $this->settings->getAllWithLanguage();
return view('admin.settings.overview', [ return view('admin.settings.overview', [
'formSections' => $sections, 'formSections' => $sections,
@@ -38,32 +40,27 @@ class Settings extends Controller
public function saveSettings(Requests\SaveSettingsRequest $request) public function saveSettings(Requests\SaveSettingsRequest $request)
{ {
$data = $request->all(); $data = $request->all();
$commonSettings = isset($data['commonSettings']) ? $data['commonSettings'] : [];
// remove CSRF token field // remove CSRF token field
unset($data['_token']); unset($data['_token'], $data['commonSettings']);
$settings = $this->settings->getAll();
$updated = 0;
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if (isset($settings->$key)) { // select language for this setting key
if ($settings->$key != $value) { $language = isset($commonSettings[$key]) ? null : LaravelLocalization::getCurrentLocale();
$updated++; $setting = SettingsModel::firstOrNew(['language' => $language, 'key' => $key]);
\App\Models\Settings::where('key', $key)
->update(['value' => $value]); if ($setting->value == $value) {
continue;
} }
} else {
\App\Models\Settings::create(['key' => $key, 'value' => $value]); // set value and save or delete
$updated++; $setting->value = $value;
} empty($value) ? $setting->delete() : $setting->save();
} }
if ($updated > 0) {
return redirect() return redirect()
->route('settings.overview') ->route('settings.overview')
->with('success', trans('admin/settings.successfullySaved')); ->with('success', trans('admin/settings.successfullySaved'));
} else {
return redirect()
->route('settings.overview')
->with('error', trans('admin/settings.nothingToSave'));
}
} }
} }
+38 -3
View File
@@ -3,12 +3,26 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class Settings extends Model class Settings extends Model
{ {
protected $table = 'settings'; protected $table = 'settings';
public $timestamps = false; 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 * Load all settings
@@ -17,12 +31,33 @@ class Settings extends Model
*/ */
public function getAll() public function getAll()
{ {
$settings = Settings::select(['key', 'value']) $settings = $this->getSettingsQuery()
->get(); ->get();
return $this->returnPairs($settings); 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 * Load only config for views
* *
@@ -30,7 +65,7 @@ class Settings extends Model
*/ */
public function getForView() public function getForView()
{ {
$settings = Settings::select(['key', 'value']) $settings = $this->getSettingsQuery()
->where('load_into_views', 1) ->where('load_into_views', 1)
->get(); ->get();
@@ -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> <label>
@lang('contactform::admin/settings.recaptchaSiteKey') @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>
<label> <label>
@lang('contactform::admin/settings.recaptchaSecret') @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> </label>
<em><a href="https://www.google.com/recaptcha/admin#list" target="_blank">reCAPTCHA</a></em> <em><a href="https://www.google.com/recaptcha/admin#list" target="_blank">reCAPTCHA</a></em>
+1
View File
@@ -11,6 +11,7 @@ return [
'successfullySaved' => 'Settings successfully saved', 'successfullySaved' => 'Settings successfully saved',
'nothingToSave' => 'You have submitted unchanged form. Try to change some attribute first.', 'nothingToSave' => 'You have submitted unchanged form. Try to change some attribute first.',
'meta' => 'Meta information', 'meta' => 'Meta information',
'commonSettings' => 'Use for all languages?',
'socialSites' => 'Social networks', 'socialSites' => 'Social networks',
'social' => [ 'social' => [
'facebook' => 'Facebook profile', 'facebook' => 'Facebook profile',
+2 -2
View File
@@ -5,7 +5,7 @@
<meta http-equiv="x-ua-compatible" content="ie=edge"> <meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
@yield('head_meta') @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/foundation.min.css')}}" />
<link rel="stylesheet" href="{{asset('resources/assets/css/font-awesome.min.css')}}" /> <link rel="stylesheet" href="{{asset('resources/assets/css/font-awesome.min.css')}}" />
<link rel="stylesheet" href="{{asset('resources/assets/css/admin.css')}}" /> <link rel="stylesheet" href="{{asset('resources/assets/css/admin.css')}}" />
@@ -62,7 +62,7 @@
</div> </div>
<footer class="row text-center"> <footer class="row text-center">
&copy; {{$pageName}} {{date('Y')}} &copy; {{isset($pageName) ? $pageName : ''}} {{date('Y')}}
</footer> </footer>
</div> </div>
<script src="{{asset('resources/assets/js/vendor/jquery.min.js')}}"></script> <script src="{{asset('resources/assets/js/vendor/jquery.min.js')}}"></script>
@@ -1,17 +1,27 @@
<label> <label>
@lang('admin/settings.pageName') @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>
<label> <label>
@lang('admin/settings.author') @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>
<label> <label>
@lang('admin/settings.googleAnalyticsCode') @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> </label>
@@ -1,11 +1,13 @@
<label> <label>
@lang('admin/settings.metaKeywords') @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>
<label> <label>
@lang('admin/settings.metaDescription') @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> </label>
@@ -1,29 +1,29 @@
<label> <label>
@lang('admin/settings.social.facebook') @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>
<label> <label>
@lang('admin/settings.social.twitter') @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>
<label> <label>
@lang('admin/settings.social.pinterest') @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>
<label> <label>
@lang('admin/settings.social.gplus') @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>
<label> <label>
@lang('admin/settings.social.instagram') @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> </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