/** * Do the standard massive actions * * @since version 0.84 * * This must not be overloaded in Class * @param $input array of input datas * * @return an array of results (ok, ko, noright counts, may include REDIRECT field to set REDIRECT page) **/ function doMassiveActions($input = array()) { global $CFG_GLPI; if (!isset($input["item"]) || count($input["item"]) == 0) { return false; } $res = array('ok' => 0, 'ko' => 0, 'noright' => 0); switch ($input['action']) { case 'add_document': case 'remove_document': $doc = new Document(); return $doc->doSpecificMassiveActions($input); case "add_transfer_list": if (!isset($_SESSION['glpitransfer_list'])) { $_SESSION['glpitransfer_list'] = array(); } if (!isset($_SESSION['glpitransfer_list'][$input["itemtype"]])) { $_SESSION['glpitransfer_list'][$input["itemtype"]] = array(); } foreach ($input["item"] as $key => $val) { if ($val == 1) { $_SESSION['glpitransfer_list'][$input["itemtype"]][$key] = $key; $res['ok']++; } } $res['REDIRECT'] = $CFG_GLPI['root_doc'] . '/front/transfer.action.php'; break; case "delete": foreach ($input["item"] as $key => $val) { if ($val == 1) { if ($this->can($key, 'd')) { if ($this->delete(array("id" => $key))) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } } break; case "purge": foreach ($input["item"] as $key => $val) { if ($val == 1) { if ($this->can($key, 'd')) { $force = 1; // Only mark deletion for if ($this->maybeDeleted() && $this->useDeletedToLockIfDynamic() && $this->isDynamic()) { $force = 0; } if ($this->delete(array("id" => $key), $force)) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } } break; case "restore": foreach ($input["item"] as $key => $val) { if ($val == 1) { if ($this->can($key, 'd')) { if ($this->restore(array("id" => $key))) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } } break; case "update": $searchopt = Search::getCleanedOptions($input["itemtype"], 'w'); if (isset($searchopt[$input["id_field"]])) { /// Infocoms case if (!isPluginItemType($input["itemtype"]) && Search::isInfocomOption($input["itemtype"], $input["id_field"])) { $ic = new Infocom(); $link_entity_type = -1; /// Specific entity item if ($searchopt[$input["id_field"]]["table"] == "glpi_suppliers") { $ent = new Supplier(); if ($ent->getFromDB($input[$input["field"]])) { $link_entity_type = $ent->fields["entities_id"]; } } foreach ($input["item"] as $key => $val) { if ($val == 1) { if ($this->getFromDB($key)) { if ($link_entity_type < 0 || $link_entity_type == $this->getEntityID() || $ent->fields["is_recursive"] && in_array($link_entity_type, getAncestorsOf("glpi_entities", $this->getEntityID()))) { $input2["items_id"] = $key; $input2["itemtype"] = $input["itemtype"]; if ($ic->can(-1, 'w', $input2)) { // Add infocom if not exists if (!$ic->getFromDBforDevice($input["itemtype"], $key)) { $input2["items_id"] = $key; $input2["itemtype"] = $input["itemtype"]; unset($ic->fields); $ic->add($input2); $ic->getFromDBforDevice($input["itemtype"], $key); } $id = $ic->fields["id"]; unset($ic->fields); if ($ic->update(array('id' => $id, $input["field"] => $input[$input["field"]]))) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } else { $res['ko']++; } } else { $res['ko']++; } } } } else { /// Not infocoms $link_entity_type = array(); /// Specific entity item $itemtable = getTableForItemType($input["itemtype"]); $itemtype2 = getItemTypeForTable($searchopt[$input["id_field"]]["table"]); if ($item2 = getItemForItemtype($itemtype2)) { if ($input["id_field"] != 80 && $searchopt[$input["id_field"]]["table"] != $itemtable && $item2->isEntityAssign() && $this->isEntityAssign()) { if ($item2->getFromDB($input[$input["field"]])) { if (isset($item2->fields["entities_id"]) && $item2->fields["entities_id"] >= 0) { if (isset($item2->fields["is_recursive"]) && $item2->fields["is_recursive"]) { $link_entity_type = getSonsOf("glpi_entities", $item2->fields["entities_id"]); } else { $link_entity_type[] = $item2->fields["entities_id"]; } } } } } foreach ($input["item"] as $key => $val) { if ($val == 1) { if ($this->can($key, 'w') && $this->canMassiveAction($input['action'], $input['field'], $input[$input["field"]])) { if (count($link_entity_type) == 0 || in_array($this->fields["entities_id"], $link_entity_type)) { if ($this->update(array('id' => $key, $input["field"] => $input[$input["field"]]))) { $res['ok']++; } else { $res['ko']++; } } else { $res['ko']++; } } else { $res['noright']++; } } } } } break; case "activate_infocoms": $ic = new Infocom(); if ($ic->canCreate()) { foreach ($input["item"] as $key => $val) { $input = array('itemtype' => $input['itemtype'], 'items_id' => $key); if (!$ic->getFromDBforDevice($input['itemtype'], $key)) { if ($ic->can(-1, 'w', $input)) { if ($ic->add($input)) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } else { $res['ko']++; } } } break; case "add_contract_item": $contractitem = new Contract_Item(); foreach ($input["item"] as $key => $val) { if (isset($input['items_id'])) { // Add items to contracts $input = array('itemtype' => $input["item_itemtype"], 'items_id' => $input["items_id"], 'contracts_id' => $key); } if (isset($input['contracts_id'])) { // Add contract to item $input = array('itemtype' => $input["itemtype"], 'items_id' => $key, 'contracts_id' => $input['contracts_id']); } else { return false; } if ($contractitem->can(-1, 'w', $input)) { if ($contractitem->add($input)) { $res['ok']++; } else { $res['ko']++; } } else { $res['noright']++; } } break; case "remove_contract_item": foreach ($input["item"] as $key => $val) { if (isset($input['items_id'])) { // Remove item to contracts $input = array('itemtype' => $input["item_itemtype"], 'items_id' => $input["items_id"], 'contracts_id' => $key); } else { // Remove contract to items $input = array('itemtype' => $input["itemtype"], 'items_id' => $key, 'contracts_id' => $input['contracts_id']); } $contractitem = new Contract_Item(); if ($contractitem->can(-1, 'w', $input)) { if ($item = getItemForItemtype($input["itemtype"])) { if ($item->getFromDB($input['items_id'])) { $contract = new Contract(); if ($contract->getFromDB($input['contracts_id'])) { if ($contractitem->getFromDBForItems($contract, $item)) { if ($contractitem->delete(array('id' => $contractitem->getID()))) { $res['ok']++; } else { $res['ko']++; } } else { $res['ko']++; } } else { $res['ko']++; } } else { $res['ko']++; } } else { $res['ko']++; } } else { $res['noright']++; } } break; //Lock management //Lock management case 'unlock_Printer': case 'unlock_Monitor': case 'unlock_NetworkPort': case 'unlock_NetworkName': case 'unlock_IPAddress': case 'unlock_ComputerDisk': case 'unlock_ComputerVirtualMachine': case 'unlock_Peripheral': case 'unlock_SoftwareVersion': $itemtype = Lock::getItemTypeForMassiveAction($input["action"]); if ($itemtype) { $res = Lock::unlockItems($itemtype, $this->getType(), $input["item"]); } break; case 'unlock_Device': foreach (Item_Devices::getDeviceTypes() as $itemtype) { $res = Lock::unlockItems($itemtype, $this->getType(), $input["item"]); } break; default: // Plugin specific actions $split = explode('_', $input["action"]); $res = ''; if ($split[0] == 'plugin' && isset($split[1])) { // Normalized name plugin_name_action // Allow hook from any plugin on any (core or plugin) type $res = Plugin::doOneHook($split[1], 'MassiveActionsProcess', $input); // } else if ($plug=isPluginItemType($input["itemtype"])) { // non-normalized name // hook from the plugin defining the type // $res = Plugin::doOneHook($plug['plugin'], 'MassiveActionsProcess', $input); } else { $res = $this->doSpecificMassiveActions($input); } break; } return $res; }
static function resetOCSLocks($objectId, $entities) { global $DB; $res = array('ok' => 0, 'ko' => 0); $object = new $objectId(); $objecttable = getTableForItemType($objectId); $restrict = ""; if ($object->maybeTemplate()) { $restrict = "`is_template` = '0'"; } if ($object->isEntityAssign() && $entities != -1) { $restrict .= getEntitiesRestrictRequest(" AND ", $objecttable, '', $entities, $object->maybeRecursive()); } $count = countElementsInTable($objecttable, $restrict); $items = getAllDatasFromTable($objecttable, $restrict); $computers = array(); if (!empty($items)) { echo "<div class='center'>"; echo "<table class='tab_cadre_fixe'>"; echo "<tr class='tab_bg_2'><td>"; Html::createProgressBar(__('Work in progress...')); echo "</td></tr></table></div></br>\n"; $i = 0; foreach ($items as $item) { //Fields if (PluginOcsinventoryngOcsServer::deleteInOcsArray($item['id'], 'all', "computer_update", true)) { $res['ok']++; } $i++; $computers[$item['id']] = 1; Html::changeProgressBarPosition($i, $count); } foreach (Item_Devices::getDeviceTypes() as $itemtype) { $res[] = Lock::unlockItems($itemtype, 'Computer', $computers); } $res[] = Lock::unlockItems('Monitor', 'Monitor', $computers); $res[] = Lock::unlockItems('Printer', 'Printer', $computers); $res[] = Lock::unlockItems('Peripheral', 'Peripheral', $computers); $res[] = Lock::unlockItems('SoftwareVersion', 'SoftwareVersion', $computers); $res[] = Lock::unlockItems('NetworkPort', 'Computer', $computers); $res[] = Lock::unlockItems('NetworkName', 'NetworkPort', $computers); $res[] = Lock::unlockItems('IPAddress', 'NetworkName', $computers); $res[] = Lock::unlockItems('ComputerDisk', 'ComputerDisk', $computers); $res[] = Lock::unlockItems('ComputerVirtualMachine', 'ComputerVirtualMachine', $computers); Html::changeProgressBarPosition($i, $count, __('Task completed.')); } return $res; }