grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026.
--------------------------------------------------------------------------
*/
/** @file
* @brief
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/**
* SlaLevel class
**/
class SlaLevel extends RuleTicket {
protected $rules_id_field = 'slalevels_id';
protected $ruleactionclass = 'SlaLevelAction';
// No criteria
protected $rulecriteriaclass = 'SlaLevelCriteria';
static $rightname = 'sla';
/**
* Constructor
**/
function __construct() {
// Override in order not to use glpi_rules table.
}
// Temporary hack for this class in 0.84
static function getTable() {
return 'glpi_slalevels';
}
/**
* @since version 0.85
**/
static function getConditionsArray() {
// Override ruleticket one
return array();
}
/**
* @since version 0.84
**/
function getForbiddenStandardMassiveAction() {
$forbidden = parent::getForbiddenStandardMassiveAction();
$forbidden[] = 'update';
return $forbidden;
}
static function getTypeName($nb=0) {
return _n('Escalation level', 'Escalation levels', $nb);
}
function cleanDBonPurge() {
global $DB;
parent::cleanDBOnPurge();
$sql = "DELETE
FROM `glpi_slalevels_tickets`
WHERE `".$this->rules_id_field."` = '".$this->fields['id']."'";
$DB->query($sql);
}
/**
* @param $slt SLT object
*
* @since version 9.1 (before showForSLA)
**/
function showForSLT(SLT $slt) {
global $DB;
$ID = $slt->getField('id');
if (!$slt->can($ID, READ)) {
return false;
}
$canedit = $slt->can($ID, UPDATE);
$rand = mt_rand();
if ($canedit) {
echo "
";
}
$query = "SELECT *
FROM `glpi_slalevels`
WHERE `slts_id` = '$ID'
ORDER BY `execution_time`";
$result = $DB->query($query);
$numrows = $DB->numrows($result);
echo "";
if ($canedit && $numrows) {
Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
$massiveactionparams = array('num_displayed' => min($_SESSION['glpilist_limit'], $numrows),
'container' => 'mass'.__CLASS__.$rand);
Html::showMassiveActions($massiveactionparams);
}
echo "
";
echo "";
if ($canedit && $numrows) {
echo "| ".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand)." | ";
}
echo "".__('Name')." | ";
echo "".__('Execution')." | ";
echo "".__('Active')." | ";
echo "
";
Session::initNavigateListItems('SlaLevel',
//TRANS: %1$s is the itemtype name, %2$s is the name of the item (used for headings of a list)
sprintf(__('%1$s = %2$s'), SLT::getTypeName(1),
$slt->getName()));
while ($data = $DB->fetch_assoc($result)) {
Session::addToNavigateListItems('SlaLevel', $data["id"]);
echo "";
if ($canedit) {
echo "| ".Html::getMassiveActionCheckBox(__CLASS__, $data["id"])." | ";
}
echo "";
if ($canedit) {
echo "";
}
echo $data["name"];
if (empty($data["name"])) {
echo "(".$data['id'].")";
}
if ($canedit) {
echo "";
}
echo " | ";
echo "".(($data["execution_time"] <> 0)
?Html::timestampToString($data["execution_time"], false)
:__('Time to resolve'))." | ";
echo "".Dropdown::getYesNo($data["is_active"])." | ";
echo "
";
echo "| ";
$this->getRuleWithCriteriasAndActions($data['id'],1,1);
$this->showCriteriasList($data["id"], array('readonly' => true));
echo " | ";
$this->showActionsList($data["id"], array('readonly' => true));
echo " |
";
}
echo "
";
if ($canedit && $numrows) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "
";
}
function getActions() {
$actions = parent::getActions();
unset($actions['slts_id']);
$actions['recall']['name'] = __('Automatic reminders of SLT');
$actions['recall']['type'] = 'yesonly';
$actions['recall']['force_actions'] = array('send');
// Only append actors
$actions['_users_id_requester']['force_actions'] = array('append');
$actions['_groups_id_requester']['force_actions'] = array('append');
$actions['_users_id_assign']['force_actions'] = array('append');
$actions['_groups_id_assign']['force_actions'] = array('append');
$actions['_suppliers_id_assign']['force_actions'] = array('append');
$actions['_users_id_observer']['force_actions'] = array('append');
$actions['_groups_id_observer']['force_actions'] = array('append');
return $actions;
}
/**
* @since version 0.84
*
* @see RuleTicket::getCriterias()
**/
function getCriterias() {
$actions = parent::getActions();
unset($actions['slts_id']);
// Could not be used as criteria
unset($actions['users_id_validate_requester_supervisor']);
unset($actions['users_id_validate_assign_supervisor']);
unset($actions['affectobject']);
unset($actions['groups_id_validate']);
unset($actions['users_id_validate']);
unset($actions['validation_percent']);
$actions['status']['name'] = __('Status');
$actions['status']['type'] = 'dropdown_status';
return $actions;
}
/**
* Show the rule
*
* @param $ID ID of the rule
* @param $options array of possible options
*
* @return nothing
**/
function showForm($ID, $options=array()) {
$canedit = $this->can('sla',UPDATE);
$this->initForm($ID, $options);
$this->showFormHeader($options);
echo "";
echo "| ".__('Name')." | ";
echo "";
Html::autocompletionTextField($this, "name");
echo " | ";
echo "".__('Active')." | ";
echo "";
Dropdown::showYesNo("is_active", $this->fields["is_active"]);
echo" |
\n";
$slt = new SLT();
$slt->getFromDB($this->fields['slts_id']);
echo "";
echo "| ".SLT::getTypeName(1)." | ";
echo "".$slt->getLink()." | ";
echo "".__('Execution')." | ";
echo "";
$delay = $slt->getSLTTime();
self::dropdownExecutionTime('execution_time',
array('max_time'
=> $delay,
'used'
=> self::getAlreadyUsedExecutionTime($slt->fields['id']),
'value'
=> $this->fields['execution_time']));
echo " |
\n";
echo "";
echo "| ".__('Logical operator')." | ";
echo "";
$this->dropdownRulesMatch(array('value' => $this->fields["match"]));
echo " | ";
echo " |
";
$this->showFormButtons($options);
}
/**
* Dropdown execution time for SLT
*
* @param $name string name of the select
* @param $options array of possible options:
* - value : default value
* - max_time : max time to use
* - used : already used values
*
* @return nothing
**/
static function dropdownExecutionTime($name, $options=array()) {
$p['value'] = '';
$p['max_time'] = 4*DAY_TIMESTAMP;
$p['used'] = array();
if (is_array($options) && count($options)) {
foreach ($options as $key => $val) {
$p[$key] = $val;
}
}
// Display default value;
if (($key = array_search($p['value'], $p['used'])) !== false) {
unset($p['used'][$key]);
}
$possible_values = array();
for ($i=10 ; $i<60 ; $i+=10) {
if (!in_array($i*MINUTE_TIMESTAMP,$p['used'])) {
$possible_values[$i*MINUTE_TIMESTAMP] = sprintf(_n('+ %d minute','+ %d minutes',$i), $i);
}
if (!in_array(-$i*MINUTE_TIMESTAMP,$p['used'])) {
if ($p['max_time'] >= $i*MINUTE_TIMESTAMP) {
$possible_values[-$i*MINUTE_TIMESTAMP] = sprintf(_n('- %d minute','- %d minutes',$i), $i);
}
}
}
for ($i=1 ; $i<24 ; $i++) {
if (!in_array($i*HOUR_TIMESTAMP,$p['used'])) {
$possible_values[$i*HOUR_TIMESTAMP] = sprintf(_n('+ %d hour','+ %d hours',$i), $i);
}
if (!in_array(-$i*HOUR_TIMESTAMP,$p['used'])) {
if ($p['max_time'] >= $i*HOUR_TIMESTAMP) {
$possible_values[-$i*HOUR_TIMESTAMP] = sprintf(_n('- %d hour','- %d hours',$i),
$i);
}
}
}
for ($i=1 ; $i<30 ; $i++) {
if (!in_array($i*DAY_TIMESTAMP,$p['used'])) {
$possible_values[$i*DAY_TIMESTAMP] = sprintf(_n('+ %d day','+ %d days',$i), $i);
}
if (!in_array(-$i*DAY_TIMESTAMP,$p['used'])) {
if ($p['max_time'] >= $i*DAY_TIMESTAMP) {
$possible_values[-$i*DAY_TIMESTAMP] = sprintf(_n('- %d day','- %d days',$i), $i);
}
}
}
if (!in_array(0,$p['used'])) {
$possible_values[0] = __('Time to resolve');
}
ksort($possible_values);
Dropdown::showFromArray($name, $possible_values, array('value' => $p['value']));
}
/**
* Get already used execution time for a SLT
*
* @param $slts_id integer id of the SLT
*
* @return array of already used execution times
**/
static function getAlreadyUsedExecutionTime($slts_id) {
global $DB;
$result = array();
$query = "SELECT DISTINCT `execution_time`
FROM `glpi_slalevels`
WHERE `slts_id` = '$slts_id';";
foreach ($DB->request($query) as $data) {
$result[$data['execution_time']] = $data['execution_time'];
}
return $result;
}
/**
* Get first level for a SLT
*
* @param $slts_id integer id of the SLT
*
* @since version 9.1 (before getFirst SlaLevel)
*
* @return id of the sla level : 0 if not exists
**/
static function getFirstSltLevel($slts_id) {
global $DB;
$query = "SELECT `id`
FROM `glpi_slalevels`
WHERE `slts_id` = '$slts_id'
AND `is_active` = 1
ORDER BY `execution_time` ASC LIMIT 1;";
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
return $DB->result($result,0,0);
}
}
return 0;
}
/**
* Get next level for a SLT
*
* @param $slts_id integer id of the SLT
* @param $slalevels_id integer id of the current SLT level
*
* @return id of the sla level : 0 if not exists
**/
static function getNextSltLevel($slts_id, $slalevels_id) {
global $DB;
$query = "SELECT `execution_time`
FROM `glpi_slalevels`
WHERE `id` = '$slalevels_id';";
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
$execution_time = $DB->result($result,0,0);
$query = "SELECT `id`
FROM `glpi_slalevels`
WHERE `slts_id` = '$slts_id'
AND `id` <> '$slalevels_id'
AND `execution_time` > '$execution_time'
AND `is_active` = 1
ORDER BY `execution_time` ASC LIMIT 1 ;";
if ($result = $DB->query($query)) {
if ($DB->numrows($result)) {
return $DB->result($result,0,0);
}
}
}
}
return 0;
}
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
if (!$withtemplate) {
$nb = 0;
switch ($item->getType()) {
case 'SLT' :
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = countElementsInTable($this->getTable(), "`slts_id` = '".$item->getID()."'");
}
return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb);
}
}
return '';
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
if ($item->getType() == 'SLT') {
$slalevel = new self();
$slalevel->showForSLT($item);
}
return true;
}
}
?>