Skip to content

Commit 87bafb2

Browse files
committed
BUG: attachment_name search was very slow. Reuse attachment search done appart in any_text_field
1 parent c560855 commit 87bafb2

2 files changed

Lines changed: 32 additions & 32 deletions

File tree

src/Controllers/TicketsController.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -176,42 +176,15 @@ public function getTicketCollectionFrom($ticketList)
176176
});
177177
}
178178

179-
if (isset($search_fields['attachment_name'])) {
180-
$collection->where(function ($query) use ($search_fields) {
181-
$query->whereHas('attachments', function ($q1) use ($search_fields) {
182-
$q1->where('original_filename', 'like', '%'.$search_fields['attachment_name'].'%')
183-
->orWhere('new_filename', 'like', '%'.$search_fields['attachment_name'].'%')
184-
->orWhere('description', 'like', '%'.$search_fields['attachment_name'].'%');
185-
})
186-
->orWhereHas('comments', function ($q2) use ($search_fields) {
187-
$q2->whereHas('attachments', function ($q3) use ($search_fields) {
188-
$q3->where('original_filename', 'like', '%'.$search_fields['attachment_name'].'%')
189-
->orWhere('new_filename', 'like', '%'.$search_fields['attachment_name'].'%')
190-
->orWhere('description', 'like', '%'.$search_fields['attachment_name'].'%');
191-
});
192-
});
193-
});
179+
if (isset($search_fields['attachment_name'])){
180+
$collection->whereIn('panichd_tickets.id', $this->listTicketsWhereAttachmentHas($search_fields['attachment_name']));
194181
}
195182

196183
if (isset($search_fields['any_text_field'])) {
197-
// Coincidence in Ticket / Comment attachments
198-
$attachments_col = Attachment::where('original_filename', 'like', '%' . $search_fields['any_text_field'] . '%')
199-
->orWhere('new_filename', 'like', '%' . $search_fields['any_text_field'] . '%')
200-
->orWhere('description', 'like', '%' . $search_fields['any_text_field'] . '%')
201-
->with('ticket', 'comment.ticket')->get();
202-
203-
$a_ticket_ids = [];
204-
foreach ($attachments_col as $att){
205-
if (!is_null($att->ticket)){
206-
$a_ticket_ids[] = $att->ticket->id;
207-
208-
}elseif (!is_null($att->comment) and !is_null($att->comment->ticket)){
209-
$a_ticket_ids[] = $att->comment->ticket->id;
210-
}
211-
}
184+
$a_attachment_tickets = $this->listTicketsWhereAttachmentHas($search_fields['any_text_field']);
212185

213186
// Coincidence in any ticket field
214-
$collection->where(function ($query) use ($search_fields, $a_ticket_ids) {
187+
$collection->where(function ($query) use ($search_fields, $a_attachment_tickets) {
215188
$query->where('subject', 'like', '%'.$search_fields['any_text_field'].'%')
216189
->orWhere('content', 'like', '%'.$search_fields['any_text_field'].'%')
217190
->orWhere('intervention', 'like', '%'.$search_fields['any_text_field'].'%')
@@ -220,7 +193,7 @@ public function getTicketCollectionFrom($ticketList)
220193
})
221194

222195
// Attachment with coincidence with "any_text_field"
223-
->orWhereIn('panichd_tickets.id', $a_ticket_ids);
196+
->orWhereIn('panichd_tickets.id', $a_attachment_tickets);
224197
});
225198
}
226199
}

src/Traits/Attachments.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,31 @@ protected function deleteThumbnail($file_name)
609609

610610
return \File::exists($thumbnail_path.$file_name) ? false : true;
611611
}
612+
613+
/*
614+
* Search a string in all attachment fields
615+
* Return ticket id's that contain the found attachments
616+
*
617+
* @return Array
618+
*/
619+
public function listTicketsWhereAttachmentHas($needle)
620+
{
621+
// Coincidence in Ticket / Comment attachments
622+
$attachments_col = Attachment::where('original_filename', 'like', '%' . $needle . '%')
623+
->orWhere('new_filename', 'like', '%' . $needle . '%')
624+
->orWhere('description', 'like', '%' . $needle . '%')
625+
->with('ticket', 'comment.ticket')->get();
626+
627+
$a_ticket_ids = [];
628+
foreach ($attachments_col as $att){
629+
if (!is_null($att->ticket)){
630+
$a_ticket_ids[] = $att->ticket->id;
631+
632+
}elseif (!is_null($att->comment) and !is_null($att->comment->ticket)){
633+
$a_ticket_ids[] = $att->comment->ticket->id;
634+
}
635+
}
636+
637+
return $a_ticket_ids;
638+
}
612639
}

0 commit comments

Comments
 (0)