From d84299d3ab0c54b4e3cb3fd41c30a41550ea70e2 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 20 Jun 2023 08:43:10 +0200 Subject: [PATCH] wip api integration pandora #1355 --- .../godmode/users/user_management.php | 1 - pandora_console/include/javascript/ITSM.js | 34 +++++ pandora_console/include/lib/ITSM/ITSM.php | 20 ++- pandora_console/include/lib/ITSM/Manager.php | 144 ++++++++++++++++-- .../include/styles/integriaims.css | 20 +++ .../views/ITSM/ITSMCustomFields.php | 101 ++++++++++++ .../views/ITSM/ITSMTicketEditView.php | 34 +++-- 7 files changed, 324 insertions(+), 30 deletions(-) create mode 100644 pandora_console/include/javascript/ITSM.js create mode 100644 pandora_console/views/ITSM/ITSMCustomFields.php diff --git a/pandora_console/godmode/users/user_management.php b/pandora_console/godmode/users/user_management.php index 21a05b6d02..30f783f9f7 100644 --- a/pandora_console/godmode/users/user_management.php +++ b/pandora_console/godmode/users/user_management.php @@ -811,7 +811,6 @@ if ($config['integria_enabled'] && $config['integria_user_level_conf']) { $row['control'] .= ''; $table_remote->data['integria_test'] = $row; - hd('Entra'); // echo '
'; $userManagementTable->colspan['pandoraitsm'] = 2; $userManagementTable->data['pandoraitsm'] = html_print_table($table_remote, true); diff --git a/pandora_console/include/javascript/ITSM.js b/pandora_console/include/javascript/ITSM.js new file mode 100644 index 0000000000..974af45a37 --- /dev/null +++ b/pandora_console/include/javascript/ITSM.js @@ -0,0 +1,34 @@ +/* global $ jQuery */ + +/* Function get custom fields incidences */ +// eslint-disable-next-line no-unused-vars +function getInputFieldsIncidenceType(idIncidenceType, fieldsData, ajaxUrl) { + console.log(idIncidenceType); + console.log(fieldsData); + console.log(ajaxUrl); + // Failed request handler. + var handleFail = function(jqXHR, textStatus, errorThrown) { + console.log(jqXHR, textStatus, errorThrown); + }; + + // Function which handle success case. + var handleSuccess = function(data) { + $(".object-type-fields").empty(); + $(".object-type-fields").append(data); + }; + + // Visual Console container request. + jQuery + .get( + ajaxUrl, + { + page: "operation/ITSM/itsm", + method: "getInputFieldsIncidenceType", + idIncidenceType: idIncidenceType, + fieldsData: fieldsData + }, + "html" + ) + .done(handleSuccess) + .fail(handleFail); +} diff --git a/pandora_console/include/lib/ITSM/ITSM.php b/pandora_console/include/lib/ITSM/ITSM.php index fddd63650c..8c9756ab06 100644 --- a/pandora_console/include/lib/ITSM/ITSM.php +++ b/pandora_console/include/lib/ITSM/ITSM.php @@ -45,7 +45,6 @@ class ITSM if ($this->userLevelConf === true) { $this->userBearer = $user_info['integria_user_level_pass']; } - } @@ -77,6 +76,8 @@ class ITSM $path = $this->pathAction($action, $queryParams, $id); $url = $this->url.$path; + // Debugger. + // hd($url, true); $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $url); @@ -85,15 +86,14 @@ class ITSM curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, true); - $result = curl_exec($ch); + $response = curl_exec($ch); - $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); - - $result = json_decode($result, true); + $result = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { - throw new \Exception(__('Invalid response')); + throw new \Exception(__('Invalid response').', '.$response); } + $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_status !== 200) { throw new \Exception($result['error']); } @@ -147,6 +147,14 @@ class ITSM $path = '/incidence/'.$id; break; + case 'incidenceTypeFields': + $path = '/incidencetype/'.$id.'/field/list'; + break; + + case 'incidence': + $path = '/incidence/'.$id; + break; + default: // Not posible. break; diff --git a/pandora_console/include/lib/ITSM/Manager.php b/pandora_console/include/lib/ITSM/Manager.php index 0fbe7bcd49..2431547dd8 100644 --- a/pandora_console/include/lib/ITSM/Manager.php +++ b/pandora_console/include/lib/ITSM/Manager.php @@ -26,6 +26,7 @@ class Manager public $AJAXMethods = [ 'getListTickets', 'getUserSelect', + 'getInputFieldsIncidenceType', ]; /** @@ -106,12 +107,14 @@ class Manager */ public function run() { + ui_require_css_file('integriaims'); switch ($this->operation) { case 'list': $this->showList(); break; case 'edit': + \ui_require_javascript_file('ITSM'); $this->showEdit(); break; @@ -150,19 +153,9 @@ class Manager $create_incidence = (bool) \get_parameter('create_incidence', 0); $update_incidence = (bool) \get_parameter('update_incidence', 0); $idIncidence = \get_parameter('idIncidence', 0); - $incidence = [ - 'title' => \get_parameter('title', ''), - 'idIncidenceType' => \get_parameter('idIncidenceType', 0), - 'idGroup' => \get_parameter('idGroup', 0), - 'priority' => 'LOW', - // \get_parameter('priority', 'LOW'), - 'status' => \get_parameter('status', 'NEW'), - 'idCreator' => \get_parameter('idCreator', ''), - 'owner' => \get_parameter('owner_hidden', ''), - 'resolution' => \get_parameter('resolution', null), - 'description' => \get_parameter('description', ''), - ]; + // Debugger. + // hd($_POST); $error = ''; $ITSM = new ITSM(); try { @@ -171,12 +164,47 @@ class Manager // $priorities = $ITSM->callApi('listPriorities'); $resolutions = $this->getResolutions($ITSM); $status = $this->getStatus($ITSM); + + if (empty($idIncidence) === false) { + $incidenceData = $this->getIncidence($ITSM, $idIncidence); + } } catch (\Throwable $th) { $error = $th->getMessage(); } + // TODO: END POINT priorities. + // \get_parameter('priority', 'LOW'). + $incidence = [ + 'title' => \get_parameter('title', ($incidenceData['title'] ?? '')), + 'idIncidenceType' => \get_parameter('idIncidenceType', ($incidenceData['idIncidenceType'] ?? 0)), + 'idGroup' => \get_parameter('idGroup', ($incidenceData['idGroup'] ?? 0)), + 'priority' => 'LOW', + 'status' => \get_parameter('status', ($incidenceData['status'] ?? 'NEW')), + 'idCreator' => \get_parameter('idCreator', ($incidenceData['idCreator'] ?? '')), + 'owner' => \get_parameter('owner_hidden', ''), + 'resolution' => \get_parameter('resolution', ($incidenceData['resolution'] ?? null)), + 'description' => \get_parameter('description', ($incidenceData['description'] ?? '')), + ]; + $successfullyMsg = ''; try { + if (empty($incidence['idIncidenceType']) === false + && ($create_incidence === true || $update_incidence === true) + ) { + $customFields = \get_parameter('custom-fields', []); + if (empty($customFields) === false) { + $typeFieldData = []; + foreach ($customFields as $idField => $data) { + $typeFieldData[] = [ + 'idIncidenceTypeField' => $idField, + 'data' => $data, + ]; + } + } + + $incidence['typeFieldData'] = $typeFieldData; + } + if ($create_incidence === true) { $incidence = $this->createIncidence($ITSM, $incidence); $idIncidence = $incidence['idIncidence']; @@ -325,6 +353,52 @@ class Manager } + /** + * Get Incidence. + * + * @param ITSM $ITSM Object for callApi. + * @param integer $idIncidence Incidence ID. + * + * @return array Data incidence + */ + private function getIncidence(ITSM $ITSM, int $idIncidence): array + { + $result = $ITSM->callApi( + 'incidence', + [], + [], + $idIncidence, + 'GET' + ); + + return $result; + } + + + /** + * Get fields incidence type. + * + * @param ITSM $ITSM Object for callApi. + * @param integer $idIncidenceType Incidence Type ID. + * + * @return array Fields array. + */ + private function getFieldsIncidenceType(ITSM $ITSM, int $idIncidenceType): array + { + $result = $ITSM->callApi( + 'incidenceTypeFields', + [ + 'page' => 0, + 'sizePage' => 0, + ], + [], + $idIncidenceType + ); + + return $result; + } + + /** * Draw list dashboards. * @@ -332,8 +406,6 @@ class Manager */ private function showDashboard() { - global $config; - View::render( 'ITSM/ITSMDashboardView', [ @@ -467,4 +539,48 @@ class Manager } + /** + * Get Input fields of type incidence. + * + * @return void + */ + public function getInputFieldsIncidenceType() + { + $idIncidenceType = (int) get_parameter('idIncidenceType', true); + $fieldsData = json_decode(io_safe_output(get_parameter('fieldsData', [])), true); + if (empty($fieldsData) === false) { + $fieldsData = array_reduce( + $fieldsData, + function ($carry, $user) { + $carry[$user['idIncidenceField']] = $user['data']; + return $carry; + } + ); + } else { + $fieldsData = []; + } + + $error = ''; + try { + $ITSM = new ITSM(); + $result = $this->getFieldsIncidenceType($ITSM, $idIncidenceType); + $customFields = $result['data']; + } catch (Throwable $e) { + $error = $e->getMessage(); + } + + View::render( + 'ITSM/ITSMCustomFields', + [ + 'ajaxController' => $this->ajaxController, + 'urlAjax' => \ui_get_full_url('ajax.php'), + 'customFields' => $customFields, + 'fieldsData' => $fieldsData, + 'error' => $error, + ] + ); + exit; + } + + } diff --git a/pandora_console/include/styles/integriaims.css b/pandora_console/include/styles/integriaims.css index bca6df5038..e55c992bbe 100644 --- a/pandora_console/include/styles/integriaims.css +++ b/pandora_console/include/styles/integriaims.css @@ -123,3 +123,23 @@ div.integria_details_description textarea { .comment_body { padding: 15px 20px 15px 20px; } + +/*---*/ +div.incidence-type-custom-fields { + display: flex; + flex-direction: row; + justify-content: center; + align-items: baseline; + flex-wrap: wrap; +} + +.incidence-type-custom-fields > div { + flex: 1 1 45%; + display: flex; + margin: 0px 20px; + flex-direction: column; +} + +.incidence-type-custom-fields > div.incidence-type-custom-fields-textarea { + flex: 1 1 100%; +} diff --git a/pandora_console/views/ITSM/ITSMCustomFields.php b/pandora_console/views/ITSM/ITSMCustomFields.php new file mode 100644 index 0000000000..6837ff43f4 --- /dev/null +++ b/pandora_console/views/ITSM/ITSMCustomFields.php @@ -0,0 +1,101 @@ + true, + 'message' => __('Incidence type not fields'), + ] + ); +} else { + $output = '
'; + foreach ($customFields as $field) { + $options = [ + 'name' => 'custom-fields['.$field['idIncidenceTypeField'].']', + 'required' => $field['isRequired'], + 'return' => true, + 'value' => ($fieldsData[$field['idIncidenceTypeField']] ?? null), + ]; + + $class = ''; + + switch ($field['type']) { + case 'COMBO': + $options['type'] = 'select'; + $options['fields'] = explode(',', $field['comboValue']); + break; + + case 'TEXT': + $options['type'] = 'text'; + break; + + case 'CHECKBOX': + $options['type'] = 'checkbox'; + break; + + case 'DATE': + $options['type'] = 'text'; + break; + + case 'NUMERIC': + $options['type'] = 'number'; + break; + + case 'TEXTAREA': + $options['type'] = 'textarea'; + $options['rows'] = 4; + $options['columns'] = 0; + $class = 'incidence-type-custom-fields-textarea'; + break; + + default: + // Not posible. + break; + } + + $output .= html_print_label_input_block( + $field['label'], + html_print_input($options), + ['div_class' => $class] + ); + } + + $output .= '
'; + + echo $output; +} diff --git a/pandora_console/views/ITSM/ITSMTicketEditView.php b/pandora_console/views/ITSM/ITSMTicketEditView.php index b1f0330fe6..7ebfaa69ad 100644 --- a/pandora_console/views/ITSM/ITSMTicketEditView.php +++ b/pandora_console/views/ITSM/ITSMTicketEditView.php @@ -69,12 +69,10 @@ $table = new stdClass(); $table->width = '100%'; $table->id = 'edit-ticket-itms'; $table->class = 'databox filter-table-adv'; -$table->head = []; $table->data = []; -$table->size = []; -$table->size = []; $table->colspan[0][0] = 2; -$table->colspan[4][0] = 3; +$table->colspan[2][0] = 3; +$table->colspan[5][0] = 3; $table->colspan[6][0] = 3; $table->data[0][0] = html_print_label_input_block( @@ -158,7 +156,9 @@ $table->data[1][2] = html_print_label_input_block( ) ); -$table->data[2][0] = html_print_label_input_block( +$table->data[2][0] = '
WIP...
'; + +$table->data[3][0] = html_print_label_input_block( __('Status'), html_print_select( $status, @@ -176,7 +176,7 @@ $table->data[2][0] = html_print_label_input_block( ) ); -$table->data[2][1] = html_print_label_input_block( +$table->data[3][1] = html_print_label_input_block( __('Creator').ui_print_help_tip(__('This field corresponds to the Integria IMS user specified in Integria IMS setup'), true), html_print_input_text( 'idCreator', @@ -192,7 +192,7 @@ $table->data[2][1] = html_print_label_input_block( ) ); -$table->data[2][2] = html_print_label_input_block( +$table->data[3][2] = html_print_label_input_block( __('Owner').ui_print_help_tip(__('Type at least two characters to search the user.'), true), html_print_autocomplete_users_from_integria( 'owner', @@ -205,7 +205,7 @@ $table->data[2][2] = html_print_label_input_block( ) ); -$table->data[3][0] = '