66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
/**
|
|
* A request of a visitor. The folder is decided by `Support\Leads\Screening`: the inbox (a real request), "other" (acquisition and
|
|
* what the visitor called something else) or spam.
|
|
*/
|
|
class Lead extends Model
|
|
{
|
|
public const INBOX = 'inbox';
|
|
|
|
public const OTHER = 'other';
|
|
|
|
public const SPAM = 'spam';
|
|
|
|
public const FOLDERS = [self::INBOX, self::OTHER, self::SPAM];
|
|
|
|
public const WON = 'won';
|
|
|
|
public const LOST = 'lost';
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'reasons' => 'array', 'answers' => 'array', 'notes' => 'array', 'notify_failed' => 'boolean',
|
|
'subject_id' => 'integer', 'opened_at' => 'datetime', 'handled_at' => 'datetime', 'notified_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/** The real requests that still wait for the owner. */
|
|
public function scopeWaiting(Builder $query): void
|
|
{
|
|
$query->where('folder', self::INBOX)->whereNull('handled_at');
|
|
}
|
|
|
|
/** The real requests that were handled. */
|
|
public function scopeHandled(Builder $query): void
|
|
{
|
|
$query->where('folder', self::INBOX)->whereNotNull('handled_at');
|
|
}
|
|
|
|
/** What the owner reads for the kind: a name from `manager.leads.kinds`, else the kind itself. */
|
|
public function kindLabel(): string
|
|
{
|
|
return (string) __((string) (config('manager.leads.kinds')[$this->kind] ?? ucfirst($this->kind)));
|
|
}
|
|
|
|
/** What it is about, in a few words: the subject the site gave, or the kind of request. */
|
|
public function aboutLine(): string
|
|
{
|
|
return $this->subject !== null && $this->subject !== '' ? $this->subject : $this->kindLabel();
|
|
}
|
|
|
|
/** The text as it is compared with earlier ones: lowercase, without the spaces and marks around the words. */
|
|
public static function hashOf(?string $message): ?string
|
|
{
|
|
$text = trim(preg_replace('/[^\p{L}\p{N}]+/u', ' ', mb_strtolower((string) $message)) ?? '');
|
|
|
|
// A few words ("call me") are the same for many honest people.
|
|
return mb_strlen($text) < 40 ? null : hash('sha256', $text);
|
|
}
|
|
}
|