Example #1
0
 static function showForItem(CommonGLPI $item, $withtemplate = 0)
 {
     global $CFG_GLPI;
     $is_device = $item instanceof CommonDevice;
     $ID = $item->getField('id');
     if (!$item->can($ID, READ)) {
         return false;
     }
     $canedit = $withtemplate != 2 && $item->canEdit($ID) && Session::haveRightsOr('device', array(UPDATE, PURGE));
     echo "<div class='spaced'>";
     $rand = mt_rand();
     if ($canedit) {
         echo "\n<form id='form_device_add{$rand}' name='form_device_add{$rand}'\n                  action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "' method='post'>\n";
         echo "\t<input type='hidden' name='items_id' value='{$ID}'>\n";
         echo "\t<input type='hidden' name='itemtype' value='" . $item->getType() . "'>\n";
     }
     $table = new HTMLTableMain();
     $table->setTitle(_n('Component', 'Components', Session::getPluralNumber()));
     if ($canedit) {
         $delete_all_column = $table->addHeader('delete all', Html::getCheckAllAsCheckbox("form_device_action{$rand}", '__RAND__'));
         $delete_all_column->setHTMLClass('center');
     } else {
         $delete_all_column = NULL;
     }
     $column_label = $is_device ? _n('Item', 'Items', Session::getPluralNumber()) : __('Type of component');
     $common_column = $table->addHeader('common', $column_label);
     $specific_column = $table->addHeader('specificities', __('Specificities'));
     $specific_column->setHTMLClass('center');
     $dynamic_column = '';
     if ($item->isDynamic()) {
         $dynamic_column = $table->addHeader('is_dynamic', __('Automatic inventory'));
         $dynamic_column->setHTMLClass('center');
     }
     if ($canedit) {
         $massiveactionparams = array('container' => "form_device_action{$rand}", 'fixed' => false, 'display_arrow' => false);
         $content = array(array('function' => 'Html::showMassiveActions', 'parameters' => array($massiveactionparams)));
         $delete_column = $table->addHeader('delete one', $content);
         $delete_column->setHTMLClass('center');
     } else {
         $delete_column = NULL;
     }
     $table_options = array('canedit' => $canedit, 'rand' => $rand);
     if ($is_device) {
         Session::initNavigateListItems(static::getType(), sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName()));
         foreach (array_merge(array(''), self::getConcernedItems()) as $itemtype) {
             $table_options['itemtype'] = $itemtype;
             $link = getItemForItemtype(static::getType());
             $link->getTableGroup($item, $table, $table_options, $delete_all_column, $common_column, $specific_column, $delete_column, $dynamic_column);
         }
     } else {
         $devtypes = array();
         foreach (self::getItemAffinities($item->getType()) as $link_type) {
             $devtypes[] = $link_type::getDeviceType();
             $link = getItemForItemtype($link_type);
             Session::initNavigateListItems($link_type, sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName()));
             $link->getTableGroup($item, $table, $table_options, $delete_all_column, $common_column, $specific_column, $delete_column, $dynamic_column);
         }
     }
     if ($canedit) {
         echo "<table class='tab_cadre_fixe'><tr class='tab_bg_1'><td>";
         echo __('Add a new component') . "</td><td class=left width='70%'>";
         if ($is_device) {
             Dropdown::showNumber('number_devices_to_add', array('value' => 0, 'min' => 0, 'max' => 10));
         } else {
             Dropdown::showSelectItemFromItemtypes(array('itemtype_name' => 'devicetype', 'items_id_name' => 'devices_id', 'itemtypes' => $devtypes, 'entity_restrict' => $item->getEntityID(), 'showItemSpecificity' => $CFG_GLPI['root_doc'] . '/ajax/selectUnaffectedOrNewItem_Device.php'));
         }
         echo "</td><td>";
         echo "<input type='submit' class='submit' name='add' value='" . _sx('button', 'Add') . "'>";
         echo "</td></tr></table>";
         Html::closeForm();
     }
     if ($canedit) {
         echo "\n<form id='form_device_action{$rand}' name='form_device_action{$rand}'\n                  action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "' method='post'>\n";
         echo "\t<input type='hidden' name='items_id' value='{$ID}'>\n";
         echo "\t<input type='hidden' name='itemtype' value='" . $item->getType() . "'>\n";
     }
     $table->display(array('display_super_for_each_group' => false, 'display_title_for_each_group' => false));
     if ($canedit) {
         //          echo "<input type='submit' class='submit' name='updateall' value='" .
         //               _sx('button', 'Save')."'>";
         Html::closeForm();
     }
     echo "</div>";
     // Force disable selected items
     $_SESSION['glpimassiveactionselected'] = array();
 }
Example #2
0
 /**
  * Show ports for an item
  *
  * @param $item                     CommonDBTM object
  * @param $withtemplate   integer   withtemplate param (default '')
  **/
 static function showForItem(CommonDBTM $item, $withtemplate = '')
 {
     global $DB, $CFG_GLPI;
     $rand = mt_rand();
     $itemtype = $item->getType();
     $items_id = $item->getField('id');
     if (!Session::haveRight('networking', 'r') || !$item->can($items_id, 'r')) {
         return false;
     }
     $netport = new self();
     $netport->item = $item;
     if ($itemtype == 'NetworkPort') {
         $canedit = false;
     } else {
         $canedit = $item->can($items_id, 'w');
     }
     $showmassiveactions = false;
     if ($withtemplate != 2) {
         $showmassiveactions = $canedit;
     }
     // Show Add Form
     if ($canedit && (empty($withtemplate) || $withtemplate != 2)) {
         echo "\n<form method='get' action='" . $netport->getFormURL() . "'>\n";
         echo "<input type='hidden' name='items_id' value='" . $item->getID() . "'>\n";
         echo "<input type='hidden' name='itemtype' value='" . $item->getType() . "'>\n";
         echo "<div class='firstbloc'><table class='tab_cadre_fixe'>\n";
         echo "<tr class='tab_bg_2'><td class='center'>\n";
         _e('Network port type to be added');
         echo "&nbsp;";
         $instantiations = array();
         foreach (self::getNetworkPortInstantiations() as $inst_type) {
             if (call_user_func(array($inst_type, 'canCreate'))) {
                 $instantiations[$inst_type] = call_user_func(array($inst_type, 'getTypeName'));
             }
         }
         Dropdown::showFromArray('instantiation_type', $instantiations, array('value' => 'NetworkPortEthernet'));
         echo "</td>\n";
         echo "<td class='tab_bg_2 center' width='50%'>";
         _e('Add several ports');
         echo "&nbsp;<input type='checkbox' name='several' value='1'></td>\n";
         echo "<td>\n";
         echo "<input type='submit' name='create' value=\"" . _sx('button', 'Add') . "\" class='submit'>\n";
         echo "</td></tr></table></div>\n";
         Html::closeForm();
     }
     if ($showmassiveactions) {
         $checkbox_column = true;
         Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
     } else {
         $checkbox_column = false;
     }
     $is_active_network_port = false;
     Session::initNavigateListItems('NetworkPort', sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName()));
     if ($itemtype == 'NetworkPort') {
         $porttypes = array('NetworkPortAlias', 'NetworkPortAggregate');
     } else {
         $porttypes = self::getNetworkPortInstantiations();
         // Manage NetworkportMigration
         $porttypes[] = '';
     }
     $display_options = self::getDisplayOptions($itemtype);
     $table = new HTMLTableMain();
     $number_port = self::countForItem($item);
     $table_options = array('canedit' => $canedit, 'display_options' => &$display_options);
     // Make table name and add the correct show/hide parameters
     $table_name = sprintf(__('%1$s: %2$d'), self::getTypeName($number_port), $number_port);
     // Add the link to the popup to display the options ...
     $table_namelink = self::getDisplayOptionsLink($itemtype);
     $table_name = sprintf(__('%1$s - %2$s'), $table_name, $table_namelink);
     $table->setTitle($table_name);
     $c_main = $table->addHeader('main', self::getTypeName(2));
     if ($display_options['dynamic_import'] && $item->isDynamic()) {
         $table_options['display_isDynamic'] = true;
     } else {
         $table_options['display_isDynamic'] = false;
     }
     if ($display_options['characteristics']) {
         $c_instant = $table->addHeader('Instantiation', __('Characteristics'));
         $c_instant->setHTMLClass('center');
     }
     if ($display_options['internet']) {
         $options = array('names' => 'NetworkName', 'aliases' => 'NetworkAlias', 'ipaddresses' => 'IPAddress', 'ipnetworks' => 'IPNetwork');
         $table_options['dont_display'] = array();
         foreach ($options as $option => $itemtype_for_option) {
             if (!$display_options[$option]) {
                 $table_options['dont_display'][$itemtype_for_option] = true;
             }
         }
         $c_network = $table->addHeader('Internet', _n(__('Internet information'), __('Internet information'), 2));
         $c_network->setHTMLClass('center');
     } else {
         $c_network = NULL;
     }
     foreach ($porttypes as $portType) {
         if (empty($portType)) {
             $group_name = 'Migration';
             $group_title = __('Network ports waiting for manual migration');
         } else {
             $group_name = $portType;
             $group_title = $portType::getTypeName(2);
         }
         $t_group = $table->createGroup($group_name, $group_title);
         if ($withtemplate != 2 && $canedit) {
             $c_checkbox = $t_group->addHeader('checkbox', Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand, '__RAND__'), $c_main);
         } else {
             $c_checkbox = NULL;
         }
         $c_number = $t_group->addHeader('NetworkPort', "#", $c_main);
         $c_name = $t_group->addHeader("Name", __('Name'), $c_main);
         $c_name->setItemType('NetworkPort');
         $c_name->setHTMLClass('center');
         if ($table_options['display_isDynamic']) {
             $c_dynamic = $t_group->addHeader("Dynamic", __('Automatic inventory'), $c_main);
             $c_dynamic->setHTMLClass('center');
         }
         if ($display_options['characteristics']) {
             if (empty($portType)) {
                 NetworkPortMigration::getMigrationInstantiationHTMLTableHeaders($t_group, $c_instant, $c_network, NULL, $table_options);
             } else {
                 $instantiation = new $portType();
                 $instantiation->getInstantiationHTMLTableHeaders($t_group, $c_instant, $c_network, NULL, $table_options);
                 unset($instantiation);
             }
         }
         if ($display_options['internet'] && !$display_options['characteristics']) {
             NetworkName::getHTMLTableHeader(__CLASS__, $t_group, $c_network, NULL, $table_options);
         }
         if ($itemtype == 'NetworkPort') {
             switch ($portType) {
                 case 'NetworkPortAlias':
                     $search_table = 'glpi_networkportaliases';
                     $search_request = "`networkports_id_alias`='{$items_id}'";
                     break;
                 case 'NetworkPortAggregate':
                     $search_table = 'glpi_networkportaggregates';
                     $search_request = "`networkports_id_list` like '%\"{$items_id}\"%'";
                     break;
             }
             $query = "SELECT `networkports_id` AS id\n                      FROM  `{$search_table}`\n                      WHERE {$search_request}";
         } else {
             $query = "SELECT `id`\n                      FROM `glpi_networkports`\n                      WHERE `items_id` = '{$items_id}'\n                            AND `itemtype` = '{$itemtype}'\n                            AND `instantiation_type` = '{$portType}'\n                            AND `is_deleted` = '0'\n                      ORDER BY `name`,\n                               `logical_number`";
         }
         if ($result = $DB->query($query)) {
             echo "<div class='spaced'>";
             $number_port = $DB->numrows($result);
             if ($number_port != 0) {
                 $is_active_network_port = true;
                 $save_canedit = $canedit;
                 if (!empty($portType)) {
                     $name = sprintf(__('%1$s (%2$s)'), self::getTypeName($number_port), call_user_func(array($portType, 'getTypeName')));
                     $name = sprintf(__('%1$s: %2$s'), $name, $number_port);
                 } else {
                     $name = __('Network ports waiting for manual migration');
                     $canedit = false;
                 }
                 while ($devid = $DB->fetch_row($result)) {
                     $t_row = $t_group->createRow();
                     $netport->getFromDB(current($devid));
                     // No massive action for migration ports
                     if ($withtemplate != 2 && $canedit && !empty($portType)) {
                         $ce_checkbox = $t_row->addCell($c_checkbox, "<input type='checkbox' name='item[" . $netport->fields["id"] . "]' value='1'>");
                     } else {
                         $ce_checkbox = NULL;
                     }
                     $content = "<span class='b'>";
                     // Display link based on default rights
                     if ($save_canedit && $withtemplate != 2) {
                         if (!empty($portType)) {
                             $content .= "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/networkport.form.php?id=" . $netport->fields["id"] . "\">";
                         } else {
                             $content .= "<a href=\"" . $CFG_GLPI["root_doc"] . "/front/networkportmigration.form.php?id=" . $netport->fields["id"] . "\">";
                         }
                     }
                     $content .= $netport->fields["logical_number"];
                     if ($canedit && $withtemplate != 2) {
                         $content .= "</a>";
                     }
                     $content .= "</span>";
                     $content .= Html::showToolTip($netport->fields['comment'], array('display' => false));
                     $t_row->addCell($c_number, $content);
                     $value = $netport->fields["name"];
                     $t_row->addCell($c_name, $value, NULL, $netport);
                     if ($table_options['display_isDynamic']) {
                         $t_row->addCell($c_dynamic, Dropdown::getYesNo($netport->fields['is_dynamic']));
                     }
                     $instant_cell = NULL;
                     if ($display_options['characteristics']) {
                         $instantiation = $netport->getInstantiation();
                         if ($instantiation !== false) {
                             $instantiation->getInstantiationHTMLTable($netport, $t_row, NULL, $table_options);
                             unset($instantiation);
                         }
                     } else {
                         if ($display_options['internet']) {
                             NetworkName::getHTMLTableCellsForItem($t_row, $netport, NULL, $table_options);
                         }
                     }
                 }
                 $canedit = $save_canedit;
             }
             echo "</div>";
         }
     }
     if ($is_active_network_port && $showmassiveactions) {
         $massiveactionparams = array('num_displayed' => $number_port, 'check_itemtype' => $itemtype, 'check_items_id' => $items_id);
         Html::showMassiveActions('NetworkPort', $massiveactionparams);
     }
     $table->display(array('display_thead' => false, 'display_tfoot' => false));
     unset($table);
     if (!$is_active_network_port) {
         echo "<table class='tab_cadre_fixe'><tr><th>" . __('No network port found') . "</th></tr>";
         echo "</table>";
     }
     if ($showmassiveactions) {
         $massiveactionparams['ontop'] = false;
         Html::showMassiveActions('NetworkPort', $massiveactionparams);
         Html::closeForm();
     }
 }
 static function showForItem(CommonGLPI $item, $withtemplate = 0)
 {
     $ID = $item->getField('id');
     if (!$item->can($ID, 'r')) {
         return false;
     }
     $canedit = $withtemplate != 2 && $item->can($ID, 'w');
     echo "<div class='spaced'>";
     $rand = mt_rand();
     if ($canedit) {
         echo "\n<form id='form_device_action{$rand}' name='form_device_action{$rand}'\n                  action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "' method='post'>\n";
         echo "\t<input type='hidden' name='items_id' value='{$ID}'>\n";
         echo "\t<input type='hidden' name='itemtype' value='" . $item->getType() . "'>\n";
     }
     $table = new HTMLTableMain();
     $table->setTitle(_n('Component', 'Components', 2));
     if ($canedit) {
         $delete_all_column = $table->addHeader('delete all', Html::getCheckAllAsCheckbox("form_device_action{$rand}", '__RAND__'));
         $delete_all_column->setHTMLClass('center');
     } else {
         $delete_all_column = NULL;
     }
     $common_column = $table->addHeader('common', __('Type of component'));
     $specific_column = $table->addHeader('specificities', __('Specificities'));
     $specific_column->setHTMLClass('center');
     $dynamic_column = '';
     if ($item->isDynamic()) {
         $dynamic_column = $table->addHeader('is_dynamic', __('Automatic inventory'));
         $dynamic_column->setHTMLClass('center');
     }
     if ($canedit) {
         $content = "<input type='submit' class='submit' name='delete' value='" . _sx('button', 'Delete permanently') . "'>";
         $delete_column = $table->addHeader('delete one', $content);
         $delete_column->setHTMLClass('center');
     } else {
         $delete_column = NULL;
     }
     $table_options = array('canedit' => $canedit);
     $devtypes = array();
     foreach (self::getDeviceTypes() as $link_type) {
         $devtypes[] = $link_type::getDeviceType();
         $link_type::getTableGroup($item, $table, $table_options, $delete_all_column, $common_column, $specific_column, $delete_column, $dynamic_column);
     }
     if ($canedit) {
         echo "<table class='tab_cadre_fixe'><tr class='tab_bg_1'><td>";
         echo __('Add a new component') . "</td><td class=left width='70%'>";
         Dropdown::showAllItems('devices_id', '', 0, -1, $devtypes, false, false, 'devicetype');
         echo "</td><td>";
         echo "<input type='submit' class='submit' name='add' value='" . _sx('button', 'Add') . "'>";
         echo "</td></tr></table>";
     }
     $table->display(array('display_super_for_each_group' => false, 'display_title_for_each_group' => false));
     if ($canedit) {
         echo "<input type='submit' class='submit' name='updateall' value='" . _sx('button', 'Save') . "'>";
         Html::closeForm();
     }
     echo "</div>";
 }