mein-sterntours/app/Repositories/LeadMailRepository.php
2021-11-09 18:38:44 +01:00

404 lines
No EOL
17 KiB
PHP

<?php
namespace App\Repositories;
use App\Services\Util;
use App\Models\LeadMail;
use App\Mail\MailSendInfo;
use App\Models\CMSContent;
use App\Models\EmailTemplate;
use App\Services\Placeholder;
use App\Models\LeadFile;
use Illuminate\Support\Facades\Mail;
use App\Models\Lead;
use Illuminate\Database\Eloquent\Collection;
class LeadMailRepository extends BaseRepository {
public function __construct(LeadMail $model)
{
$this->model = $model;
}
public function update($data)
{
return $this->model;
}
public function sendAndStore($data){
//send or draft
//$data['action']
if(isset($data['send_mail_to']) && is_array($data['send_mail_to'])) {
//has Attachments
$lead_files = [];
if(isset($data['message_attachment_id']) && is_array($data['message_attachment_id'])){
foreach ($data['message_attachment_id'] as $message_attachment_id){
if($LeadFile = LeadFile::find($message_attachment_id)){
$lead_files[] = $LeadFile;
}
}
}
foreach ($data['send_mail_to'] as $lead_id => $on) {
$lead = Lead::find($lead_id);
if ($lead->customer) {
$data['draft'] = (isset($data['action']) && $data['action'] === 'draft' ? true : false);
if(!$data['draft']){
$data['message'] = $this->prepareContent($lead, $data['message']);
$data['subject'] = $this->prepareContent($lead, $data['subject']);
}
$reply_id = isset($data['lead_mail_id']) ? $data['lead_mail_id'] : NULL;
$email = isset($data['send_mail_to_mail'][$lead_id]) ? $data['send_mail_to_mail'][$lead_id] : $lead->customer->email;
$lead_mail = $this->store($lead, $data, $email, false, $reply_id);
foreach ($lead_files as $file) {
$file->lead_id = $lead->id;
$file->lead_mail_id = $lead_mail->id;
$file->identifier = 'mail';
$file->save();
}
if(isset($data['action']) && $data['action'] === 'send'){ //not at draft
$this->sendMail($lead_mail);
}
}
}
}
}
public function forwardMail($lead_mail, $data){
//send or draft
//$data['action']
if(isset($data['lead_mail_forward_email']) && !empty($data['lead_mail_forward_email'])) {
$to_mails = [];
if(strpos($data['lead_mail_forward_email'], ',')){
$to_mails = array_map('trim', explode(',', $data['lead_mail_forward_email']));
}else{
$to_mails[] = $data['lead_mail_forward_email'];
}
$lead_files = $lead_mail->lead_files;
$full_message = $this->prepareMessageFull($lead_mail);
try {
//
Mail::to($to_mails)
->bcc($lead_mail->bcc ?: [])
->send(new MailSendInfo($lead_mail->subject, $full_message, $lead_files));
}
catch(\Exception $e){
// Never reached
$forward = array();
$forward[now()->format("d.m.Y H:i:s")] = ['fail'=> true, 'to'=> $data['lead_mail_forward_email'], 'error'=>$e->getMessage()];
$lead_mail->setForwardMessage($forward);
return false;
}
$forward = array();
$forward[now()->format("d.m.Y H:i:s")] = ['sent'=> true, 'to'=> $data['lead_mail_forward_email']];
$lead_mail->setForwardMessage($forward);
return true;
}
}
public function replyStore($data){
if(isset($data['lead_id']) && $lead = Lead::find($data['lead_id'])) {
//has Attachments
$lead_files = [];
if(isset($data['message_attachment_id']) && is_array($data['message_attachment_id'])){
foreach ($data['message_attachment_id'] as $message_attachment_id){
if($LeadFile = LeadFile::find($message_attachment_id)){
$lead_files[] = $LeadFile;
}
}
}
if ($lead->customer) {
$data['draft'] = (isset($data['action']) && $data['action'] === 'draft' ? true : false);
$mail_from = isset($data['mail_from']) ? $data['mail_from'] : $lead->travel_user->email;
$sent_at = isset($data['sent_at']) ? \Carbon::parse(str_replace("- ", "", $data['sent_at'])) : now();
$reply_id = isset($data['lead_mail_id']) ? $data['lead_mail_id'] : NULL;
$lead_mail = $this->store($lead, $data, $mail_from, true, $reply_id, $sent_at);
foreach ($lead_files as $file) {
$file->lead_id = $lead->id;
$file->lead_mail_id = $lead_mail->id;
$file->identifier = 'mail';
$file->save();
}
}
}
}
public function store($lead, $data, $mail_from, $is_answer = false, $reply_id = NULL, $sent_at=false){
if(isset($data['save_lead_mail_id'])){
$lead_mail = LeadMail::find($data['save_lead_mail_id']);
$lead_mail->fill([
'lead_id' => $lead->id,
'customer_id' => $lead->customer_id,
'is_answer' => $is_answer,
'reply_id' => $reply_id,
'email' => $mail_from,
'recipient' => isset($data['recipient']) ? Util::_explodeLines($data['recipient']) : null,
'cc' => isset($data['cc']) ? Util::_explodeLines($data['cc']) : null,
'bcc' => isset($data['bcc']) ? Util::_explodeLines($data['bcc']) : null,
'subject' => $data['subject'],
'message' => $data['message'],
'dir' => isset($data['dir']) ? $data['dir'] : 0,
'subdir' => isset($data['subdir']) ? $data['subdir'] : 0,
'draft' => $data['draft'],
'sent_at' => $sent_at ? $sent_at : now(),
])->save();
}else{
$lead_mail = LeadMail::create([
'lead_id' => $lead->id,
'customer_id' => $lead->customer_id,
'is_answer' => $is_answer,
'reply_id' => $reply_id,
'email' => $mail_from,
'recipient' => isset($data['recipient']) ? Util::_explodeLines($data['recipient']) : null,
'cc' => isset($data['cc']) ? Util::_explodeLines($data['cc']) : null,
'bcc' => isset($data['bcc']) ? Util::_explodeLines($data['bcc']) : null,
'subject' => $data['subject'],
'message' => $data['message'],
'dir' => isset($data['dir']) ? $data['dir'] : 0,
'subdir' => isset($data['subdir']) ? $data['subdir'] : 0,
'draft' => $data['draft'],
'sent_at' => $sent_at ? $sent_at : now(),
]);
}
return $lead_mail;
}
private function sendMail($lead_mail){
$to_mails = [];
if(strpos($lead_mail->email, ',')){
$to_mails = array_map('trim', explode(',', $lead_mail->email));
}else{
$to_mails[] = $lead_mail->email;
}
if($lead_mail->recipient){
$to_mails = array_merge($to_mails, $lead_mail->recipient);
}
$lead_files = $lead_mail->lead_files;
$full_message = $this->prepareMessageFull($lead_mail);
try {
//
Mail::to($to_mails)
->cc($lead_mail->cc ?: [])
->bcc($lead_mail->bcc ?: [])
->send(new MailSendInfo($lead_mail->subject, $full_message, $lead_files));
}
catch(\Exception $e){
// Never reached
$lead_mail->fail = true;
$lead_mail->error = $e->getMessage();
$lead_mail->save();
return false;
}
$lead_mail->send = true;
$lead_mail->sent_at = now();
$lead_mail->save();
return true;
}
private function prepareMessageFull($lead_mail, $deep = 0){
$ret = "";
if($deep === 0){
$ret .= $lead_mail->message;
}else{
$ret .= "--------------------------------\n";
$ret .= $lead_mail->is_answer ? "Antwort von: " : "Gesendet an: ";
$ret .= "&lt;".$lead_mail->email."&gt; ".$lead_mail->sent_at."\n";
$ret .= "<strong>".$lead_mail->subject."</strong>\n";
$ret .= $lead_mail->message;
}
if($lead_mail->lead_mail){
$ret .= $this->prepareMessageFull($lead_mail->lead_mail, $deep+1);
}
return $ret;
}
private function prepareContent($lead, $content){
$content = Placeholder::replaceLead($lead, $content);
return $content;
}
private static function prepareContactMails($value){
if(isset($value->customers)){ //&& $value->lead_mail_dir !== NULL
$first_mail = "";
if($value->lead_mail_dir < 10){ // && $value->lead_mail_subdir > 0
$lead_mail_dir = \App\Services\Lead::getCustomerMailDir($value->lead_mail_dir);
$contact_emails = \App\Services\Lead::getCustomerMailEmails($lead_mail_dir, $value->lead_mail_subdir);
if($value->lead_mail_dir == 0){
$value->recipient = Util::_implodeLines($contact_emails);
return $value;
}else{
if($contact_emails && count($contact_emails) > 0) {
$first_mail = array_shift($contact_emails);
if (count($contact_emails) > 0) {
$value->recipient = Util::_implodeLines($contact_emails);
}
}
}
}
foreach ($value->customers as $key=>$val){
$val['email'] = $first_mail;
$value->customers[$key] = $val;
}
}else{
if(isset($value->replay_email)){
if($value->lead_mail_dir < 10){ // && $value->lead_mail_subdir > 0
$lead_mail_dir = \App\Services\Lead::getCustomerMailDir($value->lead_mail_dir);
$contact_emails = \App\Services\Lead::getCustomerMailEmails($lead_mail_dir, $value->lead_mail_subdir);
if($contact_emails && count($contact_emails) > 0) {
$value->replay_email = array_shift($contact_emails);
}
}
}
}
return $value;
}
public static function loadModal($data)
{
$value = new Collection();
$value->title = "";
$value->subtitle = "";
$value->url = "";
$value->recipient = "";
$value->show_move_dirs = true;
$value->cc = "";
$value->bcc = "";
$value->lead_title_id = "";
$value->lead = "";
$value->filter_email_templates_directories = EmailTemplate::join('email_template_dirs', 'email_template_dir_id', '=', 'email_template_dirs.id')->get()->pluck('name', 'id')->unique()->toArray();
/*Ansicht*/
if ($data['action'] === "show-lead-mail") {
if (isset($data['lead_mail_id']) && $lead_mail = LeadMail::find($data['lead_mail_id'])) {
$value->url = $data['url'];
$value->title = "E-Mail Ansicht";
if(isset($data['preview']) && $data['preview']){
$value->show_move_dirs = false;
}
return view("lead.modal-show-mail", compact('data', 'value', 'lead_mail'))->render();
}
}
/* neue Mail */
if ($data['action'] === "edit-lead-mail") {
$value->id = $data['id']; //
$lead_mail = LeadMail::find($value->id);
$lead = $lead_mail->lead;
$value->lead_files = $lead_mail->lead_files;
$value->save_lead_mail_id = $lead_mail->id;
$value->draft = true;
$value->id = $lead_mail->travel_user_lead_id;
$value->lead = $lead;
$value->show = 'single';
$value->lead_title_id = " - (".$value->lead->id.")";
$tmp = [];
$tmp['email'] = $lead_mail->customer ? $lead_mail->email : "";
$tmp['name'] = $lead->customer ? $lead->customer->firstname . " " . $lead->customer->name . " | " : "- | ";
$tmp['name'] .= $lead->id ? $lead->id. " | " : "- | ";
$data['customers'][$lead->id] = $tmp;
$value->customers = $data['customers'];
$value->subject = $lead_mail->subject;
$value->message = $lead_mail->message;
$value->recipient = Util::_implodeLines($lead_mail->recipient);
$value->cc = Util::_implodeLines($lead_mail->cc);
$value->bcc = Util::_implodeLines($lead_mail->bcc);
$value->title = "E-Mail- Nachricht an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet.";
if($lead_mail->reply_id){
$value->title = "E-Mail Antwort an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet, die im System als Antwort gespeichert wird.";
$value->lead_mail = $lead_mail->lead_mail;
}
$value->s_placeholder = "Betreff der E-Mail";
$value->m_placeholder = "Nachricht der E-Mail";
$value->url = $data['url'];
$value->lead_mail_dir = $lead_mail->dir ? $lead_mail->dir : 0;
$value->lead_mail_subdir = $lead_mail->subdir ? $lead_mail->subdir : 0;
return view("lead.modal-new-mail", compact('data', 'value'))->render();
}
/* neue Mail */
if ($data['action'] === "new-lead-mail") {
$value->id = "";
$value->draft = false;
$value->lead_title_id = "-";
//singel
if (isset($data['lead_id']) && $lead = Lead::find($data['lead_id'])) {
$value->id = $data['lead_id'];
$value->lead = $lead;
$value->show = 'single';
$value->draft = true;
$value->lead_title_id = " - (".$value->lead->id.")";
}else{
//multi
$value->show = 'multi';
}
$value->customers = $data['customers'];
$value->subject = $value->lead_title_id;
$value->message = CMSContent::getContentBySlug('mailvorlage');
$value->s_placeholder = "Betreff der E-Mail";
$value->m_placeholder = "Nachricht der E-Mail";
if(isset($data['lead_mail_id']) && $lead_mail = LeadMail::find($data['lead_mail_id'])){
$value->subject = "Re: ".Util::_first_replace($lead_mail->subject);
$value->lead_mail = $lead_mail;
}
$value->title = "E-Mail- Nachricht an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet.";
if($data['id'] === 'reply-send'){
$value->title = "E-Mail Antwort an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet, die im System als Antwort gespeichert wird.";
}
$value->url = $data['url'];
$value->lead_mail_dir = isset($data['lead_mail_dir']) ? $data['lead_mail_dir'] : 0;
$value->lead_mail_subdir = isset($data['lead_mail_subdir']) ? $data['lead_mail_subdir'] : 0;
$value = self::prepareContactMails($value);
return view("lead.modal-new-mail", compact('data', 'value'))->render();
}
/*Antwort speichern*/
if ($data['action'] === "reply-lead-mail") {
if (isset($data['lead_id']) && $lead = Lead::find($data['lead_id'])) {
$value->id = $data['lead_id'];
$value->draft = false;
$value->lead = $lead;
$value->message = "";
$value->subject = " - (".$value->lead->id.")";
$value->lead_title_id = " - (".$value->lead->id.")";
$value->s_placeholder = "Betreff des Kunden";
$value->m_placeholder = "Nachricht des Kunden";
if(isset($data['lead_mail_id']) && $lead_mail = LeadMail::find($data['lead_mail_id'])){
$value->subject = "Re: ".Util::_first_replace($lead_mail->subject);
$value->lead_mail = $lead_mail;
}
$value->title = "E-Mail Antwort speichern";
$value->subtitle = "Die E-Mail wird im System gespeichert.";
if($data['id'] === 'reply-save'){
$value->subtitle = "Die E-Mail wird im System als Antwort gespeichert.";
}
$value->url = $data['url'];
$value->show = 'reply';
$value->lead_mail_dir = isset($data['lead_mail_dir']) ? $data['lead_mail_dir'] : 0;
$value->lead_mail_subdir = isset($data['lead_mail_subdir']) ? $data['lead_mail_subdir'] : 0;
$value->replay_email = $value->lead->customer->email;
$value = self::prepareContactMails($value);
return view("lead.modal-new-mail", compact('data', 'value'))->render();
}
}
}
}