Files
zemailnator/app/Filament/Pages/Settings.php
2025-04-23 04:44:46 +05:30

204 lines
8.2 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Models\Setting;
use Filament\Actions\Action;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
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) {
$imapSettings = $setting->imap_settings ?? [];
$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,
'imap_settings' => $imapSettings,
]);
} else {
$this->form->fill([
'app_admin' => $auth_email,
'app_social' => [
null => null,
],
]);
}
}
public function form(Form $form): Form
{
return $form
->schema([
Section::make('Website Information')
->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),
])
]),
Section::make('Imap')
->description('Enter your imap server')
->collapsed()
->schema([
TextInput::make('imap_settings.hostname')->label('Hostname')->required(),
TextInput::make('imap_settings.port')->label('Port')->required(),
Select::make('imap_settings.encryption')->options([
'none' => 'None',
'ssl' => 'SSL',
'tls' => 'TLS'
]),
Checkbox::make('imap_settings.validate_cert')->label('Validate Encryption Certificate')->default(false),
TextInput::make('imap_settings.username')->label('Username')->required(),
TextInput::make('imap_settings.password')->label('Password')->required(),
TextInput::make('imap_settings.default_account')->label('Default Account')->default('default')->placeholder('default'),
TextInput::make('imap_settings.protocol')->label('Protocol')->default('imap')->placeholder('imap'),
Checkbox::make('imap_settings.cc_check')->label('Check CC Field')->default(false)->helperText('If enabled, we will check the CC field as well while fetching mails.'),
])
])
->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'],
'imap_settings' => $data['imap_settings'],
];
$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 '.$exception->getMessage())
->danger()
->send();
}
}
}