Skip to content

Commit

Permalink
Admin Settings page initial #48 #49
Browse files Browse the repository at this point in the history
- Settings resources
- Settings Helper class
  • Loading branch information
Zlimon committed Sep 11, 2021
1 parent 614c81e commit b7f4a17
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 3 deletions.
23 changes: 23 additions & 0 deletions app/Helpers/SettingHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Helpers;

use App\Setting;

class SettingHelper
{
public static function getSetting($key, $default = null)
{
if (is_null($key)) {
return new Setting;
}

if (is_array($key)) {
return Setting::set($key[0], $key[1]);
}

$value = Setting::get($key);

return is_null($value) ? value($default) : $value;
}
}
42 changes: 42 additions & 0 deletions app/Http/Controllers/Admin/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Setting;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.setting.index');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$rules = Setting::getValidationRules();
$data = $this->validate($request, $rules);

$validSettings = array_keys($rules);

foreach ($data as $key => $val) {
if (in_array($key, $validSettings)) {
Setting::set($key, $val, Setting::getDataType($key));
}
}

return view('admin.setting.index');
}
}
203 changes: 203 additions & 0 deletions app/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Collection;

class Setting extends Model
{
use HasFactory;

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* Get a settings value
*
* @param $key
* @param null $default
* @return bool|int|mixed
*/
public static function get($key, $default = null)
{
if (self::has($key)) {
$setting = self::getAllSettings()->where('key', $key)->first();
return self::castValue($setting->value, $setting->type);
}

return self::getDefaultValue($key, $default);
}

/**
* Add a settings value
*
* @param $key
* @param $value
* @param string $type
* @return bool
*/
public static function add($key, $value, $type = 'string')
{
if (self::has($key)) {
return self::set($key, $value, $type);
}

return self::create(['key' => $key, 'value' => $value, 'type' => $type]) ? $value : false;
}

/**
* Set a value for setting
*
* @param $key
* @param $value
* @param string $type
* @return bool
*/
public static function set($key, $value, $type = 'string')
{
if ($setting = self::getAllSettings()->where('key', $key)->first()) {
return $setting->update(
[
'key' => $key,
'value' => $value,
'type' => $type
]
) ? $value : false;
}

return self::add($key, $value, $type);
}

/**
* Remove a setting
*
* @param $key
* @return bool
*/
public static function remove($key)
{
if (self::has($key)) {
return self::whereName($key)->delete();
}

return false;
}

/**
* Check if setting exists
*
* @param $key
* @return bool
*/
public static function has($key)
{
return (boolean)self::getAllSettings()->whereStrict('key', $key)->count();
}

/**
* Get the validation rules for setting fields
*
* @return array
*/
public static function getValidationRules()
{
return self::getDefinedSettingFields()->pluck('rules', 'key')
->reject(
function ($value) {
return is_null($value);
}
)->toArray();
}

/**
* Get the data type of a setting
*
* @param $field
* @return mixed
*/
public static function getDataType($field)
{
$type = self::getDefinedSettingFields()
->pluck('data', 'key')
->get($field);

return is_null($type) ? 'string' : $type;
}

/**
* Get default value for a setting
*
* @param $field
* @return mixed
*/
public static function getDefaultValueForField($field)
{
return self::getDefinedSettingFields()
->pluck('value', 'key')
->get($field);
}

/**
* Get default value from config if no value passed
*
* @param $key
* @param $default
* @return mixed
*/
private static function getDefaultValue($key, $default)
{
return is_null($default) ? self::getDefaultValueForField($key) : $default;
}

/**
* Get all the settings fields from config
*
* @return Collection
*/
private static function getDefinedSettingFields()
{
return collect(config('settings'))->pluck('elements')->flatten(1);
}

/**
* caste value into respective type
*
* @param $value
* @param $castTo
* @return bool|int
*/
private static function castValue($value, $castTo)
{
switch ($castTo) {
case 'int':
case 'integer':
return intval($value);
break;

case 'bool':
case 'boolean':
return boolval($value);
break;

default:
return $value;
}
}

/**
* Get all the settings
*
* @return mixed
*/
public static function getAllSettings()
{
return self::all();
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'Helper' => App\Helpers\Helper::class,
'Setting' => App\Helpers\SettingHelper::class,
'Pusher' => Pusher\Pusher::class,
],

Expand Down
Loading

0 comments on commit b7f4a17

Please sign in to comment.