Commit 58eb659b authored by Nicolas Widart's avatar Nicolas Widart

Add 'Modules/Setting/' from commit '56f557f5'

git-subtree-dir: Modules/Setting
git-subtree-mainline: 61de4f40
git-subtree-split: 56f557f5
parents 61de4f40 56f557f5
<?php namespace Modules\Setting\Composers;
use Illuminate\View\View;
class SidebarViewComposer
{
public function compose(View $view)
{
$view->items->put('setting', [
'weight' => 5,
'request' => "*/$view->prefix/settings*",
'route' => 'dashboard.setting.index',
'icon-class' => 'fa fa-cog',
'title' => 'Settings',
]);
}
}
<?php
return [
'settings' => [
'index'
]
];
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('settings');
}
}
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSettingTranslationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('setting_translations', function(Blueprint $table)
{
$table->increments('id');
$table->integer('setting_id')->unsigned();
$table->string('locale')->index();
$table->string('value');
$table->text('description');
$table->unique(['setting_id','locale']);
$table->foreign('setting_id')->references('id')->on('settings')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('setting_translations');
}
}
<?php namespace Modules\Setting\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class SettingDatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call("OthersTableSeeder");
}
}
<?php namespace Modules\Setting\Entities;
use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
use Translatable;
public $translatedAttributes = ['value', 'description'];
protected $fillable = ['name', 'value', 'description'];
}
<?php namespace Modules\Setting\Entities;
use Illuminate\Database\Eloquent\Model;
class SettingTranslation extends Model
{
public $timestamps = false;
protected $fillable = ['value', 'description'];
}
<?php namespace Modules\Setting\Http\Controllers\Admin;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Laracasts\Flash\Flash;
use Modules\Core\Http\Controllers\Admin\AdminBaseController;
use Modules\Setting\Http\Requests\SettingRequest;
use Modules\Setting\Repositories\SettingRepository;
use Pingpong\Modules\Module;
class SettingController extends AdminBaseController
{
/**
* @var SettingRepository
*/
private $setting;
/**
* @var Module
*/
private $module;
public function __construct(SettingRepository $setting, Module $module)
{
parent::__construct();
$this->setting = $setting;
$this->module = $module;
}
public function index()
{
$settings = $this->setting->all();
$modulesWithSettings = $this->setting->moduleSettings($this->module->enabled());
return View::make('setting::admin.settings', compact('settings', 'modulesWithSettings'));
}
public function store(SettingRequest $request)
{
$this->setting->createOrUpdate($request->all());
Flash::success('Settings saved!');
return Redirect::route('dashboard.setting.index');
}
public function getModuleSettings($module)
{
$moduleSettings = $this->setting->moduleSettings($module);
$settings = $this->setting->savedModuleSettings($module);
return View::make('setting::admin.module-settings', compact('module', 'moduleSettings', 'settings'));
}
}
<?php namespace Modules\Setting\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SettingRequest extends FormRequest
{
public function rules()
{
return [];
}
public function authorize()
{
return true;
}
public function messages()
{
return [];
}
}
<?php
$router->group(['prefix' => LaravelLocalization::setLocale(), 'before' => 'LaravelLocalizationRedirectFilter|auth.admin'], function($router) {
$router->group(['prefix' => Config::get('core::core.admin-prefix'), 'namespace' => 'Modules\Setting\Http\Controllers'], function($router)
{
$router->get('settings/{module}', ['as' => 'dashboard.module.settings', 'uses' => 'Admin\SettingController@getModuleSettings']);
$router->resource('settings', 'Admin\SettingController', ['except' => ['show', 'edit', 'update', 'destroy', 'create'], 'names' => [
'index' => 'dashboard.setting.index',
'store' => 'dashboard.setting.store'
]]);
});
});
<?php namespace Modules\Setting\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* The root namespace to assume when generating URLs to actions.
*
* @var string
*/
protected $rootUrlNamespace = 'Modules\Setting\Http\Controllers';
/**
* The controllers to scan for route annotations.
*
* @var array
*/
protected $scan = [
'Modules\Setting\Http\Controllers',
];
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*
* @param Router $router
* @return void
*/
public function before(Router $router)
{
//
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map(Router $router)
{
require __DIR__ . '/../Http/routes.php';
}
}
<?php namespace Modules\Setting\Providers;
use Illuminate\Support\ServiceProvider;
use Modules\Setting\Entities\Setting;
use Modules\Setting\Repositories\Eloquent\EloquentSettingRepository;
class SettingServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->booted(function () {
$this->registerBindings();
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
private function registerBindings()
{
$this->app->bind(
'Modules\Setting\Repositories\SettingRepository',
function() {
return new EloquentSettingRepository(new Setting);
}
);
}
}
<?php namespace Modules\Setting\Repositories\Eloquent;
use Illuminate\Support\Facades\Config;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Setting\Repositories\SettingRepository;
class EloquentSettingRepository extends EloquentBaseRepository implements SettingRepository
{
/**
* Update a resource
* @param $id
* @param $data
* @return mixed
*/
public function update($id, $data)
{
}
/**
* @param mixed $data
* @return mixed
*/
public function create($data)
{
}
/**
* Return all settings, with the setting name as key
* @return array
*/
public function all()
{
$rawSettings = parent::all();
$settings = [];
foreach ($rawSettings as $setting) {
$settings[$setting->name] = $setting;
}
return $settings;
}
/**
* Create or update the settings
* @param $settings
* @return mixed|void
*/
public function createOrUpdate($settings)
{
$this->removeTokenKey($settings);
foreach ($settings as $settingName => $settingValues) {
// Check if setting exists
if ($setting = $this->findByName($settingName)) {
$this->updateSetting($setting, $settingValues);
continue;
}
$this->createForName($settingName, $settingValues);
}
}
/**
* Remove the token from the input array
* @param $settings
*/
private function removeTokenKey(&$settings)
{
unset($settings['_token']);
}
/**
* Find a setting by its name
* @param $settingName
* @return mixed
*/
public function findByName($settingName)
{
return $this->model->whereHas(
'translations',
function ($q) use ($settingName) {
$q->where('name', $settingName);
}
)->first();
}
/**
* Create a setting with the given name
* @param $settingName
* @param $settingValues
*/
private function createForName($settingName, $settingValues)
{
$setting = new $this->model;
$setting->name = $settingName;
$this->setTranslatedAttributes($settingValues, $setting);
return $setting->save();
}
/**
* Update the given setting
* @param $setting
* @param $settingValues
*/
private function updateSetting($setting, $settingValues)
{
$this->setTranslatedAttributes($settingValues, $setting);
return $setting->save();
}
/**
* @param $settingValues
* @param $setting
*/
private function setTranslatedAttributes($settingValues, $setting)
{
foreach ($settingValues as $lang => $value) {
$setting->translate($lang)->value = $value;
}
}
/**
* Return all modules that have settings
* with its settings
* @param array|string $modules
* @return array
*/
public function moduleSettings($modules)
{
if (is_string($modules)) {
return Config::get(strtolower($modules) . "::settings");
}
$modulesWithSettings = [];
foreach ($modules as $module) {
if ($moduleSettings = Config::get(strtolower($module) . "::settings")) {
$modulesWithSettings[$module] = $moduleSettings;
}
}
return $modulesWithSettings;
}
/**
* Return the saved module settings
* @param $module
* @return mixed
*/
public function savedModuleSettings($module)
{
$moduleSettings = [];
foreach ($this->findByModule($module) as $setting) {
$moduleSettings[$setting->name] = $setting;
}
return $moduleSettings;
}
/**
* Find settings by module name
* @param string $module Module name
* @return mixed
*/
private function findByModule($module)
{
return $this->model->where('name', 'LIKE', $module . '_%')->get();
}
}
<?php namespace Modules\Setting\Repositories;
use Modules\Core\Repositories\BaseRepository;
interface SettingRepository extends BaseRepository
{
/**
* Create or update the settings
* @param $settings
* @return mixed
*/
public function createOrUpdate($settings);
/**
* Find a setting by its name
* @param $settingName
* @return mixed
*/
public function findByName($settingName);
/**
* Return all modules that have settings
* with its settings
* @param array|string $modules
* @return array
*/
public function moduleSettings($modules);
/**
* Return the saved module settings
* @param $module
* @return mixed
*/
public function savedModuleSettings($module);
}
<?php
return [
'title' => [
'settings' => 'Settings',
'general settings' => 'General settings',
'module settings' => 'Module settings',
'module name settings' => ':module module settings',
],
'breadcrumb' => [
'settings' => 'Settings',
'module settings' => ':module module settings',
]
];
<?php
return [
'title' => [
'settings' => 'Configurations',
'general settings' => 'Configuration générale',
'module settings' => 'Configuration des modules',
'module name settings' => 'Configuration du module :module',
],
'breadcrumb' => [
'settings' => 'Configurations',
'module settings' => 'Configuration du :module',
]
];
@extends('core::layouts.master')
@section('content-header')
<h1>
{{ trans('setting::settings.title.module name settings', ['module' => ucfirst($module)]) }}
</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>
</ol>
@stop
@section('styles')
<link href="{{{ Module::asset('core', 'css/vendor/iCheck/flat/blue.css') }}}" rel="stylesheet" type="text/css" />
@stop
@section('content')
@include('flash::message')
{!! Form::open(['route' => ['dashboard.setting.store'], 'method' => 'post']) !!}
<div class="row">
<div class="col-md-12">
<div class="box box-info">
<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">
<?php foreach($moduleSettings as $settingName => $moduleInfo): ?>
@include($moduleInfo['view'], [
'lang' => 'en',
'moduleSettings' => $moduleSettings,
'settings' => $settings,
'module' => $module,
'setting' => $settingName,
'moduleInfo' => $moduleInfo,
])
<?php endforeach; ?>
</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>
</div>
<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
@section('scripts')
<script>
$( document ).ready(function() {
$('input[type="checkbox"].flat-blue, input[type="radio"].flat-blue').iCheck({
checkboxClass: 'icheckbox_flat-blue',
radioClass: 'iradio_flat-blue'
});
$('input[type="checkbox"]').on('ifChecked', function(){
$(this).parent().find('input[type=hidden]').remove();
});
$('input[type="checkbox"]').on('ifUnchecked', function(){
var name = $(this).attr('name'),
input = '<input type="hidden" name="' + name + '" value="0" />';
$(this).parent().append(input);
});
});
</script>
@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 $settingName = $module . '_' . $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' : '' }}
value="1" />
{{ $moduleInfo['description'] }}
</label>
</div>
<?php $settingName = $module . '_' . $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 else: ?>
{!! Form::text($settingName . "[$lang]", Input::old($settingName . "[$lang]"), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
</div>
<?php $settingName = $module . '_' . $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 else: ?>
{!! Form::textarea($settingName . "[$lang]", Input::old($settingName . "[$lang]"), ['class' => 'form-control', 'placeholder' => $moduleInfo['description']]) !!}
<?php endif; ?>
</div>
@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
View::composer('core::partials.sidebar-nav', 'Modules\Setting\Composers\SidebarViewComposer');
{
"name": "Setting",
"alias": "setting",
"description": "",
"keywords": [
],
"active": 1,
"providers": [
"Modules\\Setting\\Providers\\SettingServiceProvider",
"Modules\\Setting\\Providers\\RouteServiceProvider"
]
}
\ No newline at end of file
# Setting Module
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/92d544b4-a3ca-4c2a-9ffd-0741c521cb14/mini.png)](https://insight.sensiolabs.com/projects/92d544b4-a3ca-4c2a-9ffd-0741c521cb14)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nWidart-Modules/Setting/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nWidart-Modules/Setting/?branch=master)
<?php
/*
|--------------------------------------------------------------------------
| Register The Module Namespaces
|--------------------------------------------------------------------------
|
| Here is you can register the namespace for this module.
| You may to edit this namespace if you want.
|
*/
View::addNamespace('setting', __DIR__ . '/Resources/views/');
Lang::addNamespace('setting', __DIR__ . '/Resources/lang/');
Config::addNamespace('setting', __DIR__ . '/Config/');
require __DIR__ . '/composers.php';
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