Added a settings page allowing the date format and application name to be configured

This commit is contained in:
Andy Heathershaw 2016-09-05 15:28:56 +01:00
parent ef95fd31ba
commit 00ab249476
14 changed files with 231 additions and 8 deletions

18
app/Facade/UserConfig.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Facade;
use Illuminate\Support\Facades\Facade;
class UserConfig extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'user_config';
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace App\Helpers;
use App\Configuration;
class ConfigHelper
{
public function allowedDateFormats()
{
return [
'Y-m-d - H:i',
'd/m/Y - H:i',
'm/d/Y - H:i',
'jS F Y - H:i',
];
}
public function defaults()
{
return array(
'app_name' => trans('global.app_name'),
'date_format' => $this->allowedDateFormats()[0]
);
}
public function get($key, $defaultIfUnset = true)
{
$config = Configuration::where('key', $key)->first();
return (is_null($config) ? $this->defaults()[$key] : $config->value);
}
public function getAll()
{
$results = array();
foreach (Configuration::all() as $config)
{
$results[$config->key] = $config->value;
}
return $results;
}
public function getOrCreateModel($key)
{
$config = Configuration::where('key', $key)->first();
if (is_null($config) || $config === false)
{
$config = new Configuration();
$config->key = $key;
$config->value = '';
$config->save();
}
return $config;
}
}

View File

@ -3,8 +3,13 @@
namespace App\Http\Controllers\Admin;
use App\Album;
use App\Configuration;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\ConfigHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\SaveSettingsRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DefaultController extends Controller
@ -19,4 +24,44 @@ class DefaultController extends Controller
'album_count' => $albumCount
]);
}
public function saveSettings(SaveSettingsRequest $request)
{
$updateKeys = [
'app_name',
'date_format'
];
foreach ($updateKeys as $key)
{
$config = UserConfig::getOrCreateModel($key);
$config->value = $request->request->get($key);
$config->save();
}
$request->session()->flash('success', trans('admin.settings_saved_message'));
return redirect(route('admin.settings'));
}
public function settings(Request $request)
{
$this->authorize('admin-access');
// Load the current configuration
$config = array_merge(UserConfig::defaults(), UserConfig::getAll());
$dateFormats = UserConfig::allowedDateFormats();
$dateFormatsLookup = [];
foreach ($dateFormats as $dateFormat)
{
$dateFormatsLookup[$dateFormat] = date($dateFormat);
}
return Theme::render('admin.settings', [
'config' => $config,
'date_formats' => $dateFormatsLookup,
'success' => $request->session()->get('success')
]);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SaveSettingsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'app_name' => 'required|max:255',
'date_format' => 'required',
];
}
}

View File

@ -4,6 +4,8 @@ namespace App\Providers;
use App\Album;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Helpers\ConfigHelper;
use App\Helpers\ImageHelper;
use App\Helpers\ThemeHelper;
use Illuminate\Database\QueryException;
@ -27,6 +29,10 @@ class AppServiceProvider extends ServiceProvider
{
return new ThemeHelper();
});
$this->app->singleton('user_config', function($app)
{
return new ConfigHelper();
});
// When running migrations or CLI tasks, don't need to add things to the view
if (php_sapi_name() != 'cli')

View File

@ -1,7 +1,7 @@
<?php
return [
// Version number of Photo Perfect
// Version number of Blue Twilight
'version' => '1.0.0-ALPHA',
/*
@ -230,6 +230,7 @@ return [
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
'Theme' => App\Facade\Theme::class,
'UserConfig' => App\Facade\UserConfig::class
],
];

View File

@ -31,5 +31,5 @@
.tab-content {
border: solid 1px #ddd;
border-top: 0;
padding: 10px;
padding: 20px;
}

View File

@ -11,6 +11,10 @@ return [
'list_albums_name_column' => 'Album name',
'no_albums_text' => 'You have no photo albums yet. Click the button below to create one.',
'no_albums_title' => 'No Photo Albums',
'settings_link' => 'Settings',
'settings_save_action' => 'Update Settings',
'settings_saved_message' => 'The settings were updated successfully.',
'settings_title' => 'Settings',
'stats_albums' => 'album|albums',
'stats_panel' => 'Statistics'
];

View File

@ -22,6 +22,7 @@
<div class="panel-heading">@lang('admin.actions_panel')</div>
<div class="panel-body">
<a href="{{ route('albums.create') }}" class="btn btn-default"><i class="fa fa-fw fa-plus"></i> @lang('admin.create_album_link')</a>
<a href="{{ route('admin.settings') }}" class="btn btn-default"><i class="fa fa-fw fa-cog"></i> @lang('admin.settings_link')</a>
</div>
</div>
</div>

View File

@ -0,0 +1,48 @@
@extends('themes.base.layout')
@section('title', trans('admin.settings_title'))
@section('content')
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>@yield('title')</h1>
<hr/>
{!! Form::model($config, ['route' => 'admin.saveSettings', 'method' => 'POST']) !!}
<div>
{{-- Nav tabs --}}
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#upload-tab" aria-controls="upload-tab" role="tab" data-toggle="tab"><i class="fa fa-fw fa-info-circle"></i> General</a></li>
</ul>
{{-- Tab panes --}}
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="upload-tab">
<div class="form-group">
{!! Form::label('app_name', 'Gallery name:', ['class' => 'control-label']) !!}
{!! Form::text('app_name', old('app_name'), ['class' => 'form-control']) !!}
</div>
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="form-group">
{!! Form::label('date_format', 'Date format:', ['class' => 'control-label']) !!}
{!! Form::select('date_format', $date_formats, old('date_format'), ['class' => 'form-control']) !!}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="text-right" style="margin-top: 15px;">
<a href="{{ route('admin') }}" class="btn btn-default">@lang('forms.cancel_action')</a>
{!! Form::submit(trans('admin.settings_save_action'), ['class' => 'btn btn-success']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
@endsection

View File

@ -33,7 +33,7 @@
@if (strlen($photo->taken_at) > 0)
<tr>
<td class="metadata_name">Date taken:</td>
<td class="metadata_value">{{ $photo->taken_at }}</td>
<td class="metadata_value">{{ date(UserConfig::get('date_format'), strtotime($photo->taken_at)) }}</td>
</tr>
@endif

View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="generator" content="{{ config('app.name') }} v{{ config('app.version') }} (framework v{{ App::VERSION() }})">
<title>@yield('title') | @lang('global.app_name')</title>
<title>@yield('title') | {{ UserConfig::get('app_name') }}</title>
<base href="{{ url('/') }}">
{{-- Cannot use $theme_url here: if a theme uses the base layout, it would also have to provide all these dependencies! --}}
@ -33,6 +33,14 @@
</div>
@endif
@if (isset($success))
<div class="container">
<div class="alert alert-success">
<strong><i class="fa fa-info-circle fa-fw"></i></strong> {{ $success }}
</div>
</div>
@endif
@yield('content')
</div>

View File

@ -8,7 +8,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{{ route('home') }}">@lang('global.app_name')</a>
<a class="navbar-brand" href="{{ route('home') }}">{{ UserConfig::get('app_name') }}</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->

View File

@ -16,6 +16,8 @@ Auth::routes();
// Administration
Route::group(['prefix' => 'admin'], function () {
Route::get('/', 'Admin\DefaultController@index')->name('admin');
Route::post('settings/save', 'Admin\DefaultController@saveSettings')->name('admin.saveSettings');
Route::get('settings', 'Admin\DefaultController@settings')->name('admin.settings');
// Album management
Route::get('albums/{id}/delete', 'Admin\AlbumController@delete')->name('albums.delete');
@ -30,6 +32,6 @@ Route::group(['prefix' => 'admin'], function () {
// Gallery
Route::get('/', 'Gallery\DefaultController@index')->name('home');
Route::get('/{albumUrlAlias}', 'Gallery\AlbumController@index')->name('viewAlbum');
Route::get('/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@show')->name('viewPhoto');
Route::get('/photo/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@download')->name('downloadPhoto');
Route::get('{albumUrlAlias}', 'Gallery\AlbumController@index')->name('viewAlbum');
Route::get('{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@show')->name('viewPhoto');
Route::get('photo/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@download')->name('downloadPhoto');