public function addRecipient($data) { //If this user is already a mailing recipient in the system if (MailingRecipient::where('email_address', $data['email_address'])->count() > 0) { $recipient = MailingRecipient::where('email_address', $data['email_address'])->first(); //If the recipient is already in the list, do nothing if ($this->recipients()->where('id', $recipient->id)->count() > 0) { return 0; } else { DB::table('mailing_list_recipients')->insert(['mailing_list_id' => $this->id, 'recipient_id' => $recipient->id]); return 1; } } else { $recipient = MailingRecipient::addNew(['first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'email_address' => $data['email_address'], 'mobile_number' => $data['mobile_number']]); DB::table('mailing_list_recipients')->insert(['mailing_list_id' => $this->id, 'recipient_id' => $recipient->id]); $recipient->attemptLinkToPatient(); return 1; } }
public function doRecipientSearch(Request $request) { $recipient_string = explode(' ', $request->input('search_string')); $list = null; if ($request->has('list')) { $recipient_ids = DB::table('mailing_recipients')->select('mailing_recipients.id', 'mailing_list_recipients.mailing_list_id')->join('mailing_list_recipients', 'mailing_recipients.id', '=', 'mailing_list_recipients.recipient_id')->where('mailing_list_recipients.mailing_list_id', $request->input('list'))->where(function ($outer_query) use($recipient_string) { $outer_query->where(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`first_name` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->orWhere(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`last_name` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->orWhere(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`email_address` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->orWhere(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`mobile_number` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); }); })->take(50)->get(); $ids = []; foreach ($recipient_ids as $id) { $ids[] = $id->id; } $recipients = MailingRecipient::whereIn('id', $ids)->get(); $list = MailingList::findOrFail($request->has('list')); } else { $recipients = MailingRecipient::where(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`first_name` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->orWhere(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`last_name` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->orWhere(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`email_address` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->orWhere(function ($query) use($recipient_string) { $where = []; foreach ($recipient_string as $string) { $where[] = "`mobile_number` LIKE '%" . $string . "%'"; } $query->whereRaw(implode(' OR ', $where)); })->take(50)->get(); } $response = ['html' => view('backend.partials.marketing.recipient-list')->with(['recipients' => $recipients, 'list' => $list])->render()]; header('Content-Type: application/json'); die(json_encode($response)); }