devmodels.php 7.9 KB

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