dropdownConnectNetworkPort.php 4.9 KB
Newer Older
1
<?php
2 3 4
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
5
 * Copyright (C) 2015-2018 Teclib' and contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * http://glpi-project.org
 *
 * based on GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2003-2014 by the INDEPNET Development Team.
 *
 * ---------------------------------------------------------------------
 *
 * LICENSE
 *
 * This file is part of GLPI.
 *
 * GLPI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GLPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GLPI. If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
moyooo's avatar
moyooo committed
31
 */
32

Anael Mobilia's avatar
Anael Mobilia committed
33 34 35
/**
 * @since 0.84
 */
36

yllen's avatar
yllen committed
37
$AJAX_INCLUDE = 1;
remi's avatar
remi committed
38

39
include ('../inc/includes.php');
yllen's avatar
yllen committed
40

moyooo's avatar
moyooo committed
41
header("Content-Type: text/html; charset=UTF-8");
yllen's avatar
yllen committed
42
Html::header_nocache();
moyooo's avatar
moyooo committed
43

moyooo's avatar
moyooo committed
44
Session::checkRight("networking", UPDATE);
moyooo's avatar
moyooo committed
45

moyooo's avatar
moyooo committed
46
// Make a select box
yllen's avatar
yllen committed
47 48
if (class_exists($_POST["itemtype"])
    && isset($_POST["item"])) {
yllen's avatar
yllen committed
49
   $table = getTableForItemType($_POST["itemtype"]);
remi's avatar
remi committed
50

51 52 53
   $joins = [];
   $name_field = new QueryExpression("'' AS " . $DB->quoteName('npname'));

54
   if ($_POST['instantiation_type'] == 'NetworkPortEthernet') {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      $name_field = 'glpi_netpoints.name AS npname';
      $joins = [
         'glpi_networkportethernets'   => [
            'ON'  => [
               'glpi_networkportethernets'   => 'id',
               'glpi_networkports'           => 'id'
            ]
         ],
         'glpi_netpoints'              => [
            'ON'  => [
               'glpi_networkportethernets'   => 'netpoints_id',
               'glpi_netpoints'              => 'id'
            ]
         ]
      ];
70 71
   }

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
   $criteria = [
      'SELECT'    => [
         'glpi_networkports_networkports.id AS wid',
         'glpi_networkports.id AS did',
         "$table.name AS cname",
         'glpi_networkports.name AS nname',
         $name_field
      ],
      'DISTINCT'  => true,
      'FROM'      => $table,
      'LEFT JOIN' => [
         'glpi_networkports'  => [
            'ON'  => [
               'glpi_networkports'  => 'items_id',
               $table               => 'id', [
                  'AND' => [
                     'glpi_networkports.items_id'           => $_POST['item'],
                     'glpi_networkports.itemtype'           => $_POST["itemtype"],
                     'glpi_networkports.instantiation_type' => $_POST['instantiation_type']
                  ]
               ]
            ]
         ],
         'glpi_networkports_networkports' => [
            'ON'  => [
               'glpi_networkports_networkports' => 'networkports_id_1',
               'glpi_networkports'              => 'id', [
                  'OR'  => [
                     'glpi_networkports_networkports.networkports_id_2' => $DB->quoteName('glpi_networkports.id')
                  ]
               ]
            ]
         ]
      ] + $joins,
      'WHERE'     => [
         'glpi_networkports_networkports.id' => null,
         'NOT'                               => ['glpi_networkports.id' => null],
         'glpi_networkports.id'              => ['<>', $_POST['networkports_id']],
         "$table.is_deleted"                 => 0,
         "$table.is_template"                => 0
      ],
      'ORDERBY'   => 'glpi_networkports.id'
   ];
   $iterator = $Db->request($criteria);
remi's avatar
remi committed
116 117

   echo "<br>";
yllen's avatar
yllen committed
118

Cédric Anne's avatar
Cédric Anne committed
119
   $values = [];
120 121 122
   while ($data = $iterator->next()) {
      // Device name + port name
      $output = $output_long = $data['cname'];
yllen's avatar
yllen committed
123

124 125 126 127 128
      if (!empty($data['nname'])) {
         $output      = sprintf(__('%1$s - %2$s'), $output, $data['nname']);
         //TRANS: %1$s is device name, %2$s is port name
         $output_long = sprintf(__('%1$s - The port %2$s'), $output_long, $data['nname']);
      }
yllen's avatar
yllen committed
129

130 131 132 133 134 135 136
      // display netpoint (which will be copied)
      if (!empty($data['npname'])) {
         $output      = sprintf(__('%1$s - %2$s'), $output, $data['npname']);
         //TRANS: %1$s is a string (device name - port name...), %2$s is network outlet name
         $output_long = sprintf(__('%1$s - Network outlet %2$s'), $output_long, $data['npname']);
      }
      $ID = $data['did'];
yllen's avatar
yllen committed
137

138 139 140
      if ($_SESSION["glpiis_ids_visible"] || empty($output) || empty($output_long)) {
         $output      = sprintf(__('%1$s (%2$s)'), $output, $ID);
         $output_long = sprintf(__('%1$s (%2$s)'), $output_long, $ID);
remi's avatar
remi committed
141
      }
142
      $values[$ID] = $output_long;
remi's avatar
remi committed
143
   }
144
   Dropdown::showFromArray($_POST['myname'], $values, ['display_emptychoice' => true]);
moyooo's avatar
moyooo committed
145
}