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 Racks. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
include '../../../inc/includes.php';
if (!isset($_GET["id"])) {
    $_GET["id"] = "";
}
if (!isset($_GET["withtemplate"])) {
    $_GET["withtemplate"] = "";
}
$PluginRacksRack = new PluginRacksRack();
$PluginRacksOther = new PluginRacksOther();
$PluginRacksRack_Item = new PluginRacksRack_Item();
if (isset($_POST["add"])) {
    $PluginRacksRack->check(-1, CREATE, $_POST);
    $PluginRacksRack->add($_POST);
    Html::back();
} elseif (isset($_POST["delete"])) {
    $PluginRacksRack->check($_POST['id'], DELETE);
    $PluginRacksRack->delete($_POST);
    $PluginRacksRack->redirectToList();
} elseif (isset($_POST["restore"])) {
    $PluginRacksRack->check($_POST['id'], PURGE);
    $PluginRacksRack->restore($_POST);
    $PluginRacksRack->redirectToList();
} else {
    if (isset($_POST["purge"])) {
 function showItemFromPlugin($instID, $face)
 {
     global $DB, $CFG_GLPI;
     if (!$this->canView()) {
         return false;
     }
     $rand = mt_rand();
     $PluginRacksRack = new PluginRacksRack();
     $PluginRacksConfig = new PluginRacksConfig();
     if ($PluginRacksRack->getFromDB($instID)) {
         $canedit = $PluginRacksRack->can($instID, UPDATE);
         if ($canedit) {
             $this->AddItemToRack($PluginRacksRack, $instID, $face);
         }
         //LIST
         echo "<form method='post' name='racks_form{$rand}' id='racks_form{$rand}'  \n               action=\"" . PluginRacksRack::getFormURL(true) . "\">";
         echo "<div class='center'><table class='tab_cadre_fixe'>";
         echo "<tr><th colspan='12'>" . __('Rack enclosure arrangement', 'racks') . ":</th></tr><tr>";
         if ($face == PluginRacksRack::FRONT_FACE) {
             $query = "SELECT `" . $this->getTable() . "`.*\n              FROM `" . $this->getTable() . "`,`glpi_plugin_racks_itemspecifications`\n              WHERE `" . $this->getTable() . "`.`plugin_racks_itemspecifications_id` = `glpi_plugin_racks_itemspecifications`.`id` \n              AND `" . $this->getTable() . "`.`plugin_racks_racks_id` = '{$instID}'\n              AND (`" . $this->getTable() . "`.`faces_id` = '" . PluginRacksRack::FRONT_FACE . "' \n               OR (`" . $this->getTable() . "`.`faces_id` ='" . PluginRacksRack::BACK_FACE . "' \n                  AND `glpi_plugin_racks_itemspecifications`.`length` = 1 ))\n              ORDER BY `" . $this->getTable() . "`.`position` ASC";
         } else {
             $query = "SELECT `" . $this->getTable() . "`.*\n              FROM `" . $this->getTable() . "`,`glpi_plugin_racks_itemspecifications`\n              WHERE `" . $this->getTable() . "`.`plugin_racks_itemspecifications_id` = `glpi_plugin_racks_itemspecifications`.`id` \n              AND `" . $this->getTable() . "`.`plugin_racks_racks_id` = '{$instID}'\n              AND (`" . $this->getTable() . "`.`faces_id` = '" . PluginRacksRack::BACK_FACE . "' \n               OR (`" . $this->getTable() . "`.`faces_id` ='" . PluginRacksRack::FRONT_FACE . "' \n                  AND `glpi_plugin_racks_itemspecifications`.`length` = 1 ))\n              ORDER BY `" . $this->getTable() . "`.`position` ASC";
         }
         $result = $DB->query($query);
         $number = $DB->numrows($result);
         $amps_tot = 0;
         $flow_rate_tot = 0;
         $dissip_tot = 0;
         $weight_tot = $PluginRacksRack->fields["weight"];
         $nbcordons = 0;
         $nbcordons_tot = 0;
         $cordons_amps_tot = 0;
         $computer_tot = 0;
         $computer_size_tot = 0;
         $networking_tot = 0;
         $networking_size_tot = 0;
         $peripheral_tot = 0;
         $peripheral_size_tot = 0;
         $others_tot = 0;
         $others_size_tot = 0;
         $next = 0;
         $device_size = 0;
         echo "<th>&nbsp;</th>";
         echo "<th>" . __('Position', 'racks') . "</th>";
         echo "<th>" . __('Name') . "</th>";
         // nom
         echo "<th>" . __('Type') . "</th>";
         // type de materiel
         echo "<th>" . __('Model') . "</th>";
         echo "<th>" . __('Power supply 1', 'racks') . "</th>";
         //alim1
         echo "<th>" . __('Power supply 2', 'racks') . "</th>";
         //alim2
         echo "<th>" . __('C13 Power Cord Quantity', 'racks') . "</th>";
         // nb cordons
         echo "<th>" . __('Total Current', 'racks') . "<br>(" . __('amps', 'racks') . ")</th>";
         // Courant consommé
         echo "<th>" . __('Calorific waste', 'racks') . "<br>";
         // Dissipation calorifique
         echo " (";
         $PluginRacksConfig->getUnit("dissipation");
         echo ")</th>";
         echo "<th>" . __('Flow Rate', 'racks') . "<br>";
         // Débit d'air frais
         echo " (";
         $PluginRacksConfig->getUnit("dissipation");
         echo ")</th>";
         echo "<th>" . __('Weight', 'racks') . "<br>";
         // poids
         echo " (";
         $PluginRacksConfig->getUnit("weight");
         echo ")</th>";
         echo "</tr>";
         for ($i = $PluginRacksRack->fields['rack_size']; $i >= 1; $i--) {
             $alim1 = 0;
             $alim2 = 0;
             $j = $i;
             if ($i < 10) {
                 $j = "0" . $i;
             }
             if ($face == PluginRacksRack::FRONT_FACE) {
                 // recherche de l'equipement a la position courante
                 $query = "SELECT `" . $this->getTable() . "`.*\n              FROM `" . $this->getTable() . "`,`glpi_plugin_racks_itemspecifications`\n              WHERE `" . $this->getTable() . "`.`plugin_racks_itemspecifications_id` = `glpi_plugin_racks_itemspecifications`.`id` \n              AND `" . $this->getTable() . "`.`plugin_racks_racks_id` = '{$instID}'\n              AND (`" . $this->getTable() . "`.`faces_id` = '" . PluginRacksRack::FRONT_FACE . "' \n                  OR (`" . $this->getTable() . "`.`faces_id` = '" . PluginRacksRack::BACK_FACE . "' \n                     AND `glpi_plugin_racks_itemspecifications`.`length` = 1)) AND `position` ='{$j}'\n              ORDER BY `" . $this->getTable() . "`.`position` ASC";
             } else {
                 $query = "SELECT `" . $this->getTable() . "`.*\n              FROM `" . $this->getTable() . "`,`glpi_plugin_racks_itemspecifications`\n              WHERE `" . $this->getTable() . "`.`plugin_racks_itemspecifications_id` = `glpi_plugin_racks_itemspecifications`.`id` \n               AND `" . $this->getTable() . "`.`plugin_racks_racks_id` = '{$instID}'\n               AND (`" . $this->getTable() . "`.`faces_id` = '" . PluginRacksRack::BACK_FACE . "' \n                  OR (`" . $this->getTable() . "`.`faces_id` = '" . PluginRacksRack::FRONT_FACE . "' \n                     AND `glpi_plugin_racks_itemspecifications`.`length` = 1)) AND `position` ='{$j}'\n              ORDER BY `" . $this->getTable() . "`.`position` ASC";
             }
             $result = $DB->query($query);
             $number = $DB->numrows($result);
             // Si equipement
             if ($number != 0) {
                 $data = $DB->fetch_array($result);
                 $class = substr($data["itemtype"], 0, -5);
                 $item = new $class();
                 $table = getTableForItemType($class);
                 $r = $DB->query("SELECT * FROM `" . $table . "` WHERE `id` = '" . $data["items_id"] . "' ");
                 $device = $DB->fetch_array($r);
                 $modelclass = $data["itemtype"];
                 $model_table = getTableForItemType($modelclass);
                 $modelfield = getForeignKeyFieldForTable(getTableForItemType($modelclass));
                 $query = "SELECT `" . $model_table . "`.`name` AS model,`" . $model_table . "`.`id` AS modelid, `glpi_plugin_racks_itemspecifications`.* \n                        FROM `glpi_plugin_racks_itemspecifications` " . " LEFT JOIN `" . $model_table . "` \n                        ON (`glpi_plugin_racks_itemspecifications`.`model_id` = `" . $model_table . "`.`id`)" . " LEFT JOIN `" . $table . "` \n                        ON (`glpi_plugin_racks_itemspecifications`.`model_id` = `" . $table . "`.`" . $modelfield . "` \n                           AND `glpi_plugin_racks_itemspecifications`.`itemtype` = '" . $modelclass . "')" . " WHERE `" . $table . "`.`id` = '" . $data["items_id"] . "' ";
                 //Rack recursivity .getEntitiesRestrictRequest(" AND ",$table,'','',$item->maybeRecursive())
                 $res = $DB->query($query);
                 $device_spec = $DB->fetch_array($res);
                 $device_size = $device_spec["size"];
                 if ($data["first_powersupply"] > 0) {
                     $nbcordons += 1;
                     $nbcordons_tot += 1;
                 }
                 if ($data["second_powersupply"] > 0) {
                     $nbcordons += 1;
                     $nbcordons_tot += 1;
                 }
                 if ($data["itemtype"] == 'ComputerModel') {
                     $computer_tot += 1;
                     $computer_size_tot += $device_spec["size"];
                 } else {
                     if ($data["itemtype"] == 'PeripheralModel') {
                         $peripheral_tot += 1;
                         $peripheral_size_tot += $device_spec["size"];
                     } else {
                         if ($data["itemtype"] == 'NetworkEquipmentModel') {
                             $networking_tot += 1;
                             $networking_size_tot += $device_spec["size"];
                         } else {
                             if ($data["itemtype"] == 'PluginRacksOtherModel') {
                                 $others_tot += 1;
                                 $others_size_tot += $device_spec["size"];
                             }
                         }
                     }
                 }
                 for ($t = 0; $t < $device_size; $t++) {
                     if ($t == 0) {
                         if ($data["itemtype"] == 'ComputerModel') {
                             echo "<tr class='plugin_racks_device_computers_color'>";
                         } else {
                             if ($data["itemtype"] == 'PeripheralModel') {
                                 echo "<tr class='plugin_racks_device_peripherals_color'>";
                             } else {
                                 if ($data["itemtype"] == 'NetworkEquipmentModel') {
                                     echo "<tr class='plugin_racks_device_networking_color'>";
                                 } else {
                                     if ($data["itemtype"] == 'PluginRacksOtherModel') {
                                         echo "<tr class='plugin_racks_device_others_color'>";
                                     }
                                 }
                             }
                         }
                         echo "<td width='10' rowspan='" . $device_size . "'>";
                         $sel = "";
                         if (isset($_GET["select"]) && $_GET["select"] == "all") {
                             $sel = "checked";
                         }
                         echo "<input type='checkbox' name='item[" . $data["id"] . "]' value='1' {$sel}>";
                         echo "</td>";
                         echo "<td class='center'>U";
                         if ($canedit) {
                             echo "<input type='text' size='3' name='position" . $data["id"] . "' value='{$j}'>";
                             echo "&nbsp;<input type='image' name='updateDevice[" . $data["id"] . "]' value=\"" . _sx('button', 'Save') . "\" src='" . $CFG_GLPI["root_doc"] . "/pics/actualiser.png' class='calendrier'>";
                         } else {
                             echo $j;
                         }
                         echo "</td>";
                         $link = Toolbox::getItemTypeFormURL(substr($data["itemtype"], 0, -5));
                         if ($data["itemtype"] != 'PluginRacksOtherModel') {
                             $name = "<a href=\"" . $link . "?id=" . $data["items_id"] . "\">" . $device["name"] . "</a>";
                         } else {
                             $name = $device["name"];
                         }
                         echo "<input type='hidden' name='plugin_racks_racks_id' value='" . $PluginRacksRack->fields['id'] . "'>";
                         echo "<input type='hidden' name='rack_size' value='" . $PluginRacksRack->fields['rack_size'] . "'>";
                         echo "<input type='hidden' name='type" . $data["id"] . "' value='" . $data["itemtype"] . "'>";
                         echo "<input type='hidden' name='items_id" . $data["id"] . "' value='" . $data["items_id"] . "'>";
                         echo "<input type='hidden' name='plugin_racks_itemspecifications_id" . $data["id"] . "' value='" . $data["plugin_racks_itemspecifications_id"] . "'>";
                         echo "<input type='hidden' name='update_server' value='1'>";
                         echo "<input type='hidden' name='faces_id' value='" . $face . "'>";
                         if ($data["itemtype"] != 'PluginRacksOtherModel') {
                             echo "<td class='center' " . (isset($data['is_deleted']) && $data['is_deleted'] ? "class='tab_bg_2_2'" : "") . " >" . $name . "</td>";
                         } else {
                             $PluginRacksOther = new PluginRacksOther();
                             $PluginRacksOther->GetfromDB($data["items_id"]);
                             echo "<td class='center'><input type='text' name='name" . $data["id"] . "' value='" . $PluginRacksOther->fields["name"] . "' size='10'></td>";
                         }
                         echo "<td class='center'>" . $item::getTypeName(2) . "</td>";
                         $linkmodel = Toolbox::getItemTypeFormURL($modelclass);
                         echo "<td class='center'><a href=\"" . $linkmodel . "?id=" . $device_spec["modelid"] . "\">" . $device_spec["model"] . " (" . $device_spec["size"] . "U)</a></td>";
                         echo "<td class='center'>";
                         echo Dropdown::getDropdownName("glpi_plugin_racks_connections", $data["first_powersupply"]);
                         echo "</td>";
                         echo "<td class='center'>";
                         echo Dropdown::getDropdownName("glpi_plugin_racks_connections", $data["second_powersupply"]);
                         echo "</td>";
                         echo "<td class='center'>" . $nbcordons . "</td>";
                         if ($data["amps"] == '0.0000') {
                             $amps = $device_spec["amps"];
                         } else {
                             $amps = $data["amps"];
                         }
                         $cordons_amps_tot += $amps * $nbcordons;
                         echo "<td class='center'>" . Html::formatNumber($amps, true) . "</td>";
                         if ($data["dissipation"] == '0.0000') {
                             $dissipation = $device_spec["dissipation"];
                         } else {
                             $dissipation = $data["dissipation"];
                         }
                         echo "<td class='center'>" . Html::formatNumber($dissipation, true) . "</td>";
                         if ($data["flow_rate"] == '0.0000') {
                             $flow_rate = $device_spec["flow_rate"];
                         } else {
                             $flow_rate = $data["flow_rate"];
                         }
                         echo "<td class='center'>" . Html::formatNumber($flow_rate, true) . "</td>";
                         if ($data["weight"] == '0.0000') {
                             $weight = $device_spec["weight"];
                         } else {
                             $weight = $data["weight"];
                         }
                         echo "<td class='center'>" . Html::formatNumber($weight, true) . "</td>";
                         echo "</tr>";
                         if ($data["amps"] == '0.0000') {
                             $amps_tot += $device_spec["amps"];
                         } else {
                             $amps_tot += $data["amps"];
                         }
                         if ($data["flow_rate"] == '0.0000') {
                             $flow_rate_tot += $device_spec["flow_rate"];
                         } else {
                             $flow_rate_tot += $data["flow_rate"];
                         }
                         if ($data["dissipation"] == '0.0000') {
                             $dissip_tot += $device_spec["dissipation"];
                         } else {
                             $dissip_tot += $data["dissipation"];
                         }
                         if ($data["weight"] == '0.0000') {
                             $weight_tot += $device_spec["weight"];
                         } else {
                             $weight_tot += $data["weight"];
                         }
                     } else {
                         $name = $j - $t;
                         if ($data["itemtype"] == 'ComputerModel') {
                             echo "<tr class='plugin_racks_device_computers_color'>";
                         } else {
                             if ($data["itemtype"] == 'PeripheralModel') {
                                 echo "<tr class='plugin_racks_device_peripherals_color'>";
                             } else {
                                 if ($data["itemtype"] == 'NetworkEquipmentModel') {
                                     echo "<tr class='plugin_racks_device_networking_color'>";
                                 } else {
                                     if ($data["itemtype"] == 'PluginRacksOtherModel') {
                                         echo "<tr class='plugin_racks_device_others_color'>";
                                     }
                                 }
                             }
                         }
                         echo "<td class='center'>U{$name}</td><td colspan='10'></td></tr>";
                     }
                 }
                 if ($device_size > 1) {
                     for ($d = 1; $d < $device_size; $d++) {
                         $i--;
                     }
                 }
             } else {
                 // Si pas d'equipement a la position courante
                 echo "<tr class='tab_bg_1'><td></td><td class='center'>" . __('U', 'racks') . $j;
                 echo "</td><td colspan='10'></td></tr>";
             }
             $nbcordons = 0;
         }
         echo "<tr class='tab_bg_1'>";
         echo "<td></td>";
         echo "<td class='center'><b>" . __('Total') . "</b></td>";
         echo "<td colspan='3' class='center'><b>";
         if ($computer_tot != 0) {
             echo __('Total Servers', 'racks') . " : " . $computer_tot . " (" . $computer_size_tot . __('U', 'racks') . ")<br>";
         }
         if ($networking_tot != 0) {
             echo __('Total Network equipements', 'racks') . " : " . $networking_tot . " (" . $networking_size_tot . __('U', 'racks') . ")<br>";
         }
         if ($peripheral_tot != 0) {
             echo __('Total Peripherals', 'racks') . " : " . $peripheral_tot . " (" . $peripheral_size_tot . __('U', 'racks') . ")<br>";
         }
         if ($others_tot != 0) {
             echo __('Total Others', 'racks') . " : " . $others_tot . " (" . $others_size_tot . __('U', 'racks') . ")<br>";
         }
         //number of U availables
         $available = $PluginRacksRack->fields['rack_size'] - $computer_size_tot - $networking_size_tot - $peripheral_size_tot - $others_size_tot;
         if ($available > 0) {
             echo "<font color='green'>" . $available . " " . __('U availables', 'racks') . "</font>";
         } else {
             echo "<font color='red'>" . $available . " " . __('U availables', 'racks') . "</font>";
         }
         echo "</b></td>";
         echo "<td colspan='3' class='center'><b>" . __('Total power Cords', 'racks') . " : " . $nbcordons_tot . "</b><br>";
         echo "<b>" . __('Amperage on power Cords', 'racks') . " : " . $cordons_amps_tot . " " . __('amps', 'racks') . "</b></td>";
         echo "<td class='center'><b>" . Html::formatNumber($amps_tot, true) . " " . __('amps', 'racks') . "</b></td>";
         echo "<td class='center'><b>" . Html::formatNumber($dissip_tot, true) . " ";
         $PluginRacksConfig->getUnit("dissipation");
         echo "</b></td>";
         echo "<td class='center'><b>" . Html::formatNumber($flow_rate_tot, true) . " ";
         $PluginRacksConfig->getUnit("rate");
         echo "</b></td>";
         echo "<td class='center'><b>" . Html::formatNumber($weight_tot, true) . " ";
         $PluginRacksConfig->getUnit("weight");
         echo "</b></td>";
         echo "</tr>";
         echo "</table></div>";
         if ($canedit) {
             Html::openArrowMassives("racks_form{$rand}", true);
             Html::closeArrowMassives(array('deleteDevice' => _sx('button', 'Delete permanently')));
         } else {
             echo "<input type='hidden' name='rack_size' value='" . $PluginRacksRack->fields['rack_size'] . "'>";
             echo "</table></div>";
         }
         Html::closeForm();
         ////////////////////////////////////////////////////
         // Recherche des racks a gauche et a droite
         // Recuperation de la rangee
         $qPos = "SELECT `name`\n                                                        FROM `glpi_plugin_racks_roomlocations`\n                                                        WHERE `id` = '" . $PluginRacksRack->fields['plugin_racks_roomlocations_id'] . "' ";
         $rPos = $DB->query($qPos);
         $nbPos = $DB->numrows($rPos);
         $pos = "";
         $next = "";
         $prev = "";
         if ($nbPos != 0) {
             $dataPos = $DB->fetch_array($rPos);
             $pos = $dataPos['name'];
         }
         // Incrementation & docrementation de la lettre de rang
         if (!empty($pos)) {
             // Z is the last letter...
             if ($pos[0] != "Z") {
                 $next = chr(ord($pos[0]) + 1);
                 for ($h = 1; $h < strlen($pos); $h++) {
                     $next .= $pos[$h];
                 }
             }
             // A is the first letter....
             if ($pos[0] != "A") {
                 $prev = chr(ord($pos[0]) - 1);
                 for ($h = 1; $h < strlen($pos); $h++) {
                     $prev .= $pos[$h];
                 }
             }
             $qLeft = "SELECT `glpi_plugin_racks_racks`.`id`, `glpi_plugin_racks_roomlocations`.`name`\n            FROM `glpi_plugin_racks_racks`\n            LEFT JOIN `glpi_plugin_racks_roomlocations`\n            ON (`glpi_plugin_racks_roomlocations`.`id` = `glpi_plugin_racks_racks`.`plugin_racks_roomlocations_id`)\n            WHERE `glpi_plugin_racks_racks`.`is_deleted` = '0' AND `glpi_plugin_racks_roomlocations`.`name` = '" . $prev . "' " . getEntitiesRestrictRequest(" AND ", "glpi_plugin_racks_racks", '', '', $PluginRacksRack->maybeRecursive());
             $rLeft = $DB->query($qLeft);
             $nb = $DB->numrows($rLeft);
             echo "<br><br>";
             echo "<div align='center'><table border='0' width='950px'><tr><td class='left'>";
             if ($nb != 0) {
                 $left_racks = $DB->fetch_array($rLeft);
                 echo "<a href =\"" . $CFG_GLPI["root_doc"] . "/plugins/racks/front/rack.form.php?id=" . $left_racks['id'] . "\">\n               <img src=\"" . $CFG_GLPI["root_doc"] . "/pics/left.png\" alt=''>&nbsp;" . __('Left rack enclosure', 'racks') . " " . $left_racks['name'] . "</a>";
             } else {
                 echo __('No rack enclosure on the left', 'racks');
             }
             echo "</td>";
             echo "<td>";
             echo "</td>";
             echo "<td class='right'>";
             $qRight = "SELECT `glpi_plugin_racks_racks`.`id`, `glpi_plugin_racks_roomlocations`.`name`\n            FROM `glpi_plugin_racks_racks`\n            LEFT JOIN `glpi_plugin_racks_roomlocations`\n            ON (`glpi_plugin_racks_roomlocations`.`id` = `glpi_plugin_racks_racks`.`plugin_racks_roomlocations_id`)\n            WHERE `glpi_plugin_racks_racks`.`is_deleted` = '0' AND `glpi_plugin_racks_roomlocations`.`name` = '" . $next . "' " . getEntitiesRestrictRequest(" AND ", "glpi_plugin_racks_racks", '', '', $PluginRacksRack->maybeRecursive());
             $rRight = $DB->query($qRight);
             $nb = $DB->numrows($rRight);
             if ($nb != 0) {
                 $right_racks = $DB->fetch_array($rRight);
                 echo "<a href =\"" . $CFG_GLPI["root_doc"] . "/plugins/racks/front/rack.form.php?id=" . $right_racks['id'] . "\">" . __('Right rack enclosure', 'racks') . " " . $right_racks['name'] . "&nbsp;<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/right.png\" alt=''></a>";
             } else {
                 echo __('No rack enclosure on the right', 'racks');
             }
             echo "</td></tr></table></div>";
         }
     }
 }