Kalifast_Bug_Reporter/reporter.php

317 lines
12 KiB
PHP

<?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