- composer require mews/purifier - Add user.name in AddStatusMessage + html tags bold - purify unescaped output
129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Role;
|
|
use App\Enums\TicketStatus;
|
|
use App\Enums\TicketType;
|
|
use App\Events\NotifySubscriptions;
|
|
use App\Events\TicketStatusChanged;
|
|
use App\Jobs\SendTicketMentionMail;
|
|
use App\Mail\TicketMention;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Collection;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class Ticket extends Model
|
|
{
|
|
use HasFactory;
|
|
use CastsEnums;
|
|
|
|
protected $enumCasts = [
|
|
'type' => TicketType::class,
|
|
'status' => TicketStatus::class
|
|
];
|
|
|
|
protected $fillable = [
|
|
'site_id',
|
|
'type',
|
|
'status',
|
|
'text'
|
|
];
|
|
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (Ticket $ticket) {
|
|
// If evolution force the ticket status on estimateTodo
|
|
if ($ticket->type->equals(TicketType::evolution())) {
|
|
$ticket->status = TicketStatus::estimateTodo();
|
|
}
|
|
});
|
|
static::updating(function (Ticket $ticket) {
|
|
$oldStatus = Ticket::where('id', $ticket->id)->first()->status;
|
|
if (!$ticket->status->equals($oldStatus)) {
|
|
event(new TicketStatusChanged($ticket, $oldStatus, auth()->user()));
|
|
event(new NotifySubscriptions($ticket, sprintf(
|
|
'Le ticket à changé de status : %s',
|
|
$ticket->status->label
|
|
)));
|
|
}
|
|
});
|
|
}
|
|
|
|
public function site()
|
|
{
|
|
return $this->belongsTo(Site::class);
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
public function messages()
|
|
{
|
|
return $this->hasMany(Message::class, 'ticket_id');
|
|
}
|
|
|
|
public function subscriptions()
|
|
{
|
|
return $this->belongsToMany(User::class, 'subscriptions');
|
|
}
|
|
|
|
public static function saveFile(Site $site, Ticket $ticket, TicketType $type, UploadedFile $file)
|
|
{
|
|
$siteAssetFullPath = Site::ticketAssetsFullPath($site->slug_name);
|
|
$fileAssetPath = Site::ticketAssetsPath($site->slug_name);
|
|
$fileName = sprintf(
|
|
'%s-%s-%d-%d.%s',
|
|
$site->slug_name,
|
|
$type->getDefinition(),
|
|
$ticket->id,
|
|
rand(),
|
|
$file->extension()
|
|
);
|
|
$file->move(sprintf('%s/%d', $siteAssetFullPath, $ticket->id), $fileName);
|
|
return sprintf('%s/%d/%s', $fileAssetPath, $ticket->id, $fileName);
|
|
}
|
|
|
|
/**
|
|
* Parse message @user tags and send emails to concerned users
|
|
* returns and array with to users notified
|
|
*
|
|
* @param Message $message
|
|
* @return array[User]
|
|
*/
|
|
public function sendBeenMentionnedToUser(Message $message): Collection
|
|
{
|
|
$ticket = $this;
|
|
// find @user + send mail si un seul user est trouvé
|
|
$usersNotified = collect([]);
|
|
$matches = [];
|
|
$ticket->load('site');
|
|
\preg_match_all('/@\w*/', $message->message, $matches);
|
|
$matches = collect($matches[0] ?? [])->unique();
|
|
foreach ($matches as $pseudo_usertag) {
|
|
try {
|
|
$user = User::where('name', 'LIKE', '%' . ltrim($pseudo_usertag, '@') . '%')
|
|
->where('email', '!=', $ticket->user->email)
|
|
->where(function ($q) use ($ticket) {
|
|
$q->whereHas('sites', function ($q) use ($ticket) {
|
|
$q->where('id', $ticket->site->id);
|
|
})->orWhere('role', Role::admin()->value)
|
|
->orWhere('role', Role::developper()->value);
|
|
})->firstOrFail();
|
|
$usersNotified->add($user);
|
|
SendTicketMentionMail::dispatchAfterResponse(
|
|
$user,
|
|
new TicketMention($ticket, $message->message)
|
|
);
|
|
} catch (ModelNotFoundException $e) {
|
|
continue;
|
|
}
|
|
}
|
|
return $usersNotified;
|
|
}
|
|
}
|