Add settings page
This commit is contained in:
13
app/Facades/Options.php
Normal file
13
app/Facades/Options.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Facades;
|
||||
use \Illuminate\Support\Facades\Facade;
|
||||
|
||||
class Options extends Facade
|
||||
{
|
||||
public static function getFacadeAccessor()
|
||||
{
|
||||
return 'sm-options';
|
||||
}
|
||||
}
|
83
app/Helpers/Options.php
Normal file
83
app/Helpers/Options.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Options as Option;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class Options
|
||||
{
|
||||
|
||||
public function getOption(string $option): string
|
||||
{
|
||||
$value = Cache::get($option);
|
||||
|
||||
if (is_null($value))
|
||||
{
|
||||
$value = Option::where('option_name', $option)->first();
|
||||
if (is_null($value))
|
||||
throw new \Exception('This option does not exist.');
|
||||
|
||||
Cache::put($option, $value);
|
||||
Cache::put($option . '_desc', 'Undefined description');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function setOption(string $option, string $value, string $description)
|
||||
{
|
||||
Option::create([
|
||||
'option_name' => $option,
|
||||
'option_value' => $value,
|
||||
'friendly_name' => $description
|
||||
]);
|
||||
|
||||
Cache::put($option, $value, now()->addDay());
|
||||
Cache::put($option . '_desc', $description, now()->addDay());
|
||||
}
|
||||
|
||||
public function pullOption($option): array
|
||||
{
|
||||
$oldOption = Option::where('option_name', $option)->first();
|
||||
Option::find($oldOption->id)->delete();
|
||||
|
||||
// putMany is overkill here
|
||||
return [
|
||||
Cache::pull($option),
|
||||
Cache::pull($option . '_desc')
|
||||
];
|
||||
}
|
||||
|
||||
public function changeOption($option, $newValue)
|
||||
{
|
||||
$dbOption = Option::where('option_name', $option);
|
||||
|
||||
if ($dbOption->first())
|
||||
{
|
||||
$dbOptionInstance = Option::find($dbOption->id);
|
||||
Cache::forget($option);
|
||||
|
||||
|
||||
$dbOptionInstance->option_value = $newValue;
|
||||
$dbOptionInstance->save();
|
||||
|
||||
Cache::put('option_name', $newValue, now()->addDay());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception('This option does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function optionExists(string $option): bool
|
||||
{
|
||||
$dbOption = Option::where('option_name', $option)->first();
|
||||
$locallyCachedOption = Cache::get($option);
|
||||
|
||||
return !is_null($dbOption) || !is_null($locallyCachedOption);
|
||||
}
|
||||
|
||||
}
|
55
app/Http/Controllers/OptionsController.php
Normal file
55
app/Http/Controllers/OptionsController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\Options;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class OptionsController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\Response|\Illuminate\View\View
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$options = Options::all();
|
||||
|
||||
|
||||
return view('dashboard.administration.positions')
|
||||
->with('options');
|
||||
}
|
||||
|
||||
public function saveSettings(Request $request)
|
||||
{
|
||||
if (Auth::user()->hasPermission('admin.settings.edit'))
|
||||
{
|
||||
foreach($request->all() as $optionName => $option)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Options::optionExists($option))
|
||||
{
|
||||
Options::changeOption($optionName, $option);
|
||||
}
|
||||
}
|
||||
catch(\Exception $ex)
|
||||
{
|
||||
// Silently ignore, because the only way this would happen is if someone manipulates the page,
|
||||
// and obviously we can't save arbitrary option values even if the user has permission to do so.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$request->session()->flash('success', 'Settings saved successfully!');
|
||||
}
|
||||
else
|
||||
{
|
||||
$request->session()->flash('error', 'You do not have permission to update this resource.');
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
13
app/Options.php
Normal file
13
app/Options.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Options extends Model
|
||||
{
|
||||
public $fillable = [
|
||||
'option_name',
|
||||
'option_value'
|
||||
];
|
||||
}
|
31
app/Providers/OptionsProvider.php
Normal file
31
app/Providers/OptionsProvider.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use App;
|
||||
|
||||
class OptionsProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
App::bind('sm-options', function (){
|
||||
return new App\Helpers\Options();
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user