Files
GLPI-Plugin-SOC-Case-Manage…/inc/case.class.php
2025-05-31 10:26:04 +02:00

325 lines
8.9 KiB
PHP

<?php
/**
* SOC Case Class
*/
class PluginSocCase extends CommonDBTM {
static $rightname = 'plugin_soc_case';
// Severity levels
const SEVERITY_CRITICAL = 'critical';
const SEVERITY_HIGH = 'high';
const SEVERITY_MEDIUM = 'medium';
const SEVERITY_LOW = 'low';
// Case statuses
const STATUS_NEW = 'new';
const STATUS_ASSIGNED = 'assigned';
const STATUS_IN_PROGRESS = 'in_progress';
const STATUS_PENDING = 'pending';
const STATUS_RESOLVED = 'resolved';
const STATUS_CLOSED = 'closed';
/**
* @return string
*/
static function getTypeName($nb = 0) {
return _n('SOC Case', 'SOC Cases', $nb);
}
/**
* Define tabs to display
*
* @param array $options
* @return array
*/
function defineTabs($options = []) {
$tabs = [];
$tabs[1] = __('Main');
$tabs[2] = __('Related Tickets');
$tabs[3] = __('Related Changes');
$tabs[4] = __('Timeline');
$tabs[5] = __('Documents');
$tabs[6] = __('Notes');
$tabs[7] = __('History');
return $tabs;
}
/**
* Get severity options
*
* @return array
*/
static function getSeverityOptions() {
return [
self::SEVERITY_CRITICAL => __('Critical', 'soc'),
self::SEVERITY_HIGH => __('High', 'soc'),
self::SEVERITY_MEDIUM => __('Medium', 'soc'),
self::SEVERITY_MEDIUM => __('Low', 'soc')
];
}
/**
* Get status options
*
* @return array
*/
static function getStatusOptions() {
return [
self::STATUS_NEW => __('New', 'soc'),
self::STATUS_ASSIGNED => __('Assigned', 'soc'),
self::STATUS_IN_PROGRESS => __('In Progress', 'soc'),
self::STATUS_PENDING => __('Pending', 'soc'),
self::STATUS_RESOLVED => __('Resolved', 'soc'),
self::STATUS_CLOSED => __('Closed', 'soc')
];
}
/**
* Show form
*
* @param integer $ID
* @param array $options
* @return boolean
*/
function showForm($ID, $options = []) {
global $CFG_GLPI;
$this->initForm($ID, $options);
$this->showFormHeader($options);
echo "<tr class='tab_bg_1'>";
echo "<td>" . __('Name') . "</td>";
echo "<td>";
echo Html::input('name', ['value' => $this->fields['name']]);
echo "</td>";
echo "<td>" . __('Status') . "</td>";
echo "<td>";
Dropdown::showFromArray('status', self::getStatusOptions(), ['value' => $this->fields['status']]);
echo "</td>";
echo "</tr>";
echo "<tr class='tab_bg_1'>";
echo "<td>" . __('Severity', 'soc') . "</td>";
echo "<td>";
Dropdown::showFromArray('severity', self::getSeverityOptions(), ['value' => $this->fields['severity']]);
echo "</td>";
echo "<td>" . __('Technician') . "</td>";
echo "<td>";
User::dropdown(['name' => 'users_id_tech',
'value' => $this->fields['users_id_tech'],
'entity' => $this->fields['entities_id'],
'right' => 'interface']);
echo "</td>";
echo "</tr>";
echo "<tr class='tab_bg_1'>";
echo "<td>" . __('Description') . "</td>";
echo "<td colspan='3'>";
Html::textarea([
'name' => 'description',
'value' => $this->fields['description'],
'cols' => 125,
'rows' => 5
]);
echo "</td>";
echo "</tr>";
$this->showFormButtons($options);
return true;
}
/**
* Get search options
*
* @return array
*/
function rawSearchOptions() {
$tab = [];
$tab[] = [
'id' => 'common',
'name' => self::getTypeName(2)
];
$tab[] = [
'id' => '1',
'table' => $this->getTable(),
'field' => 'name',
'name' => __('Name'),
'datatype' => 'itemlink',
'massiveaction' => false
];
$tab[] = [
'id' => '2',
'table' => $this->getTable(),
'field' => 'id',
'name' => __('ID'),
'massiveaction' => false,
'datatype' => 'number'
];
$tab[] = [
'id' => '3',
'table' => $this->getTable(),
'field' => 'severity',
'name' => __('Severity', 'soc'),
'datatype' => 'specific',
'searchtype' => ['equals', 'notequals']
];
$tab[] = [
'id' => '4',
'table' => $this->getTable(),
'field' => 'status',
'name' => __('Status'),
'datatype' => 'specific',
'searchtype' => ['equals', 'notequals']
];
$tab[] = [
'id' => '5',
'table' => $this->getTable(),
'field' => 'date_creation',
'name' => __('Creation date'),
'datatype' => 'datetime',
'massiveaction' => false
];
$tab[] = [
'id' => '6',
'table' => $this->getTable(),
'field' => 'date_mod',
'name' => __('Last update'),
'datatype' => 'datetime',
'massiveaction' => false
];
$tab[] = [
'id' => '7',
'table' => 'glpi_users',
'field' => 'name',
'linkfield' => 'users_id_tech',
'name' => __('Technician'),
'datatype' => 'dropdown'
];
return $tab;
}
/**
* Create a ticket from this case
*
* @param array $input
* @return integer|boolean
*/
function createTicket($input) {
$ticket = new Ticket();
$ticket_input = [
'name' => sprintf(__('[SOC Case #%s] %s', 'soc'), $this->fields['id'], $this->fields['name']),
'content' => $this->fields['description'],
'entities_id' => $this->fields['entities_id'],
'urgency' => self::mapSeverityToUrgency($this->fields['severity']),
'users_id_recipient' => Session::getLoginUserID()
];
$tickets_id = $ticket->add($ticket_input);
if ($tickets_id) {
$this->addTicket($tickets_id);
return $tickets_id;
}
return false;
}
/**
* Create a change from this case
*
* @param array $input
* @return integer|boolean
*/
function createChange($input) {
$change = new Change();
$change_input = [
'name' => sprintf(__('[SOC Case #%s] %s', 'soc'), $this->fields['id'], $this->fields['name']),
'content' => $this->fields['description'],
'entities_id' => $this->fields['entities_id'],
'urgency' => self::mapSeverityToUrgency($this->fields['severity']),
'users_id_recipient' => Session::getLoginUserID()
];
$changes_id = $change->add($change_input);
if ($changes_id) {
$this->addChange($changes_id);
return $changes_id;
}
return false;
}
/**
* Map SOC severity to GLPI urgency
*
* @param string $severity
* @return integer
*/
static function mapSeverityToUrgency($severity) {
switch ($severity) {
case self::SEVERITY_CRITICAL:
return 5; // Very high
case self::SEVERITY_HIGH:
return 4; // High
case self::SEVERITY_MEDIUM:
return 3; // Medium
case self::SEVERITY_LOW:
return 2; // Low
default:
return 3; // Medium by default
}
}
/**
* Add a ticket to this case
*
* @param integer $tickets_id
* @return boolean
*/
function addTicket($tickets_id) {
global $DB;
$case_ticket = new PluginSocCaseTicket();
return $case_ticket->add([
'plugin_soc_cases_id' => $this->fields['id'],
'tickets_id' => $tickets_id,
'date_creation' => $_SESSION["glpi_currenttime"]
]);
}
/**
* Add a change to this case
*
* @param integer $changes_id
* @return boolean
*/
function addChange($changes_id) {
global $DB;
$case_change = new PluginSocCaseChange();
return $case_change->add([
'plugin_soc_cases_id' => $this->fields['id'],
'changes_id' => $changes_id,
'date_creation' => $_SESSION["glpi_currenttime"]
]);
}
}