KodeStar
7 years ago
19 changed files with 446 additions and 13 deletions
@ -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.', |
|||
]); |
|||
} |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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'); |
|||
} |
|||
} |
@ -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'); |
|||
} |
|||
} |
@ -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'); |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
|
|||
} |
|||
} |
@ -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" |
|||
} |
@ -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 |
Loading…
Reference in new issue