/** * if Computer_Item deleted * * @param $comp Computer_Item object **/ static function purgeComputer_Item(Computer_Item $comp) { if ($device = getItemForItemtype($comp->fields['itemtype'])) { if ($device->getFromDB($comp->fields['items_id'])) { if (isset($comp->input['_ocsservers_id'])) { $ocsservers_id = $comp->input['_ocsservers_id']; } else { $ocsservers_id = PluginOcsinventoryngOcsServer::getByMachineID($comp->fields['computers_id']); } if ($ocsservers_id > 0) { //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, $comp->fields['itemtype']); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi if ($decoConf == "delete") { $tmp["id"] = $comp->fields['items_id']; $device->delete($tmp, 1); //Put periph in dustbin } else { if ($decoConf == "trash") { $tmp["id"] = $comp->fields['items_id']; $device->delete($tmp, 0); } } } } // $ocsservers_id>0 } } }
/** * if Computer_Item deleted * * @param $comp Computer_Item object **/ static function purgeComputer_Item(Computer_Item $comp) { global $DB; if ($device = getItemForItemtype($comp->fields['itemtype'])) { if ($device->getFromDB($comp->fields['items_id'])) { if (isset($comp->input['_ocsservers_id'])) { $ocsservers_id = $comp->input['_ocsservers_id']; } else { $ocsservers_id = PluginOcsinventoryngOcsServer::getByMachineID($comp->fields['computers_id']); } if ($ocsservers_id > 0) { //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, $comp->fields['itemtype']); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi // if ($decoConf == "delete") { // $tmp["id"] = $comp->fields['items_id']; // $device->delete(array('id' => $tmp['id']), 1); // Put periph in dustbin // } else if ($decoConf == "trash") { // $tmp["id"] = $comp->fields['items_id']; // $device->delete(array('id' => $tmp['id']), 0); // } if ($decoConf == "delete") { $tmp["id"] = $comp->getID(); $query = "DELETE\n FROM `glpi_computers_items`\n WHERE `id`='" . $tmp['id'] . "'"; $result = $DB->query($query); //Put periph in dustbin } else { if ($decoConf == "trash") { $tmp["id"] = $comp->getID(); $query = "UPDATE\n `glpi_computers_items`\n\t\t\t\t\t\t SET `is_deleted` = 1\n WHERE `id`='" . $tmp['id'] . "'"; $result = $DB->query($query); } } } } // $ocsservers_id>0 } } }
/** * * Import peripherals from OCS * @since 1.0 * @param $cfg_ocs OCSNG mode configuration * @param $computers_id computer's id in GLPI * @param $ocsid computer's id in OCS * @param $ocsservers_id OCS server id * @param $entity the entity in which the peripheral will be created * @param $dohistory record in history link between peripheral and computer */ static function importPeripheral($cfg_ocs, $computers_id, $ocsservers_id, $ocsComputer, $entity, $dohistory) { global $DB; $already_processed = array(); $p = new Peripheral(); $conn = new Computer_Item(); if (isset($ocsComputer["INPUTS"])) { if (count($ocsComputer["INPUTS"]) > 0) { foreach ($ocsComputer["INPUTS"] as $peripheral) { if ($peripheral["CAPTION"] !== '') { $peripherals[] = $peripheral; } } if (count($peripherals) > 0) { foreach ($peripherals as $peripheral) { $peripheral = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($peripheral)); $periph = array(); $periph["name"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["CAPTION"]); //Look for a monitor with the same name (and serial if possible) already connected //to this computer $query = "SELECT `p`.`id`, `gci`.`is_deleted`\n FROM `glpi_printers` as `p`, `glpi_computers_items` as `gci`\n WHERE `p`.`id` = `gci`.`items_id`\n AND `gci`.`is_dynamic`='1'\n AND `computers_id`='{$computers_id}'\n AND `itemtype`='Peripheral'\n AND `p`.`name`='" . $periph["name"] . "'"; $results = $DB->query($query); $id = false; $lock = false; if ($DB->numrows($results) > 0) { $id = $DB->result($results, 0, 'id'); $lock = $DB->result($results, 0, 'is_deleted'); } if (!$id) { // Clean peripheral object $p->reset(); if ($peripheral["MANUFACTURER"] != "NULL") { $periph["brand"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["MANUFACTURER"]); } if ($peripheral["INTERFACE"] != "NULL") { $periph["comment"] = self::encodeOcsDataInUtf8($cfg_ocs["ocs_db_utf8"], $peripheral["INTERFACE"]); } $periph["peripheraltypes_id"] = Dropdown::importExternal('PeripheralType', $peripheral["TYPE"]); $id_periph = 0; if ($cfg_ocs["import_periph"] == 1) { //Config says : manage peripherals as global //check if peripherals already exists in GLPI $periph["is_global"] = 1; $query = "SELECT `id`\n FROM `glpi_peripherals`\n WHERE `name` = '" . $periph["name"] . "'\n AND `is_global` = '1'\n AND `entities_id` = '{$entity}'"; $result_search = $DB->query($query); if ($DB->numrows($result_search) > 0) { //Periph is already in GLPI //Do not import anything just get periph ID for link $id_periph = $DB->result($result_search, 0, "id"); } else { $input = $periph; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } else { if ($cfg_ocs["import_periph"] == 2) { //Config says : manage peripherals as single units //Import all peripherals as non global. $input = $periph; $input["is_global"] = 0; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $input["entities_id"] = $entity; $id_periph = $p->add($input); } } if ($id_periph) { $already_processed[] = $id_periph; $conn = new Computer_Item(); if ($connID = $conn->add(array('computers_id' => $computers_id, 'itemtype' => 'Peripheral', 'items_id' => $id_periph, '_no_history' => !$dohistory, 'is_dynamic' => 1))) { //Update column "is_deleted" set value to 0 and set status to default $input = array(); $input["id"] = $id_periph; $input["is_deleted"] = 0; $input["entities_id"] = $entity; if ($cfg_ocs["states_id_default"] > 0) { $input["states_id"] = $cfg_ocs["states_id_default"]; } $p->update($input); } } } else { $already_processed[] = $id; } } } } } //Look for all peripherals, not locked, not linked to the computer anymore $query = "SELECT `id`\n FROM `glpi_computers_items`\n WHERE `itemtype`='Peripheral'\n AND `computers_id`='{$computers_id}'\n AND `is_dynamic`='1'\n AND `is_deleted`='0'"; if (!empty($already_processed)) { $query .= "AND `items_id` NOT IN (" . implode(',', $already_processed) . ")"; } foreach ($DB->request($query) as $data) { // Delete all connexions //Get OCS configuration $ocs_config = PluginOcsinventoryngOcsServer::getConfig($ocsservers_id); //Get the management mode for this device $mode = PluginOcsinventoryngOcsServer::getDevicesManagementMode($ocs_config, 'Peripheral'); $decoConf = $ocs_config["deconnection_behavior"]; //Change status if : // 1 : the management mode IS NOT global // 2 : a deconnection's status have been defined // 3 : unique with serial if ($mode >= 2 && strlen($decoConf) > 0) { //Delete periph from glpi if ($decoConf == "delete") { $query = "DELETE\n FROM `glpi_computers_items`\n WHERE `id`='" . $data['id'] . "'"; $result = $DB->query($query); //Put periph in dustbin } else { if ($decoConf == "trash") { $query = "UPDATE\n `glpi_computers_items`\n SET `is_deleted` = 1\n WHERE `id`='" . $data['id'] . "'"; $result = $DB->query($query); } } } // foreach ($DB->request($query) as $data){ // Delete all connexions // $conn->delete(array('id' => $data['id'], // '_ocsservers_id' => $ocsservers_id), true); } }