2024-06-26 12:28:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Models\Ticket;
|
|
|
|
|
use App\Models\Comment;
|
|
|
|
|
use App\Models\Response;
|
2024-06-28 06:58:04 +00:00
|
|
|
use App\Models\Company;
|
2024-06-26 12:28:46 +00:00
|
|
|
use Carbon\Carbon;
|
2024-06-28 06:58:04 +00:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2024-06-26 12:28:46 +00:00
|
|
|
|
|
|
|
|
class TicketController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function allTickets()
|
|
|
|
|
{
|
2024-06-28 06:58:04 +00:00
|
|
|
$tickets = get_current_company_tickets();
|
2024-06-26 12:28:46 +00:00
|
|
|
return view('all-tickets', ['tickets' => $tickets]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storeResponse(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$this->validate($request, [
|
2024-06-28 06:58:04 +00:00
|
|
|
'message' => 'required' ,
|
|
|
|
|
'ticket_id' => 'required' ,
|
2024-06-26 12:28:46 +00:00
|
|
|
]);
|
|
|
|
|
|
2024-06-28 06:58:04 +00:00
|
|
|
$ticket_id = $request->ticket_id;
|
|
|
|
|
|
2024-06-26 12:28:46 +00:00
|
|
|
// Load the HTML content into DOMDocument
|
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
|
libxml_use_internal_errors(true); // Prevents HTML errors from being thrown as exceptions
|
|
|
|
|
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $request->message);
|
|
|
|
|
libxml_clear_errors();
|
|
|
|
|
|
|
|
|
|
// Get all <p> tags
|
|
|
|
|
$paragraphs = $dom->getElementsByTagName('p');
|
|
|
|
|
|
|
|
|
|
// Add classes to each <p> tag
|
|
|
|
|
foreach ($paragraphs as $paragraph) {
|
|
|
|
|
$existingClasses = $paragraph->getAttribute('class');
|
|
|
|
|
$paragraph->setAttribute('class', trim($existingClasses . ' user-message bg-light-green-color color-light'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the modified HTML
|
|
|
|
|
$messageWithClasses = $dom->saveHTML($dom->documentElement);
|
|
|
|
|
|
2024-06-28 06:58:04 +00:00
|
|
|
// create response
|
|
|
|
|
$response = createResponse($ticket_id,$messageWithClasses,1);
|
|
|
|
|
|
|
|
|
|
$ticket = Ticket::find($ticket_id);
|
|
|
|
|
$companyId = Session::get('selected_company');
|
|
|
|
|
$company = get_company('id',$companyId);
|
|
|
|
|
//Send mail to mailgun
|
|
|
|
|
|
|
|
|
|
$domain = $company->domain;
|
|
|
|
|
$from = $company->email;
|
|
|
|
|
$to = $ticket->from_email;
|
|
|
|
|
$subject = $ticket->subject;
|
|
|
|
|
$html = $request->message;
|
|
|
|
|
|
|
|
|
|
sendEmailViaMailgun($domain, $from, $to, $subject, $html);
|
2024-06-26 12:28:46 +00:00
|
|
|
|
|
|
|
|
// Return the updated response and time
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => strip_tags($response->message), // Stripping HTML tags
|
|
|
|
|
'created_at' => $response->created_at->format('h:i A') // Formatting time
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updateStatus(Request $request, $ticketId)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'status' => 'required|in:open,waiting,done',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$ticket = Ticket::find($ticketId);
|
|
|
|
|
$ticket->status = $request->status;
|
|
|
|
|
$ticket->save();
|
|
|
|
|
|
|
|
|
|
// Return a response if necessary
|
|
|
|
|
return response()->json(['message' => 'Ticket status updated successfully']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storeComment(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'ticket_id' => 'required|exists:tickets,id',
|
|
|
|
|
'comment' => 'required|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Assuming authenticated user
|
|
|
|
|
$user_id = auth()->id();
|
|
|
|
|
|
|
|
|
|
$comment = new Comment();
|
|
|
|
|
$comment->author = $user_id;
|
|
|
|
|
$comment->ticket_id = $request->ticket_id;
|
|
|
|
|
$comment->comment = $request->comment;
|
|
|
|
|
$comment->save();
|
|
|
|
|
|
|
|
|
|
return $comment;
|
|
|
|
|
}
|
2024-06-28 06:58:04 +00:00
|
|
|
|
|
|
|
|
public function deleteComment($commentId)
|
|
|
|
|
{
|
|
|
|
|
$comment = Comment::findOrFail($commentId);
|
|
|
|
|
$comment->delete();
|
|
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Comment Deleted Successfully']);
|
|
|
|
|
}
|
2024-06-26 12:28:46 +00:00
|
|
|
}
|