Start repository

This commit is contained in:
tips-of-mine
2025-05-31 10:26:04 +02:00
commit 486d93a4f0
30 changed files with 5788 additions and 0 deletions

325
inc/case.class.php Normal file
View File

@ -0,0 +1,325 @@
<?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"]
]);
}
}

80
inc/casechange.class.php Normal file
View File

@ -0,0 +1,80 @@
<?php
/**
* SOC Case-Change relation class
*/
class PluginSocCaseChange extends CommonDBRelation {
// From CommonDBRelation
static public $itemtype_1 = 'PluginSocCase';
static public $items_id_1 = 'plugin_soc_cases_id';
static public $itemtype_2 = 'Change';
static public $items_id_2 = 'changes_id';
/**
* Get changes for a case
*
* @param integer $cases_id
* @return DBmysqlIterator
*/
static function getChangesForCase($cases_id) {
global $DB;
$iterator = $DB->request([
'SELECT' => [
'glpi_changes.*',
'glpi_plugin_soc_case_changes.id AS link_id'
],
'FROM' => 'glpi_plugin_soc_case_changes',
'LEFT JOIN' => [
'glpi_changes' => [
'FKEY' => [
'glpi_plugin_soc_case_changes' => 'changes_id',
'glpi_changes' => 'id'
]
]
],
'WHERE' => [
'glpi_plugin_soc_case_changes.plugin_soc_cases_id' => $cases_id
],
'ORDER' => [
'glpi_changes.date_creation DESC'
]
]);
return $iterator;
}
/**
* Get cases for a change
*
* @param integer $changes_id
* @return DBmysqlIterator
*/
static function getCasesForChange($changes_id) {
global $DB;
$iterator = $DB->request([
'SELECT' => [
'glpi_plugin_soc_cases.*',
'glpi_plugin_soc_case_changes.id AS link_id'
],
'FROM' => 'glpi_plugin_soc_case_changes',
'LEFT JOIN' => [
'glpi_plugin_soc_cases' => [
'FKEY' => [
'glpi_plugin_soc_case_changes' => 'plugin_soc_cases_id',
'glpi_plugin_soc_cases' => 'id'
]
]
],
'WHERE' => [
'glpi_plugin_soc_case_changes.changes_id' => $changes_id
],
'ORDER' => [
'glpi_plugin_soc_cases.date_creation DESC'
]
]);
return $iterator;
}
}

80
inc/caseticket.class.php Normal file
View File

@ -0,0 +1,80 @@
<?php
/**
* SOC Case-Ticket relation class
*/
class PluginSocCaseTicket extends CommonDBRelation {
// From CommonDBRelation
static public $itemtype_1 = 'PluginSocCase';
static public $items_id_1 = 'plugin_soc_cases_id';
static public $itemtype_2 = 'Ticket';
static public $items_id_2 = 'tickets_id';
/**
* Get tickets for a case
*
* @param integer $cases_id
* @return DBmysqlIterator
*/
static function getTicketsForCase($cases_id) {
global $DB;
$iterator = $DB->request([
'SELECT' => [
'glpi_tickets.*',
'glpi_plugin_soc_case_tickets.id AS link_id'
],
'FROM' => 'glpi_plugin_soc_case_tickets',
'LEFT JOIN' => [
'glpi_tickets' => [
'FKEY' => [
'glpi_plugin_soc_case_tickets' => 'tickets_id',
'glpi_tickets' => 'id'
]
]
],
'WHERE' => [
'glpi_plugin_soc_case_tickets.plugin_soc_cases_id' => $cases_id
],
'ORDER' => [
'glpi_tickets.date_creation DESC'
]
]);
return $iterator;
}
/**
* Get cases for a ticket
*
* @param integer $tickets_id
* @return DBmysqlIterator
*/
static function getCasesForTicket($tickets_id) {
global $DB;
$iterator = $DB->request([
'SELECT' => [
'glpi_plugin_soc_cases.*',
'glpi_plugin_soc_case_tickets.id AS link_id'
],
'FROM' => 'glpi_plugin_soc_case_tickets',
'LEFT JOIN' => [
'glpi_plugin_soc_cases' => [
'FKEY' => [
'glpi_plugin_soc_case_tickets' => 'plugin_soc_cases_id',
'glpi_plugin_soc_cases' => 'id'
]
]
],
'WHERE' => [
'glpi_plugin_soc_case_tickets.tickets_id' => $tickets_id
],
'ORDER' => [
'glpi_plugin_soc_cases.date_creation DESC'
]
]);
return $iterator;
}
}

108
inc/profile.class.php Normal file
View File

@ -0,0 +1,108 @@
<?php
/**
* SOC Profile class
*/
class PluginSocProfile extends Profile {
static $rightname = "profile";
/**
* @param string $right
* @return integer
*/
function getRight($right) {
return $this->fields[$right];
}
/**
* Init profiles
*
* @return void
*/
static function initProfile() {
global $DB;
$profile = new self();
// Add new rights in glpi_profilerights table
foreach ($profile->getAllRights() as $right) {
if (!countElementsInTable("glpi_profilerights", ['name' => $right['field']])) {
ProfileRight::addProfileRights([$right['field']]);
}
}
}
/**
* Get all rights
*
* @return array
*/
static function getAllRights() {
$rights = [
[
'field' => 'plugin_soc_case',
'name' => __('SOC Case', 'soc'),
'rights' => [
CREATE => __('Create'),
READ => __('Read'),
UPDATE => __('Update'),
DELETE => __('Delete'),
PURGE => __('Purge')
]
]
];
return $rights;
}
/**
* Create first access for super-admin user
*
* @param integer $ID
* @return void
*/
static function createFirstAccess($ID) {
$profile = new self();
foreach ($profile->getAllRights() as $right) {
self::addDefaultProfileInfos($ID, [$right['field'] => ALLSTANDARDRIGHT]);
}
}
/**
* Show profile form
*
* @param integer $profiles_id
* @param boolean $openform
* @param boolean $closeform
* @return void
*/
function showForm($profiles_id = 0, $openform = true, $closeform = true) {
echo "<div class='firstbloc'>";
if (($canedit = Session::haveRightsOr(self::$rightname, [CREATE, UPDATE, DELETE, PURGE]))
&& $openform) {
$profile = new Profile();
echo "<form method='post' action='".$profile->getFormURL()."'>";
}
$profile = new Profile();
$profile->getFromDB($profiles_id);
$rights = $this->getAllRights();
$profile->displayRightsChoiceMatrix($rights, ['canedit' => $canedit,
'default_class' => 'tab_bg_2',
'title' => __('General')]);
if ($canedit && $closeform) {
echo "<div class='center'>";
echo Html::hidden('id', ['value' => $profiles_id]);
echo Html::submit(_sx('button', 'Save'), ['name' => 'update']);
echo "</div>\n";
Html::closeForm();
}
echo "</div>";
}
}