operation = \get_parameter('operation', 0); // Urls. $this->url = \ui_get_full_url( 'index.php?sec=reporting&sec2=operation/dashboard/dashboard' ); $this->ajaxController = $page; } /** * Checks if target method is available to be called using AJAX. * * @param string $method Target method. * * @return boolean True allowed, false not. */ public function ajaxMethod(string $method): bool { return in_array($method, $this->AJAXMethods); } /** * Prints error. * * @param string $msg Message. * * @return void */ public function error(string $msg) { if ((bool) \is_ajax() === true) { echo json_encode(['error' => $msg]); } else { \ui_print_error_message($msg); } } /** * Init manager ITSM. * * @return void */ public function run() { \ui_require_css_file('pandoraitsm'); \ui_require_javascript_file('ITSM'); switch ($this->operation) { case 'list': $this->showList(); break; case 'edit': $this->showEdit(); break; case 'detail': $this->showDetail(); break; case 'dashboard': default: $this->showDashboard(); break; } } /** * Draw list tickets. * * @return void */ private function showList() { $idIncidence = \get_parameter('idIncidence', 0); $error = ''; $successfullyMsg = ''; $groups = []; $status = []; $priorities = []; $headerTabs = $this->headersTabs('list'); try { $ITSM = new ITSM(); $groups = $ITSM->getGroups(); $status = $ITSM->getStatus(); $priorities = $ITSM->getPriorities(); if (empty($idIncidence) === false) { $this->deleteIncidence($ITSM, $idIncidence); $successfullyMsg = __('Delete ticket successfully'); } } catch (\Throwable $th) { $error = $th->getMessage(); } View::render( 'ITSM/ITSMTicketListView', [ 'ajaxController' => $this->ajaxController, 'urlAjax' => \ui_get_full_url('ajax.php'), 'error' => $error, 'successfullyMsg' => $successfullyMsg, 'groups' => $groups, 'status' => $status, 'priorities' => $priorities, 'headerTabs' => $headerTabs, ] ); } /** * Draw list tickets. * * @return void */ private function showEdit() { global $config; $create_incidence = (bool) \get_parameter('create_incidence', 0); $update_incidence = (bool) \get_parameter('update_incidence', 0); $idIncidence = (int) \get_parameter('idIncidence', 0); $idEvent = (int) \get_parameter('from_event', 0); $headerTabs = $this->headersTabs('edit', $idIncidence); $error = ''; try { $ITSM = new ITSM(); $objectTypes = $ITSM->getObjectypes(); $groups = $ITSM->getGroups(); $priorities = $ITSM->getPriorities(); $resolutions = $this->getResolutions($ITSM); $status = $ITSM->getStatus(); if (empty($idIncidence) === false) { $incidenceData = $this->getIncidence($ITSM, $idIncidence); } } catch (\Throwable $th) { $error = $th->getMessage(); } $default_values = [ 'title' => '', 'idIncidenceType' => 0, 'idGroup' => 0, 'priority' => 'LOW', 'status' => 'NEW', 'idCreator' => '', 'owner' => '', 'resolution' => null, 'description' => '', ]; if (empty($idEvent) === false) { $default_values = [ 'title' => $config['cr_incident_title'], 'idIncidenceType' => $config['cr_incident_type'], 'idGroup' => $config['cr_default_group'], 'priority' => $config['cr_default_criticity'], 'status' => $config['cr_incident_status'], 'idCreator' => '', 'owner' => $config['cr_default_owner'], 'resolution' => null, 'description' => $config['cr_incident_content'], ]; } $incidence = [ 'title' => \get_parameter('title', ($incidenceData['title'] ?? $default_values['title'])), 'idIncidenceType' => \get_parameter('idIncidenceType', ($incidenceData['idIncidenceType'] ?? $default_values['idIncidenceType'])), 'idGroup' => \get_parameter('idGroup', ($incidenceData['idGroup'] ?? $default_values['idGroup'])), 'priority' => \get_parameter('priority', ($incidenceData['priority'] ?? $default_values['priority'])), 'status' => \get_parameter('status', ($incidenceData['status'] ?? $default_values['status'])), 'idCreator' => \get_parameter('idCreator', ($incidenceData['idCreator'] ?? $default_values['idCreator'])), 'owner' => \get_parameter('owner_hidden', ($incidenceData['owner'] ?? $default_values['owner'])), 'resolution' => \get_parameter('resolution', ($incidenceData['resolution'] ?? $default_values['resolution'])), 'description' => \get_parameter('description', ($incidenceData['description'] ?? $default_values['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; } else { $incidence['typeFieldData'] = $incidenceData['typeFieldData']; } if ($create_incidence === true) { $incidence = $this->createIncidence($ITSM, $incidence); $idIncidence = $incidence['idIncidence']; $successfullyMsg = __('Successfully create ticket'); } if ($update_incidence === true) { $incidence = $this->updateIncidence($ITSM, $incidence, $idIncidence); $successfullyMsg = __('Successfully update ticket'); } } catch (\Throwable $th) { $error = $th->getMessage(); } View::render( 'ITSM/ITSMTicketEditView', [ 'ajaxController' => $this->ajaxController, 'urlAjax' => \ui_get_full_url('ajax.php'), 'objectTypes' => $objectTypes, 'groups' => $groups, 'priorities' => [], 'resolutions' => $resolutions, 'status' => $status, 'priorities' => $priorities, 'error' => $error, 'incidence' => $incidence, 'idIncidence' => $idIncidence, 'successfullyMsg' => $successfullyMsg, 'headerTabs' => $headerTabs, ] ); } /** * Draw list tickets. * * @return void */ private function showDetail() { $idIncidence = (int) \get_parameter('idIncidence', 0); $uploadFile = (bool) \get_parameter('upload_file', 0); $idAttachment = (int) \get_parameter('idAttachment', 0); $addComment = (bool) \get_parameter('addComment', 0); $headerTabs = $this->headersTabs('detail', $idIncidence); $error = ''; $error_upload = ''; $error_comment = ''; $error_delete_attachment = ''; $successfullyMsg = null; $incidence = null; $objectTypes = null; $groups = null; $resolutions = null; $status = null; $wus = null; $files = null; $users = null; $priorities = null; $priorityDiv = null; $inventories = null; $ITSM = new ITSM(); try { if ($uploadFile === true) { $attachment = [ 'description' => get_parameter('file_description', ''), ]; $incidenceAttachment = $this->createIncidenceAttachment( $ITSM, $idIncidence, $attachment, get_parameter('userfile') ); if ($incidenceAttachment !== false) { $successfullyMsg = __('File added succesfully'); } } } catch (\Exception $e) { hd($e->getMessage()); $error_upload = $e->getMessage(); } try { if ($addComment === true) { $wu = [ 'description' => get_parameter('comment_description', ''), ]; $incidenceAttachment = $this->createIncidenceWu( $ITSM, $idIncidence, $wu ); if ($incidenceAttachment !== false) { $successfullyMsg = __('Comment added succesfully'); } } } catch (\Exception $e) { $error_comment = $e->getMessage(); } try { if (empty($idAttachment) === false) { $this->deleteIncidenceAttachment($ITSM, $idIncidence, $idAttachment); $successfullyMsg = __('Delete File successfully'); } } catch (\Exception $e) { $error_delete_attachment = $e->getMessage(); } try { if (empty($idIncidence) === false) { $incidence = $this->getIncidence($ITSM, $idIncidence); $objectTypes = $ITSM->getObjectypes(); $groups = $ITSM->getGroups(); $resolutions = $this->getResolutions($ITSM); $status = $ITSM->getStatus(); $priorities = $ITSM->getPriorities(); $wus = $this->getIncidenceWus($ITSM, $idIncidence); $files = $this->getIncidenceFiles($ITSM, $idIncidence); $usersInvolved = []; $usersInvolved[$incidence['idCreator']] = $incidence['idCreator']; $usersInvolved[$incidence['owner']] = $incidence['owner']; $usersInvolved[$incidence['closedBy']] = $incidence['closedBy']; foreach ($wus['data'] as $wu) { $usersInvolved[$wu['idUser']] = $wu['idUser']; } foreach ($files['data'] as $file) { $usersInvolved[$file['idUser']] = $file['idUser']; } $users = $this->getUsers($ITSM, $usersInvolved); $inventories = []; $priorityDiv = $this->priorityDiv($incidence['priority'], $priorities[$incidence['priority']]); if (empty($incidence) === false && isset($incidence['inventories']) === true && empty($incidence['inventories']) === false ) { foreach ($incidence['inventories'] as $inventory) { $inventories[] = $this->getInventory($ITSM, $inventory['idInventory']); } } } } catch (\Exception $e) { $error = $e->getMessage(); } View::render( 'ITSM/ITSMTicketDetailView', [ 'ajaxController' => $this->ajaxController, 'urlAjax' => \ui_get_full_url('ajax.php'), 'error' => $error, 'error_upload' => $error_upload, 'error_comment' => $error_comment, 'error_delete_attachment' => $error_delete_attachment, 'successfullyMsg' => $successfullyMsg, 'incidence' => $incidence, 'objectTypes' => $objectTypes, 'groups' => $groups, 'resolutions' => $resolutions, 'status' => $status, 'wus' => $wus, 'files' => $files, 'users' => $users, 'priorities' => $priorities, 'priorityDiv' => $priorityDiv, 'headerTabs' => $headerTabs, 'inventories' => $inventories, ] ); } /** * Draw list dashboards. * * @return void */ private function showDashboard() { $error = ''; $headerTabs = $this->headersTabs('dashboard'); try { $ITSM = new ITSM(); $status = $ITSM->getStatus(); $incidencesByStatus = $this->getIncidencesGroupedByStatus($ITSM, $status); $priorities = $ITSM->getPriorities(); $incidencesByPriorities = $this->getIncidencesGroupedByPriorities($ITSM, $priorities); $incidencesByGroups = $this->getIncidencesGroupedByGroups($ITSM); $incidencesByOwners = $this->getIncidencesGroupedByOwners($ITSM); } catch (\Throwable $th) { $error = $th->getMessage(); } View::render( 'ITSM/ITSMDashboardView', [ 'ajaxController' => $this->ajaxController, 'urlAjax' => \ui_get_full_url('ajax.php'), 'incidencesByStatus' => $incidencesByStatus, 'incidencesByPriorities' => $incidencesByPriorities, 'incidencesByGroups' => $incidencesByGroups, 'incidencesByOwners' => $incidencesByOwners, 'error' => $error, 'headerTabs' => $headerTabs, ] ); } /** * Get Resolutions. * * @param ITSM $ITSM Object for callApi. * * @return array Return mode select. */ private function getResolutions(ITSM $ITSM): array { $listResolutions = $ITSM->callApi('listResolutions'); $result = []; foreach ($listResolutions['data'] as $resolution) { $result[$resolution['idIncidenceResolution']] = $resolution['name']; } return $result; } /** * Create incidence * * @param ITSM $ITSM Object for callApi. * @param array $incidence Params insert. * * @return array */ private function createIncidence(ITSM $ITSM, array $incidence): array { $incidence = $ITSM->callApi('createIncidence', null, $incidence); return $incidence; } /** * Update incidence * * @param ITSM $ITSM Object for callApi. * @param array $incidence Params insert. * @param integer $idIncidence Id incidence. * * @return array */ private function updateIncidence(ITSM $ITSM, array $incidence, int $idIncidence): array { $incidence = $ITSM->callApi( 'updateIncidence', null, $incidence, $idIncidence, 'PUT' ); return $incidence; } /** * 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; } /** * Delete Incidence. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Incidence ID. * * @return void */ private function deleteIncidence(ITSM $ITSM, int $idIncidence): void { $ITSM->callApi( 'deleteIncidence', [], [], $idIncidence, 'DELETE' ); } /** * 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, 'field' => 'idIncidenceTypeField', 'direction' => 'ascending', ], [], $idIncidenceType ); return $result; } /** * Get Incidence Work units. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Incidence ID. * * @return array Data workUnits incidence. */ private function getIncidenceWus(ITSM $ITSM, int $idIncidence): array { $result = $ITSM->callApi( 'incidenceWus', [ 'page' => 0, 'sizePage' => 0, ], [], $idIncidence ); return $result; } /** * Get Incidence Attachments. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Incidence ID. * * @return array Data attachment incidence. */ private function getIncidenceFiles(ITSM $ITSM, int $idIncidence): array { $result = $ITSM->callApi( 'incidenceFiles', [ 'page' => 0, 'sizePage' => 0, ], [], $idIncidence ); return $result; } /** * Get Users. * * @param ITSM $ITSM Object for callApi. * @param array $users Users ID. * * @return array Users. */ private function getUsers(ITSM $ITSM, array $users): array { $result = $ITSM->callApi( 'listUsers', [ 'page' => 0, 'sizePage' => 0, ], [ 'multipleSearchString' => [ 'field' => 'idUser', 'data' => $users, ], ] ); $res = []; if (empty($result['data']) === false) { $res = array_reduce( $result['data'], function ($carry, $user) { $carry[$user['idUser']] = [ 'fullName' => $user['fullName'], 'idCompany' => $user['idCompany'], ]; return $carry; } ); } return $res; } /** * Get Companies. * * @param ITSM $ITSM Object for callApi. * @param array $companies Companies ID. * * @return array Companies. */ private function getCompanies(ITSM $ITSM, array $companies): array { $result = $ITSM->callApi( 'listCompanies', [ 'page' => 0, 'sizePage' => 0, ], [ 'multipleSearchString' => [ 'field' => 'idCompany', 'data' => $companies, ], ] ); $res = []; if (empty($result['data']) === false) { $res = array_reduce( $result['data'], function ($carry, $company) { $carry[$company['idCompany']] = $company['name']; return $carry; } ); } return $res; } /** * Create incidence Attachment. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Id incidence. * @param array $attachment Params insert. * @param array $file Info file. * * @return array */ private function createIncidenceAttachment(ITSM $ITSM, int $idIncidence, array $attachment, array $file): array { $incidenceAttachment = $ITSM->callApi( 'createIncidenceAttachment', null, $attachment, $idIncidence, 'POST', $file ); return $incidenceAttachment; } /** * Create incidence Wu. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Id incidence. * @param array $wu Params insert. * * @return array */ private function createIncidenceWu(ITSM $ITSM, int $idIncidence, array $wu): array { $incidenceWu = $ITSM->callApi( 'createIncidenceWu', null, $wu, $idIncidence, 'POST' ); return $incidenceWu; } /** * Delete Attachment. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Incidence ID. * @param integer $idAttachment Attachment ID. * * @return void */ private function deleteIncidenceAttachment(ITSM $ITSM, int $idIncidence, int $idAttachment): void { $ITSM->callApi( 'deleteIncidenceAttachment', [], [], [ 'idIncidence' => $idIncidence, 'idAttachment' => $idAttachment, ], 'DELETE' ); } /** * Download Attachment. * * @param ITSM $ITSM Object for callApi. * @param integer $idIncidence Incidence ID. * @param integer $idAttachment Attachment ID. * * @return mixed */ private function downloadIncidenceAttachment(ITSM $ITSM, int $idIncidence, int $idAttachment) { return $ITSM->callApi( 'downloadIncidenceAttachment', [], [], [ 'idIncidence' => $idIncidence, 'idAttachment' => $idAttachment, ], 'GET', null, true ); } /** * Get Incidences group by for status. * * @param ITSM $ITSM Object for callApi. * @param array $status Status. * * @return array */ private function getIncidencesGroupedByStatus(ITSM $ITSM, array $status): array { $listStatus = $ITSM->callApi('getIncidencesGroupedByStatus'); $result = []; foreach ($status as $key => $value) { if (isset($listStatus[$key]) === false) { $listStatus[$key] = 0; } $result[$value] = $listStatus[$key]; } return $result; } /** * Get Incidences group by for priorities. * * @param ITSM $ITSM Object for callApi. * @param array $priorities Priorities. * * @return array */ private function getIncidencesGroupedByPriorities(ITSM $ITSM, array $priorities): array { $listPriorities = $ITSM->callApi('getIncidencesGroupedByPriorities'); $result = []; foreach ($priorities as $key => $value) { if (isset($listPriorities[$key]) === false) { $listPriorities[$key] = 0; } $result[$value] = $listPriorities[$key]; } return $result; } /** * Get Inventory. * * @param ITSM $ITSM Object for callApi. * @param integer $idInventory Inventory ID. * * @return array Data inventory */ private function getInventory(ITSM $ITSM, int $idInventory): array { $result = $ITSM->callApi( 'inventory', [], [], $idInventory, 'GET' ); return $result; } /** * Get Incidences group by for groups. * * @param ITSM $ITSM Object for callApi. * * @return array */ private function getIncidencesGroupedByGroups(ITSM $ITSM): array { return $ITSM->callApi('getIncidencesGroupedByGroups'); } /** * Get Incidences group by for owner. * * @param ITSM $ITSM Object for callApi. * * @return array */ private function getIncidencesGroupedByOwners(ITSM $ITSM): array { return $ITSM->callApi('getIncidencesGroupedByOwners'); } /** * Draw priority div. * * @param string $priority Priority incidence. * @param string $label Name. * * @return string Html output. */ private function priorityDiv(string $priority, string $label) { $output = ''; switch ($priority) { case 'LOW': $color = COL_NORMAL; break; case 'INFORMATIVE': $color = COL_UNKNOWN; break; case 'MEDIUM': $color = COL_WARNING; break; case 'SERIOUS': $color = COL_ALERTFIRED; break; case 'VERY_SERIOUS': $color = COL_CRITICAL; break; default: $color = COL_NOTINIT; break; } $output = '