430 lines
20 KiB
PHP
Executable file
430 lines
20 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerFewoMail;
|
|
use App\Models\CustomerFile;
|
|
use App\Models\CustomerMail;
|
|
use App\Models\EmailTemplate;
|
|
use App\Repositories\CustomerFewoMailRepository;
|
|
use App\Repositories\CustomerMailRepository;
|
|
use App\Repositories\CustomerFileRepository;
|
|
use App\Services\Util;
|
|
use Carbon;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Request;
|
|
use Response;
|
|
|
|
class CustomerMailController extends Controller
|
|
{
|
|
|
|
protected $customerMailRepo;
|
|
|
|
public function __construct(CustomerMailRepository $customerMailRepo)
|
|
{
|
|
$this->middleware('admin');
|
|
$this->customerMailRepo = $customerMailRepo;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = [
|
|
|
|
];
|
|
return view('customer.mail.index', $data);
|
|
}
|
|
|
|
public function detail($id)
|
|
{
|
|
if($id === "new") {
|
|
$customer_mail = new CustomerMail();
|
|
$id = 'new';
|
|
}else{
|
|
$customer_mail = CustomerMail::findOrFail($id);
|
|
$id = $customer_mail->id;
|
|
}
|
|
$data = [
|
|
'customer_mail' => $customer_mail,
|
|
'id' => $id,
|
|
'back' => URL::previous(),
|
|
];
|
|
return view('customer.mail.detail', $data);
|
|
|
|
}
|
|
|
|
public function store($id, $action=false)
|
|
{
|
|
$data = Request::all();
|
|
|
|
if($action === 'move-mail'){
|
|
$customer_mail = CustomerMail::findOrFail($id);
|
|
$data['subdir'] = isset($data['subdir']) && $data['subdir'] ? $data['subdir'] : null;
|
|
$customer_mail->dir = $data['dir'];
|
|
$customer_mail->subdir = $data['subdir'];
|
|
$customer_mail->save();
|
|
|
|
}
|
|
if($action === 'move-mail-fewo'){
|
|
$customer_mail = CustomerFewoMail::findOrFail($id);
|
|
$data['subdir'] = isset($data['subdir']) && $data['subdir'] ? $data['subdir'] : null;
|
|
$customer_mail->dir = $data['dir'];
|
|
$customer_mail->subdir = $data['subdir'];
|
|
$customer_mail->save();
|
|
}
|
|
|
|
if($action === 'forward_fewo-email'){
|
|
$customer_mail = CustomerFewoMail::findOrFail($id);
|
|
$customerMailFewoRepo = new CustomerFewoMailRepository($customer_mail);
|
|
$customerMailFewoRepo->forwardMail($customer_mail, $data);
|
|
\Session()->flash('alert-success', __('E-Mail weitergeleitet'));
|
|
|
|
}
|
|
|
|
if($action === 'forward_email'){
|
|
$customer_mail = CustomerMail::findOrFail($id);
|
|
$this->customerMailRepo->forwardMail($customer_mail, $data);
|
|
\Session()->flash('alert-success', __('E-Mail weitergeleitet'));
|
|
|
|
|
|
}
|
|
return back();
|
|
}
|
|
|
|
public function delete($id){
|
|
$customer_mail = CustomerMail::find($id);
|
|
$customer_mail->dir = 12;
|
|
$customer_mail->subdir = 0;
|
|
$customer_mail->save();
|
|
|
|
\Session()->flash('alert-success', __('Mail gelöscht'));
|
|
return back();
|
|
}
|
|
|
|
|
|
/* public function loadModal(){
|
|
$data = Request::all();
|
|
$ret = "";
|
|
if(Request::ajax()){
|
|
|
|
$customers = [];
|
|
$query = $this->getSearchRequests();
|
|
$bookings = $query->orderBy('id', 'DESC')->limit(50)->get();
|
|
foreach ($bookings as $booking){
|
|
$tmp = "";
|
|
$tmp .= $booking->customer ? $booking->customer->email." | " : "- | ";
|
|
$tmp .= $booking->customer ? $booking->customer->firstname." ".$booking->customer->name." | " : "- | ";
|
|
$tmp .= $booking->travel_country_id ? $booking->travel_country->name." | " : "- | ";
|
|
$tmp .= $booking->travelagenda_id ? $booking->travel_agenda->name."" : "-";
|
|
|
|
$customers[$booking->id] = $tmp;
|
|
}
|
|
|
|
// return TravelAgenda::whereIn('id', $ret)->get()->pluck('name', 'id');
|
|
|
|
if($data['action'] === "send-customer-mail"){
|
|
$value = new Collection();
|
|
$value->id = "add";
|
|
$value->customers = $customers;
|
|
$value->message = "Sehr #geehrte/r# #Anrede# #Vorname# #Nachname#,\n\nText ....";
|
|
$data['title'] = "E-Mail-Nachricht an Auswahl";
|
|
$url = route('requests_send_customer_mail');
|
|
$ret = view("customer.mail.modal-mail", compact('data','value', 'url') )->render();
|
|
}
|
|
|
|
}
|
|
return response()->json(['response' => $data, 'html'=>$ret]);
|
|
}*/
|
|
|
|
|
|
public function sendMail(CustomerMailRepository $customerMailRepository){
|
|
$data = Request::all();
|
|
$customerMailRepository->sendAndStore($data);
|
|
if($data['action'] == 'draft'){
|
|
\Session()->flash('alert-success', "Entwurf gespeichert!");
|
|
}else{
|
|
\Session()->flash('alert-success', "Mail gesendet!");
|
|
}
|
|
return back();
|
|
}
|
|
|
|
public function replyMail(CustomerMailRepository $customerMailRepository){
|
|
$data = Request::all();
|
|
$customerMailRepository->replyStore($data);
|
|
\Session()->flash('alert-success', "Mail gespeichert!");
|
|
return back();
|
|
}
|
|
|
|
public function getCustomerMails()
|
|
{
|
|
$query = CustomerMail::with('booking')->with('customer')->select('customer_mails.*');
|
|
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('action_edit', function (CustomerMail $customer_mail) {
|
|
return '<a href="'.route('customer_mail_detail', [$customer_mail->id]).'" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
|
|
})
|
|
->addColumn('id', function (CustomerMail $customer_mail) {
|
|
return '<a data-order="'.$customer_mail->id.'" href="'.route('customer_mail_detail', [$customer_mail->id]).'" data-id="'.$customer_mail->id.'">'.$customer_mail->id.'</a>';
|
|
})
|
|
->addColumn('booking', function (CustomerMail $customer_mail) {
|
|
$out = $customer_mail->booking->travel_country_id ? $customer_mail->booking->travel_country->name." | " : "- | ";
|
|
$out .= $customer_mail->booking->travelagenda_id ? $customer_mail->booking->travel_agenda->name."" : "-";
|
|
return $out;
|
|
})
|
|
->addColumn('booking_id', function (CustomerMail $customer_mail) {
|
|
return '<a data-order="'.$customer_mail->booking_id.'" href="'.route('booking_detail', [$customer_mail->booking_id]).'">'.$customer_mail->booking_id.'</a>';
|
|
})
|
|
->addColumn('customer_id', function (CustomerMail $customer_mail) {
|
|
return '<a data-order="'.$customer_mail->customer_id.'" href="'.route('customer_detail', [$customer_mail->customer_id]).'">'.$customer_mail->customer_id.'</a>';
|
|
})
|
|
->addColumn('send', function (CustomerMail $customer_mail) {
|
|
return $customer_mail->send ? '<span class="badge badge-pill badge-success"><i class="fa fa-check"></i></span>' : '<span class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span>';
|
|
})
|
|
->addColumn('date', function (CustomerMail $customer_mail) {
|
|
if($customer_mail->send){
|
|
return '<span class="badge badge-success" style="background-color: #94ae59"><i class="fa fa-check-circle"></i> '.$customer_mail->sent_at.'</span>';
|
|
}
|
|
return '<span class="badge badge-default"><i class="fa fa-times-circle"></i> '.$customer_mail->sent_at.'</span>';
|
|
})
|
|
->orderColumn('id', 'id $1')
|
|
->orderColumn('booking_id', 'booking_id $1')
|
|
->orderColumn('customer_id', 'customer_id $1')
|
|
->orderColumn('send', 'send $1')
|
|
->orderColumn('date', 'sent_at $1')
|
|
|
|
->filterColumn('id', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('id', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->filterColumn('customer_id', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('customer_id', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->filterColumn('booking_id', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('booking_id', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->rawColumns(['action_edit', 'send', 'date', 'customer_id', 'booking_id', 'id'])
|
|
->make(true);
|
|
}
|
|
|
|
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('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 CustomerFileRepository(new CustomerFile());
|
|
if($id === 'tmp'){
|
|
$fileRepo->_set('disk', 'customer');
|
|
$fileRepo->_set('dir', '/attachment/'.date('Y/m').'/');
|
|
$fileRepo->_set('customer_id', NULL);
|
|
$fileRepo->_set('customer_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()){
|
|
if($data['action'] === 'toggle_important'){
|
|
$customer_mail = CustomerMail::find($data['id']);
|
|
$customer_mail->important = ($customer_mail->important ? false : true);
|
|
$customer_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'){
|
|
$customer_file = CustomerFile::find($data['id']);
|
|
$fileRepo = new CustomerFileRepository($customer_file);
|
|
$fileRepo->_set('disk', 'customer');
|
|
$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 CustomerFileRepository(new CustomerFile());
|
|
$fileRepo->_set('disk', 'customer');
|
|
$fileRepo->_set('dir', '/attachment/'.date('Y/m').'/');
|
|
$fileRepo->_set('customer_id', NULL);
|
|
$fileRepo->_set('customer_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('booking_id')) {
|
|
return false;
|
|
}
|
|
$query = CustomerMail::where('booking_id', '=', Request::get('booking_id'));
|
|
if (Request::get('customer_mail_dir') == 11) { //draft
|
|
$query->where('draft', '=', true)->where('dir', '!=', 12);
|
|
}else{
|
|
$query->where('dir', '=', Request::get('customer_mail_dir')); //with('lead'
|
|
|
|
}
|
|
if (Request::get('customer_mail_subdir')) {
|
|
$query->where('subdir', '=', Request::get('customer_mail_subdir'));
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function getRequests(){
|
|
|
|
$query = $this->getSearchRequests();
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('checkbox', function (CustomerMail $customer_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 (CustomerMail $customer_mail) {
|
|
$icon = ($customer_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('customer_mail_ajax').'" data-id="'.$customer_mail->id.'" data-important="'.$customer_mail->important.'" data-action="toggle_important"></a>';
|
|
})
|
|
->addColumn('subject', function (CustomerMail $customer_mail) {
|
|
|
|
|
|
$icon = $customer_mail->reply_id ? '<i class="ion ion-ios-redo ui-w-20 text-center" style="opacity: 0.7"></i>' : '<i class="ion ion-ios-mail ui-w-20 text-center" style="opacity: 0.7"></i>';
|
|
$badge = $customer_mail->is_answer ? 'badge-next' : 'badge-secondary';
|
|
$badge = $customer_mail->draft ? 'badge-default' : $badge;
|
|
$to_icon = $customer_mail->draft ? '<i class="ion ion-ios-warning" style="opacity: 0.7"></i>' : '';
|
|
$action = $customer_mail->draft ? 'edit-customer-mail' : 'show-customer-mail';
|
|
$id = $customer_mail->draft ? $customer_mail->id : 'new';
|
|
$url = $customer_mail->draft ? route('customer_mail_send_mail') : '';
|
|
|
|
$deep = $customer_mail->customer_mail_deep();
|
|
for ($i=1; $i < $deep; $i++) {
|
|
$icon .= '<i class="ion ion-ios-redo ui-w-20 text-center" style="opacity: 0.7"></i>';
|
|
}
|
|
|
|
return '<a href="javascript:void(0)" class="badge '.$badge.'" data-toggle="modal"
|
|
data-target="#modals-load-content" data-id="'.$id.'" data-model="customerMail"
|
|
data-action="'.$action.'" data-url="'.$url.'" data-redirect="back"
|
|
data-customer_mail_id="'.$customer_mail->id.'" data-route="'.route('booking_modal_load').'">
|
|
'.$to_icon.''.$icon.' '.$customer_mail->subject.'
|
|
'.($customer_mail->customer_files->count() ? ' <i class="ion ion-md-attach"> <span class="badge badge-primary indicator">'.$customer_mail->customer_files->count().'</span></i>' : '');
|
|
|
|
})
|
|
->addColumn('date', function (CustomerMail $customer_mail) {
|
|
if($customer_mail->send){
|
|
return '<span class="badge badge-success" style="background-color: #94ae59"><i class="fa fa-check-circle"></i> '.$customer_mail->sent_at.'</span>';
|
|
}
|
|
return '<span class="badge badge-default"><i class="fa fa-times-circle"></i> '.$customer_mail->sent_at.'</span>';
|
|
})
|
|
->addColumn('action', function (CustomerMail $customer_mail) {
|
|
$ret = '';
|
|
if(!$customer_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="customerMail" data-action="new-customer-mail"
|
|
data-url="'.route('customer_mail_send_mail').'" data-redirect="back" data-customer_mail_id="'.$customer_mail->id.'"
|
|
data-booking_id="'.$customer_mail->booking_id.'" data-route="'.route('booking_modal_load').'" data-customer_mail_dir="'.$customer_mail->dir.'" data-customer_mail_subdir="'.$customer_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="customerMail" data-action="reply-customer-mail"
|
|
data-url="'.route('customer_mail_reply_mail').'" data-redirect="back" data-customer_mail_id="'.$customer_mail->id.'"
|
|
data-booking_id="'.$customer_mail->booking_id.'" data-route="'.route('booking_modal_load').'" data-customer_mail_dir="'.$customer_mail->dir.'" data-customer_mail_subdir="'.$customer_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('customer_mail_delete', [$customer_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>';
|
|
})
|
|
/* ->filter(function ($query) {
|
|
if (request()->has('search.value')) {
|
|
$query->where('subject', 'like', "%" . request('search.value') . "%");
|
|
}
|
|
if (request()->has('data_table_search')) {
|
|
$query->where('subject', 'like', "%" . request('data_table_search') . "%");
|
|
}
|
|
|
|
})*/
|
|
/*->filterColumn('subject', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->whereRaw("subject LIKE ?", '%'.$keyword.'%');
|
|
|
|
}
|
|
})*/
|
|
->orderColumn('date', 'sent_at $1')
|
|
->orderColumn('subject', 'subject $1')
|
|
->orderColumn('important', 'important $1')
|
|
->rawColumns(['checkbox', 'important', 'subject', 'date', 'action'])
|
|
->make(true);
|
|
}
|
|
}
|
|
|
|
|