Files
laravel-manager-packagist/src/Modules/Leads/LeadsModule.php
T
2026-12-04 17:15:56 +00:00

93 lines
3.7 KiB
PHP

<?php
namespace IriksIT\Manager\Modules\Leads;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use IriksIT\Manager\Console\LeadsPruneCommand;
use IriksIT\Manager\Modules\BaseModule;
use IriksIT\Manager\Support\Names;
use IriksIT\Manager\Support\Permission;
/**
* The requests of visitors (callback requests, questions, quotes, a contact form): stored, screened into folders, handled and kept for
* a while. The forms are the site's own and call `Support\Leads\Intake`. With a feature `leads` in `manager.features`, one switch
* turns the module on and off.
*/
class LeadsModule extends BaseModule
{
public function key(): string
{
return 'leads';
}
public function label(): string
{
return 'Requests';
}
public function tables(): array
{
return ['leads', 'lead_blocks', 'lead_taps'];
}
public function permissions(): array
{
return [
new Permission('leads.view', 'See the requests that came in', 'Requests'),
new Permission('leads.handle', 'Handle requests: mark as handled, add notes', 'Requests', needs: ['leads.view']),
new Permission('leads.manage', 'See what the screening kept away, move requests and block senders', 'Requests', needs: ['leads.handle']),
new Permission('leads.delete', 'Delete requests', 'Requests', needs: ['leads.view']),
];
}
public function navigation(): array
{
return [['label' => 'Requests', 'section' => 'Visitors', 'route' => Names::route('leads.index'), 'permission' => 'leads.view', 'simple' => true, 'hint' => 'The messages and requests from your website']];
}
/** @return array<string, array<string, mixed>> what the settings screen can change */
public function settings(): array
{
return [
'manager.leads.notify' => ['label' => 'Mail new requests to', 'type' => 'text', 'group' => 'Requests', 'sensitive' => true, 'help' => 'The mail address that is told about every real request', 'env' => 'MANAGER_LEADS_NOTIFY'],
'manager.leads.confirm' => ['label' => 'Mail the visitor a confirmation', 'type' => 'toggle', 'group' => 'Requests'],
'manager.leads.keep_days' => ['label' => 'Days a request is kept', 'type' => 'number', 'group' => 'Requests'],
'manager.leads.keep_spam_days' => ['label' => 'Days spam is kept', 'type' => 'number', 'group' => 'Requests'],
];
}
public function boot(): void
{
// A form may be sent so many times an hour per address, and by everyone together. Over the limit the visitor is told to wait.
RateLimiter::for('manager-leads', fn (Request $request) => [
Limit::perHour(max(1, (int) config('manager.leads.form.per_hour', 6)))->by('lead|'.$request->ip()),
Limit::perHour(max(1, (int) config('manager.leads.form.per_hour_all', 120)))->by('lead-all'),
]);
RateLimiter::for('manager-leads-token', fn (Request $request) => Limit::perMinute(60)->by((string) $request->ip()));
}
public function commands(): array
{
return [LeadsPruneCommand::class];
}
public function actions(): array
{
return [
'leads.handled' => ['Marked a request as handled or open'],
'leads.note' => ['Added a note to a request'],
'leads.folder' => ['Moved a request to another folder'],
'leads.block' => ['Blocked a sender'],
'leads.unblock' => ['Lifted the block of a sender'],
'leads.destroy' => ['Deleted a request'],
];
}
public function views(): array
{
return ['leads.index' => 'Opened the requests', 'leads.opened' => 'Opened a request'];
}
}