Skip to content

Feature/multipage #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">= 7.1.0",
"laravel/framework": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"qcod/laravel-settings": "~1.0"
},
2 changes: 2 additions & 0 deletions src/AppSettingsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ public function boot()

$this->publishes([
__DIR__ . '/config/app_settings.php' => config_path('app_settings.php'),
__DIR__ . '/config/app_settings_page_branding.php' => config_path('app_settings_page_branding.php'),
__DIR__ . '/config/app_settings_page_email.php' => config_path('app_settings_page_email.php'),
], 'config');

$this->loadRoutesFrom(__DIR__ . '/routes/web.php');
15 changes: 12 additions & 3 deletions src/SavesSettings.php
Original file line number Diff line number Diff line change
@@ -16,12 +16,21 @@ trait SavesSettings
* @return \Illuminate\View\View
* @param AppSettings $appSettings
*/
public function index(AppSettings $appSettings)
public function index(AppSettings $appSettings, string $page)
{
$settingsUI = $appSettings->loadConfig(config('app_settings', []));
$settingViewName = config('app_settings.setting_page_view');

return view($settingViewName, compact('settingsUI'));
$settingsPage = preg_replace("/[^A-Za-z0-9 ]/", '', $page);

if ( !is_array( config('app_settings.sections.'.$settingsPage) ) ) {
$settingsPage = config('app_settings.default_page');
}

return view($settingViewName, [
'settingsUI' => $settingsUI,
'settingsPage' => $settingsPage,
]);
}

/**
@@ -39,7 +48,7 @@ public function store(Request $request, AppSettings $appSettings)
// save settings
$appSettings->save($request);

return redirect(config('app_settings.url', '/'))
return redirect()->route('settings.index', config('app_settings.default_page'))
->with([
'status' => config('app_settings.submit_success_message', 'Settings Saved.')
]);
14 changes: 8 additions & 6 deletions src/Setting/AppSettings.php
Original file line number Diff line number Diff line change
@@ -102,8 +102,10 @@ public function remove($name)
*/
public function save($request)
{
$settingsPage = preg_replace("/[^A-Za-z0-9 ]/", '', $request->page);

// get all defined settings from config
$allDefinedSettings = $this->getAllSettingFields();
$allDefinedSettings = $this->getAllSettingFields($settingsPage);

// set all the fields with updated values
$allDefinedSettings->each(function ($setting) use ($request) {
@@ -151,19 +153,19 @@ public function loadConfig($config)
*
* @return \Illuminate\Support\Collection
*/
protected function getSettingUISections()
protected function getSettingUISections(string $settingsPage)
{
return collect(config('app_settings.sections', []));
return collect(config('app_settings.sections.'.$settingsPage , []));
}

/**
* Get all the setting fields defined from all sections
*
* @param $settingsPage string : the name of the page of settings to use
* @return \Illuminate\Support\Collection
*/
public function getAllSettingFields()
public function getAllSettingFields(string $settingsPage = '' )
{
return $this->getSettingUISections()->flatMap(function ($field) {
return $this->getSettingUISections($settingsPage)->flatMap(function ($field) {
return array_get($field, 'inputs', []);
});
}
64 changes: 11 additions & 53 deletions src/config/app_settings.php
Original file line number Diff line number Diff line change
@@ -4,62 +4,13 @@

// All the sections for the settings page
'sections' => [
'app' => [
'title' => 'General Settings',
'descriptions' => 'Application general settings.', // (optional)
'icon' => 'fa fa-cog', // (optional)

'inputs' => [
[
'name' => 'app_name', // unique key for setting
'type' => 'text', // type of input can be text, number, textarea, select, boolean, checkbox etc.
'label' => 'App Name', // label for input
// optional properties
'placeholder' => 'Application Name', // placeholder for input
'class' => 'form-control', // override global input_class
'style' => '', // any inline styles
'rules' => 'required|min:2|max:20', // validation rules for this input
'value' => 'QCode', // any default value
'hint' => 'You can set the app name here' // help block text for input
],
[
'name' => 'logo',
'type' => 'image',
'label' => 'Upload logo',
'hint' => 'Must be an image and cropped in desired size',
'rules' => 'image|max:500',
'disk' => 'public', // which disk you want to upload
'path' => 'app', // path on the disk,
'preview_class' => 'thumbnail',
'preview_style' => 'height:40px'
]
]
],
'email' => [
'title' => 'Email Settings',
'descriptions' => 'How app email will be sent.',
'icon' => 'fa fa-envelope',

'inputs' => [
[
'name' => 'from_email',
'type' => 'email',
'label' => 'From Email',
'placeholder' => 'Application from email',
'rules' => 'required|email',
],
[
'name' => 'from_name',
'type' => 'text',
'label' => 'Email from Name',
'placeholder' => 'Email from Name',
]
]
]
'branding' => require(__DIR__ . '/app_settings_page_branding.php'),
'email' => require(__DIR__ . '/app_settings_page_email.php'),
],

// Setting page url, will be used for get and post request
'url' => 'settings',
'url' => '/settings',
'default_page' => 'email',

// Any middleware you want to run on above route
'middleware' => [],
@@ -88,6 +39,13 @@
// Remove any setting which declaration removed later from sections
'remove_abandoned_settings' => false,

// when the setting is not found, can we try to get the config setting for this key
'allow_passthrough_config' => true,

// Should we throw and exception if we cannot find the setting after we checked the config files
// only in addition to 'allow_passthrough_config'
'exception_on_nodefined_config' => true,

// Controller to show and handle save setting
'controller' => '\QCod\AppSettings\Controllers\AppSettingController',
];
36 changes: 36 additions & 0 deletions src/config/app_settings_page_branding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

return [
'app' => [
'title' => 'General Settings',
'descriptions' => 'Application general settings.', // (optional)
'icon' => 'fa fa-cog', // (optional)

'inputs' => [
[
'name' => 'app.name', // unique key for setting
'type' => 'text', // type of input can be text, number, textarea, select, boolean, checkbox etc.
'view' => 'text', // the name of the view blade file , if left out, the type is the name of the view
'label' => 'App Name', // label for input
// optional properties
'placeholder' => config('app.name'), // placeholder for input
'class' => 'form-control', // override global input_class
'style' => '', // any inline styles
'rules' => 'required|min:2|max:20', // validation rules for this input
'value' => config('app.name'),
'hint' => 'You can set the app name here' // help block text for input
],
[
'name' => 'logo',
'type' => 'image',
'label' => 'Upload logo',
'hint' => 'Must be an image and cropped in desired size',
'rules' => 'image|max:500',
'disk' => 'public', // which disk you want to upload
'path' => 'app', // path on the disk,
'preview_class' => 'thumbnail',
'preview_style' => 'height:40px'
]
]
],
];
26 changes: 26 additions & 0 deletions src/config/app_settings_page_email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

return [
'email' => [
'title' => 'Email Settings',
'descriptions' => 'How app email will be sent.',
'icon' => 'fa fa-envelope',

'inputs' => [
[
'name' => 'from_email',
'type' => 'email',
'view' => 'email', // the name of the view, if left out, the type is the name of the view
'label' => 'From Email',
'placeholder' => 'Application from email',
'rules' => 'required|email',
],
[
'name' => 'from_name',
'type' => 'text',
'label' => 'Email from Name',
'placeholder' => 'Email from Name',
]
]
]
];
10 changes: 7 additions & 3 deletions src/resources/views/_settings.blade.php
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@

<form method="post" action="{{ config('app_settings.url') }}" class="form-horizontal mb-3" enctype="multipart/form-data" role="form">
{!! csrf_field() !!}

<input type='hidden' name='page' value='{{$settingsPage}}'>
@if( isset($settingsUI) && count($settingsUI) )

@foreach(array_get($settingsUI, 'sections', []) as $section => $fields)
@foreach(array_get($settingsUI, 'sections'. $settingsPage, []) as $section => $fields)
@component('app_settings::section', compact('fields'))
<div class="{{ array_get($fields, 'section_body_class', config('app_settings.section_body_class', 'card-body')) }}">
@foreach(array_get($fields, 'inputs', []) as $field)
@@ -20,7 +20,11 @@
You can create a <code>fields/{{ $field['type'] }}.balde.php</code> to render this input however you want.
</div>
@endif
@includeIf('app_settings::fields.' . $field['type'] )
@if (isset($field['view']))
@includeIf('app_settings::fields.' . $field['view'])
@else
@includeIf('app_settings::fields.' . $field['type'])
@endif
@endforeach
</div>
@endcomponent
4 changes: 2 additions & 2 deletions src/routes/web.php
Original file line number Diff line number Diff line change
@@ -3,6 +3,6 @@
Route::group([
'middleware' => array_merge(['web'], config('app_settings.middleware', []))
], function () {
Route::get(config('app_settings.url'), config('app_settings.controller').'@index');
Route::post(config('app_settings.url'), config('app_settings.controller').'@store');
Route::get(config('app_settings.url'). '/{page}', config('app_settings.controller').'@index');
Route::post(config('app_settings.url'). '/{page}', config('app_settings.controller').'@store');
});