. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } class NetworkPort_Vlan extends CommonDBRelation { // From CommonDBRelation static public $itemtype_1 = 'NetworkPort'; static public $items_id_1 = 'networkports_id'; static public $itemtype_2 = 'Vlan'; static public $items_id_2 = 'vlans_id'; static public $checkItem_2_Rights = self::HAVE_VIEW_RIGHT_ON_ITEM; /** * @since 0.84 **/ function getForbiddenStandardMassiveAction() { $forbidden = parent::getForbiddenStandardMassiveAction(); $forbidden[] = 'update'; return $forbidden; } /** * @param $portID * @param $vlanID **/ function unassignVlan($portID, $vlanID) { $this->getFromDBByCrit([ 'networkports_id' => $portID, 'vlans_id' => $vlanID ]); return $this->delete($this->fields); } /** * @param $port * @param $vlan * @param $tagged **/ function assignVlan($port, $vlan, $tagged) { $input = ['networkports_id' => $port, 'vlans_id' => $vlan, 'tagged' => $tagged]; return $this->add($input); } /** * @param $port NetworkPort object **/ static function showForNetworkPort(NetworkPort $port) { global $DB, $CFG_GLPI; $ID = $port->getID(); if (!$port->can($ID, READ)) { return false; } $canedit = $port->canEdit($ID); $rand = mt_rand(); $iterator = $DB->request([ 'SELECT' => [ 'glpi_networkports_vlans.id as assocID', 'glpi_networkports_vlans.tagged', 'glpi_vlans.*' ], 'FROM' => 'glpi_networkports_vlans', 'LEFT JOIN' => [ 'glpi_vlans' => [ 'ON' => [ 'glpi_networkports_vlans' => 'vlans_id', 'glpi_vlans' => 'id' ] ] ], 'WHERE' => ['networkports_id' => $ID] ]); $number = count($iterator); $vlans = []; $used = []; while ($line = $iterator->next()) { $used[$line["id"]] = $line["id"]; $vlans[$line["assocID"]] = $line; } if ($canedit) { echo "
\n"; echo "
\n"; echo "\n"; echo ""; echo ""; echo ""; echo ""; echo "\n"; echo "
".__('Associate a VLAN')."
"; echo ""; Vlan::dropdown(['used' => $used]); echo "".__('Tagged').""; echo "
\n"; Html::closeForm(); echo "
\n"; } 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 .= ""; $header_end .= ""; echo $header_begin.$header_top.$header_end; $used = []; foreach ($vlans as $data) { 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 ""; echo ""; echo ""; } if ($number) { echo $header_begin.$header_top.$header_end; } echo "
"; $header_top .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand).""; $header_bottom .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand)."".__('Name')."".Entity::getTypeName(1)."".__('Tagged')."".__('ID TAG')."
"; Html::showMassiveActionCheckBox(__CLASS__, $data["assocID"]); echo " ".$name. ""; echo "".Dropdown::getDropdownName("glpi_entities", $data["entities_id"]); echo "".Dropdown::getYesNo($data["tagged"])."".$data["tag"]."
"; if ($canedit && $number) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } /** * @param $portID **/ static function getVlansForNetworkPort($portID) { global $DB; $vlans = []; $iterator = $DB->request([ 'SELECT' => 'vlans_id', 'FROM' => 'glpi_networkports_vlans', 'WHERE' => ['networkports_id' => $portID] ]); while ($data = $iterator->next()) { $vlans[$data['vlans_id']] = $data['vlans_id']; } return $vlans; } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (!$withtemplate) { $nb = 0; switch ($item->getType()) { case 'NetworkPort' : if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable($this->getTable(), ["networkports_id" => $item->getID()]); } return self::createTabEntry(Vlan::getTypeName(), $nb); } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { if ($item->getType()=='NetworkPort') { self::showForNetworkPort($item); } return true; } /** * @since 0.85 * * @see CommonDBRelation::getRelationMassiveActionsSpecificities() **/ static function getRelationMassiveActionsSpecificities() { $specificities = parent::getRelationMassiveActionsSpecificities(); // Set the labels for add_item and remove_item $specificities['button_labels']['add'] = _sx('button', 'Associate'); $specificities['button_labels']['remove'] = _sx('button', 'Dissociate'); return $specificities; } static function showRelationMassiveActionsSubForm(MassiveAction $ma, $peer_number) { if ($ma->getAction() == 'add') { echo "

". __('Tagged'). Html::getCheckbox(['name' => 'tagged']); } } static function getRelationInputForProcessingOfMassiveActions($action, CommonDBTM $item, array $ids, array $input) { if ($action == 'add') { return ['tagged' => $input['tagged']]; } return []; } }