1
0

devmodels.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. $default_displayed=25;
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  4. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  5. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/vendorfilter.php");
  6. if (getPOST("save")) {
  7. $f_ids = getPOST("f_id", null, []);
  8. if (!empty($f_ids) && is_array($f_ids)) {
  9. $f_ids = array_map('intval', getPOST("f_id", null, []));
  10. $r_ids = array_map('intval', getPOST("r_id", null, []));
  11. $f_vendors = getPOST("f_vendor", null, []);
  12. $f_names = getPOST("f_name", null, []);
  13. $f_poe_ins = getPOST("f_poe_in", null, []);
  14. $f_poe_outs = getPOST("f_poe_out", null, []);
  15. $f_nagios = getPOST("f_nagios", null, []);
  16. foreach ($f_ids as $save_id) {
  17. if (!isset($r_ids[$save_id])) continue; // на всякий случай
  18. $new = [
  19. 'poe_in' => (int)($f_poe_ins[$save_id] ?? 0),
  20. 'poe_out' => (int)($f_poe_outs[$save_id] ?? 0),
  21. 'nagios_template' => trim($f_nagios[$save_id] ?? '')
  22. ];
  23. if ($save_id >= 10000) {
  24. $new['vendor_id'] = (int)($f_vendors[$save_id] ?? 1);
  25. $new['model_name'] = trim($f_names[$save_id] ?? '');
  26. }
  27. update_record($db_link, "device_models", "id = ?", $new, [$save_id]);
  28. }
  29. }
  30. header("Location: " . $_SERVER["REQUEST_URI"]);
  31. exit;
  32. }
  33. if (getPOST("remove")) {
  34. $f_ids = getPOST("f_id", null, []);
  35. if (!empty($f_ids) && is_array($f_ids)) {
  36. $f_ids = array_map('intval', array_filter($f_ids, fn($id) => $id >= 10000));
  37. foreach ($f_ids as $id) {
  38. // set default - Unknown computer
  39. update_records($db_link, "devices", "device_model_id = ?", ['device_model_id' => 87], [$id]);
  40. delete_record($db_link, "device_models", "id = ?", [$id]);
  41. }
  42. }
  43. header("Location: " . $_SERVER["REQUEST_URI"]);
  44. exit;
  45. }
  46. if (getPOST("create")) {
  47. $model_name = trim(getPOST("new_model", null, ''));
  48. if ($model_name !== '') {
  49. $max_record = get_record_sql($db_link, "SELECT MAX(id) AS max_id FROM device_models");
  50. $f_vendor_id = (int)getPOST("new_vendor_id", null, 1);
  51. $next_id = (isset($max_record['max_id']) && $max_record['max_id'] >= 10000)
  52. ? (int)$max_record['max_id'] + 1
  53. : 10000;
  54. $new = [
  55. 'id' => $next_id,
  56. 'vendor_id' => (int)($f_vendor_id ?? 1),
  57. 'model_name' => $model_name
  58. ];
  59. insert_record($db_link, "device_models", $new);
  60. }
  61. header("Location: " . $_SERVER["REQUEST_URI"]);
  62. exit;
  63. }
  64. unset($_POST);
  65. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  66. print_control_submenu($page_url);
  67. ?>
  68. <div id="cont">
  69. <br>
  70. <form name="def" action="devmodels.php" method="post">
  71. <table class="data">
  72. <tr>
  73. <td><b><?php echo WEB_list_models; ?></b></td>
  74. <td><?php print_vendor_select($db_link, 'vendor_select', $f_vendor_select); ?></td>
  75. <td><?php echo WEB_rows_at_page . "&nbsp:"; print_row_at_pages('rows', $displayed); ?></td>
  76. <td><input type="submit" name="OK" value="<?php echo WEB_btn_show; ?>"></td>
  77. </tr>
  78. </table>
  79. <?php
  80. $params = [];
  81. $filter = '';
  82. if (!empty($f_vendor_select)) {
  83. $filter = 'WHERE vendor_id = ?';
  84. $params[] = $f_vendor_select;
  85. }
  86. $countSQL = "SELECT COUNT(*) FROM device_models $filter";
  87. $count_records = get_single_field($db_link, $countSQL, $params);
  88. $total = ceil($count_records / $displayed);
  89. if ($page > $total) { $page = $total; }
  90. if ($page < 1) { $page = 1; }
  91. $start = ($page * $displayed) - $displayed;
  92. print_navigation($page_url, $page, $displayed, $count_records, $total);
  93. $sql = "SELECT * FROM device_models $filter ORDER BY vendor_id, model_name LIMIT ? OFFSET ?";
  94. $params[] = $displayed;
  95. $params[] = $start;
  96. $t_models = get_records_sql($db_link, $sql, $params);
  97. ?>
  98. <br>
  99. <table class="data">
  100. <tr align="center">
  101. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  102. <td><b>Id</b></td>
  103. <td><b><?php echo WEB_model_vendor; ?></b></td>
  104. <td><b><?php echo WEB_cell_name; ?></b></td>
  105. <td><b><?php echo WEB_cell_poe_in; ?></b></td>
  106. <td><b><?php echo WEB_cell_poe_out; ?></b></td>
  107. <td><b><?php echo WEB_nagios_template; ?></b></td>
  108. <td></td>
  109. </tr>
  110. <?php
  111. foreach ($t_models as $row) {
  112. $is_system = ($row['id'] < 10000);
  113. $disabled_attr = $is_system ? 'disabled' : '';
  114. $checkbox_attr = $is_system ? 'disabled title="System model — cannot be edited or deleted"' : '';
  115. echo "<tr align=center>\n";
  116. echo "<td class=\"data\" style='padding:0'>";
  117. echo "<input type=\"checkbox\" name=\"f_id[{$row['id']}]\" value=\"{$row['id']}\">";
  118. echo "</td>\n";
  119. echo "<td class=\"data\"><input type=\"hidden\" name=\"r_id[{$row['id']}]\" value=\"{$row['id']}\">{$row['id']}</td>\n";
  120. echo "<td class=\"data\" width=150>";
  121. print_vendor_set($db_link, "f_vendor[{$row['id']}]", $row['vendor_id'], $is_system);
  122. echo "</td>\n";
  123. echo "<td class=\"data\">";
  124. echo "<input type=\"text\" name=\"f_name[{$row['id']}]\" value=\"" . htmlspecialchars($row['model_name']) . "\" $disabled_attr>";
  125. echo "</td>\n";
  126. echo "<td class=\"data\">";
  127. print_qa_select("f_poe_in[{$row['id']}]", $row['poe_in']);
  128. echo "</td>\n";
  129. echo "<td class=\"data\">";
  130. print_qa_select("f_poe_out[{$row['id']}]", $row['poe_out']);
  131. echo "</td>\n";
  132. echo "<td class=\"data\">";
  133. echo "<input type=\"text\" name=\"f_nagios[{$row['id']}]\" value=\"" . htmlspecialchars($row['nagios_template']) . "\">";
  134. echo "</td>\n";
  135. echo "<td class=\"data\"></td>\n";
  136. echo "</tr>\n";
  137. }
  138. ?>
  139. </table>
  140. <!-- Кнопки под таблицей -->
  141. <div style="margin-top: 15px; display: flex; justify-content: space-between; align-items: center;">
  142. <!-- Массовые действия -->
  143. <div>
  144. <input type="submit"
  145. name="save"
  146. value="<?php echo WEB_btn_save; ?>"
  147. <?php if (empty($t_models)) echo 'disabled'; ?>>
  148. <input type="submit"
  149. name="remove"
  150. value="<?php echo WEB_btn_delete; ?>"
  151. onclick="return confirm('<?php echo WEB_msg_delete; ?>?')"
  152. <?php if (empty($t_models) || !array_filter($t_models, fn($r) => $r['id'] >= 10000)) echo 'disabled'; ?>>
  153. </div>
  154. <!-- Создание новой записи -->
  155. <div style="display: flex; gap: 8px; align-items: center;">
  156. <?php print WEB_model_vendor."&nbsp"; print_vendor_select($db_link, 'new_vendor_id', $f_vendor_select ?: 1); ?>
  157. <input type="text" name="new_model" value="Unknown" placeholder="Model name" style="width: 120px;">
  158. <input type="submit" name="create" value="<?php echo WEB_btn_add; ?>">
  159. </div>
  160. </div>
  161. </form>
  162. <?php
  163. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  164. ?>