Added Website Setting Page
This commit is contained in:
179
app/Filament/Pages/Settings.php
Normal file
179
app/Filament/Pages/Settings.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Pages\Page;
|
||||
|
||||
class Settings extends Page implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
public ?array $data = [];
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';
|
||||
|
||||
protected static string $view = 'filament.pages.settings';
|
||||
|
||||
protected static ?string $navigationGroup = 'Web Master';
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
$auth_email = $user->email;
|
||||
$setting = Setting::where('app_admin', $auth_email)->first();
|
||||
|
||||
if ($setting) {
|
||||
$this->form->fill([
|
||||
'app_name' => $setting->app_name,
|
||||
'app_version' => $setting->app_version,
|
||||
'app_base_url' => $setting->app_base_url,
|
||||
'app_admin' => $setting->app_admin,
|
||||
'app_contact' => $setting->app_contact,
|
||||
'app_title' => $setting->app_title,
|
||||
'app_description' => $setting->app_description,
|
||||
'app_keyword' => $setting->app_keyword,
|
||||
'app_meta' => $setting->app_meta,
|
||||
'app_social' => $setting->app_social,
|
||||
'app_header' => $setting->app_header,
|
||||
'app_footer' => $setting->app_footer
|
||||
]);
|
||||
|
||||
|
||||
} else {
|
||||
$this->form->fill([
|
||||
'app_admin' => $auth_email,
|
||||
'app_social' => [
|
||||
null => null,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Section::make('Website Infomation')
|
||||
->description('Change Basic Website Information')
|
||||
->collapsible()
|
||||
->schema([
|
||||
TextInput::make('app_name')->label('Website Name')->required(),
|
||||
TextInput::make('app_version')->label('Website Version')->required(),
|
||||
TextInput::make('app_base_url')->label('Base URL')->required(),
|
||||
TextInput::make('app_admin')->label('Website Admin')->email()->required(),
|
||||
TextInput::make('app_contact')->label('Contact Email')->email()->required(),
|
||||
]),
|
||||
Section::make('Custom Codes')
|
||||
->description('Add custom codes to header and footer')
|
||||
->collapsed()
|
||||
->columns(2)
|
||||
->schema([
|
||||
Textarea::make('app_header')->label('Custom Header')->rows(6)->columnSpan(1),
|
||||
Textarea::make('app_footer')->label('Custom Footer')->rows(6)->columnSpan(1),
|
||||
]),
|
||||
Section::make('Meta Information')
|
||||
->description('Change Website Meta Information')
|
||||
->collapsed()
|
||||
->schema([
|
||||
TextInput::make('app_title')->label('Title')->required(),
|
||||
Textarea::make('app_description')->label('Description')->rows(3),
|
||||
TextInput::make('app_keyword')->label('Keyword'),
|
||||
KeyValue::make('app_meta')
|
||||
->label('Meta (Optional)')
|
||||
->keyPlaceholder('Name')
|
||||
->valuePlaceholder('Content'),
|
||||
]),
|
||||
Section::make('Social Links')
|
||||
->description('Enter your social links')
|
||||
->collapsed()
|
||||
->schema([
|
||||
|
||||
|
||||
Repeater::make('app_social')
|
||||
->statePath('app_social')
|
||||
->schema([
|
||||
TextInput::make('icon')->label('Icon')->required()->maxLength(100),
|
||||
TextInput::make('url')->label('URL')->url()->required()->maxLength(255),
|
||||
])
|
||||
|
||||
]),
|
||||
])
|
||||
->statePath('data');
|
||||
}
|
||||
|
||||
protected function getFormActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('save')
|
||||
->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
|
||||
->submit('save'),
|
||||
];
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
try {
|
||||
|
||||
$data = $this->form->getState();
|
||||
$setting = Setting::where('id', 1)->first();
|
||||
|
||||
$user = auth()->user();
|
||||
$auth_email = $user->email;
|
||||
|
||||
if ($setting) {
|
||||
$app_admin = $setting->app_admin;
|
||||
if ($app_admin == $auth_email) {
|
||||
$update_res = $setting->update($data);
|
||||
if($update_res) {
|
||||
Notification::make()
|
||||
->title('Saved successfully')
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
} else {
|
||||
Notification::make()
|
||||
->title('Please Login With Administrator Credentials')
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
} else {
|
||||
$data = [
|
||||
'id' => 1,
|
||||
'app_name' => $data['app_name'],
|
||||
'app_version' => $data['app_version'],
|
||||
'app_base_url' => $data['app_base_url'],
|
||||
'app_admin' => $auth_email,
|
||||
'app_contact' => $data['app_contact'],
|
||||
'app_title' => $data['app_title'],
|
||||
'app_description' => $data['app_description'],
|
||||
'app_keyword' => $data['app_keyword'],
|
||||
'app_social' => $data['app_social'],
|
||||
];
|
||||
$create_res = Setting::create(array_merge($data));
|
||||
|
||||
if($create_res) {
|
||||
Notification::make()
|
||||
->title('Saved successfully')
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
Notification::make()
|
||||
->title('Something went wrong')
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
33
app/Models/Setting.php
Normal file
33
app/Models/Setting.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'app_name',
|
||||
'app_version',
|
||||
'app_base_url',
|
||||
'app_title',
|
||||
'app_description',
|
||||
'app_keyword',
|
||||
'app_admin',
|
||||
'app_contact',
|
||||
'app_meta',
|
||||
'app_social',
|
||||
'app_header',
|
||||
'app_footer',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'app_meta' => 'json',
|
||||
'app_social' => 'json'
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user