grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026.
--------------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/**
* This class manages locks
* Lock management is available for objects and link between objects. It relies on the use of
* a is_dynamic field, to incidate if item supports lock, and is_deleted field to incidate if the
* item or link is locked
* By setting is_deleted to 0 again, the item is unlock
*
* Note : GLPI's core supports locks for objects. It's up to the external inventory tool to manage
* locks for fields
*
* @since version 0.84
**/
class Lock {
static function getTypeName($nb=0) {
return _n('Lock', 'Locks', $nb);
}
/**
* Display form to unlock fields and links
*
* @param CommonDBTM $item the source item
**/
static function showForItem(CommonDBTM $item) {
global $DB;
$ID = $item->getID();
$itemtype = $item->getType();
$header = false;
//If user doesn't have write right on the item, lock form must not be displayed
if (!$item->canCreate()) {
return false;
}
echo "
\n";
}
/**
* @see CommonGLPI::getTabNameForItem()
*
* @param $item CommonGLPI object
* @param $withtemplate (default 0)
**/
function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
if ($item->isDynamic() && $item->canCreate()) {
return Lock::getTypeName(Session::getPluralNumber());
}
return '';
}
/**
* @param $item CommonGLPI object
* @param $tabnum (default 1)
* @param $withtemplate (default 0)
**/
static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
if ($item->isDynamic()) {
self::showForItem($item);
}
return true;
}
/**
* Get infos to build an SQL query to get locks fields in a table
*
* @param $itemtype itemtype of the item to look for locked fields
* @param $baseitemtype itemtype of the based item
*
* @return an array which contains necessary informations to build the SQL query
**/
static function getLocksQueryInfosByItemType($itemtype, $baseitemtype) {
$condition = array();
$table = false;
$field = '';
$type = $itemtype;
switch ($itemtype) {
case 'Peripheral' :
case 'Monitor' :
case 'Printer' :
case 'Phone' :
$condition = array('itemtype' => $itemtype,
'is_dynamic' => 1,
'is_deleted' => 1);
$table = 'glpi_computers_items';
$field = 'computers_id';
$type = 'Computer_Item';
break;
case 'NetworkPort' :
$condition = array('itemtype' => $baseitemtype,
'is_dynamic' => 1,
'is_deleted' => 1);
$table = 'glpi_networkports';
$field = 'items_id';
break;
case 'NetworkName' :
$condition = array('`glpi_networknames`.`is_dynamic`' => 1,
'`glpi_networknames`.`is_deleted`' => 1,
'`glpi_networknames`.`itemtype`' => 'NetworkPort',
'`glpi_networknames`.`items_id`' => '`glpi_networkports`.`id`',
'`glpi_networkports`.`itemtype`' => $baseitemtype);
$condition['FIELDS']
= array('glpi_networknames' => 'id');
$table = array('glpi_networknames', 'glpi_networkports');
$field = '`glpi_networkports`.`items_id`';
break;
case 'IPAddress' :
$condition = array('`glpi_ipaddresses`.`is_dynamic`' => 1,
'`glpi_ipaddresses`.`is_deleted`' => 1,
'`glpi_ipaddresses`.`itemtype`' => 'NetworkName',
'`glpi_ipaddresses`.`items_id`' => '`glpi_networknames`.`id`',
'`glpi_networknames`.`itemtype`' => 'NetworkPort',
'`glpi_networknames`.`items_id`' => '`glpi_networkports`.`id`',
'`glpi_networkports`.`itemtype`' => $baseitemtype);
$condition['FIELDS']
= array('glpi_ipaddresses' => 'id');
$table = array('glpi_ipaddresses', 'glpi_networknames', 'glpi_networkports');
$field = '`glpi_networkports`.`items_id`';
break;
case 'ComputerDisk' :
$condition = array('is_dynamic' => 1,
'is_deleted' => 1);
$table = 'glpi_computerdisks';
$field = 'computers_id';
break;
case 'ComputerVirtualMachine' :
$condition = array('is_dynamic' => 1,
'is_deleted' => 1);
$table = 'glpi_computervirtualmachines';
$field = 'computers_id';
break;
case 'SoftwareVersion' :
$condition = array('is_dynamic' => 1,
'is_deleted' => 1);
$table = 'glpi_computers_softwareversions';
$field = 'computers_id';
$type = 'Computer_SoftwareVersion';
break;
default :
// Devices
if (preg_match('/^Item\_Device/',$itemtype)) {
$condition = array('itemtype' => $baseitemtype,
'is_dynamic' => 1,
'is_deleted' => 1);
$table = getTableForItemType($itemtype);
$field = 'items_id';
}
}
return array('condition' => $condition,
'table' => $table,
'field' => $field,
'type' => $type);
}
/**
* @since version 0.85
*
* @see CommonDBTM::getMassiveActionsForItemtype()
**/
static function getMassiveActionsForItemtype(array &$actions, $itemtype, $is_deleted=0,
CommonDBTM $checkitem=NULL) {
$action_name = __CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'unlock';
if (Session::haveRight('computer', UPDATE)
&& ($itemtype == 'Computer')) {
$actions[$action_name] = __('Unlock components');
}
}
/**
* @since version 0.85
*
* @see CommonDBTM::showMassiveActionsSubForm()
**/
static function showMassiveActionsSubForm(MassiveAction $ma) {
switch ($ma->getAction()) {
case 'unlock' :
$types = array('Monitor' => _n('Monitor', 'Monitors', Session::getPluralNumber()),
'Peripheral' => _n('Device', 'Devices', Session::getPluralNumber()),
'Printer' => _n('Printer', 'Printers', Session::getPluralNumber()),
'SoftwareVersion' => _n('Version', 'Versions', Session::getPluralNumber()),
'NetworkPort' => _n('Network port', 'Network ports', Session::getPluralNumber()),
'NetworkName' => _n('Network name', 'Network names', Session::getPluralNumber()),
'IPAddress' => _n('IP address', 'IP addresses', Session::getPluralNumber()),
'ComputerDisk' => _n('Volume', 'Volumes', Session::getPluralNumber()),
'Device' => _n('Component', 'Components', Session::getPluralNumber()),
'ComputerVirtualMachine' => _n('Virtual machine', 'Virtual machines', Session::getPluralNumber()));
_e('Select the type of the item that must be unlock');
echo "
\n";
Dropdown::showFromArray('attached_item', $types,
array('multiple' => true,
'size' => 5,
'values' => array_keys($types)));
echo "
".Html::submit(_x('button','Post'), array('name' => 'massiveaction'));
return true;
}
return false;
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $baseitem,
array $ids) {
global $DB;
switch ($ma->getAction()) {
case 'unlock' :
$input = $ma->getInput();
if (isset($input['attached_item'])) {
$attached_items = $input['attached_item'];
if (($device_key = array_search('Device', $attached_items)) !== false) {
unset($attached_items[$device_key]);
$attached_items = array_merge($attached_items, Item_Devices::getDeviceTypes());
}
$links = array();
foreach ($attached_items as $attached_item) {
$infos = self::getLocksQueryInfosByItemType($attached_item, $baseitem->getType());
if ($item = getItemForItemtype($infos['type'])) {
$infos['item'] = $item;
$links[$attached_item] = $infos;
}
}
foreach ($ids as $id) {
$action_valid = false;
foreach ($links as $infos) {
$infos['condition'][$infos['field']] = $id;
foreach ($DB->request($infos['table'], $infos['condition']) as $data) {
// Restore without history
$action_valid = $infos['item']->restore(array('id' => $data['id']));
}
}
if ($action_valid) {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
$ma->addMessage($infos['item']->getErrorMessage(ERROR_ON_ACTION));
}
}
}
return;
}
}
}
?>