S 6 - Adding kalifast API route configuration

This commit is contained in:
Max Ducoudré 2024-08-08 11:55:21 +02:00
parent c881caff99
commit 250365c483
3 changed files with 251 additions and 5 deletions

98
config_menu.php Normal file
View File

@ -0,0 +1,98 @@
<?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
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.kft_api_host;
document.getElementById("kft_bug_reporter_api_key").value = config_data.data.kft_api_key;
document.querySelector(".kft_status").src = "https://kalifast.com/assets/img/devices_enable.png";
}
// Set the configuration of the Kalifast API in the form
document.getElementById("submit_kft_bug_reporter_config").addEventListener("click", submitConfigAPIForm);
});
// 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,
});
console.log(response);
if(response.success) {
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";
}
}
</script>
<div class="wrap">
<h2>KFT-Bug-Reporter configuration</h2>
<div id="kft-bug-reporter-config-api-form" class="kft-bug-reporter-config-form" >
<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" >
<img class="kft_status" src="https://kalifast.com/assets/img/devices_not_connected.png" alt="Kalifast logo">
</td>
</tr>
</table>
</div>
</div>
<?php
}

View File

@ -11,7 +11,7 @@
/* 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');
require_once(ABSPATH.'/wp-content/plugins/Kalifast_Bug_Reporter/reporter.php');
/* Register the module */
@ -21,7 +21,7 @@ function install_kalifast_bug_reporter() {
$instance->moduleInstall(array
(
'application' => 'eisge_api',
'module' => 'Kalifast-Bug-Reporter',
'module' => 'Kalifast_Bug_Reporter',
'module_desc' => 'A module to let the user create KALIFAST tickets',
'libraries' => array(
array(
@ -31,14 +31,48 @@ function install_kalifast_bug_reporter() {
),
'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
$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`)
)');
}
/* 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`');
}
/* 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' );
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');
Hyper Icon

View File

@ -12,12 +12,126 @@ class Reporter extends EISGE_Library_Core {
"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"
),
)
)
);
}
/**
* 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!");
}
return $this->functionFailed("not_implemented", "Not implemented yet!");
}
}
/**
* This function is called when the user wants to get the Kalifast API host and key
*/
public function get_kalifast_api_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->functionSuccess($row);
}
return $this->functionFailed("no_data", "No data found in the database");
} catch (PDOException $e) {
error_log($e->getMessage());
return $this->functionFailed("database_error", "Error while getting kalifast API data from the database");
}
}
/**
* 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");
}
}
}