File: /home/posscale/www/printmanager/app/Filament/Resources/TenantResource/Pages/TenantAction.php
<?php
namespace App\Filament\Resources\TenantResource\Pages;
use App\Filament\Resources\TenantResource;
use App\Mail\RegistraionApproveEmail;
use App\Mail\RegistraionDeclineEmail;
use App\Models\Tenant;
use App\Models\User;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Filament\Resources\Pages\EditRecord;
use Filament\Pages\Actions;
use Filament\Support\Exceptions\Halt;
use Illuminate\Support\Facades\Mail;
use Stancl\Tenancy\Database\Models\Domain;
class TenantAction extends EditRecord
{
use InteractsWithRecord;
protected static string $resource = TenantResource::class;
// protected static string $view = 'filament.resources.tenants.pages.view';
protected static ?string $title = 'Custom Page Title';
protected static ?string $slug = 'view';
protected ?string $prev_status = '';
protected ?string $new_status = '';
public function mount($record): void
{
$this->record = $this->resolveRecord($record);
$this->authorizeAccess();
$this->fillForm();
$this->prev_status = $this->record->status;
$this->previousUrl = url()->previous();
}
protected function getActions(): array
{
$resource = static::getResource();
return [];
}
public function form(Form $form): Form
{
return $form
->schema([
Select::make('status')->options([
'active' => 'Approve',
'decliend' => 'Decline',
'pending' => 'Pending',
])->columnSpan(2),
RichEditor::make('message_to_user')->label('Message to user')
->columnSpan(2),
RichEditor::make('message')->label('Reason of action')
->columnSpan(2),
]);
}
public function save(bool $shouldRedirect = true, bool $shouldSendSavedNotification = true): void
{
$this->authorizeAccess();
try {
$this->callHook('beforeValidate');
$data = $this->form->getState();
$this->callHook('afterValidate');
$data = $this->mutateFormDataBeforeSave($data);
$this->callHook('beforeSave');
$this->record->status = $data['status'];
$this->record->save();
// $this->handleRecordUpdate($this->getRecord(), array('data' => $data));
$this->new_status = $this->record->status;
$this->callHook('afterSave');
// if ($this->new_status != $this->prev_status) {
if ($this->new_status == 'active') {
$domain = Domain::where('tenant_id', $this->record->id)->first();
tenancy()->initialize($this->record);
$user = User::find(1);
if ($user) {
Mail::to(env('ADMIN_EMAIL', $user->email))->send(new RegistraionApproveEmail([
'name' => $this->record->id,
'user_name' => $user->name,
'domain' => $domain->domain,
'message' => $this->record->message,
]));
}
tenancy()->end();
}
if ($this->new_status == 'decliend') {
$domain = Domain::where('tenant_id', $this->record->id)->first();
tenancy()->initialize($this->record);
$user = User::find(1);
if ($user) {
Mail::to(env('ADMIN_EMAIL', $user->email))->send(new RegistraionDeclineEmail([
'name' => $this->record->id,
'user_name' => $user->name,
'domain' => $domain->domain,
'message' => $this->record->message,
]));
}
tenancy()->end();
}
// }
} catch (Halt $exception) {
return;
}
$this->getSavedNotification()?->send();
if ($shouldRedirect && ($redirectUrl = $this->getRedirectUrl())) {
$this->redirect($redirectUrl);
}
}
}