325 lines
15 KiB
PHP
325 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Carbon;
|
|
use Request;
|
|
use Response;
|
|
use App\Models\Lead;
|
|
use App\Services\Util;
|
|
use App\Models\LeadFile;
|
|
use App\Models\LeadMail;
|
|
|
|
use App\Models\EmailTemplate;
|
|
use App\Services\Placeholder;
|
|
use Illuminate\Support\Facades\URL;
|
|
use App\Models\TravelUserBookingFewo;
|
|
use App\Repositories\LeadFileRepository;
|
|
use App\Repositories\LeadMailRepository;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use App\Repositories\BookingFewoFileRepository;
|
|
use App\Repositories\CustomerFewoFileRepository;
|
|
use App\Repositories\CustomerFewoMailRepository;
|
|
|
|
class LeadMailController extends Controller
|
|
{
|
|
|
|
protected $leadMailRepo;
|
|
|
|
public function __construct(LeadMailRepository $leadMailRepo)
|
|
{
|
|
$this->middleware(['admin', '2fa']);
|
|
$this->leadMailRepo = $leadMailRepo;
|
|
}
|
|
|
|
|
|
public function store($id, $action=false)
|
|
{
|
|
$data = Request::all();
|
|
$lead_mail = LeadMail::findOrFail($id);
|
|
|
|
|
|
if($action === 'move-mail-lead'){
|
|
$lead_mail = LeadMail::findOrFail($id);
|
|
$data['subdir'] = isset($data['subdir']) && $data['subdir'] ? $data['subdir'] : null;
|
|
$lead_mail->dir = $data['dir'];
|
|
$lead_mail->subdir = $data['subdir'];
|
|
$lead_mail->save();
|
|
}
|
|
|
|
if($action === 'forward-mail-lead'){
|
|
$lead_mail = LeadMail::findOrFail($id);
|
|
$customerMailFewoRepo = new LeadMailRepository($lead_mail);
|
|
$customerMailFewoRepo->forwardMail($lead_mail, $data);
|
|
\Session()->flash('alert-success', __('E-Mail weitergeleitet'));
|
|
|
|
}
|
|
return back()->with('collapse_shows', $data['collapse_shows'] ? $data['collapse_shows'] : []);
|
|
}
|
|
|
|
public function delete($id){
|
|
$lead_mail = LeadMail::find($id);
|
|
$lead_mail->dir = 12;
|
|
$lead_mail->subdir = 0;
|
|
$lead_mail->save();
|
|
|
|
\Session()->flash('alert-success', __('Mail gelöscht'));
|
|
return back();
|
|
}
|
|
|
|
public function loadModal(){
|
|
$data = Request::all();
|
|
$ret = "";
|
|
|
|
if(Request::ajax()) {
|
|
if ($data['action'] === "new-lead-mail" || $data['action'] === "reply-lead-mail" || $data['action'] === "show-lead-mail" || $data['action'] === "edit-lead-mail"){
|
|
$data['customers'] = [];
|
|
if ($data['action'] === "new-lead-mail" && isset($data['lead_id']) && $lead = Lead::find($data['lead_id'])) {
|
|
$tmp = [];
|
|
$tmp['email'] = $lead->customer ? $lead->customer->email : "";
|
|
$tmp['name'] = $lead->customer ? $lead->customer->firstname . " " . $lead->customer->name . " | " : "- | ";
|
|
$data['customers'][$lead->id] = $tmp;
|
|
}
|
|
$ret = LeadMailRepository::loadModal($data);
|
|
}
|
|
|
|
if($data['action'] === "modal-upload-lead-file") {
|
|
$ret = view("lead.upload_modal", compact('data'))->render();
|
|
}
|
|
|
|
if($data['action'] === "upload-lead-file"){
|
|
if($data['lead_id']){
|
|
$bookingFileRepo = new LeadFileRepository(new LeadFile());
|
|
$bookingFileRepo->_set('disk', 'lead');
|
|
$bookingFileRepo->_set('lead_id', $data['lead_id']);
|
|
$bookingFileRepo->_set('dir', '/files/'.date('Y/m').'/');
|
|
$bookingFileRepo->_set('identifier', 'lead');
|
|
return $bookingFileRepo->uploadFile(Request::all());
|
|
}
|
|
}
|
|
}
|
|
return response()->json(['response' => $data, 'html'=>$ret]);
|
|
}
|
|
|
|
|
|
public function sendMail(LeadMailRepository $leadMailRepository){
|
|
$data = Request::all();
|
|
$leadMailRepository->sendAndStore($data);
|
|
if($data['action'] == 'draft'){
|
|
\Session()->flash('alert-success', "Entwurf gespeichert!");
|
|
}else{
|
|
\Session()->flash('alert-success', "Mail gesendet!");
|
|
}
|
|
return back()->with('collapse_shows', $data['collapse_shows'] ? $data['collapse_shows'] : []);
|
|
}
|
|
|
|
public function replyMail(LeadMailRepository $LeadMailRepository){
|
|
$data = Request::all();
|
|
$LeadMailRepository->replyStore($data);
|
|
\Session()->flash('alert-success', "Mail gespeichert!");
|
|
return back()->with('collapse_shows', $data['collapse_shows'] ? $data['collapse_shows'] : []);
|
|
}
|
|
|
|
|
|
public function getEmailTemplates()
|
|
{
|
|
$query = EmailTemplate::with('email_template_dir')->select('email_templates.*')->where('active', '=', true);
|
|
|
|
if(Request::get('filter_email_templates_directory') != ""){
|
|
$query->where('email_template_dir_id', '=', Request::get('filter_email_templates_directory'));
|
|
}
|
|
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('action', function (EmailTemplate $emailTemplate) {
|
|
return '<a href="javascript:void(0)" class="btn icon-btn btn-sm btn-primary email-template-action"
|
|
data-url="'.route('customer_mail_ajax').'" data-id="'.$emailTemplate->id.'" data-action="load_email_template"
|
|
title="Vorlage laden" data-placement="left" rel="tooltip"><span class="ion ion-ios-arrow-dropup"></span></a>';
|
|
})
|
|
->addColumn('email_template_dir.name', function (EmailTemplate $emailTemplate) {
|
|
return $emailTemplate->email_template_dir ? '<span class="badge badge-default" style="background-color: '.$emailTemplate->email_template_dir->color.'">'.$emailTemplate->email_template_dir->name.'</span>' : ' ';
|
|
})
|
|
->orderColumn('id', 'id $1')
|
|
->orderColumn('subject', 'subject $1')
|
|
->orderColumn('name', 'name $1')
|
|
->orderColumn('email_template_dir.name', 'email_template_dir.name $1')
|
|
->orderColumn('message', 'message $1')
|
|
->filterColumn('id', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('id', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->filterColumn('subject', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('name', 'LIKE', '%'.$keyword.'%');
|
|
$query->OrWhere('subject', 'LIKE', '%'.$keyword.'%');
|
|
$query->OrWhere('message', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->rawColumns(['action', 'email_template_dir.name'])
|
|
->make(true);
|
|
}
|
|
|
|
|
|
public function uploadAttachment($id){
|
|
|
|
$fileRepo = new LeadFileRepository(new LeadFile());
|
|
if($id === 'tmp'){
|
|
$fileRepo->_set('disk', 'lead');
|
|
$fileRepo->_set('dir', '/attachment/'.date('Y/m').'/');
|
|
$fileRepo->_set('lead_id', NULL);
|
|
$fileRepo->_set('lead_mail_id', NULL);
|
|
$fileRepo->_set('identifier', 'tmp');
|
|
return $fileRepo->uploadFile(Request::all());
|
|
}
|
|
|
|
return Response::json([
|
|
'error' => true,
|
|
'code' => 200
|
|
], 200);
|
|
}
|
|
|
|
public function ajax(){
|
|
$data = Request::all();
|
|
$ret = "";
|
|
$status = false;
|
|
if(Request::ajax() && isset($data['action'])){
|
|
if($data['action'] === 'load_preview_mail'){
|
|
$lead = Lead::findOrFail($data['lead_id']);
|
|
$ret = Placeholder::replaceLead($lead, $data['content']);
|
|
$data['subject'] = Placeholder::replaceLead($lead, $data['subject']);
|
|
$status = 'success';
|
|
}
|
|
if($data['action'] === 'toggle_important'){
|
|
$lead_mail = LeadMail::find($data['id']);
|
|
$lead_mail->important = ($lead_mail->important ? false : true);
|
|
$lead_mail->save();
|
|
$status = 'success';
|
|
}
|
|
if($data['action'] === 'load_email_template'){
|
|
$email_template = EmailTemplate::find($data['id']);
|
|
$ret = $email_template->message;
|
|
$data['subject'] = $email_template->subject;
|
|
$status = 'success';
|
|
}
|
|
if($data['action'] === 'delete_mail_attachment'){
|
|
$lead_file = LeadFile::find($data['id']);
|
|
$fileRepo = new LeadFileRepository($lead_file);
|
|
$fileRepo->_set('disk', 'lead');
|
|
$ret = $fileRepo->delete();
|
|
$status = 'success';
|
|
}
|
|
|
|
if($data['action'] === 'add_attachment'){
|
|
$arrContextOptions=array(
|
|
"ssl"=>array(
|
|
"verify_peer"=>false,
|
|
"verify_peer_name"=>false,
|
|
),
|
|
);
|
|
$contents = file_get_contents($data['target'], false, stream_context_create($arrContextOptions));
|
|
$mine = Util::getMimeFromHeader($http_response_header);
|
|
$extension = Util::getExtensionFromMime($mine);
|
|
$fileRepo = new LeadFileRepository(new LeadFile());
|
|
$fileRepo->_set('disk', 'lead');
|
|
$fileRepo->_set('dir', '/attachment/'.date('Y/m').'/');
|
|
$fileRepo->_set('lead_id', NULL);
|
|
$fileRepo->_set('lead_mail_id', NULL);
|
|
$fileRepo->_set('identifier', 'tmp');
|
|
$fileRepo->_set('originalName', $data['name']);
|
|
$fileRepo->_set('mine', $mine);
|
|
$fileRepo->_set('extension', $extension);
|
|
return $fileRepo->storeFile($contents);
|
|
}
|
|
}
|
|
return response()->json(['response' => $data, 'html'=>$ret, 'status'=>$status]);
|
|
}
|
|
|
|
private function getSearchRequests()
|
|
{
|
|
if (!Request::get('lead_id')) {
|
|
return false;
|
|
}
|
|
$query = LeadMail::where('lead_id', '=', Request::get('lead_id'));
|
|
if (Request::get('lead_mail_dir') == 11) { //draft
|
|
$query->where('draft', '=', true)->where('dir', '!=', 12);
|
|
}else{
|
|
$query->where('dir', '=', Request::get('lead_mail_dir')); //with('lead'
|
|
}
|
|
if (Request::get('lead_mail_subdir')) {
|
|
$query->where('subdir', '=', Request::get('lead_mail_subdir'));
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function getRequests(){
|
|
|
|
$query = $this->getSearchRequests();
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('checkbox', function (LeadMail $lead_mail) {
|
|
return '<div class="message-checkbox mr-1">
|
|
<label class="custom-control custom-checkbox">
|
|
<input type="checkbox" class="custom-control-input">
|
|
<span class="custom-control-label"></span>
|
|
</label>
|
|
</div>';
|
|
})
|
|
->addColumn('important', function (LeadMail $lead_mail) {
|
|
$icon = ($lead_mail->important ? 'ion-md-star' : 'ion-md-star-outline');
|
|
return '<a href="javascript:void(0)" class="ion '.$icon.' d-block text-secondary text-big mr-3 customer-mail-ajax-action"
|
|
data-url="'.route('lead_mail_ajax').'" data-id="'.$lead_mail->id.'" data-important="'.$lead_mail->important.'" data-action="toggle_important"></a>';
|
|
})
|
|
->addColumn('subject', function (LeadMail $lead_mail) {
|
|
|
|
$icon = $lead_mail->reply_id ? 'ion-ios-redo' : 'ion-ios-mail';
|
|
$badge = $lead_mail->is_answer ? 'badge-next' : 'badge-secondary';
|
|
$badge = $lead_mail->draft ? 'badge-default' : $badge;
|
|
$to_icon = $lead_mail->draft ? '<i class="ion ion-ios-warning" style="opacity: 0.7"></i>' : '';
|
|
$action = $lead_mail->draft ? 'edit-lead-mail' : 'show-lead-mail';
|
|
$id = $lead_mail->draft ? $lead_mail->id : 'new';
|
|
$url = $lead_mail->draft ? route('lead_mail_send_mail') : '';
|
|
|
|
return '<a href="javascript:void(0)" class="badge '.$badge.'" data-toggle="modal"
|
|
data-target="#modals-load-content" data-id="'.$id.'" data-model="LeadMail"
|
|
data-action="'.$action.'" data-url="'.$url.'" data-redirect="back"
|
|
data-lead_mail_id="'.$lead_mail->id.'" data-route="'.route('lead_mail_modal_load').'">
|
|
'.$to_icon.'<i class="ion '.$icon.' ui-w-30 text-center" style="opacity: 0.7"></i>'.$lead_mail->subject.'
|
|
'.($lead_mail->lead_files->count() ? ' <i class="ion ion-md-attach"> <span class="badge badge-primary indicator">'.$lead_mail->lead_files->count().'</span></i>' : '');
|
|
|
|
})
|
|
->addColumn('date', function (LeadMail $lead_mail) {
|
|
if($lead_mail->send){
|
|
return '<span class="badge badge-success" style="background-color: #94ae59"><i class="fa fa-check-circle"></i> '.$lead_mail->sent_at.'</span>';
|
|
}
|
|
return '<span class="badge badge-default"><i class="fa fa-times-circle"></i> '.$lead_mail->sent_at.'</span>';
|
|
})
|
|
->addColumn('action', function (LeadMail $lead_mail) {
|
|
$ret = '';
|
|
if(!$lead_mail->draft){
|
|
$ret = '<a href="javascript:void(0)" class="btn btn-xs btn-secondary" data-toggle="modal"
|
|
data-target="#modals-load-content" data-id="reply-send" data-model="LeadMail" data-action="new-lead-mail"
|
|
data-url="'.route('lead_mail_send_mail').'" data-redirect="back" data-lead_mail_id="'.$lead_mail->id.'"
|
|
data-lead_id="'.$lead_mail->lead_id.'" data-route="'.route('lead_mail_modal_load').'" data-lead_mail_dir="'.$lead_mail->dir.'" data-lead_mail_subdir="'.$lead_mail->subdir.'">
|
|
<span title="Antwort auf E-Mail senden" data-placement="left" rel="tooltip"><i class="ion ion-ios-redo"></i> <i class="ion ion-md-mail-open"></i></span>
|
|
</a>
|
|
|
|
<a href="javascript:void(0)" class="btn btn-xs btn-default" data-toggle="modal"
|
|
data-target="#modals-load-content" data-id="reply-save" data-model="LeadMail" data-action="reply-lead-mail"
|
|
data-url="'.route('lead_mail_reply_mail').'" data-redirect="back" data-lead_mail_id="'.$lead_mail->id.'"
|
|
data-lead_id="'.$lead_mail->lead_id.'" data-route="'.route('lead_mail_modal_load').'" data-lead_mail_dir="'.$lead_mail->dir.'" data-lead_mail_subdir="'.$lead_mail->subdir.'">
|
|
<span title="Antwort auf E-Mail speichern" data-placement="left" rel="tooltip"><i class="ion ion-ios-redo"></i> <i class="ion ion-md-mail-unread"></i></span>
|
|
</a>
|
|
';
|
|
}
|
|
$ret .= '<a href="'.route('lead_mail_delete', [$lead_mail->id]).'" class="btn btn-xs btn-default text-danger" onclick="return confirm(\'In den Papierkorb verschieben?\');"><i class="ion ion-md-trash"></i></a>';
|
|
return '<div style="white-space: nowrap;">'.$ret.'</div>';
|
|
})
|
|
|
|
->orderColumn('date', 'sent_at $1')
|
|
->orderColumn('subject', 'subject $1')
|
|
->orderColumn('important', 'important $1')
|
|
->rawColumns(['checkbox', 'important', 'subject', 'date', 'action'])
|
|
->make(true);
|
|
}
|
|
}
|
|
|
|
|