feat: Add Settings update route & Validation functionality

This commit is contained in:
IceToast 2023-02-10 00:04:00 +01:00 committed by IceToast
parent 20f0e1dfc2
commit c3420b1684
4 changed files with 125 additions and 68 deletions

View file

@ -7,6 +7,9 @@ use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use Qirolab\Theme\Theme; use Qirolab\Theme\Theme;
class SettingsController extends Controller class SettingsController extends Controller
@ -61,4 +64,32 @@ class SettingsController extends Controller
'active_theme' => Theme::active(), 'active_theme' => Theme::active(),
]); ]);
} }
/**
* Update the specified resource in storage.
*
*/
public function update(Request $request)
{
$category = request()->get('category');
error_log($category);
$className = 'App\\Settings\\' . $category . 'Settings';
if (method_exists($className, 'getValidations')) {
$validations = $className::getValidations();
} else {
$validations = [];
}
$validator = Validator::make($request->all(), $validations);
if ($validator->fails()) {
return Redirect::to('admin/settings' . '#' . $category)->withErrors($validator)->withInput();
}
return Redirect::to('admin/settings' . '#' . $category)->with('success', 'Settings updated successfully.');
}
} }

View file

@ -34,14 +34,16 @@ class GeneralSettings extends Settings
]; ];
} }
public static function validation() public static function getValidations()
{ {
// create validation rules that can be used in the controller // create validation rules that can be used in the controller
return [ return [
'main_site' => 'required|url', 'main_site' => 'required|string|max:1',
'credits_display_name' => 'required|string', 'credits_display_name' => 'required|string',
'initial_user_credits' => 'required|numeric', 'initial_user_credits' => 'required|numeric',
'initial_server_limit' => 'required|numeric', 'initial_server_limit' => 'required|numeric',
'recaptcha_enabled' => 'required|boolean',
]; ];
} }

View file

@ -113,7 +113,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
Route::get('ticket/show/{ticket_id}', [TicketsController::class, 'show'])->name('ticket.show'); Route::get('ticket/show/{ticket_id}', [TicketsController::class, 'show'])->name('ticket.show');
Route::post('ticket/reply', [TicketsController::class, 'reply'])->middleware(['throttle:ticket-reply'])->name('ticket.reply'); Route::post('ticket/reply', [TicketsController::class, 'reply'])->middleware(['throttle:ticket-reply'])->name('ticket.reply');
Route::post('ticket/close/{ticket_id}', [TicketsController::class, 'close'])->name('ticket.close'); Route::post('ticket/close/{ticket_id}', [TicketsController::class, 'close'])->name('ticket.close');
//admin //admin
Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () { Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () {
@ -165,13 +165,8 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
Route::get('settings/checkPteroClientkey', [System::class, 'checkPteroClientkey'])->name('settings.checkPteroClientkey'); Route::get('settings/checkPteroClientkey', [System::class, 'checkPteroClientkey'])->name('settings.checkPteroClientkey');
Route::redirect('settings#system', 'system')->name('settings.system'); Route::redirect('settings#system', 'system')->name('settings.system');
//settings Route::get('settings', [SettingsController::class, 'index'])->name('settings.index');
Route::patch('settings/update/invoice-settings', [Invoices::class, 'updateSettings'])->name('settings.update.invoicesettings'); Route::post('settings', [SettingsController::class, 'update'])->name('settings.update');
Route::patch('settings/update/language', [Language::class, 'updateSettings'])->name('settings.update.languagesettings');
Route::patch('settings/update/payment', [Payments::class, 'updateSettings'])->name('settings.update.paymentsettings');
Route::patch('settings/update/misc', [Misc::class, 'updateSettings'])->name('settings.update.miscsettings');
Route::patch('settings/update/system', [System::class, 'updateSettings'])->name('settings.update.systemsettings');
Route::resource('settings', SettingsController::class)->only('index');
//invoices //invoices
Route::get('invoices/download-invoices', [InvoiceController::class, 'downloadAllInvoices'])->name('invoices.downloadAllInvoices'); Route::get('invoices/download-invoices', [InvoiceController::class, 'downloadAllInvoices'])->name('invoices.downloadAllInvoices');
@ -221,8 +216,7 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () {
Route::get('ticket/category/datatable', [TicketCategoryController::class, 'datatable'])->name('ticket.category.datatable'); Route::get('ticket/category/datatable', [TicketCategoryController::class, 'datatable'])->name('ticket.category.datatable');
Route::resource("ticket/category", TicketCategoryController::class,['as' => 'ticket']); Route::resource("ticket/category", TicketCategoryController::class, ['as' => 'ticket']);
}); });
Route::get('/home', [HomeController::class, 'index'])->name('home'); Route::get('/home', [HomeController::class, 'index'])->name('home');

View file

@ -62,80 +62,110 @@
<!-- /.sidebar-menu --> <!-- /.sidebar-menu -->
<!-- Content in $settings --> <!-- Content in $settings -->
<div class="col-10 p-0"> <div class="col-10 p-0">
<div class="tab-content ml-3" style="width: 100%;"> <div class="tab-content ml-3" style="width: 100%;">
@foreach ($settings as $category => $options) @foreach ($settings as $category => $options)
<div container class="tab-pane fade container {{ $loop->first ? 'active show' : '' }}" <div container class="tab-pane fade container {{ $loop->first ? 'active show' : '' }}"
id="{{ $category }}" role="tabpanel"> id="{{ $category }}" role="tabpanel">
@foreach ($options as $key => $value)
<div class="row">
<div class="col-4 d-flex align-items-center">
<label for="{{ $key }}">{{ $value['label'] }}</label>
</div>
<div class="col-8"> <form action="{{ route('admin.settings.update') }}" method="POST">
<div class="custom-control mb-3 d-flex align-items-center"> @csrf
@if ($value['description']) @method('POST')
<i class="fas fa-info-circle mr-4" data-toggle="popover"
data-trigger="hover" data-placement="top" data-html="true"
data-content="{{ $value['description'] }}"></i>
@else
<i class="fas fa-info-circle mr-4 invisible"></i>
@endif
@switch($value) @foreach ($options as $key => $value)
@case($value['type'] == 'string') <input type="hidden" name="category" value="{{ $category }}">
<input type="text" class="form-control" <div class="row">
name="{{ $key }}" value="{{ $value['value'] }}"> <div class="col-4 d-flex align-items-center">
@break <label for="{{ $key }}">{{ $value['label'] }}</label>
</div>
@case($value['type'] == 'boolean') <div class="col-8">
<input type="checkbox" name="{{ $key }}" <div class="custom-control mb-3 d-flex align-items-center">
value="{{ $value['value'] }}"> @if ($value['description'])
@break <i class="fas fa-info-circle mr-4" data-toggle="popover"
data-trigger="hover" data-placement="top"
data-html="true"
data-content="{{ $value['description'] }}"></i>
@else
<i class="fas fa-info-circle mr-4 invisible"></i>
@endif
@case($value['type'] == 'number') <div class=" w-100">
<input type="number" class="form-control" @switch($value)
name="{{ $key }}" value="{{ $value['value'] }}"> @case($value['type'] == 'string')
@break <input type="text" class="form-control"
name="{{ $key }}"
value="{{ $value['value'] }}">
@break
@case($value['type'] == 'select') @case($value['type'] == 'boolean')
<select id="{{ $key }}" class="custom-select w-100" <input type="checkbox" name="{{ $key }}"
name="{{ $key }}"> value="{{ $value['value'] }}">
@foreach ($value['options'] as $option) @break
<option value="{{ $option }}"
{{ $value['value'] == $option ? 'selected' : '' }}>
{{ __($option) }}
</option>
@endforeach
</select>
@break
@case($value['type'] == 'multiselect') @case($value['type'] == 'number')
<select id="{{ $key }}" class="custom-select w-100" <input type="number" class="form-control"
name="{{ $key }}" multiple> name="{{ $key }}"
@foreach ($value['options'] as $option) value="{{ $value['value'] }}">
<option value="{{ $option }}" @break
{{ $value['value'] == $option ? 'selected' : '' }}>
{{ __($option) }}
</option>
@endforeach
</select>
@break
@case($value['type'] == 'textarea') @case($value['type'] == 'select')
<textarea class="form-control" name="{{ $key }}" rows="3">{{ $value['value'] }}</textarea> <select id="{{ $key }}"
@break class="custom-select w-100" name="{{ $key }}">
@foreach ($value['options'] as $option)
<option value="{{ $option }}"
{{ $value['value'] == $option ? 'selected' : '' }}>
{{ __($option) }}
</option>
@endforeach
</select>
@break
@default @case($value['type'] == 'multiselect')
@endswitch <select id="{{ $key }}"
class="custom-select w-100" name="{{ $key }}"
multiple>
@foreach ($value['options'] as $option)
<option value="{{ $option }}"
{{ $value['value'] == $option ? 'selected' : '' }}>
{{ __($option) }}
</option>
@endforeach
</select>
@break
@case($value['type'] == 'textarea')
<textarea class="form-control" name="{{ $key }}" rows="3">{{ $value['value'] }}</textarea>
@break
@default
@endswitch
@error($key)
<div class="text-danger ">
{{ $message }}
</div>
@enderror
</div>
</div>
</div> </div>
</div> </div>
@endforeach
<div class="row">
<div class="col-12">
<button type="submit" class="btn btn-primary float-right">Save</button>
</div>
<div class="col-12">
<button type="reset"
class="btn btn-secondary float-right mr-2">Reset</button>
</div>
</div> </div>
@endforeach </form>
</div> </div>
@endforeach @endforeach
</div> </div>
</div> </div>
</div> </div>