Skip to content

Commit

Permalink
deleteAttachments() function
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Dec 31, 2021
1 parent b4b0921 commit 53240b6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,35 @@ public static function deleteForever($attachments)
self::whereIn('id', $attachments->pluck('id')->toArray())->delete();
}

/**
* Delete attachments and update Thread & Conversation.
*/
public static function deleteAttachments($attachments)
{
if (!$attachments instanceof \Illuminate\Support\Collection) {
$attachments = collect($attachments);
}

foreach ($attachments as $attachment) {
if ($attachment->thread_id && $attachment->thread
&& count($attachment->thread->attachments) <= 1
) {
$attachment->thread->has_attachments = false;
$attachment->thread->save();
// Update conversation.
$conversation = $attachment->thread->conversation;
foreach ($conversation->threads as $thread) {
if ($thread->has_attachments) {
break 2;
}
}
$conversation->has_attachments = false;
$conversation->save();
}
}
Attachment::deleteForever($attachments);
}

/**
* Create a copy of the attachment and it's file.
*/
Expand Down

0 comments on commit 53240b6

Please sign in to comment.