From cdf4118b09d8e00688cf18f6d264d550dc6b4d21 Mon Sep 17 00:00:00 2001 From: o-psi Date: Thu, 15 Feb 2024 16:31:35 +0000 Subject: [PATCH] Allow canceling scheduled tickets --- functions.php | 30 ++++++++ post/ticket.php | 135 +++++++++++++++++++++++++++++++++ ticket_edit_schedule_modal.php | 5 ++ 3 files changed, 170 insertions(+) diff --git a/functions.php b/functions.php index 13459581..31c7e575 100644 --- a/functions.php +++ b/functions.php @@ -1091,3 +1091,33 @@ function isMobile() // Check if the user agent is a mobile device return preg_match('/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|opera mini|palm|phone|pie|tablet|up.browser|up.link|webos|wos)/i', $_SERVER['HTTP_USER_AGENT']); } + +function createiCalStrCancel($originaliCalStr) { + require_once "plugins/zapcal/zapcallib.php"; + + // Import the original iCal string + $cal_event = new ZCiCal($originaliCalStr); + + // Iterate through the iCalendar object to find VEVENT nodes + foreach($cal_event->tree->child as $node) { + if($node->getName() == "VEVENT") { + // Check if STATUS node exists, update it, or add a new one + $statusFound = false; + foreach($node->data as $key => $value) { + if($key == "STATUS") { + $value->setValue("CANCELLED"); + $statusFound = true; + break; // Exit the loop once the STATUS is updated + } + } + // If STATUS node is not found, add a new STATUS node + if (!$statusFound) { + $node->addNode(new ZCiCalDataNode("STATUS:CANCELLED")); + } + } + } + + // Return the modified iCal string + return $cal_event->export(); +} + diff --git a/post/ticket.php b/post/ticket.php index 260004b3..e006319f 100644 --- a/post/ticket.php +++ b/post/ticket.php @@ -1632,3 +1632,138 @@ if (isset($_POST['edit_ticket_schedule'])) { exit; } + +if (isset($_GET['cancel_ticket_schedule'])) { + + validateTechRole(); + + $ticket_id = intval($_GET['cancel_ticket_schedule']); + + $sql = mysqli_query($mysqli, "SELECT * FROM tickets WHERE ticket_id = $ticket_id"); + $row = mysqli_fetch_array($sql); + + $client_id = intval($row['ticket_client_id']); + $ticket_prefix = sanitizeInput($row['ticket_prefix']); + $ticket_number = intval($row['ticket_number']); + $ticket_subject = sanitizeInput($row['ticket_subject']); + $ticket_schedule = sanitizeInput($row['ticket_schedule']); + $ticket_cal_str = sanitizeInput($row['ticket_cal_str']); + + mysqli_query($mysqli, "UPDATE tickets SET ticket_schedule = NULL, ticket_status = 'Open' WHERE ticket_id = $ticket_id"); + + //Create iCal event + $cal_str = createiCalStrCancel($ticket_cal_str); + + //Send emails + + $sql = mysqli_query($mysqli, "SELECT * FROM tickets + LEFT JOIN clients ON ticket_client_id = client_id + LEFT JOIN contacts ON ticket_contact_id = contact_id + LEFT JOIN locations on contact_location_id = location_id + LEFT JOIN users ON ticket_assigned_to = user_id + WHERE ticket_id = $ticket_id + "); + $row = mysqli_fetch_array($sql); + + $client_id = intval($row['ticket_client_id']); + $client_name = sanitizeInput($row['client_name']); + $ticket_details = sanitizeInput($row['ticket_details']); + $contact_name = sanitizeInput($row['contact_name']); + $contact_email = sanitizeInput($row['contact_email']); + $ticket_prefix = sanitizeInput($row['ticket_prefix']); + $ticket_number = intval($row['ticket_number']); + $ticket_subject = sanitizeInput($row['ticket_subject']); + $user_name = sanitizeInput($row['user_name']); + $user_email = sanitizeInput($row['user_email']); + + $data = [ + [ //Client Contact Email + 'from' => $config_ticket_from_email, + 'from_name' => $config_ticket_from_name, + 'recipient' => $contact_email, + 'recipient_name' => $contact_name, + 'subject' => "Ticket Schedule Cancelled - [$ticket_prefix$ticket_number] - $ticket_subject", + 'body' => mysqli_escape_string($mysqli, "
+ Hello, $contact_name +
+ Your ticket regarding $ticket_subject has been cancelled. +

+ Access your ticket here +

+ Please do not reply to this email. +

+ Ticket: $ticket_prefix$ticket_number
+ Subject: $ticket_subject
+

+ +
+ This is an automated message. Please do not reply directly to this email. +
"), + 'cal_str' => $cal_str + ], + [ + // User Email + 'from' => $config_ticket_from_email, + 'from_name' => $config_ticket_from_name, + 'recipient' => $user_email, + 'recipient_name' => $user_name, + 'subject' => "Ticket Schedule Cancelled - [$ticket_prefix$ticket_number] - $ticket_subject", + 'body' => "Hello, " . $user_name . "

The ticket regarding $ticket_subject has been cancelled.

--------------------------------
$ticket_link
--------------------------------

Please do not reply to this email.

Ticket: $ticket_prefix$ticket_number
Subject: $ticket_subject
Portal: https://$config_base_url/ticket.php?id=$ticket_id

~
$session_company_name
Support Department
$config_ticket_from_email", + 'cal_str' => $cal_str + ] + ]; + + //Send all watchers an email + $sql_watchers = mysqli_query($mysqli, "SELECT watcher_email FROM ticket_watchers WHERE watcher_ticket_id = $ticket_id"); + while ($row = mysqli_fetch_assoc($sql_watchers)) { + $watcher_email = sanitizeInput($row['watcher_email']); + $data[] = [ + 'from' => $config_ticket_from_email, + 'from_name' => $config_ticket_from_name, + 'recipient' => $watcher_email, + 'recipient_name' => $watcher_email, + 'subject' => "Ticket Schedule Cancelled - [$ticket_prefix$ticket_number] - $ticket_subject", + 'body' => mysqli_escape_string($mysqli, nullable_htmlentities("
+ Hello, +
+ Your ticket regarding $ticket_subject has been cancelled. +

+ $ticket_link +

+ Please do not reply to this email. +

+ Ticket: $ticket_prefix$ticket_number
+ Subject: $ticket_subject
+ Portal: Access your ticket here +

+ +
+ This is an automated message. Please do not reply directly to this email. +
")), + 'cal_str' => $cal_str + ]; + } + + $response = addToMailQueue($mysqli, $data); + + // Update ticket reply + $ticket_reply_note = "Ticket schedule cancelled."; + mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '$ticket_reply_note', ticket_reply_type = 'Internal', ticket_reply_time_worked = '00:01:00', ticket_reply_by = $session_user_id, ticket_reply_ticket_id = $ticket_id"); + + //Logging + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Ticket', log_action = 'Modify', log_description = '$session_name cancelled ticket schedule', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $ticket_id"); + + $_SESSION['alert_message'] = "Ticket schedule cancelled"; + + header("Location: " . $_SERVER["HTTP_REFERER"]); +} \ No newline at end of file diff --git a/ticket_edit_schedule_modal.php b/ticket_edit_schedule_modal.php index d297423f..d0d96e32 100644 --- a/ticket_edit_schedule_modal.php +++ b/ticket_edit_schedule_modal.php @@ -34,6 +34,11 @@