added support ticket and added more stat widgets

This commit is contained in:
Gitea
2025-05-17 05:11:29 +05:30
parent f30d7fa096
commit 175a7203f3
19 changed files with 975 additions and 6 deletions

View File

@@ -0,0 +1,220 @@
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource\RelationManagers;
use App\Models\Ticket;
use App\Models\TicketResponse;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Columns\BadgeColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\HtmlString;
class TicketResource extends Resource
{
protected static ?string $model = Ticket::class;
protected static ?string $navigationIcon = 'heroicon-o-ticket';
protected static ?string $navigationGroup = 'Support';
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')
->relationship('user', 'name')
->searchable()
->preload()
->required(),
Select::make('status')
->label('Status')
->options([
'open' => 'Open',
'pending' => 'In Progress',
'closed' => 'Closed',
])
->default('open')
->required(),
TextInput::make('subject')
->label('Subject')
->required()
->maxLength(255)
->columnSpanFull(),
TextArea::make('message')
->label('Message')
->required()
->rows(7)
->maxLength(2000)
->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->defaultSort('created_at', 'desc')
->columns([
TextColumn::make('ticket_id')
->label('Ticket ID')
->sortable(),
TextColumn::make('user.name'),
TextColumn::make('subject')
->limit(50)
->label('Subject'),
BadgeColumn::make('status')
->colors([
'success' => fn ($state) => $state === 'open',
'warning' => fn ($state) => $state === 'pending',
'danger' => fn ($state) => $state === 'closed',
])
->sortable(),
TextColumn::make('created_at')
->label('Created At')
->dateTime('F d, Y • h:i A')->sortable(),
TextColumn::make('last_response_at')
->label('Last Response')
->sortable()
->formatStateUsing(fn ($state) => $state?->diffForHumans()),
])
->filters([
SelectFilter::make('status')
->label('Status')
->options([
'open' => 'Open',
'pending' => 'In Progress',
'closed' => 'Closed',
])
->attribute('status'),
Filter::make('created_at')
->form([
DatePicker::make('created_from')->label('Created From'),
DatePicker::make('created_until')->label('Created Until'),
])
->query(function ($query, array $data) {
return $query
->when($data['created_from'], fn ($query, $date) => $query->whereDate('created_at', '>=', $date))
->when($data['created_until'], fn ($query, $date) => $query->whereDate('created_at', '<=', $date));
}),
])
// ->actions([
// Tables\Actions\EditAction::make(),
// ])
->actions([
Action::make('view')
->label('View & Respond')
->icon('heroicon-o-eye')
->form(function (Ticket $ticket): array {
return [
TextArea::make('response')
->label('Your Response')
->required()
->rows(7)
->placeholder('Type your response here...'),
Select::make('status')
->label('Ticket Status')
->options([
'open' => 'Open',
'pending' => 'In Progress',
'closed' => 'Closed',
])
->default($ticket->status === 'open' ? 'pending' : $ticket->status)
->required(),
];
})
->modalContent(function (Ticket $ticket) {
$html = '<div class="space-y-3 mb-3 text-sm">';
// Ticket Subject & Message
$html .= '<div class="bg-gray-100 dark:bg-gray-900 border p-2 rounded-md">';
$html .= '<h6 class="xs font-semibold text-gray-800 dark:text-gray-100">Subject: ' . e($ticket->subject) . '</h6>';
$html .= '<p class="mt-1 text-gray-700 dark:text-gray-300">Message: ' . nl2br(e($ticket->message)) . '</p>';
$html .= '</div>';
// Responses Section
$html .= '<h6 class="text-xs font-bold text-gray-700 dark:text-gray-200 mt-2">Previous Responses</h6>';
foreach ($ticket->responses as $response) {
$html .= '<div class="rounded-md border p-2 bg-gray-50 dark:bg-gray-800">';
$html .= '<div class="text-xs text-gray-600 dark:text-gray-300">';
$html .= '<strong>' . e($response->user->name) . '</strong>';
$html .= '<span class="ml-2 text-[11px] text-gray-500">' . e($response->created_at->diffForHumans()) . '</span>';
$html .= '</div>';
$html .= '<p class="mt-1 text-gray-800 dark:text-gray-100">' . nl2br(e($response->response)) . '</p>';
$html .= '</div>';
}
if ($ticket->responses->isEmpty()) {
$html .= '<p class="text-xs text-gray-500">No responses yet.</p>';
}
$html .= '</div>';
return new \Illuminate\Support\HtmlString($html);
})
->action(function (array $data, Ticket $ticket) {
TicketResponse::create([
'ticket_id' => $ticket->id,
'user_id' => auth()->id(),
'response' => $data['response'],
]);
// Update ticket status and last response time
$ticket->update([
'status' => $data['status'],
'last_response_at' => now(),
]);
// Send success notification
Notification::make()
->title('Response sent successfully!')
->success()
->send();
})
->modalHeading('View & Respond to Ticket')
->modalSubmitActionLabel('Send Reply'),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
RelationManagers\ResponsesRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListTickets::route('/'),
'create' => Pages\CreateTicket::route('/create'),
'edit' => Pages\EditTicket::route('/{record}/edit'),
];
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateTicket extends CreateRecord
{
protected static string $resource = TicketResource::class;
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditTicket extends EditRecord
{
protected static string $resource = TicketResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListTickets extends ListRecords
{
protected static string $resource = TicketResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Filament\Resources\TicketResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class ResponsesRelationManager extends RelationManager
{
protected static string $relationship = 'responses';
public function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')
->relationship('user', 'name')
->searchable()
->preload()
->default(1)
->required()
->columnSpanFull(),
Textarea::make('response')
->required()
->rows(7)
->maxLength(255)
->columnSpanFull(),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('response')
->columns([
TextColumn::make('user.name'),
TextColumn::make('ip_address'),
TextColumn::make('response'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}

View File

@@ -4,6 +4,8 @@ namespace App\Filament\Widgets;
use App\Models\Log;
use App\Models\Meta;
use App\Models\PremiumEmail;
use App\Models\Ticket;
use App\Models\User;
use DB;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
@@ -15,10 +17,14 @@ class StatsOverview extends BaseWidget
{
return [
Stat::make('Total Users', $this->getUser()),
Stat::make('Customers', $this->getCustomerCount()),
Stat::make('Paid Users', $this->getUserPaid()),
Stat::make('Logs Count', $this->getLogsCount()),
Stat::make('Total Mailbox', $this->getTotalMailbox()),
Stat::make('Emails Received', $this->getTotalEmailsReceived()),
Stat::make('Emails Stored', $this->getStoreEmailsCount()),
Stat::make('Open Tickets', $this->getOpenTicketsCount()),
Stat::make('Closed Tickets', $this->getClosedTicketsCount()),
];
}
private function getUser(): int
@@ -44,4 +50,22 @@ class StatsOverview extends BaseWidget
{
return Meta::select('value')->where(['key' => 'messages_received'])->first()->value;
}
private function getCustomerCount(): int
{
return User::whereNotNull('stripe_id')->count();
}
private function getStoreEmailsCount(): int
{
return PremiumEmail::all()->count();
}
private function getOpenTicketsCount(): int
{
return Ticket::whereIn('status', ['open', 'pending'])->count();
}
private function getClosedTicketsCount(): int
{
return Ticket::whereIn('status', ['closed'])->count();
}
}

View File

@@ -0,0 +1,149 @@
<?php
namespace App\Livewire\Dashboard;
use App\Models\Ticket;
use App\Models\TicketResponse;
use Livewire\Component;
use Request;
use Str;
class Support extends Component
{
public $tickets = [];
public $subject;
public $message;
public $response;
public $list = false;
public $open = 0;
public $closed = 0;
public function store()
{
$this->validate([
'subject' => 'required|string|max:255',
'message' => 'required|string',
]);
try {
$ticket = Ticket::create([
'user_id' => auth()->id(),
'ticket_id' => strtoupper(Str::random(6)),
'subject' => $this->subject,
'message' => $this->message,
'ip_address' => $this->getClientIp(),
'last_response_at' => now(),
]);
$this->dispatch('showAlert', ['type' => 'success', 'message' => 'Your ticket has been created successfully!']);
$this->dispatch('closeModal');
$this->reset(['subject','message']);
$this->tickets = Ticket::with('responses')
->where('user_id', auth()->id())
->get();
} catch (\Exception $exception) {
$this->dispatch('showAlert', ['type' => 'error', 'message' => 'Something went wrong!']);
}
}
public function reply($ticket_id)
{
$this->validate([
'response' => 'required|string',
]);
try {
if (!is_numeric($ticket_id)) {
$this->dispatch('showAlert', ['type' => 'error', 'message' => 'Invalid ticket ID.']);
return;
}
$ticket = Ticket::find($ticket_id);
if (!$ticket) {
$this->dispatch('showAlert', ['type' => 'error', 'message' => 'Ticket not found.']);
return;
}
TicketResponse::create([
'ticket_id' => $ticket_id,
'user_id' => auth()->id(),
'response' => $this->response,
'ip_address' => $this->getClientIp(),
]);
$ticket->last_response_at = now();
$ticket->save();
$this->dispatch('showAlert', ['type' => 'success', 'message' => 'Reply sent successfully!']);
$this->reset(['response']);
$this->tickets = Ticket::with('responses')
->where('user_id', auth()->id())
->get();
} catch (\Exception $exception) {
session()->flash('error', 'Something went wrong!');
}
}
public function close($ticket_id)
{
if (!is_numeric($ticket_id)) {
$this->dispatch('showAlert', ['type' => 'error', 'message' => 'Invalid ticket ID.']);
return;
}
$ticket = Ticket::find($ticket_id);
if (!$ticket) {
$this->dispatch('showAlert', ['type' => 'error', 'message' => 'Ticket not found.']);
return;
}
$ticket->status = 'closed';
$ticket->save();
$this->tickets = Ticket::with('responses')
->where('user_id', auth()->id())
->get();
$this->updateTicketCounts();
$this->dispatch('showAlert', ['type' => 'error', 'message' => 'This ticket has been closed!']);
}
public function mount()
{
$this->tickets = Ticket::with('responses')
->where('user_id', auth()->id())
->get();
$this->updateTicketCounts();
}
public function updateTicketCounts()
{
$this->open = $this->tickets->filter(function ($ticket) {
return in_array($ticket->status, ['open', 'pending']);
})->count();
$this->closed = $this->tickets->filter(function ($ticket) {
return $ticket->status === 'closed';
})->count();
}
protected function getClientIp()
{
// Cloudflare or other proxies may send the original client IP in X-Forwarded-For header
$ip = Request::header('X-Forwarded-For');
// X-Forwarded-For can contain a comma-separated list of IPs, so we need to get the first one
if ($ip) {
return explode(',', $ip)[0]; // Get the first IP in the list
}
// Fallback to the real IP if no X-Forwarded-For header
return Request::ip();
}
public function render()
{
return view('livewire.dashboard.support')->layout('components.layouts.dashboard');
}
}

57
app/Models/Ticket.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Ticket extends Model
{
use HasFactory;
protected $fillable = [
'user_id', 'ticket_id', 'subject', 'message', 'status', 'last_response_at', 'ip_address'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function responses()
{
return $this->hasMany(TicketResponse::class);
}
protected $casts = [
'created_at' => 'datetime', // Ensures created_at is a Carbon instance
'updated_at' => 'datetime', // Ensures updated_at is a Carbon instance
'last_response_at' => 'datetime', // Cast last_response_at to Carbon instance
];
public static function autoClose(): bool
{
try {
$tickets = Ticket::where('status', 'pending')
->where('last_response_at', '<', now()->subDays(3))
->get();
if (count($tickets) > 0) {
foreach ($tickets as $ticket) {
$ticket->status = 'closed';
$ticket->save();
TicketResponse::create([
'ticket_id' => $ticket->id,
'user_id' => 1,
'response' => 'This ticket has been auto-closed due to inactivity.',
]);
}
}
return true;
} catch (\Exception $e) {
return false;
}
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TicketResponse extends Model
{
use HasFactory;
protected $fillable = [
'ticket_id', 'user_id', 'response', 'ip_address'
];
public function ticket()
{
return $this->belongsTo(Ticket::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}

View File

@@ -6,6 +6,7 @@ namespace App\Models;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
@@ -63,4 +64,9 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
{
return str_ends_with($this->email, '@zemail.me') && $this->level === 9 && $this->hasVerifiedEmail();
}
public function tickets(): HasMany
{
return $this->hasMany(Ticket::class);
}
}