2020-11-18 10:23:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2020-11-25 16:05:21 +01:00
|
|
|
use App\Events\TicketStatusChanged;
|
|
|
|
|
use App\Mail\NewTicket;
|
|
|
|
|
use App\Models\Message;
|
2020-11-18 10:23:51 +01:00
|
|
|
use App\Models\Project;
|
|
|
|
|
use App\Models\Ticket;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2020-11-25 16:05:21 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2020-11-18 10:23:51 +01:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class TicketController extends Controller
|
|
|
|
|
{
|
|
|
|
|
private $status = [1 => "Nouveau", 2 => "En cours", 3 => "Traité", 4 => "Archivé"];
|
|
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
|
$tickets = Ticket::where('status', '<>', 4)->get();
|
|
|
|
|
$status = $this->status;
|
|
|
|
|
return view('home', compact('tickets', 'status'));
|
|
|
|
|
} else {
|
|
|
|
|
return view('home');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 16:05:21 +01:00
|
|
|
public function show($id) {
|
|
|
|
|
$ticket = Ticket::find($id);
|
|
|
|
|
$status = $this->status;
|
|
|
|
|
$messages = $ticket->messages()->orderBy('created_at', 'DESC')->get();
|
|
|
|
|
if (!$ticket)
|
|
|
|
|
abort(404);
|
|
|
|
|
return view('ticket.show', compact('ticket', 'status', 'messages'));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
public function create() {
|
|
|
|
|
$projects = Project::all();
|
|
|
|
|
return view('ticket.create', compact('projects'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request) {
|
2020-11-26 12:23:19 +01:00
|
|
|
$request->validate([
|
|
|
|
|
'text' => 'required|string',
|
|
|
|
|
'file_ticket' => 'mimes:jpeg,jpg,png,pdf|max:2048',
|
|
|
|
|
'file_ticket2' => 'mimes:jpeg,jpg,png,pdf|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
if (Auth::user()->role === 1) {
|
|
|
|
|
$projectId = $request->project_id;
|
|
|
|
|
} else {
|
|
|
|
|
$projectId = Auth::user()->project_id;
|
|
|
|
|
}
|
|
|
|
|
$type = $request->type;
|
|
|
|
|
$text = $request->text;
|
|
|
|
|
$fileTicket = $request->file_ticket;
|
2020-11-25 16:05:21 +01:00
|
|
|
$fileTicket2 = $request->file_ticket2;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2020-11-26 12:23:19 +01:00
|
|
|
$ticket = new Ticket();
|
|
|
|
|
$ticket->project_id = $projectId;
|
|
|
|
|
$ticket->type = $type;
|
|
|
|
|
$ticket->text = $text;
|
|
|
|
|
$ticket->created_by = Auth::user()->id;
|
|
|
|
|
$ticket->save();
|
2020-11-25 16:05:21 +01:00
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
if ($fileTicket !== null) {
|
|
|
|
|
$projectSlug = Str::slug(Project::find($projectId)->name);
|
2020-11-26 12:23:19 +01:00
|
|
|
$fileName = $projectSlug . "-" . ($type === "0" ? "bug" : "evo") . "-" . $ticket->id . "." . $fileTicket->extension();
|
|
|
|
|
$fileTicket->move(public_path('fichiers/' . $projectSlug . "/" . $ticket->id . "/"), $fileName);
|
|
|
|
|
$filePath = 'fichiers/' . $projectSlug . "/" . $ticket->id . "/" . $fileName;
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
|
|
|
|
if ($fileTicket2 !== null) {
|
|
|
|
|
$projectSlug = Str::slug(Project::find($projectId)->name);
|
2020-11-26 12:23:19 +01:00
|
|
|
$fileName2 = $projectSlug . "-" . ($type === "0" ? "bug" : "evo") . "-" . $ticket->id . "-2." . $fileTicket2->extension();
|
|
|
|
|
$fileTicket2->move(public_path('fichiers/' . $projectSlug . "/" . $ticket->id . "/"), $fileName2);
|
|
|
|
|
$filePath2 = 'fichiers/' . $projectSlug . "/" . $ticket->id . "/" . $fileName2;
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-26 12:23:19 +01:00
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
if ($fileTicket !== null)
|
2020-11-25 16:05:21 +01:00
|
|
|
$ticket->file = $filePath;
|
2020-11-26 12:23:19 +01:00
|
|
|
|
2020-11-25 16:05:21 +01:00
|
|
|
if ($fileTicket2 !== null)
|
|
|
|
|
$ticket->file2 = $filePath2;
|
2020-11-26 12:23:19 +01:00
|
|
|
|
|
|
|
|
if ($fileTicket !== null || $fileTicket2 !== null)
|
|
|
|
|
$ticket->save();
|
|
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2020-11-25 16:05:21 +01:00
|
|
|
Mail::to(config('mail.MAIL_ADMIN'))->send(new NewTicket($ticket));
|
|
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
return redirect()->route('home')->with('success', 'Ticket enregistré !');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit($id) {
|
|
|
|
|
$ticket = Ticket::findOrFail($id);
|
|
|
|
|
if ($ticket->created_by === Auth::user()->id || Auth::user()->role === 1) {
|
|
|
|
|
$projects = Project::all();
|
|
|
|
|
$status = $this->status;
|
|
|
|
|
return view('ticket.edit', compact('ticket', 'projects', 'status'));
|
|
|
|
|
}
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, $id) {
|
2020-11-26 12:23:19 +01:00
|
|
|
$request->validate([
|
|
|
|
|
'text' => 'required|string',
|
|
|
|
|
'file_ticket' => 'mimes:jpeg,jpg,png,pdf|max:2048',
|
|
|
|
|
'file_ticket2' => 'mimes:jpeg,jpg,png,pdf|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
|
2020-11-18 10:23:51 +01:00
|
|
|
if (Auth::user()->role === 1) {
|
|
|
|
|
$projectId = $request->project_id;
|
|
|
|
|
$status = $request->status;
|
|
|
|
|
} else {
|
|
|
|
|
$projectId = Auth::user()->project_id;
|
|
|
|
|
}
|
|
|
|
|
$type = $request->type;
|
|
|
|
|
$text = $request->text;
|
|
|
|
|
$fileTicket = $request->file_ticket;
|
2020-11-25 16:05:21 +01:00
|
|
|
$fileTicket2 = $request->file_ticket2;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
|
|
|
|
$ticket = Ticket::findOrFail($id);
|
|
|
|
|
|
|
|
|
|
if ($fileTicket !== null) {
|
2020-11-25 16:05:21 +01:00
|
|
|
if ($ticket->file !== null && file_exists(public_path() . $ticket->file))
|
|
|
|
|
unlink(public_path() . $ticket->file);
|
2020-11-18 10:23:51 +01:00
|
|
|
$projectSlug = Str::slug(Project::find($projectId)->name);
|
2020-11-25 16:05:21 +01:00
|
|
|
$fileName = $projectSlug . "-" . ($type === "0" ? "bug" : "evo") . "-" . $ticket->id . "." . $fileTicket->extension();
|
2020-11-18 10:23:51 +01:00
|
|
|
$fileTicket->move(public_path('fichiers/' . $projectSlug . "/" . $ticket->id . "/"), $fileName);
|
2020-11-25 16:05:21 +01:00
|
|
|
$filePath = 'fichiers/' . $projectSlug . "/" . $ticket->id . "/" . $fileName;
|
|
|
|
|
}
|
|
|
|
|
if ($fileTicket2 !== null) {
|
|
|
|
|
if ($ticket->file2 !== null && file_exists(public_path() . $ticket->file2))
|
|
|
|
|
unlink(public_path() . $ticket->file2);
|
|
|
|
|
$projectSlug = Str::slug(Project::find($projectId)->name);
|
|
|
|
|
$fileName2 = $projectSlug . "-" . ($type === "0" ? "bug" : "evo") . "-" . $ticket->id . "." . $fileTicket2->extension();
|
|
|
|
|
$fileTicket2->move(public_path('fichiers/' . $projectSlug . "/" . $ticket->id . "/"), $fileName2);
|
|
|
|
|
$filePath2 = 'fichiers/' . $projectSlug . "/" . $ticket->id . "/" . $fileName2;
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ticket->project_id = $projectId;
|
|
|
|
|
$ticket->type = $type;
|
|
|
|
|
$ticket->text = $text;
|
|
|
|
|
if ($fileTicket !== null)
|
2020-11-25 16:05:21 +01:00
|
|
|
$ticket->file = $filePath;
|
|
|
|
|
if ($fileTicket2 !== null)
|
|
|
|
|
$ticket->file2 = $filePath2;
|
|
|
|
|
if (Auth::user()->role === 1) {
|
2020-11-18 10:23:51 +01:00
|
|
|
$ticket->status = $status;
|
2020-11-25 16:05:21 +01:00
|
|
|
event(new TicketStatusChanged($ticket, $this->status));
|
|
|
|
|
}
|
2020-11-18 10:23:51 +01:00
|
|
|
|
|
|
|
|
$ticket->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('edit-ticket', $ticket->id)->with('success', 'Modifications enregistrées !');
|
|
|
|
|
}
|
2020-11-25 16:05:21 +01:00
|
|
|
|
|
|
|
|
public function changeStatus(Request $request, $id) {
|
|
|
|
|
$ticket = Ticket::find($id);
|
|
|
|
|
if (!$ticket)
|
|
|
|
|
abort(404);
|
|
|
|
|
$ticket->status = $request->status;
|
|
|
|
|
$ticket->save();
|
|
|
|
|
event(new TicketStatusChanged($ticket, $this->status));
|
|
|
|
|
|
|
|
|
|
return back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createMessage(Request $request) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'message' => 'required',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($request->file_message !== null) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'file_message' => 'mimes:jpeg,jpg,png,pdf|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ticketId = $request->ticket_id;
|
|
|
|
|
$fileMessage = $request->file_message;
|
|
|
|
|
|
|
|
|
|
$ticket = Ticket::find($ticketId);
|
|
|
|
|
|
|
|
|
|
if (Auth::user()->role !== 1 && (!$ticket || Auth::user()->id !== $ticket->created_by))
|
|
|
|
|
return back()->withErrors("Les données entrées sont incorrects.");
|
|
|
|
|
|
|
|
|
|
$message = new Message();
|
|
|
|
|
$message->ticket_id = $ticketId;
|
|
|
|
|
$message->user_id = Auth::user()->id;
|
|
|
|
|
$message->message = $request->message;
|
|
|
|
|
$message->save();
|
|
|
|
|
|
|
|
|
|
if ($request->file_message !== null) {
|
|
|
|
|
$projectSlug = Str::slug($ticket->project->name);
|
|
|
|
|
$fileMessage = $request->file_message;
|
|
|
|
|
$fileName = $projectSlug . '-ticket-' . $ticket->id . '-message-' . $message->id . '.' . $fileMessage->extension();
|
|
|
|
|
$fileMessage->move(public_path('fichiers/' . $projectSlug . '/' . $ticket->id . '/'), $fileName);
|
|
|
|
|
$filePath = 'fichiers/' . $projectSlug . '/' . $ticket->id . '/' . $fileName;
|
|
|
|
|
$message->file = $filePath;
|
|
|
|
|
$message->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return back();
|
|
|
|
|
}
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|