S 6 - Adding the shortcode [KFT_TICKET_FORM] to create a ticket

This commit is contained in:
Max Ducoudré 2024-08-08 11:56:12 +02:00
parent 49c25ffa46
commit 610fdfdbdc
3 changed files with 194 additions and 24 deletions

105
create_ticket_shortcode.php Normal file
View File

@ -0,0 +1,105 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kalifast Bug Reporter</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div>
<style>
.kft_bug_reporter_config {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 50px;
}
.kft_bug_reporter_config form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.kft_bug_reporter_config form div {
margin: 10px;
}
.kft_bug_reporter_config form div label {
margin-bottom: 5px;
}
.kft_bug_reporter_config form div input, .kft_bug_reporter_config form div textarea {
width: 300px;
padding: 10px;
border-radius: 5px;
border: 1px solid #000;
}
.kft_bug_reporter_config form div button {
padding: 10px;
border-radius: 5px;
border: 1px solid #000;
background-color: #000;
color: #fff;
cursor: pointer;
}
</style>
<script>
async function requestCore(module, library, action, parameters = false) {
let form = new FormData();
form.append("JSON", JSON.stringify(parameters));
let nounce_token = "<?php echo (wp_create_nonce('wp_rest')); ?>";
let settings = {
"url": `${location.origin}/index.php/wp-json/api/eisge/eisge_api/${module}/${library}/${action}?_wpnonce=${nounce_token}`,
"method": "POST",
"timeout": 0,
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
return JSON.parse(await $.ajax(settings));
}
document.addEventListener("DOMContentLoaded", async function(event) {
document.getElementById("kft_bug_reporter_form").addEventListener("submit", async function(event) {
event.preventDefault();
let response = await requestCore("Kalifast_Bug_Reporter", "Reporter", "post_kalifast_ticket", {
"title": document.getElementById("kft_bug_reporter_title").value,
"content": document.getElementById("kft_bug_reporter_content").value,
"contact": document.getElementById("kft_bug_reporter_contact").value
});
if (response.success) {
alert("Your ticket has been created");
} else {
alert("An error occured");
}
});
});
</script>
<div class= "kft_bug_reporter_config">
<h2>Report a bug</h2>
<form id="kft_bug_reporter_form">
<div>
<label for="kft_bug_reporter_title">Title</label>
<input type="text" id="kft_bug_reporter_title" name="kft_bug_reporter_title" required>
</div>
<div>
<label for="kft_bug_reporter_contact">Contact</label>
<input type="text" id="kft_bug_reporter_contact" name="kft_bug_reporter_contact" required>
</div>
<div>
<label for="kft_bug_reporter_content">Content</label>
<textarea id="kft_bug_reporter_content" name="kft_bug_reporter_content" required></textarea>
</div>
<div>
<button type="submit">Send</button>
</div>
</form>
</div>
</div>
</body>
</html>

View File

@ -90,4 +90,11 @@ add_action( 'activate_Kalifast_Bug_Reporter/module_setup.php', 'install_kalifast
add_action( 'deactivate_Kalifast_Bug_Reporter/module_setup.php', 'uninstall_kalifast_bug_reporter' ); add_action( 'deactivate_Kalifast_Bug_Reporter/module_setup.php', 'uninstall_kalifast_bug_reporter' );
require_once(ABSPATH . '/wp-content/plugins/Kalifast_Bug_Reporter/config_menu.php'); require_once(ABSPATH . '/wp-content/plugins/Kalifast_Bug_Reporter/config_menu.php');
function kft_ticket_form_shortcode() {
include dirname(__FILE__) . '/create_ticket_shortcode.php';
}
add_shortcode( 'KFT_TICKET_FORM', 'kft_ticket_form_shortcode');
Hyper Icon Hyper Icon

View File

@ -79,6 +79,40 @@ class Reporter extends EISGE_Library_Core {
); );
} }
private function call_kft_app($url, $key, $host, $map_fields = false, $params = false) {
$url = $host."/api/".$key."/".$url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($params) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("json" => json_encode($params))));
}
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
if(!$map_fields) {
return $response;
}
if($response["success"]) {
$response["data"] = array_map(function($item) use ($map_fields) {
$new_item = array();
foreach($map_fields as $field) {
$new_item[$field] = $item[$field];
}
return $new_item;
}, $response["data"]);
return $response["data"];
} else {
return false;
}
}
/** /**
* This function is called when the user wants to send a ticket to the Kalifast Application * This function is called when the user wants to send a ticket to the Kalifast Application
@ -97,8 +131,54 @@ class Reporter extends EISGE_Library_Core {
return $this->functionFailed("parameters", "Syntax error on parameters!"); return $this->functionFailed("parameters", "Syntax error on parameters!");
} }
// get the configuration
return $this->functionFailed("not_implemented", "Not implemented yet!"); try {
$stmt = $this->PDO->prepare("SELECT kft_api_host, kft_api_key FROM kalifast_bug_reporter_config ORDER BY last_update DESC LIMIT 1");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$row) {
return $this->functionFailed("no_data", "No data found in the database");
}
$api_url = $row["kft_api_host"];
$api_key = $row["kft_api_key"];
$stmt = $this->PDO->prepare("SELECT ei_pool_id, ei_delivery_id, ei_user_id FROM kalifast_bug_reporter_config_ticket ORDER BY last_update DESC LIMIT 1");
$stmt->execute();
$ticket_config = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$ticket_config) {
return $this->functionFailed("no_data", "No data found in the database");
}
$ei_pool_id = $ticket_config["ei_pool_id"];
$ei_delivery_id = $ticket_config["ei_delivery_id"];
$ei_user_id = $ticket_config["ei_user_id"];
} catch (PDOException $e) {
error_log($e->getMessage());
return $this->functionFailed("database_error", "Error while getting kalifast API data from the database");
}
// Send the ticket to the Kalifast Application
$url = $api_url."/api/".$api_key."SUBJECT/MAIN/EDIT/CORE/CREATENEWSUBJECT";
$json_array = array(
"title" => $parameters["title"],
"description" => "Ticket from ".$parameters["contact"] . " : " .$parameters["content"],
"delivery_id" => $ei_delivery_id,
"type_id" => "1",
"priority_id" => "1",
"in_charge_id" => $ei_user_id,
"status_id" => "1",
"pool_id" => $ei_pool_id,
"ei_subject_external_id" => "",
"risk_list" => array()
);
error_log (json_encode($json_array));
$response = $this->call_kft_app("SUBJECT/MAIN/EDIT/CORE/CREATENEWSUBJECT", $api_key, $api_url, false, $json_array);
if($response["success"]) {
return $this->functionSuccess("Ticket created");
} else {
return $this->functionFailed("kft_error", "Error while sending the ticket to the Kalifast Application");
}
} }
@ -131,31 +211,9 @@ class Reporter extends EISGE_Library_Core {
error_log($e->getMessage()); error_log($e->getMessage());
return $this->functionFailed("database_error", "Error while inserting kalifast ticket data the database"); return $this->functionFailed("database_error", "Error while inserting kalifast ticket data the database");
} }
} }
private function call_kft_app($url, $key, $host, $map_fields) {
$url = $host."/api/".$key."/".$url;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
if($response["success"]) {
$response["data"] = array_map(function($item) use ($map_fields) {
$new_item = array();
foreach($map_fields as $field) {
$new_item[$field] = $item[$field];
}
return $new_item;
}, $response["data"]);
return $response["data"];
} else {
return false;
}
}
/** /**
* This function is called when the user wants to get the Kalifast API host and key * This function is called when the user wants to get the Kalifast API host and key