Commit 81f878ae authored by Nicolas Widart's avatar Nicolas Widart

Merge commit '6a966cc8'

* commit '6a966cc8':
  Squashed 'Modules/Setting/' changes from 7bcb95b..a5c8be9
parents 87f238ed 6a966cc8
......@@ -15,6 +15,8 @@ class CreateSettingsTable extends Migration
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('plainValue');
$table->boolean('isTranslatable');
$table->timestamps();
});
}
......
......@@ -8,5 +8,5 @@ class Setting extends Model
use Translatable;
public $translatedAttributes = ['value', 'description'];
protected $fillable = ['name', 'value', 'description'];
protected $fillable = ['name', 'value', 'description', 'isTranslatable', 'plainValue'];
}
<?php namespace Modules\Setting\Facades;
use Illuminate\Support\Facades\Facade;
class Settings extends Facade
{
protected static function getFacadeAccessor() { return 'setting.settings'; }
}
......@@ -29,11 +29,7 @@ class SettingController extends AdminBaseController
public function index()
{
$settings = $this->setting->all();
$modulesWithSettings = $this->setting->moduleSettings($this->module->enabled());
return View::make('setting::admin.settings', compact('settings', 'modulesWithSettings'));
return Redirect::route('dashboard.module.settings', ['core']);
}
public function store(SettingRequest $request)
......@@ -44,12 +40,16 @@ class SettingController extends AdminBaseController
return Redirect::route('dashboard.setting.index');
}
public function getModuleSettings($module)
public function getModuleSettings($currentModule)
{
$moduleSettings = $this->setting->moduleSettings($module);
$modulesWithSettings = $this->setting->moduleSettings($this->module->enabled());
$translatableSettings = $this->setting->translatableModuleSettings($currentModule);
$plainSettings = $this->setting->plainModuleSettings($currentModule);
$settings = $this->setting->savedModuleSettings($module);
$dbSettings = $this->setting->savedModuleSettings($currentModule);
return View::make('setting::admin.module-settings', compact('module', 'moduleSettings', 'settings'));
return View::make('setting::admin.module-settings',
compact('currentModule', 'translatableSettings', 'plainSettings', 'dbSettings', 'modulesWithSettings'));
}
}
<?php namespace Modules\Setting\Providers;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
use Modules\Setting\Entities\Setting;
use Modules\Setting\Repositories\Eloquent\EloquentSettingRepository;
use Modules\Setting\Support\Settings;
class SettingServiceProvider extends ServiceProvider {
......@@ -23,6 +25,17 @@ class SettingServiceProvider extends ServiceProvider {
$this->app->booted(function () {
$this->registerBindings();
});
$this->app['setting.settings'] = $this->app->share(function($app)
{
return new Settings($app['Modules\Setting\Repositories\SettingRepository']);
});
$this->app->booting(function()
{
$loader = AliasLoader::getInstance();
$loader->alias('Settings', 'Modules\Setting\Facades\Settings');
});
}
/**
......@@ -43,5 +56,9 @@ class SettingServiceProvider extends ServiceProvider {
return new EloquentSettingRepository(new Setting);
}
);
$this->app->bind(
'Modules\Core\Contracts\Setting',
'Modules\Setting\Support\Settings'
);
}
}
......@@ -50,7 +50,6 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
$this->removeTokenKey($settings);
foreach ($settings as $settingName => $settingValues) {
// Check if setting exists
if ($setting = $this->findByName($settingName)) {
$this->updateSetting($setting, $settingValues);
continue;
......@@ -75,24 +74,26 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
*/
public function findByName($settingName)
{
return $this->model->whereHas(
'translations',
function ($q) use ($settingName) {
$q->where('name', $settingName);
}
)->first();
return $this->model->where('name', $settingName)->first();
}
/**
* Create a setting with the given name
* @param $settingName
* @param string $settingName
* @param $settingValues
*/
private function createForName($settingName, $settingValues)
{
$setting = new $this->model;
$setting->name = $settingName;
if (is_array($settingValues)) {
$setting->isTranslatable = true;
$this->setTranslatedAttributes($settingValues, $setting);
} else {
$setting->isTranslatable = false;
$setting->plainValue = $settingValues;
}
return $setting->save();
}
......@@ -104,7 +105,11 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
*/
private function updateSetting($setting, $settingValues)
{
if (is_array($settingValues)) {
$this->setTranslatedAttributes($settingValues, $setting);
} else {
$setting->plainValue = $settingValues;
}
return $setting->save();
}
......@@ -164,21 +169,40 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
*/
public function findByModule($module)
{
return $this->model->where('name', 'LIKE', $module . '_%')->get();
return $this->model->where('name', 'LIKE', $module . '::%')->get();
}
/**
* Find the given setting name for the given module
* @param $settingName
* @param string $settingName
* @return mixed
*/
public function get($settingName)
{
return $this->model->where('name', 'LIKE', "{$settingName}")->first();
}
/**
* Return the translatable module settings
* @param $module
* @return mixed
*/
public function findSettingForModule($settingName, $module = null)
public function translatableModuleSettings($module)
{
if (is_null($module)) {
return $this->model->where('name', 'LIKE', "%{$settingName}")->first();
return array_filter($this->moduleSettings($module), function($setting) {
return isset($setting['translatable']);
});
}
return $this->model->where('name', 'LIKE', "{$module}_{$settingName}")->first();
/**
* Return the non translatable module settings
* @param $module
* @return array
*/
public function plainModuleSettings($module)
{
return array_filter($this->moduleSettings($module), function($setting) {
return !isset($setting['translatable']);
});
}
}
......@@ -42,9 +42,22 @@ interface SettingRepository extends BaseRepository
/**
* Find the given setting name for the given module
* @param $settingName
* @param $module
* @param string $settingName
* @return mixed
*/
public function findSettingForModule($settingName, $module = null);
public function get($settingName);
/**
* Return the translatable module settings
* @param $module
* @return array
*/
public function translatableModuleSettings($module);
/**
* Return the non translatable module settings
* @param $module
* @return array
*/
public function plainModuleSettings($module);
}
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class="checkbox">
<label for="{{ $settingName }}">
<input id="{{ $settingName }}"
name="{{ $settingName }}"
type="checkbox"
class="flat-blue"
{{ isset($dbSettings[$settingName]) && (bool)$dbSettings[$settingName]->value == true ? 'checked' : '' }}
value="1" />
{{ $moduleInfo['description'] }}
</label>
</div>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class='form-group'>
{!! Form::label($settingName, $moduleInfo['description']) !!}
<?php if (isset($dbSettings[$settingName])): ?>
{!! Form::input('number', $settingName, Input::old($settingName, $dbSettings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php else: ?>
{!! Form::input('number', $settingName, Input::old($settingName), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
</div>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class="checkbox">
<?php foreach($moduleInfo['options'] as $value => $optionName): ?>
<label for="{{ $optionName }}">
<input id="{{ $optionName }}"
name="{{ $settingName }}"
type="radio"
class="flat-blue"
{{ isset($dbSettings[$settingName]) && (bool)$dbSettings[$settingName]->value == $value ? 'checked' : '' }}
value="{{ $value }}" />
{{ $optionName }}
</label>
<?php endforeach; ?>
</div>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class='form-group'>
{!! Form::label($settingName, $moduleInfo['description']) !!}
<?php if (isset($dbSettings[$settingName])): ?>
{!! Form::text($settingName, Input::old($settingName, $dbSettings[$settingName]->plainValue), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php else: ?>
{!! Form::text($settingName, Input::old($settingName), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
</div>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class='form-group'>
{!! Form::label($settingName", $moduleInfo['description']) !!}
<?php if (isset($dbSettings[$settingName])): ?>
{!! Form::textarea($settingName", Input::old($settingName", $dbSettings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php else: ?>
{!! Form::textarea($settingName", Input::old($settingName"), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
</div>
<?php $settingName = $module . '_' . $setting; ?>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class="checkbox">
<label for="{{ $settingName . "[$lang]" }}">
<input id="{{ $settingName . "[$lang]" }}"
name="{{ $settingName . "[$lang]" }}"
type="checkbox"
class="flat-blue"
{{ isset($settings[$settingName]) && (bool)$settings[$settingName]->translate($lang)->value == true ? 'checked' : '' }}
{{ isset($dbSettings[$settingName]) && (bool)$dbSettings[$settingName]->translate($lang)->value == true ? 'checked' : '' }}
value="1" />
{{ $moduleInfo['description'] }}
</label>
......
<?php $settingName = $module . '_' . $setting; ?>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class='form-group'>
{!! Form::label($settingName . "[$lang]", $moduleInfo['description']) !!}
<?php if (isset($settings[$settingName])): ?>
{!! Form::input('number', $settingName . "[$lang]", Input::old($settingName . "[$lang]", $settings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php if (isset($dbSettings[$settingName])): ?>
{!! Form::input('number', $settingName . "[$lang]", Input::old($settingName . "[$lang]", $dbSettings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php else: ?>
{!! Form::input('number', $settingName . "[$lang]", Input::old($settingName . "[$lang]"), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
......
<?php $settingName = $module . '_' . $setting; ?>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class="checkbox">
<?php foreach($moduleInfo['options'] as $value => $optionName): ?>
<label for="{{ $optionName . "[$lang]" }}">
......@@ -6,7 +6,7 @@
name="{{ $settingName . "[$lang]" }}"
type="radio"
class="flat-blue"
{{ isset($settings[$settingName]) && (bool)$settings[$settingName]->translate($lang)->value == $value ? 'checked' : '' }}
{{ isset($dbSettings[$settingName]) && (bool)$dbSettings[$settingName]->translate($lang)->value == $value ? 'checked' : '' }}
value="{{ $value }}" />
{{ $optionName }}
</label>
......
<?php $settingName = $module . '_' . $setting; ?>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class='form-group'>
{!! Form::label($settingName . "[$lang]", $moduleInfo['description']) !!}
<?php if (isset($settings[$settingName])): ?>
{!! Form::text($settingName . "[$lang]", Input::old($settingName . "[$lang]", $settings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php if (isset($dbSettings[$settingName])): ?>
{!! Form::text($settingName . "[$lang]", Input::old($settingName . "[$lang]", $dbSettings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php else: ?>
{!! Form::text($settingName . "[$lang]", Input::old($settingName . "[$lang]"), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
......
<?php $settingName = $module . '_' . $setting; ?>
<?php $settingName = strtolower($currentModule) . '::' . $setting; ?>
<div class='form-group'>
{!! Form::label($settingName . "[$lang]", $moduleInfo['description']) !!}
<?php if (isset($settings[$settingName])): ?>
{!! Form::textarea($settingName . "[$lang]", Input::old($settingName . "[$lang]", $settings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php if (isset($dbSettings[$settingName])): ?>
{!! Form::textarea($settingName . "[$lang]", Input::old($settingName . "[$lang]", $dbSettings[$settingName]->translate($lang)->value), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php else: ?>
{!! Form::textarea($settingName . "[$lang]", Input::old($settingName . "[$lang]"), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
......
......@@ -2,12 +2,12 @@
@section('content-header')
<h1>
{{ trans('setting::settings.title.module name settings', ['module' => ucfirst($module)]) }}
{{ trans('setting::settings.title.module name settings', ['module' => ucfirst($currentModule)]) }}
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> {{ trans('core::core.breadcrumb.home') }}</a></li>
<li><a href="{{ URL::route('dashboard.setting.index') }}"><i class="fa fa-cog"></i> {{ trans('setting::settings.breadcrumb.settings') }}</a></li>
<li class="active"><i class="fa fa-cog"></i> {{ trans('setting::settings.breadcrumb.module settings', ['module' => ucfirst($module)]) }}</li>
<li class="active"><i class="fa fa-cog"></i> {{ trans('setting::settings.breadcrumb.module settings', ['module' => ucfirst($currentModule)]) }}</li>
</ol>
@stop
......@@ -19,48 +19,63 @@
@include('flash::message')
{!! Form::open(['route' => ['dashboard.setting.store'], 'method' => 'post']) !!}
<div class="row">
<div class="col-md-12">
<div class="col-md-2">
<div class="box box-info">
<div class="box-header"><h3 class="box-title">{{ trans('setting::settings.title.module settings') }}</h3></div>
<div class="box-body">
<ul>
<?php foreach($modulesWithSettings as $module => $settings): ?>
<li style="margin-bottom: 15px"><a href="{{ URL::route('dashboard.module.settings', [$module]) }}" class="btn btn-default btn-flat {{ $module == ucfirst($currentModule) ? 'disabled' : '' }}">{{ $module }}</a></li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
<div class="col-md-10">
<div class="box box-info">
<div class="box-header">
<h3 class="box-title">{{ trans('core::core.title.translatable fields') }}</h3>
</div>
<?php if ($translatableSettings): ?>
<div class="box-body">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="{{ App::getLocale() == 'en' ? 'active' : '' }}"><a href="#tab_1-1" data-toggle="tab">{{ trans('core::core.tab.english') }}</a></li>
<li class="{{ App::getLocale() == 'fr' ? 'active' : '' }}"><a href="#tab_2-2" data-toggle="tab">{{ trans('core::core.tab.french') }}</a></li>
<?php $i = 0; ?>
<?php foreach(LaravelLocalization::getSupportedLocales() as $locale => $language): ?>
<?php $i++; ?>
<li class="{{ App::getLocale() == $locale ? 'active' : '' }}">
<a href="#tab_{{ $i }}" data-toggle="tab">{{ trans('core::core.tab.'. strtolower($language['name'])) }}</a>
</li>
<?php endforeach; ?>
</ul>
<div class="tab-content">
<div class="tab-pane {{ App::getLocale() == 'en' ? 'active' : '' }}" id="tab_1-1">
<?php foreach($moduleSettings as $settingName => $moduleInfo): ?>
@include($moduleInfo['view'], [
'lang' => 'en',
'moduleSettings' => $moduleSettings,
'settings' => $settings,
'module' => $module,
'setting' => $settingName,
'moduleInfo' => $moduleInfo,
])
<?php endforeach; ?>
<?php $i = 0; ?>
<?php foreach(LaravelLocalization::getSupportedLocales() as $locale => $language): ?>
<?php $i++; ?>
<div class="tab-pane {{ App::getLocale() == $locale ? 'active' : '' }}" id="tab_{{ $i }}">
@include('setting::admin.partials.fields', ['settings' => $translatableSettings])
</div>
<div class="tab-pane {{ App::getLocale() == 'fr' ? 'active' : '' }}" id="tab_2-2">
<?php foreach($moduleSettings as $settingName => $moduleInfo): ?>
@include($moduleInfo['view'], [
'lang' => 'fr',
'moduleSettings' => $moduleSettings,
'settings' => $settings,
'module' => $module,
'setting' => $settingName,
'moduleInfo' => $moduleInfo,
])
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php if ($plainSettings): ?>
<div class="box box-info">
<div class="box-header">
<h3 class="box-title">{{ trans('core::core.title.non translatable fields') }}</h3>
</div>
<div class="box-body">
@include('setting::admin.partials.fields', ['settings' => $plainSettings])
</div>
</div>
<?php endif; ?>
<div class="box-footer">
<button type="submit" class="btn btn-primary btn-flat">{{ trans('core::core.button.create') }}</button>
<a class="btn btn-danger pull-right btn-flat" href="{{ URL::route('dashboard.setting.index')}}"><i class="fa fa-times"></i> {{ trans('core::core.button.cancel') }}</a>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
@stop
......
<div class='form-group{{ $errors->has("site-name[$lang]") ? ' has-error' : '' }}'>
{!! Form::label("site-name[$lang]", 'Site name:') !!}
<?php if (isset($settings['site-name'])): ?>
{!! Form::text("site-name[$lang]", Input::old("site-name[$lang]", $settings['site-name']->translate($lang)->value), ['class' => 'form-control', 'placeholder' => 'Site name']) !!}
<?php else: ?>
{!! Form::text("site-name[$lang]", Input::old("site-name[$lang]"), ['class' => 'form-control', 'placeholder' => 'Site name']) !!}
<?php endif; ?>
{!! $errors->first("site-name[$lang]", '<span class="help-block">:message</span>') !!}
</div>
<div class='form-group{{ $errors->has("site-description[$lang]") ? ' has-error' : '' }}'>
{!! Form::label("site-description[$lang]", 'Site-description:') !!}
<?php if (isset($settings['site-description'])): ?>
{!! Form::textarea("site-description[$lang]", Input::old("site-description[$lang]", $settings['site-description']->translate($lang)->value), ['class' => 'form-control', 'placeholder' => 'Site description']) !!}
<?php else: ?>
{!! Form::textarea("site-description[$lang]", Input::old("site-description[$lang]"), ['class' => 'form-control', 'placeholder' => 'Site description']) !!}
<?php endif; ?>
{!! $errors->first("site-description[$lang]", '<span class="help-block">:message</span>') !!}
</div>
<?php use Illuminate\Support\Str; ?>
<?php foreach($settings as $settingName => $moduleInfo): ?>
<?php $fieldView = Str::contains($moduleInfo['view'], '::') ? $moduleInfo['view'] : "setting::admin.fields.translatable.{$moduleInfo['view']}" ?>
@include($fieldView, [
'lang' => $locale,
'settings' => $settings,
'setting' => $settingName,
'moduleInfo' => $moduleInfo,
])
<?php endforeach;
@extends('core::layouts.master')
@section('content-header')
<h1>
{{ trans('setting::settings.title.settings') }}
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> {{ trans('core::core.breadcrumb.home') }}</a></li>
<li class="active"><i class="fa fa-cog"></i> {{ trans('setting::settings.breadcrumb.settings') }}</li>
</ol>
@stop
@section('content')
@include('flash::message')
{!! Form::open(['route' => ['dashboard.setting.store'], 'method' => 'post']) !!}
<div class="row">
<div class="col-md-8">
<div class="box box-info">
<div class="box-header">
<h3 class="box-title">{{ trans('setting::settings.title.general settings') }}</h3>
</div>
<div class="box-body">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="{{ App::getLocale() == 'en' ? 'active' : '' }}"><a href="#tab_1-1" data-toggle="tab">{{ trans('core::core.tab.english') }}</a></li>
<li class="{{ App::getLocale() == 'fr' ? 'active' : '' }}"><a href="#tab_2-2" data-toggle="tab">{{ trans('core::core.tab.french') }}</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane {{ App::getLocale() == 'en' ? 'active' : '' }}" id="tab_1-1">
@include('setting::admin.partials.fields', ['lang' => 'en'])
</div>
<div class="tab-pane {{ App::getLocale() == 'fr' ? 'active' : '' }}" id="tab_2-2">
@include('setting::admin.partials.fields', ['lang' => 'fr'])
</div>
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary btn-flat">{{ trans('core::core.button.create') }}</button>
</div>
</div>
</div>
<div class="col-md-4">
<div class="box box-info">
<div class="box-header"><h3 class="box-title">{{ trans('setting::settings.title.module settings') }}</h3></div>
<div class="box-body">
<ul>
<?php foreach($modulesWithSettings as $module => $settings): ?>
<li><a href="{{ URL::route('dashboard.module.settings', [strtolower($module)]) }}">{{ $module }}</a></li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
@stop
<?php namespace Modules\Setting\Support;
use Modules\Core\Contracts\Setting;
use Modules\Setting\Repositories\SettingRepository;
class Settings implements Setting
{
/**
* @var SettingRepository
*/
private $setting;
/**
* @param SettingRepository $setting
*/
public function __construct(SettingRepository $setting)
{
$this->setting = $setting;
}
/**
* Getting the setting
* @param string $name
* @param null $locale
* @param null $default
* @return mixed
*/
public function get($name, $locale = null, $default = null)
{
$setting = $this->setting->get($name);
if ($setting) {
if ($setting->isTranslatable) {
return $setting->translate($locale)->value;
}
return $setting->plainValue;
}
return $default;
}
/**
* Determine if the given configuration value exists.
*
* @param string $name
* @return bool
*/
public function has($name)
{
$default = microtime(true);
return $this->get($name, null, $default) !== $default;
}
/**
* Set a given configuration value.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function set($key, $value)
{
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment