2020-11-25 16:05:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
use App\Events\NotifySubscriptions;
|
2021-07-21 09:44:29 +02:00
|
|
|
use App\Events\NotifyTicketCreator;
|
2020-11-25 16:05:21 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-04-18 20:31:07 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2020-11-25 16:05:21 +01:00
|
|
|
|
|
|
|
|
class Message extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
protected $fillable = [
|
|
|
|
|
'message'
|
|
|
|
|
];
|
|
|
|
|
|
2021-07-21 09:44:29 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::created(function (Message $message) {
|
2022-03-25 17:02:13 +01:00
|
|
|
$message->load(['user' => function ($q) {
|
|
|
|
|
$q->withTrashed();
|
|
|
|
|
}]);
|
2022-02-02 13:51:45 +01:00
|
|
|
/** @var \App\Models\Ticket $ticket */
|
2021-07-21 09:44:29 +02:00
|
|
|
$ticket = $message->ticket;
|
|
|
|
|
if ($message->user) {
|
2022-03-25 17:02:13 +01:00
|
|
|
$ticket->load(['user' => function ($q) {
|
|
|
|
|
$q->withTrashed();
|
|
|
|
|
}]);
|
2022-02-02 13:51:45 +01:00
|
|
|
/** @var \App\Models\User $messageUser */
|
2022-03-25 17:02:13 +01:00
|
|
|
$messageUser = $message->user()->withTrashed()->firstOrFail();
|
2022-02-02 13:51:45 +01:00
|
|
|
// * Send mail to mentionned users
|
|
|
|
|
$usersNotified = $ticket->sendBeenMentionnedToUser($message);
|
|
|
|
|
// * If email has not been sen to ticket author yet.
|
|
|
|
|
if (!$usersNotified->contains($ticket->user) and $ticket->user->id !== $messageUser->id) {
|
|
|
|
|
// * Send mail to ticket author
|
2021-07-21 09:44:29 +02:00
|
|
|
event(new NotifyTicketCreator(
|
|
|
|
|
$ticket,
|
|
|
|
|
trans('Nouvelle réponse de :name :<br> :message sur votre ticket', [
|
2022-02-02 13:51:45 +01:00
|
|
|
'name' => $messageUser->name,
|
2021-07-21 09:44:29 +02:00
|
|
|
'message' => $message->message
|
|
|
|
|
])
|
|
|
|
|
));
|
2022-02-02 13:51:45 +01:00
|
|
|
$usersNotified->push($ticket->user);
|
2021-07-21 09:44:29 +02:00
|
|
|
}
|
2022-02-02 13:51:45 +01:00
|
|
|
// ? Is message author is admin ?
|
|
|
|
|
if ($messageUser->isAdmin()) {
|
2022-02-02 16:47:47 +01:00
|
|
|
// TODO: investigate why this make errors on prod,
|
|
|
|
|
// https://supportweb.ldc.fr/ticket/583/message/create
|
|
|
|
|
// SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '583-8' for key 'PRIMARY'
|
|
|
|
|
// $ticket->subscriptions()->attach($messageUser);
|
|
|
|
|
// Add subscription
|
|
|
|
|
if (!$messageUser->subscriptions()->find($ticket->id)) {
|
|
|
|
|
$messageUser->subscriptions()->attach($ticket->id);
|
|
|
|
|
\session()->flash('info', trans('Vous avez été inscrit.'));
|
|
|
|
|
}
|
2022-02-02 13:51:45 +01:00
|
|
|
}
|
|
|
|
|
// * Send mail to other subscribed users
|
2021-07-21 09:44:29 +02:00
|
|
|
event(new NotifySubscriptions(
|
|
|
|
|
$message->ticket,
|
|
|
|
|
trans('Nouvelle réponse de :name :<br> :message', [
|
|
|
|
|
'name' => $message->user->name,
|
|
|
|
|
'message' => $message->message
|
|
|
|
|
]),
|
|
|
|
|
$usersNotified
|
|
|
|
|
));
|
2022-03-29 16:57:54 +02:00
|
|
|
}//end if
|
2021-07-21 09:44:29 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
public function ticket()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Ticket::class, 'ticket_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'user_id');
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 11:34:16 +02:00
|
|
|
public function saveFile(Site $site, Ticket $ticket, UploadedFile $file)
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2021-07-23 11:34:16 +02:00
|
|
|
$siteAssetFullPath = Site::ticketAssetsFullPath($site->slug_name);
|
2022-03-29 16:57:54 +02:00
|
|
|
$fileAssetPath = Site::ticketAssetsPath($site->slug_name);
|
|
|
|
|
$fileName = sprintf(
|
2021-04-18 20:31:07 +02:00
|
|
|
'%s-%s-%d-%d.%s',
|
2021-07-23 11:34:16 +02:00
|
|
|
$site->slug_name,
|
2021-04-18 20:31:07 +02:00
|
|
|
$ticket->id,
|
|
|
|
|
rand(),
|
|
|
|
|
$file->extension()
|
|
|
|
|
);
|
2021-07-23 11:34:16 +02:00
|
|
|
$file->move(sprintf('%s/messages/%d', $siteAssetFullPath, $ticket->id), $fileName);
|
2021-04-18 20:31:07 +02:00
|
|
|
return sprintf('%s/%d/%s', $fileAssetPath, $ticket->id, $fileName);
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
|
|
|
|
}
|