added settings

This commit is contained in:
KodeStar 2018-02-04 20:50:59 +00:00
parent afe78c4e06
commit d21138529f
19 changed files with 446 additions and 13 deletions

4
.env
View file

@ -1,7 +1,7 @@
APP_NAME=Heimdall
APP_ENV=production
APP_ENV=local
APP_KEY=base64:I206O8ibx+GQyRE7BeOxDobn04Mfmyyc5Ptzns/C0mY=
APP_DEBUG=false
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

View file

@ -0,0 +1,80 @@
<?php
namespace App\Http\Controllers;
use App\Setting;
use App\SettingGroup;
use App\Http\Controllers\Controller;
class SettingsController extends Controller
{
/**
* @return \Illuminate\View\View
*/
public function index()
{
$settings = SettingGroup::with([
'settings',
])->orderBy('order', 'ASC')->get();
return view('settings.list')->with([
'groups' => $settings,
]);
}
/**
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function edit($id)
{
$setting = Setting::find($id);
if (!is_null($setting)) {
return view('settings.edit')->with([
'setting' => $setting,
]);
} else {
return redirect()->route('settings.list')->with([
'error' => 'This Setting does not exist.',
]);
}
}
/**
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update($id)
{
$setting = Setting::find($id);
if (!is_null($setting)) {
$data = Setting::getInput();
if ($setting->type == 'image') {
if (!is_null($data->image) && $data->image->isValid()) {
$destinationPath = uploads_path().'/settings/';
$extension = $data->image->getClientOriginalExtension();
$fileName = rand(11111111, 99999999).'.'.$extension;
$data->image->move($destinationPath, $fileName);
$setting->value = $fileName;
}
} else {
$setting->value = $data->value;
}
$setting->save();
return redirect()->route('settings.list')->with([
'success' => 'You have successfully edited this Setting!',
]);
} else {
return redirect()->route('settings.list')->with([
'error' => 'This Setting does not exist.',
]);
}
}
}

View file

@ -17,7 +17,7 @@ class AppServiceProvider extends ServiceProvider
if(!file_exists(database_path(env('DB_DATABASE')))) {
// first time setup
touch(database_path(env('DB_DATABASE')));
Artisan::call('migrate', array('--path' => 'database/migrations', '--force' => true));
Artisan::call('migrate', array('--path' => 'database/migrations', '--force' => true, '--seed' => true));
Artisan::call('storage:link');
//Cache
//Artisan::call('config:cache');
@ -32,6 +32,8 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
$this->app->singleton('settings', function () {
return new Setting();
});
}
}

88
app/Setting.php Normal file
View file

@ -0,0 +1,88 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
class Setting extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'settings';
/**
* Tell the Model this Table doesn't support timestamps.
*
* @var bool
*/
public $timestamps = false;
/**
* Cache storage for Settings.
*
* @var array
*/
protected static $cache = [];
/**
* @return array
*/
public static function getInput()
{
return (object) [
'value' => Input::get('value'),
'image' => Input::file('value'),
];
}
public function group()
{
return $this->belongsTo('App\SettingGroup', 'group_id');
}
/**
* @param string $key
*
* @return mixed
*/
public static function fetch($key)
{
if (Setting::cached($key)) {
return Setting::$cache[$key];
} else {
$find = self::where('key', '=', $key)->first();
if (!is_null($find)) {
$value = $find->value;
Setting::add($key, $value);
return $value;
} else {
return false;
}
}
}
/**
* @param string $key
* @param $value
*/
public static function add($key, $value)
{
Setting::$cache[$key] = $value;
}
/**
* @param string $key
*
* @return bool
*/
public static function cached($key)
{
return array_key_exists($key, Setting::$cache);
}
}

27
app/SettingGroup.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SettingGroup extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'setting_groups';
/**
* Tell the Model this Table doesn't support timestamps.
*
* @var bool
*/
public $timestamps = false;
public function settings()
{
return $this->hasMany('App\Setting', 'group_id');
}
}

View file

@ -13,7 +13,8 @@ return [
|
*/
'name' => env('APP_NAME', 'Laravel'),
'name' => env('APP_NAME', 'Heimdall'),
'version' => '1.1.0',
/*
|--------------------------------------------------------------------------

View file

@ -0,0 +1,37 @@
<?php
use Illuminate\Support\Facades\Schema;
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->integer('group_id')->default(0);
$table->string('key');
$table->string('type')->default('text');
$table->string('label');
$table->string('value')->nullable();
$table->string('order')->default(0);
$table->boolean('system')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSettingGroupsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('setting_groups', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('order')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('setting_groups');
}
}

View file

@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(SettingsSeeder::class);
}
}

View file

@ -0,0 +1,70 @@
<?php
use Illuminate\Database\Seeder;
use App\Setting;
use App\SettingGroup;
class SettingsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Groups
if(!SettingGroup::find(1)) {
$setting_group = new SettingGroup;
$setting_group->id = 1;
$setting_group->title = 'System';
$setting_group->order = 0;
$setting_group->save();
}
if(!SettingGroup::find(2)) {
$setting_group = new SettingGroup;
$setting_group->id = 2;
$setting_group->title = 'Appearance';
$setting_group->order = 1;
$setting_group->save();
}
if(!SettingGroup::find(3)) {
$setting_group = new SettingGroup;
$setting_group->id = 3;
$setting_group->title = 'Miscellaneous';
$setting_group->order = 2;
$setting_group->save();
}
if(!Setting::find(1)) {
$setting = new Setting;
$setting->id = 1;
$setting->group_id = 1;
$setting->key = 'version';
$setting->type = 'text';
$setting->label = 'Version';
$setting->value = config('app.version');
$setting->system = true;
$setting->save();
}
if(!Setting::find(2)) {
$setting = new Setting;
$setting->id = 2;
$setting->group_id = 2;
$setting->key = 'background_image';
$setting->type = 'image';
$setting->label = 'Background Image';
$setting->save();
}
if(!Setting::find(3)) {
$setting = new Setting;
$setting->id = 3;
$setting->group_id = 3;
$setting->key = 'homepage_search';
$setting->type = 'text';
$setting->label = 'Homepage Search';
$setting->save();
}
}
}

2
public/css/app.css vendored
View file

@ -553,7 +553,7 @@ body {
background: #f9fafd;
max-width: 1000px;
width: 100%;
margin: 0 40px;
margin: 10px 40px;
}
.module-container header,

View file

@ -1,4 +1,4 @@
{
"/css/app.css": "/css/app.css?id=2bcada6f52a2ee8447df",
"/css/app.css": "/css/app.css?id=fff34714aa687ec711d3",
"/js/app.js": "/js/app.js?id=aa9e426dc7b92d42d3b2"
}

View file

@ -258,7 +258,7 @@ body {
background: #f9fafd;
max-width: 1000px;
width: 100%;
margin: 0 40px;
margin: 10px 40px;
header, footer {
display: flex;
justify-content: space-between;

View file

@ -62,7 +62,12 @@
@if(!Request::is(['items', 'items/*']))
<a id="items" class="config" href="{{ route('items.index') }}"><i class="fas fa-list"></i></a>
@endif
<a id="config-button" class="config" href=""><i class="fas fa-cogs"></i></a>
@if(!Request::is(['settings', 'settings/*']))
<a id="settings" class="config" href="{{ route('settings.index') }}"><i class="fas fa-cogs"></i></a>
@endif
@if(Route::is('dash'))
<a id="config-button" class="config" href=""><i class="fas fa-exchange"></i></a>
@endif
</div>
</main>

View file

@ -0,0 +1,67 @@
@extends('app')
@section('content')
@foreach ($groups as $group)
<section class="module-container">
<header>
<div class="section-title">
{{ $group->title }}
</div>
</header>
<table class="table table-hover">
<thead>
<tr>
<th>Label</th>
<th style="width: 60%;">Value</th>
<th class="text-center" style="width: 75px;">Edit</th>
</tr>
</thead>
<tbody>
@if (count($group->settings) > 0)
@foreach ($group->settings as $setting)
<tr>
<td>{{ $setting->label }}</td>
<td>
@php($type = explode('|', $setting->type)[0])
@if ($type == 'image')
@if(!empty($setting->value))
<a href="/uploads/settings/{{ $setting->value }}" title="View" target="_blank">View</a>
@else
- not set -
@endif
@elseif ($type == 'select')
@if ($setting->value == 1)
YES
@else
NO
@endif
@else
{!! $setting->value !!}
@endif
</td>
<td class="text-center">
@if((bool)$setting->system !== true)
<a href="{!! route('settings.edit', ['id' => $setting->id]) !!}" title="Edit {!! $setting->label !!}" class="secondary"><i class="fa fa-pencil"></i></a>
@endif
</td>
</tr>
@endforeach
@else
<tr>
<td colspan="3" class="form-error text-center">
<strong>No items found</strong>
</td>
</tr>
@endif
</tbody>
</table>
</section>
@endforeach
@endsection

View file

@ -20,4 +20,22 @@ Route::get('items/pin/{id}', 'ItemController@pin')->name('items.pin');
Route::get('items/restore/{id}', 'ItemController@restore')->name('items.restore');
Route::get('items/unpin/{id}', 'ItemController@unpin')->name('items.unpin');
Route::get('items/pintoggle/{id}/{ajax?}', 'ItemController@pinToggle')->name('items.pintoggle');
Route::post('order', 'ItemController@setOrder')->name('items.order');
Route::post('order', 'ItemController@setOrder')->name('items.order');
/**
* Settings.
*/
Route::group([
'as' => 'settings.',
'prefix' => 'settings',
], function () {
Route::get('/', 'SettingsController@index')
->name('index');
Route::get('edit/{id}', 'SettingsController@edit')
->name('edit');
Route::post('edit/{id}', 'SettingsController@update');
});

View file

@ -14,6 +14,7 @@ return array(
'App\\Http\\Controllers\\Auth\\ResetPasswordController' => $baseDir . '/app/Http/Controllers/Auth/ResetPasswordController.php',
'App\\Http\\Controllers\\Controller' => $baseDir . '/app/Http/Controllers/Controller.php',
'App\\Http\\Controllers\\ItemController' => $baseDir . '/app/Http/Controllers/ItemController.php',
'App\\Http\\Controllers\\SettingsController' => $baseDir . '/app/Http/Controllers/SettingsController.php',
'App\\Http\\Kernel' => $baseDir . '/app/Http/Kernel.php',
'App\\Http\\Middleware\\EncryptCookies' => $baseDir . '/app/Http/Middleware/EncryptCookies.php',
'App\\Http\\Middleware\\RedirectIfAuthenticated' => $baseDir . '/app/Http/Middleware/RedirectIfAuthenticated.php',
@ -26,7 +27,8 @@ return array(
'App\\Providers\\BroadcastServiceProvider' => $baseDir . '/app/Providers/BroadcastServiceProvider.php',
'App\\Providers\\EventServiceProvider' => $baseDir . '/app/Providers/EventServiceProvider.php',
'App\\Providers\\RouteServiceProvider' => $baseDir . '/app/Providers/RouteServiceProvider.php',
'App\\Providers\\SupportedServiceProvider' => $baseDir . '/app/Providers/SupportedServiceProvider.php',
'App\\Setting' => $baseDir . '/app/Setting.php',
'App\\SettingGroup' => $baseDir . '/app/SettingGroup.php',
'App\\SupportedApps\\Contracts\\Applications' => $baseDir . '/app/SupportedApps/Contracts/Applications.php',
'App\\SupportedApps\\Nzbget' => $baseDir . '/app/SupportedApps/Nzbget.php',
'App\\SupportedApps\\Plex' => $baseDir . '/app/SupportedApps/Plex.php',
@ -2666,6 +2668,7 @@ return array(
'SebastianBergmann\\ResourceOperations\\ResourceOperations' => $vendorDir . '/sebastian/resource-operations/src/ResourceOperations.php',
'SebastianBergmann\\Version' => $vendorDir . '/sebastian/version/src/Version.php',
'SessionUpdateTimestampHandlerInterface' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/SessionUpdateTimestampHandlerInterface.php',
'SettingsSeeder' => $baseDir . '/database/seeds/SettingsSeeder.php',
'Symfony\\Component\\Console\\Application' => $vendorDir . '/symfony/console/Application.php',
'Symfony\\Component\\Console\\CommandLoader\\CommandLoaderInterface' => $vendorDir . '/symfony/console/CommandLoader/CommandLoaderInterface.php',
'Symfony\\Component\\Console\\CommandLoader\\ContainerCommandLoader' => $vendorDir . '/symfony/console/CommandLoader/ContainerCommandLoader.php',

View file

@ -325,6 +325,7 @@ class ComposerStaticInit4b6fb9210a1ea37c2db27b8ff53a1ecf
'App\\Http\\Controllers\\Auth\\ResetPasswordController' => __DIR__ . '/../..' . '/app/Http/Controllers/Auth/ResetPasswordController.php',
'App\\Http\\Controllers\\Controller' => __DIR__ . '/../..' . '/app/Http/Controllers/Controller.php',
'App\\Http\\Controllers\\ItemController' => __DIR__ . '/../..' . '/app/Http/Controllers/ItemController.php',
'App\\Http\\Controllers\\SettingsController' => __DIR__ . '/../..' . '/app/Http/Controllers/SettingsController.php',
'App\\Http\\Kernel' => __DIR__ . '/../..' . '/app/Http/Kernel.php',
'App\\Http\\Middleware\\EncryptCookies' => __DIR__ . '/../..' . '/app/Http/Middleware/EncryptCookies.php',
'App\\Http\\Middleware\\RedirectIfAuthenticated' => __DIR__ . '/../..' . '/app/Http/Middleware/RedirectIfAuthenticated.php',
@ -337,7 +338,8 @@ class ComposerStaticInit4b6fb9210a1ea37c2db27b8ff53a1ecf
'App\\Providers\\BroadcastServiceProvider' => __DIR__ . '/../..' . '/app/Providers/BroadcastServiceProvider.php',
'App\\Providers\\EventServiceProvider' => __DIR__ . '/../..' . '/app/Providers/EventServiceProvider.php',
'App\\Providers\\RouteServiceProvider' => __DIR__ . '/../..' . '/app/Providers/RouteServiceProvider.php',
'App\\Providers\\SupportedServiceProvider' => __DIR__ . '/../..' . '/app/Providers/SupportedServiceProvider.php',
'App\\Setting' => __DIR__ . '/../..' . '/app/Setting.php',
'App\\SettingGroup' => __DIR__ . '/../..' . '/app/SettingGroup.php',
'App\\SupportedApps\\Contracts\\Applications' => __DIR__ . '/../..' . '/app/SupportedApps/Contracts/Applications.php',
'App\\SupportedApps\\Nzbget' => __DIR__ . '/../..' . '/app/SupportedApps/Nzbget.php',
'App\\SupportedApps\\Plex' => __DIR__ . '/../..' . '/app/SupportedApps/Plex.php',
@ -2977,6 +2979,7 @@ class ComposerStaticInit4b6fb9210a1ea37c2db27b8ff53a1ecf
'SebastianBergmann\\ResourceOperations\\ResourceOperations' => __DIR__ . '/..' . '/sebastian/resource-operations/src/ResourceOperations.php',
'SebastianBergmann\\Version' => __DIR__ . '/..' . '/sebastian/version/src/Version.php',
'SessionUpdateTimestampHandlerInterface' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/SessionUpdateTimestampHandlerInterface.php',
'SettingsSeeder' => __DIR__ . '/../..' . '/database/seeds/SettingsSeeder.php',
'Symfony\\Component\\Console\\Application' => __DIR__ . '/..' . '/symfony/console/Application.php',
'Symfony\\Component\\Console\\CommandLoader\\CommandLoaderInterface' => __DIR__ . '/..' . '/symfony/console/CommandLoader/CommandLoaderInterface.php',
'Symfony\\Component\\Console\\CommandLoader\\ContainerCommandLoader' => __DIR__ . '/..' . '/symfony/console/CommandLoader/ContainerCommandLoader.php',