.
* ---------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/**
* State Class
**/
class State extends CommonTreeDropdown {
public $can_be_translated = true;
static $rightname = 'state';
static function getTypeName($nb = 0) {
return _n('Status of items', 'Statuses of items', $nb);
}
static function getFieldLabel() {
return __('Status');
}
/**
* @since 0.85
*
* @see CommonTreeDropdown::getAdditionalFields()
**/
function getAdditionalFields() {
$fields = parent::getAdditionalFields();
$fields[] = ['label' => __('Visibility'),
'name' => 'header',
'list' => false];
foreach ($this->getvisibilityFields() as $type => $field) {
$fields[] = ['name' => $field,
'label' => $type::getTypeName(Session::getPluralNumber()),
'type' => 'bool',
'list' => true];
}
return $fields;
}
/**
* Dropdown of states for behaviour config
*
* @param $name select name
* @param $lib string to add for -1 value (default '')
* @param $value default value (default 0)
**/
static function dropdownBehaviour($name, $lib = "", $value = 0) {
global $DB;
$elements = ["0" => __('Keep status')];
if ($lib) {
$elements["-1"] = $lib;
}
$iterator = $DB->request([
'SELECT' => ['id', 'name'],
'FROM' => 'glpi_states',
'ORDER' => 'name'
]);
while ($data = $iterator->next()) {
$elements[$data["id"]] = sprintf(__('Set status: %s'), $data["name"]);
}
Dropdown::showFromArray($name, $elements, ['value' => $value]);
}
static function showSummary() {
global $DB, $CFG_GLPI;
$state_type = $CFG_GLPI["state_types"];
$states = [];
foreach ($state_type as $key=>$itemtype) {
if ($item = getItemForItemtype($itemtype)) {
if (!$item->canView()) {
unset($state_type[$key]);
} else {
$table = getTableForItemType($itemtype);
$iterator = $DB->request([
'SELECT' => [
'states_id',
'COUNT' => '* AS cpt'
],
'FROM' => $table,
'WHERE' => [
'is_deleted' => 0,
'is_template' => 0
] + getEntitiesRestrictCriteria($table),
'GROUP' => 'states_id'
]);
while ($data = $iterator->next()) {
$states[$data["states_id"]][$itemtype] = $data["cpt"];
}
}
}
}
if (count($states)) {
// Produce headline
echo "
";
// Type
echo "| ".__('Status')." | ";
foreach ($state_type as $key => $itemtype) {
if ($item = getItemForItemtype($itemtype)) {
echo "".$item->getTypeName(Session::getPluralNumber())." | ";
$total[$itemtype] = 0;
} else {
unset($state_type[$key]);
}
}
echo "".__('Total')." | ";
echo "
";
$iterator = $DB->request([
'FROM' => 'glpi_states',
'WHERE' => getEntitiesRestrictCriteria('glpi_states', '', '', true),
'ORDER' => 'completename'
]);
// No state
$tot = 0;
echo "| --- | ";
foreach ($state_type as $itemtype) {
echo "";
if (isset($states[0][$itemtype])) {
echo $states[0][$itemtype];
$total[$itemtype] += $states[0][$itemtype];
$tot += $states[0][$itemtype];
} else {
echo " ";
}
echo " | ";
}
echo "$tot |
";
while ($data = $iterator>next()) {
$tot = 0;
echo "| ";
$opt = ['reset' => 'reset',
'sort' => 1,
'start' => 0,
'criteria' => ['0' => ['value' => '$$$$'.$data['id'],
'searchtype' => 'contains',
'field' => 31]]];
echo "".$data["completename"]." | ";
foreach ($state_type as $itemtype) {
echo "";
if (isset($states[$data["id"]][$itemtype])) {
echo $states[$data["id"]][$itemtype];
$total[$itemtype] += $states[$data["id"]][$itemtype];
$tot += $states[$data["id"]][$itemtype];
} else {
echo " ";
}
echo " | ";
}
echo "$tot | ";
echo "
";
}
echo "| ".__('Total')." | ";
$tot = 0;
foreach ($state_type as $itemtype) {
echo "".$total[$itemtype]." | ";
$tot += $total[$itemtype];
}
echo "$tot |
";
echo "
";
} else {
echo "".__('No item found')."
";
}
}
/**
* @since 0.85
*
* @see CommonDBTM::getEmpty()
**/
function getEmpty() {
parent::getEmpty();
//initialize is_visible_* fields at true to keep the same behavior as in older versions
foreach ($this->getvisibilityFields() as $type => $field) {
$this->fields[$field] = 1;
}
}
function cleanDBonPurge() {
Rule::cleanForItemCriteria($this);
}
/**
* @since 0.85
*
* @see CommonTreeDropdown::prepareInputForAdd()
**/
function prepareInputForAdd($input) {
if (!isset($input['states_id'])) {
$input['states_id'] = 0;
}
if (!$this->isUnique($input)) {
Session::addMessageAfterRedirect(
sprintf(__('%1$s must be unique!'), $this->getType(1)),
false,
ERROR
);
return false;
}
$input = parent::prepareInputForAdd($input);
$state = new self();
// Get visibility information from parent if not set
if (isset($input['states_id']) && $state->getFromDB($input['states_id'])) {
foreach ($this->getvisibilityFields() as $type => $field) {
if (!isset($input[$field]) && isset($state->fields[$field])) {
$input[$field] = $state->fields[$field];
}
}
}
return $input;
}
function rawSearchOptions() {
$tab = parent::rawSearchOptions();
$tab[] = [
'id' => '21',
'table' => $this->getTable(),
'field' => 'is_visible_computer',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'), Computer::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '22',
'table' => $this->getTable(),
'field' => 'is_visible_softwareversion',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
SoftwareVersion::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '23',
'table' => $this->getTable(),
'field' => 'is_visible_monitor',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'), Monitor::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '24',
'table' => $this->getTable(),
'field' => 'is_visible_printer',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'), Printer::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '25',
'table' => $this->getTable(),
'field' => 'is_visible_peripheral',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'), Peripheral::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '26',
'table' => $this->getTable(),
'field' => 'is_visible_phone',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'), Phone::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '27',
'table' => $this->getTable(),
'field' => 'is_visible_networkequipment',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
NetworkEquipment::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '28',
'table' => $this->getTable(),
'field' => 'is_visible_softwarelicense',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
SoftwareLicense::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '29',
'table' => $this->getTable(),
'field' => 'is_visible_certificate',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Certificate::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '30',
'table' => $this->getTable(),
'field' => 'is_visible_rack',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Rack::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '31',
'table' => $this->getTable(),
'field' => 'is_visible_line',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Line::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '32',
'table' => $this->getTable(),
'field' => 'is_visible_enclosure',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Enclosure::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '33',
'table' => $this->getTable(),
'field' => 'is_visible_pdu',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
PDU::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '34',
'table' => $this->getTable(),
'field' => 'is_visible_cluster',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Cluster::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '35',
'table' => $this->getTable(),
'field' => 'is_visible_passivedcequipment',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
PassiveDCEquipment::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '36',
'table' => $this->getTable(),
'field' => 'is_visible_contract',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Contract::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
$tab[] = [
'id' => '37',
'table' => $this->getTable(),
'field' => 'is_visible_appliance',
'name' => sprintf(__('%1$s - %2$s'), __('Visibility'),
Appliance::getTypeName(Session::getPluralNumber())),
'datatype' => 'bool'
];
return $tab;
}
function prepareInputForUpdate($input) {
if (!$this->isUnique($input)) {
Session::addMessageAfterRedirect(
sprintf(__('%1$s must be unique per level!'), $this->getType(1)),
false,
ERROR
);
return false;
}
return parent::prepareInputForUpdate($input);
}
/**
* Checks that this state is unique given the new field values.
* Unique fields checked:
* - states_id
* - name
* @param array $input Array of field names and values
* @return boolean True if the new/updated record will be unique
*/
public function isUnique($input) {
global $DB;
$unicity_fields = ['states_id', 'name'];
$has_changed = false;
$where = [];
foreach ($unicity_fields as $unicity_field) {
if (isset($input[$unicity_field]) &&
(!isset($this->fields[$unicity_field]) || $input[$unicity_field] != $this->fields[$unicity_field])) {
$has_changed = true;
}
if (isset($input[$unicity_field])) {
$where[$unicity_field] = $input[$unicity_field];
}
}
if (!$has_changed) {
//state has not changed; this is OK.
return true;
}
$query = [
'FROM' => $this->getTable(),
'COUNT' => 'cpt',
'WHERE' => $where
];
$row = $DB->request($query)->next();
return (int)$row['cpt'] == 0;
}
/**
* Get visibility fields from conf
*/
protected function getvisibilityFields() :array {
global $CFG_GLPI;
$fields = [];
foreach ($CFG_GLPI['state_types'] as $type) {
$fields[$type] = 'is_visible_' . strtolower($type);
}
return $fields;
}
}