From 49c25ffa4609aa27b28b3f23fdbf673942376ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Ducoudr=C3=A9?= Date: Thu, 8 Aug 2024 11:55:47 +0200 Subject: [PATCH] S 6 - Adding a config menu for ticket types --- config_menu.php | 117 ++++++++++++++++++++++++++++++++++------ module_setup.php | 21 ++++++-- reporter.php | 136 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 249 insertions(+), 25 deletions(-) diff --git a/config_menu.php b/config_menu.php index 09ca707..74f0d26 100644 --- a/config_menu.php +++ b/config_menu.php @@ -41,17 +41,80 @@ function kft_bug_reporter_config_page() { 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"; - } + 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"); @@ -59,23 +122,21 @@ function kft_bug_reporter_config_page() { "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"; - } + reloadFields(); + } }

KFT-Bug-Reporter configuration

+

Kalifast logo API Configuration

- - + @@ -86,13 +147,39 @@ function kft_bug_reporter_config_page() {
Host of the Kalifast AppHost of the Kalifast App
- Kalifast logo +
-
+ +
+

Tickets configuration

+ + + + + + + + + + + + + + + + + + + + + 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, @@ -50,6 +50,20 @@ function init_kalifast_bug_reporter_database() { `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 */ @@ -59,13 +73,14 @@ function uninstall_kalifast_bug_reporter_database() { // 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')); + $instance->moduleUninstall(array('application' => 'eisge_api', 'module'=>'Kalifast_Bug_Reporter')); // uninstall_kalifast_bug_reporter_database(); } @@ -75,4 +90,4 @@ add_action( 'activate_Kalifast_Bug_Reporter/module_setup.php', 'install_kalifast 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 +Hyper Icon \ No newline at end of file diff --git a/reporter.php b/reporter.php index 912a53f..418e599 100644 --- a/reporter.php +++ b/reporter.php @@ -52,7 +52,29 @@ class Reporter extends EISGE_Library_Core { "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" + ), + ) + ), ); } @@ -80,25 +102,124 @@ class Reporter extends EISGE_Library_Core { } + 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"); + } + + + } + + 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 */ 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) { - return $this->functionSuccess($row); - } - return $this->functionFailed("no_data", "No data found in the database"); - + 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")) + + ) + )); } @@ -135,3 +256,4 @@ class Reporter extends EISGE_Library_Core { } } +Hyper Icon \ No newline at end of file
Pool
Delivery
User
+ +