grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026.
--------------------------------------------------------------------------
*/
/** @file
* @brief
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/// NetworkPortMigration class : class of unknown objects defined inside the NetworkPort before 0.84
/// @since 0.84
class NetworkPortMigration extends CommonDBChild {
// From CommonDBChild
static public $itemtype = 'itemtype';
static public $items_id = 'items_id';
static public $mustBeAttached = true;
static $rightname = 'networking';
static function getTypeName($nb=0) {
return __('Network port migration');
}
static function canCreate() {
return false;
}
private function cleanDatabase() {
global $DB;
$networkport = new NetworkPort();
if ($networkport->getFromDB($this->getID())) {
if (!in_array($networkport->fields['instantiation_type'],
NetworkPort::getNetworkPortInstantiations())) {
$networkport->delete($networkport->fields);
}
}
if (countElementsInTable($this->getTable()) == 0) {
$query = "DROP TABLE `".$this->getTable()."`";
$DB->query($query);
}
}
function post_purgeItem() {
$this->cleanDatabase();
parent::post_purgeItem();
}
function post_deleteItem() {
$this->cleanDatabase();
parent::post_deleteItem();
}
/**
* @see CommonGLPI::defineTabs()
*
* @since version 0.85
**/
function defineTabs($options=array()) {
$ong = array();
$this->addDefaultFormTab($ong);
return $ong;
}
static function getMotives() {
return array( 'unknown_interface_type'
=> __('Undefined interface'),
'invalid_network'
=> __('Invalid network (already defined or with invalid addresses)'),
'invalid_gateway'
=> __('Gateway not include inside the network'),
'invalid_address'
=> __('Invalid IP address') );
}
function showForm($ID, $options=array()) {
global $CFG_GLPI, $DB;
if (!self::canView()) {
return false;
}
$this->check($ID, READ);
$recursiveItems = $this->recursivelyGetItems();
if (count($recursiveItems) > 0) {
$lastItem = $recursiveItems[count($recursiveItems) - 1];
$lastItem_entities_id = $lastItem->getField('entities_id');
} else {
$lastItem_entities_id = $_SESSION['glpiactive_entity'];
}
$options['entities_id'] = $lastItem_entities_id;
$this->showFormHeader($options);
$options['canedit'] = false;
$options['candel'] = false;
$number_errors = 0;
foreach (self::getMotives() as $key => $name) {
if ($this->fields[$key] == 1) {
$number_errors ++;
}
}
$motives = self::getMotives();
$interface_cell = "td";
$address_cell = "td";
$network_cell = "td";
$gateway_cell = "td";
$address = new IPAddress();
$netmask = new IPNetmask();
$number_real_errors = 0;
if ((!$address->setAddressFromString($this->fields['ip']))
|| (!$netmask->setNetmaskFromString($this->fields['netmask'],
$address->getVersion()))) {
unset ($address);
unset ($netmask);
} else {
$network = new IPNetwork();
$params = array("address" => $address,
"netmask" => $netmask);
if (isset($this->fields["address"])) {
$params["exclude IDs"] = $this->fields["address"];
}
if (isset($this->fields["entities_id"])) {
$entity = $this->fields["entities_id"];
} else {
$entity = -1;
}
$networkports_ids = IPNetwork::searchNetworks("equals", $params, $entity, false);
if (count($networkports_ids) == 0) {
unset($network);
} else {
$network->getFromDB($networkports_ids[0]);
}
}
if ($this->fields['unknown_interface_type'] == 1) {
$options['canedit'] = true;
$number_real_errors ++;
$interface_cell = "th";
echo "
| ". $motives['unknown_interface_type'] ." | \n".
"" .__('Transform this network port to');
echo " | ";
Dropdown::showItemTypes('transform_to', NetworkPort::getNetworkPortInstantiations(),
array('value' => "NetworkPortEthernet"));
echo " |
\n";
}
if ($this->fields['invalid_network'] == 1) {
$number_real_errors ++;
$network_cell = "th";
$address_cell = "th";
echo "| " .$motives['invalid_network'] ." | \n";
if (isset($network)) {
printf(__('Network port information conflicting with %s'), $network->getLink());
} else {
if (!isset($address) || !isset($netmask)) {
_e('Invalid address or netmask');
} else {
_e('No conflicting network');
}
echo " " .
__('you may have to add a network')."";
}
echo " |
\n";
}
if ($this->fields['invalid_gateway'] == 1) {
$number_real_errors ++;
$gateway_cell = "th";
echo "| " . $motives['invalid_gateway'] ." | \n";
if (isset($network)) {
printf(__('Append a correct gateway to the network %s'), $network->getLink());
} else {
printf(__('%1$s: %2$s'), __('Unknown network'),
"".__('Add a network')."
");
}
echo " |
\n";
}
if ($this->fields['invalid_address'] == 1) {
$number_real_errors ++;
$address_cell = "th";
echo "| " .$motives['invalid_address'] ." | \n";
$networkPort = new NetworkPort();
if ($networkPort->getFromDB($this->getID())) {
$number_real_errors ++;
echo "".
__('Add a correct IP to the network port') . "";
} else {
_e('Unknown network port');
}
echo " |
\n";
}
if ($number_real_errors == 0) {
echo "| " .
__('I don\'t understand why this migration error is not deleted.');
echo " | ";
Html::showSimpleForm($this->getFormURL(), 'delete', __('You can delete this migration error'),
array('id' => $this->getID()));
echo " |
\n";
} else {
echo "| " . __('At all events') . " | \n";
echo "";
Html::showSimpleForm($this->getFormURL(), 'delete', __('You can delete this migration error'),
array('id' => $this->getID()));
echo " |
\n";
}
echo "| |
\n";
echo "| " .__('Original network port information') ." | ".
"
\n";
echo "| ";
$this->displayRecursiveItems($recursiveItems, 'Type');
echo " | \n";
$this->displayRecursiveItems($recursiveItems, "Link");
echo " | \n";
echo "".__('Comments')." | ";
echo "" . $this->fields["comment"] . " |
\n";
echo "| ". __('Network address') ." | \n";
echo "<$network_cell>" . $this->fields['subnet'] . "$network_cell>\n";
echo "". IPNetmask::getTypeName(1) ." | \n";
echo "<$network_cell>" . $this->fields['netmask'] . "$network_cell>
\n";
echo "| ". IPAddress::getTypeName(1) ." | \n";
echo "<$address_cell>" . $this->fields['ip'] . "$address_cell>\n";
echo "". __('Gateway') ." | \n";
echo "<$gateway_cell>" . $this->fields['gateway'] . "$gateway_cell>
\n";
echo "| ". __('Network interface') ." | <$interface_cell>\n";
if (TableExists('glpi_networkinterfaces')) {
$query = "SELECT `name`
FROM `glpi_networkinterfaces`
WHERE `id`='".$this->fields['networkinterfaces_id']."'";
$result = $DB->query($query);
if ($DB->numrows($result) > 0) {
$row = $DB->fetch_assoc($result);
echo $row['name'];
} else {
_e('Unknown interface');
}
}
echo "$interface_cell>";
echo "<$interface_cell>$interface_cell>";
echo "<$interface_cell>$interface_cell>
\n";
$this->showFormButtons($options);
}
/**
* @see CommonDBTM::getSpecificMassiveActions()
**/
function getSpecificMassiveActions($checkitem=NULL) {
$isadmin = static::canUpdate();
$actions = parent::getSpecificMassiveActions($checkitem);
if ($isadmin) {
$actions[__CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'transform_to']
= __('Transform this network port to');
}
return $actions;
}
/**
* @since version 0.85
*
* @see CommonDBTM::showMassiveActionsSubForm()
**/
static function showMassiveActionsSubForm(MassiveAction $ma) {
global $CFG_GLPI;
switch ($ma->getAction()) {
case 'transform_to' :
Dropdown::showItemTypes('transform_to', NetworkPort::getNetworkPortInstantiations(),
array('value' => 'NetworkPortEthernet'));
echo "
";
echo Html::submit(_x('button','Post'), array('name' => 'massiveaction'))."";
return true;
}
return parent::showMassiveActionsSubForm($ma);
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item,
array $ids) {
global $DB;
switch ($ma->getAction()) {
case 'transform_to':
$input = $ma->getInput();
if (isset($input["transform_to"]) && !empty($input["transform_to"])) {
$networkport = new NetworkPort();
foreach ($ids as $id) {
if ($networkport->canEdit($id)
&& $item->can($id, DELETE)) {
if (empty($networkport->fields['instantiation_type'])) {
if ($networkport->switchInstantiationType($input['transform_to']) !== false) {
$instantiation = $networkport->getInstantiation();
$input2 = $item->fields;
$input2['networkports_id'] = $input2['id'];
unset($input2['id']);
if ($instantiation->add($input2)) {
$item->delete(array('id' => $id));
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
$ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
$ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
$ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
$ma->addMessage($networkport->getErrorMessage(ERROR_RIGHT));
}
}
} else {
$ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
}
return;
}
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
}
function getSearchOptions() {
global $CFG_GLPI;
$tab = parent::getSearchOptions();
$optionIndex = 10;
// From 10 to 14
foreach (self::getMotives() as $motive => $name) {
$tab[$optionIndex]['table'] = $this->getTable();
$tab[$optionIndex]['field'] = $motive;
$tab[$optionIndex]['name'] = $name;
$tab[$optionIndex]['datatype'] = 'bool';
$optionIndex ++;
}
$tab[20]['table'] = $this->getTable();
$tab[20]['field'] = 'ip';
$tab[20]['datatype'] = 'ip';
$tab[20]['name'] = IPAddress::getTypeName(1);
$tab[21]['table'] = $this->getTable();
$tab[21]['field'] = 'netmask';
$tab[21]['datatype'] = 'string';
$tab[21]['name'] = IPNetmask::getTypeName(1);
$tab[22]['table'] = $this->getTable();
$tab[22]['field'] = 'subnet';
$tab[22]['datatype'] = 'string';
$tab[22]['name'] = __('Network address');
$tab[23]['table'] = $this->getTable();
$tab[23]['field'] = 'gateway';
$tab[23]['datatype'] = 'string';
$tab[23]['name'] = IPAddress::getTypeName(1);
if (TableExists('glpi_networkinterfaces')) {
$tab[24]['table'] = 'glpi_networkinterfaces';
$tab[24]['field'] = 'name';
$tab[24]['datatype'] = 'dropdown';
$tab[24]['name'] = __('Network interface');
}
return $tab;
}
/**
* @param $group HTMLTableGroup object
* @param $super HTMLTableSuperHeader object
* @param $options array
* @param $internet_super
* @param $father
**/
static function getMigrationInstantiationHTMLTableHeaders(HTMLTableGroup $group,
HTMLTableSuperHeader $super,
HTMLTableSuperHeader $internet_super = NULL,
HTMLTableHeader $father=NULL,
array $options=array()) {
// TODO : study to display the correct informations for this undefined NetworkPort
return NULL;
}
}
?>