. * --------------------------------------------------------------------- * */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * NetworkAlias Class * * @since 0.84 ** */ class NetworkAlias extends FQDNLabel { // From CommonDBChild static public $itemtype = 'NetworkName'; static public $items_id = 'networknames_id'; public $dohistory = true; static public $checkParentRights = CommonDBConnexity::HAVE_SAME_RIGHT_ON_ITEM; static function getTypeName($nb = 0) { return _n('Network alias', 'Network aliases', $nb); } function defineTabs($options = []) { $ong = []; $this->addDefaultFormTab($ong); return $ong; } /** * Get the full name (internet name) of a NetworkName * * @param integer $ID ID of the NetworkName * * @return string its internet name, or empty string if invalid NetworkName **/ static function getInternetNameFromID($ID) { $networkAlias = new self(); if ($networkAlias->can($ID, READ)) { return FQDNLabel::getInternetNameFromLabelAndDomainID( $networkAlias->fields["name"], $networkAlias->fields["fqdns_id"]); } return ""; } /** * Print the network alias form * * @param $ID integer ID of the item * @param $options array * - target for the Form * - withtemplate template or basic computer * * @return void **/ function showForm ($ID, $options = []) { // Show only simple form to add / edit if (isset($options['parent'])) { $options['networknames_id'] = $options['parent']->getID(); } $this->initForm($ID, $options); $recursiveItems = $this->recursivelyGetItems(); if (count($recursiveItems) == 0) { return false; } $lastItem = $recursiveItems[count($recursiveItems) - 1]; $options['entities_id'] = $lastItem->getField('entities_id'); $this->showFormHeader($options); echo ""; $this->displayRecursiveItems($recursiveItems, 'Type'); echo " :\n"; if (!($ID > 0)) { echo "\n"; } $this->displayRecursiveItems($recursiveItems, "Link"); echo "" . __('Name') . "\n"; Html::autocompletionTextField($this, "name"); echo "\n"; echo ""; echo "".FQDN::getTypeName().""; Dropdown::show(getItemTypeForTable(getTableNameForForeignKeyField("fqdns_id")), ['value' => $this->fields["fqdns_id"], 'name' => 'fqdns_id', 'entity' => $this->getEntityID(), 'displaywith' => ['view']]); echo ""; echo "".__('Comments').""; echo "\n"; echo "\n"; $this->showFormButtons($options); return true; } /** * @since 0.84 * * @param $itemtype * @param $base HTMLTableBase object * @param $super HTMLTableSuperHeader object (default NULL) * @param $father HTMLTableHeader object (default NULL) * @param $options array **/ static function getHTMLTableHeader($itemtype, HTMLTableBase $base, HTMLTableSuperHeader $super = null, HTMLTableHeader $father = null, array $options = []) { $column_name = __CLASS__; if (isset($options['dont_display'][$column_name])) { return; } if ($itemtype != 'NetworkName') { return; } $content = self::getTypeName(); if (isset($options['column_links'][$column_name])) { $content = "$content"; } $this_header = $base->addHeader($column_name, $content, $super, $father); $this_header->setItemType('NetworkAlias'); } /** * @since 0.84 * * @param $row HTMLTableRow object (default NULL) * @param $item CommonDBTM object (default NULL) * @param $father HTMLTableCell object (default NULL) * @param $options array **/ static function getHTMLTableCellsForItem(HTMLTableRow $row = null, CommonDBTM $item = null, HTMLTableCell $father = null, array $options = []) { global $DB; if (empty($item)) { if (empty($father)) { return; } $item = $father->getItem(); } if ($item->getType() != 'NetworkName') { return; } $column_name = __CLASS__; if (isset($options['dont_display'][$column_name])) { return; } $header = $row->getGroup()->getHeaderByName('Internet', $column_name); if (!$header) { return; } $createRow = (isset($options['createRow']) && $options['createRow']); $options['createRow'] = false; $alias = new self(); $iterator = $DB->request([ 'SELECT' => 'id', 'FROM' => 'glpi_networkaliases', 'WHERE' => ['networknames_id' => $item->getID()] ]); while ($line = $iterator->next()) { if ($alias->getFromDB($line["id"])) { if ($createRow) { $row = $row->createRow(); } $content = "".$alias->getInternetName().""; $row->addCell($header, $content, $father, $alias); } } } /** * \brief Show aliases for an item from its form * Beware that the rendering can be different if readden from direct item form (ie : add new * NetworkAlias, remove, ...) or if readden from item of the item (for instance from the computer * form through NetworkPort::ShowForItem and NetworkName::ShowForItem). * * @param $item NetworkName object * @param $withtemplate integer withtemplate param (default 0) **/ static function showForNetworkName(NetworkName $item, $withtemplate = 0) { global $DB, $CFG_GLPI; $ID = $item->getID(); if (!$item->can($ID, READ)) { return false; } $canedit = $item->canEdit($ID); $rand = mt_rand(); $iterator = $DB->request([ 'FROM' => 'glpi_networkaliases', 'WHERE' => ['networknames_id' => $ID] ]); $number = count($iterator); $aliases = []; while ($line = $iterator->next()) { $aliases[$line["id"]] = $line; } if ($canedit) { echo "\n
"; echo ""; echo ""; echo __('Add a network alias')."\n"; echo "
\n"; } echo "
"; echo "
"; if ($canedit && $number) { Html::openMassiveActionsForm('mass'.__CLASS__.$rand); $massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $number), 'container' => 'mass'.__CLASS__.$rand]; Html::showMassiveActions($massiveactionparams); } echo ""; $header_begin = ""; $header_top = ''; $header_bottom = ''; $header_end = ''; if ($canedit && $number) { $header_top .= ""; $header_bottom .= ""; } $header_end .= ""; $header_end .= ""; $header_end .= ""; $header_end .= ""; echo $header_begin.$header_top.$header_end; foreach ($aliases as $data) { $showviewjs = ($canedit ? "style='cursor:pointer' onClick=\"viewEditAlias".$data['id']."$rand();\"" : ''); echo ""; if ($canedit) { echo ""; } $name = $data["name"]; if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) { $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]); } echo ""; echo ""; } if ($number) { echo $header_begin.$header_bottom.$header_end; } echo "
".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header_top .= "".Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header_bottom .= "".__('Name').""._n('Internet domain', 'Internet domains', 1)."".Entity::getTypeName(1)."
"; Html::showMassiveActionCheckBox(__CLASS__, $data["id"]); echo ""; if ($canedit) { echo "\n\n"; } echo "".$name.""; echo "".Dropdown::getDropdownName("glpi_fqdns", $data["fqdns_id"]); echo "".Dropdown::getDropdownName("glpi_entities", $data["entities_id"]); echo "
"; if ($canedit && $number) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } /** * Show the aliases contained by the alias * * @param CommonGLPI $item the FQDN owning the aliases * @param integer $withtemplate withtemplate param **/ static function showForFQDN(CommonGLPI $item, $withtemplate) { global $DB; $alias = new self(); $address = new NetworkName(); $item->check($item->getID(), READ); if (isset($_GET["start"])) { $start = $_GET["start"]; } else { $start = 0; } if (!empty($_GET["order"])) { $order = $_GET["order"]; } else { $order = "alias"; } $number = countElementsInTable($alias->getTable(), ['fqdns_id' => $item->getID() ]); echo "
"; if ($number < 1) { echo ""; echo ""; echo "
".self::getTypeName(1)."".__('No item found')."
\n"; } else { Html::printAjaxPager(self::getTypeName($number), $start, $number); echo ""; echo ""; // Alias echo ""; echo ""; echo "\n"; Session::initNavigateListItems($item->getType(), //TRANS : %1$s is the itemtype name, %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), self::getTypeName(1), $item->fields['name'])); $iterator = $DB->request([ 'SELECT' => [ 'glpi_networkaliases.id AS alias_id', 'glpi_networkaliases.name AS alias', 'glpi_networknames.id AS address_id', 'glpi_networkaliases.comment AS comment' ], 'FROM' => 'glpi_networkaliases', 'INNER JOIN' => [ 'glpi_networknames' => [ 'ON' => [ 'glpi_networkaliases' => 'networknames_id', 'glpi_networknames' => 'id' ] ] ], 'WHERE' => ['glpi_networkaliases.fqdns_id' => $item->getID()], 'ORDERBY' => $order, 'LIMIT' => $_SESSION['glpilist_limit'], 'START' => $start ]); while ($data = $iterator->next()) { Session::addToNavigateListItems($alias->getType(), $data["alias_id"]); if ($address->getFromDB($data["address_id"])) { echo ""; echo ""; echo ""; echo ""; echo "\n"; } } echo "
".self::getTypeName(1). "".__("Computer's name"). "".__('Comments')."
" . $data['alias']. "".$address->getInternetName(). "".$data['comment']."
\n"; Html::printAjaxPager(self::getTypeName($number), $start, $number); } echo "
\n"; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { switch ($item->getType()) { case 'NetworkName' : self::showForNetworkName($item, $withtemplate); break; case 'FQDN' : self::showForFQDN($item, $withtemplate); break; } } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if ($item->getID() && $item->can($item->getField('id'), READ)) { $nb = 0; if ($_SESSION['glpishow_count_on_tabs']) { switch ($item->getType()) { case 'NetworkName' : $nb = countElementsInTable($this->getTable(), ['networknames_id' => $item->getID() ]); break; case 'FQDN' : $nb = countElementsInTable($this->getTable(), ['fqdns_id' => $item->getID() ]); } } return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb); } return ''; } function rawSearchOptions() { $tab = parent::rawSearchOptions(); $tab[] = [ 'id' => '12', 'table' => 'glpi_fqdns', 'field' => 'fqdn', 'name' => FQDN::getTypeName(1), 'datatype' => 'string' ]; $tab[] = [ 'id' => '20', 'table' => 'glpi_networknames', 'field' => 'name', 'name' => NetworkName::getTypeName(1), 'massiveaction' => false, 'datatype' => 'dropdown' ]; return $tab; } }