Compare commits

...

5 Commits

Author SHA1 Message Date
Max Ducoudré
5ccdb47e84 S 7 - Adding a release zip file 2024-08-08 11:57:46 +02:00
Max Ducoudré
610fdfdbdc S 6 - Adding the shortcode [KFT_TICKET_FORM] to create a ticket 2024-08-08 11:56:12 +02:00
Max Ducoudré
49c25ffa46 S 6 - Adding a config menu for ticket types 2024-08-08 11:55:47 +02:00
Max Ducoudré
250365c483 S 6 - Adding kalifast API route configuration 2024-08-08 11:55:21 +02:00
Max Ducoudré
c881caff99 S 6 - Setup plugin API routes 2024-08-08 11:54:23 +02:00
5 changed files with 707 additions and 0 deletions

185
config_menu.php Normal file
View File

@ -0,0 +1,185 @@
<?php
add_action('admin_menu', 'add_kft_bug_reporter_config_menu');
function add_kft_bug_reporter_config_menu()
{
add_menu_page('KFT-Bug-Reporter configuration', 'KFT-Bug-Reporter configuration', 'administrator', 'kft-bug-reporter-config', 'kft_bug_reporter_config_page');
}
function kft_bug_reporter_config_page() {
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.wrap {
margin: 20px;
}
.kft-bug-reporter-config-form {
margin-top: 20px;
}
.kft_status {
width: 25px;
height: 25px;
vertical-align: middle;
}
</style>
<script>
/* Function to request the EISGE-API-Core */
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) {
// Get the configuration of the Kalifast API
reloadFields()
document.getElementById("submit_kft_bug_reporter_ticket_config").addEventListener("click", async function() {
let response = await requestCore("Kalifast_Bug_Reporter", "Reporter", "set_kalifast_ticket_config", {
"ei_pool_id": document.getElementById("kft_bug_reporter_pool").value,
"ei_delivery_id": document.getElementById("kft_bug_reporter_delivery").value,
"ei_user_id": document.getElementById("kft_bug_reporter_user").value
});
});
// Set the configuration of the Kalifast API in the form
document.getElementById("submit_kft_bug_reporter_config").addEventListener("click", submitConfigAPIForm);
});
async function reloadFields() {
let config_data = await requestCore("Kalifast_Bug_Reporter", "Reporter", "get_kalifast_api_configuration");
if(config_data.success) {
document.getElementById("kft_bug_reporter_url").value = config_data.data.api_config.kft_api_host;
document.getElementById("kft_bug_reporter_api_key").value = config_data.data.api_config.kft_api_key;
if(config_data.data.fields.deliveries && config_data.data.fields.pools && config_data.data.fields.users) {
document.querySelector(".kft_status").src = "https://kalifast.com/assets/img/devices_enable.png";
} else {
document.querySelector(".kft_status").src = "https://kalifast.com/assets/img/devices_not_connected.png";
}
console.log(config_data);
// Set the pools
let poolSelect = document.getElementById("kft_bug_reporter_pool");
poolSelect.innerHTML = "";
config_data.data.fields.pools.forEach(pool => {
let option = document.createElement("option");
option.value = pool.ei_pool_id;
option.text = pool.pool_name;
poolSelect.appendChild(option);
});
if(config_data.data.ticket_config) {
poolSelect.value = config_data.data.ticket_config.ei_pool_id;
}
// Set the deliveries
let deliverySelect = document.getElementById("kft_bug_reporter_delivery");
deliverySelect.innerHTML = "";
config_data.data.fields.deliveries.forEach(delivery => {
let option = document.createElement("option");
option.value = delivery.ei_delivery_id;
option.text = delivery.delivery_name;
deliverySelect.appendChild(option);
});
if(config_data.data.ticket_config) {
deliverySelect.value = config_data.data.ticket_config.ei_delivery_id;
}
// Set the users
let userSelect = document.getElementById("kft_bug_reporter_user");
userSelect.innerHTML = "";
config_data.data.fields.users.forEach(user => {
let option = document.createElement("option");
option.value = user.ei_user_id;
option.text = user.username;
userSelect.appendChild(option);
});
if(config_data.data.ticket_config) {
userSelect.value = config_data.data.ticket_config.ei_user_id;
}
}
}
// Config API form behavior
async function submitConfigAPIForm() {
console.log("Form submitted");
let response = await requestCore("Kalifast_Bug_Reporter", "Reporter", "configure_kalifast_api", {
"kft_api_host": document.getElementById("kft_bug_reporter_url").value,
"kft_api_key": document.getElementById("kft_bug_reporter_api_key").value,
});
if(response.success) {
reloadFields();
}
}
</script>
<div class="wrap">
<h2>KFT-Bug-Reporter configuration</h2>
<div id="kft-bug-reporter-config-api-form" class="kft-bug-reporter-config-form" >
<h3><img class="kft_status" src="https://kalifast.com/assets/img/devices_not_connected.png" alt="Kalifast logo"> API Configuration </h3>
<table>
<tr>
<!-- URL of the Kalifast APP -->
<td>Host of the Kalifast App </td>
<td><input type="text" id="kft_bug_reporter_url" value=""></td>
</tr>
<tr>
<!-- API Key of the Kalifast APP -->
<td>API Key</td>
<td><input type="text" id="kft_bug_reporter_api_key" value=""></td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit_kft_bug_reporter_config" >
</td>
</tr>
</table>
</div>
<div id="kft-bug-reporter-config-api-form" class="kft-bug-reporter-config-form" >
<h3> Tickets configuration </h3>
<table>
<tr>
<td>Pool</td>
<td><select id="kft_bug_reporter_pool" value=""></select></td>
</tr>
<tr>
<td>Delivery</td>
<td><select id="kft_bug_reporter_delivery" value=""></select></td>
</tr>
<tr>
<td>User</td>
<td><select id="kft_bug_reporter_user" value=""></select></td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit_kft_bug_reporter_ticket_config" >
</td>
</tr>
</div>
</div>
<?php
}
Hyper Icon

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>

100
module_setup.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/*
* Plugin Name: Kalifast bug reporter
* Plugin URI: https://kalifast.com/#/home
* Description: Add a button to report a bug on the website
* Version: 1.0
* Author: EISGE
* Author URI: https://kalifast.com/#/home
*/
/* EISGE Core plugin requirement */
require_once(ABSPATH.'wp-content/plugins/EISGE-API-Core/EISGE_Module_Core.php');
require_once(ABSPATH.'wp-content/plugins/Kalifast_Bug_Reporter/Reporter.php');
/* Register the module */
function install_kalifast_bug_reporter() {
$instance = new EISGE_Module_Core();
$instance->moduleInstall(array
(
'application' => 'eisge_api',
'module' => 'Kalifast_Bug_Reporter',
'module_desc' => 'A module to let the user create KALIFAST tickets',
'libraries' => array(
array(
"lib_name" => "Reporter",
"lib_desc" => "Contains the rest functions to report a bug on the website",
),
),
'modes' => array('DISPLAY', 'EDIT')
));
init_kalifast_bug_reporter_database();
}
/* Function to init database tables */
function init_kalifast_bug_reporter_database() {
// Open new PDO
$PDO = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
// Create the table API config
$PDO->exec('CREATE TABLE IF NOT EXISTS `kalifast_bug_reporter_config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`kft_api_host` varchar(255) NOT NULL,
`kft_api_key` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)');
// ticket config
$PDO->exec('CREATE TABLE IF NOT EXISTS `kalifast_bug_reporter_config_ticket` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ei_pool_id` varchar(255) NOT NULL,
`ei_delivery_id` varchar(255) NOT NULL,
`ei_user_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)');
}
/* Function to uninstall database tables */
function uninstall_kalifast_bug_reporter_database() {
// Open new PDO
$PDO = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
// Drop the table
$PDO->exec('DROP TABLE IF EXISTS `kalifast_bug_reporter_config`');
$PDO->exec('DROP TABLE IF EXISTS `kalifast_bug_reporter_config_ticket`');
}
/* Unregister the module */
function uninstall_kalifast_bug_reporter() {
$instance = new EISGE_Module_Core();
$instance->moduleUninstall(array('application' => 'eisge_api', 'module'=>'Kalifast_Bug_Reporter'));
// uninstall_kalifast_bug_reporter_database();
}
/* Setup hooks */
add_action( 'activate_Kalifast_Bug_Reporter/module_setup.php', 'install_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');
function kft_ticket_form_shortcode() {
include dirname(__FILE__) . '/create_ticket_shortcode.php';
}
add_shortcode( 'KFT_TICKET_FORM', 'kft_ticket_form_shortcode');
Hyper Icon

Binary file not shown.

317
reporter.php Normal file
View File

@ -0,0 +1,317 @@
<?php
require_once(ABSPATH."/wp-content/plugins/EISGE-API-Core/EISGE_Library_Core.php");
/**
* Class Reporter
* This class defines routes for the Kalifast Bug Reporter
*/
class Reporter extends EISGE_Library_Core {
public function getActions(){
return array(
"post_kalifast_ticket" => array(
"mode" => "EDIT",
"guest_mod" => true,
"desc" => "Send a ticket to the Kalifast Application",
"args" => array(
array(
"name" => "title",
"type" => "string",
"description" => "Title of the ticket"
),
array(
"name" => "content",
"type" => "string",
"description" => "Content of the ticket"
),
array(
"name" => "contact",
"type" => "string",
"description" => "Contact of the ticket's author"
),
)
),
"get_kalifast_api_configuration" => array(
"mode" => "DISPLAY",
"guest_mod" => false,
"desc" => "Get the Kalifast API host and key",
"args" => "no_args"
),
"configure_kalifast_api" => array(
"mode" => "EDIT",
"guest_mod" => false,
"desc" => "Configure the Kalifast API host and key",
"args" => array(
array(
"name" => "kft_api_host",
"type" => "string",
"description" => "Host of the Kalifast App"
),
array(
"name" => "kft_api_key",
"type" => "string",
"description" => "API Key of the Kalifast App"
),
)
),
"set_kalifast_ticket_config" => array(
"mode" => "EDIT",
"guest_mod" => false,
"desc" => "Set the Kalifast ticket configuration",
"args" => array(
array(
"name" => "ei_pool_id",
"type" => "string",
"description" => "Pool ID of the Kalifast App"
),
array(
"name" => "ei_delivery_id",
"type" => "string",
"description" => "Delivery ID of the Kalifast App"
),
array(
"name" => "ei_user_id",
"type" => "string",
"description" => "User ID of the Kalifast App"
),
)
),
);
}
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
*/
public function post_kalifast_ticket(){
/* Check the parameters */
$parameters = $this->checkArguments($this->dataIn,
array(
"title" => "string",
"content" => "string",
"contact" => "string"
)
);
if (!$parameters) {
return $this->functionFailed("parameters", "Syntax error on parameters!");
}
// get the configuration
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");
}
}
public function set_kalifast_ticket_config(){
/* Check the parameters */
$parameters = $this->checkArguments($this->dataIn,
array(
"ei_pool_id" => "string",
"ei_delivery_id" => "string",
"ei_user_id" => "string"
)
);
if (!$parameters) {
return $this->functionFailed("parameters", "Syntax error on parameters!");
}
// Insert the data into the database
try {
$stmt = $this->PDO->prepare("DELETE FROM kalifast_bug_reporter_config_ticket");
$stmt = $this->PDO->prepare("INSERT INTO kalifast_bug_reporter_config_ticket (ei_pool_id, ei_delivery_id, ei_user_id) VALUES (:ei_pool_id, :ei_delivery_id, :ei_user_id)");
$stmt->bindParam(':ei_pool_id', $parameters["ei_pool_id"]);
$stmt->bindParam(':ei_delivery_id', $parameters["ei_delivery_id"]);
$stmt->bindParam(':ei_user_id', $parameters["ei_user_id"]);
$stmt->execute();
return $this->functionSuccess("Kalifast ticket data inserted into the database");
} catch (PDOException $e) {
error_log($e->getMessage());
return $this->functionFailed("database_error", "Error while inserting kalifast ticket data the database");
}
}
/**
* This function is called when the user wants to get the Kalifast API host and key
*/
public function get_kalifast_api_configuration(){
$api_config = array();
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) {
$api_config = $row;
} else {
return $this->functionFailed("no_data", "No data found in the database");
}
$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);
} catch (PDOException $e) {
error_log($e->getMessage());
return $this->functionFailed("database_error", "Error while getting kalifast API data from the database");
}
// SUBJECT/MAIN/DISPLAY/CORE/GETPOOLLIST
$url = $api_config["kft_api_host"]."/api/".$api_config["kft_api_key"]."/SUBJECT/MAIN/DISPLAY/CORE/GETPOOLLIST";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$poolList = json_decode(curl_exec($ch), true);
curl_close($ch);
if($poolList["success"]) {
$poolList["data"] = array_map(function($pool) {
return array(
"ei_pool_id" => $pool["ei_pool_id"],
"pool_name" => $pool["pool_name"]
);
}, $poolList["data"]);
$poolList = $poolList["data"];
} else {
$poolList = false;
}
return $this->functionSuccess(array(
"api_config" => $api_config,
"ticket_config" => $ticket_config,
"fields" => array(
"pools" => $this->call_kft_app("SUBJECT/MAIN/DISPLAY/CORE/GETPOOLLIST", $api_config["kft_api_key"], $api_config["kft_api_host"], array("ei_pool_id", "pool_name")),
"deliveries" => $this->call_kft_app("SUBJECT/MAIN/DISPLAY/CORE/GETDELIVERYLISTFORSUBJECT", $api_config["kft_api_key"], $api_config["kft_api_host"], array("ei_delivery_id", "delivery_name")),
"users" => $this->call_kft_app("SUBJECT/MAIN/DISPLAY/CORE/GETUSERLIST", $api_config["kft_api_key"], $api_config["kft_api_host"], array("username", "ei_user_id"))
)
));
}
/**
* This function is called when the user wants to configure the Kalifast API host and key
*/
public function configure_kalifast_api(){
/* Check the parameters */
$parameters = $this->checkArguments($this->dataIn,
array(
"kft_api_host" => "string",
"kft_api_key" => "string"
)
);
if (!$parameters) {
return $this->functionFailed("parameters", "Syntax error on parameters!");
}
try {
$stmt = $this->PDO->prepare("DELETE FROM kalifast_bug_reporter_config");
$stmt = $this->PDO->prepare("INSERT INTO kalifast_bug_reporter_config (kft_api_host, kft_api_key) VALUES (:kft_api_host, :kft_api_key)");
$stmt->bindParam(':kft_api_host', $parameters["kft_api_host"]);
$stmt->bindParam(':kft_api_key', $parameters["kft_api_key"]);
$stmt->execute();
return $this->functionSuccess("Kalifast API data inserted into the database");
} catch (PDOException $e) {
error_log($e->getMessage());
return $this->functionFailed("database_error", "Error while inserting kalifast API data the database");
}
}
}
Hyper Icon