Attendize/app/Http/Controllers/HelpDeskController.php

108 lines
2.9 KiB
PHP
Raw Normal View History

2020-04-30 08:34:51 +00:00
<?php
namespace App\Http\Controllers;
2020-04-30 12:39:58 +00:00
use App\Http\Requests\HelpTicketCommentRequest;
use App\Http\Requests\HelpTicketRequest;
2020-05-08 13:02:16 +00:00
use App\Models\BackpackUser;
2020-04-30 12:39:58 +00:00
use App\Models\HelpTicket;
use App\Models\HelpTicketComment;
2020-05-06 16:01:08 +00:00
use App\Models\User;
2020-05-08 10:54:00 +00:00
use App\Notifications\TicketCommented;
2020-05-06 14:28:06 +00:00
use App\Notifications\TicketReceived;
2020-05-01 10:54:03 +00:00
use Illuminate\Support\Facades\Log;
2020-05-06 14:28:06 +00:00
use Illuminate\Support\Facades\Notification;
2020-04-30 08:34:51 +00:00
class HelpDeskController extends Controller
{
2020-04-30 12:39:58 +00:00
public function show($code){
2020-04-30 08:34:51 +00:00
2020-04-30 12:39:58 +00:00
$ticket = HelpTicket::with('comments')
->where('code',$code)
->first();
if(!$ticket)
abort(404);
return $this->render('Pages.HelpDeskTicket',['ticket' => $ticket]);
2020-04-30 08:34:51 +00:00
}
/**
* Show the form for creating the help desk ticket
*/
public function create(){
2020-04-30 10:19:16 +00:00
return $this->render('Pages.HelpDeskCreateForm');
2020-04-30 08:34:51 +00:00
}
2020-04-30 12:39:58 +00:00
public function store(HelpTicketRequest $request){
2020-05-04 13:32:02 +00:00
2020-05-05 08:11:38 +00:00
try{
2020-05-15 14:22:22 +00:00
$ticket = new HelpTicket([
2020-05-01 10:54:03 +00:00
'name' => $request->get('name'),
'email' => $request->get('email'),
'text' => $request->get('text'),
'phone' => $request->get('phone'),
'subject' => $request->get('subject'),
2020-05-05 07:06:17 +00:00
'attachment' => $request->file('attachment')
2020-05-01 10:54:03 +00:00
]);
2020-05-06 14:28:06 +00:00
2020-05-15 14:22:22 +00:00
if($request->get('topic'))
$ticket->ticket_category_id = $request->get('topic');
$ticket->save();
2020-05-06 14:28:06 +00:00
/**
* Notify customer that ticket is received;
*/
$ticket->notify(new TicketReceived($ticket));
2020-05-08 10:54:00 +00:00
$this->notifyAdministrators(new TicketReceived($ticket)) ;
2020-05-06 14:28:06 +00:00
return redirect()->route('help.show',['code' => $ticket->code]);
2020-05-05 08:11:38 +00:00
}
catch (\Exception $exception){
Log::error($exception);
2020-05-15 14:22:22 +00:00
session()->flash(['error' => $exception->getMessage()]);
return redirect()->back();
2020-05-05 08:11:38 +00:00
}
2020-04-30 08:34:51 +00:00
}
2020-05-08 10:54:00 +00:00
/**
* Notify administrators that ticket is arrived;
*/
private function notifyAdministrators(\Illuminate\Notifications\Notification $notification){
2020-05-08 13:02:16 +00:00
$administrators = BackpackUser::where('is_admin',1)->get(['id','email']);
2020-05-08 10:54:00 +00:00
Notification::send($administrators, $notification);
}
2020-04-30 12:39:58 +00:00
public function comment(HelpTicketCommentRequest $request,$code){
2020-05-08 10:54:00 +00:00
$ticket = HelpTicket::select('id','name','email')
2020-04-30 12:39:58 +00:00
->where('code',$code)
->first();
if(!$ticket)
abort(404);
$comment = HelpTicketComment::create([
'text' => $request->text,
2020-05-08 10:54:00 +00:00
'help_ticket_id' => $ticket->id,
'attachment' => $request->file('attachment'),
'name' => $ticket->owner,
2020-04-30 12:39:58 +00:00
]);
$ticket->update(['status' => 'pending']) ;
2020-05-08 10:54:00 +00:00
$this->notifyAdministrators(new TicketCommented($comment));
2020-04-30 12:39:58 +00:00
return redirect()->route('help.show',['code' => $code]);
2020-04-30 08:34:51 +00:00
}
}