1
0

devmodels.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. delete_record($db_link, "device_models", "id = ?", [$id]);
  43. update_records($db_link, "devices", "device_model_id = ?", ['device_model_id' => null], [$id]);
  44. }
  45. }
  46. header("Location: " . $_SERVER["REQUEST_URI"]);
  47. exit;
  48. }
  49. if (getPOST("create")) {
  50. $model_name = trim(getPOST("new_model", null, ''));
  51. if ($model_name !== '') {
  52. $max_record = get_record_sql($db_link, "SELECT MAX(id) AS max_id FROM device_models");
  53. $next_id = (isset($max_record['max_id']) && $max_record['max_id'] >= 10000)
  54. ? (int)$max_record['max_id'] + 1
  55. : 10000;
  56. $new = [
  57. 'id' => $next_id,
  58. 'vendor_id' => (int)($f_vendor_select ?? 1),
  59. 'model_name' => $model_name
  60. ];
  61. insert_record($db_link, "device_models", $new);
  62. }
  63. header("Location: " . $_SERVER["REQUEST_URI"]);
  64. exit;
  65. }
  66. unset($_POST);
  67. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  68. print_control_submenu($page_url);
  69. ?>
  70. <div id="cont">
  71. <br>
  72. <form name="def" action="devmodels.php" method="post">
  73. <table class="data">
  74. <tr>
  75. <td><b><?php echo WEB_list_models; ?></b></td>
  76. <td><?php print_vendor_select($db_link,'vendor_select',$f_vendor_select); ?></td>
  77. <td><?php print WEB_rows_at_page."&nbsp:";print_row_at_pages('rows',$displayed); ?></td>
  78. <td><input id='apply' name='apply' type="submit" name="OK" value="<?php print WEB_btn_show; ?>"></td>
  79. </tr>
  80. </table>
  81. <?php
  82. $params = [];
  83. $filter = '';
  84. if (!empty($f_vendor_select)) {
  85. $filter = 'WHERE vendor_id = ?';
  86. $params[] = $f_vendor_select;
  87. }
  88. $countSQL = "SELECT COUNT(*) FROM device_models $filter";
  89. $count_records = get_single_field($db_link, $countSQL, $params);
  90. $total=ceil($count_records/$displayed);
  91. if ($page>$total) { $page=$total; }
  92. if ($page<1) { $page=1; }
  93. $start = ($page * $displayed) - $displayed;
  94. print_navigation($page_url,$page,$displayed,$count_records,$total);
  95. $sql = "SELECT * FROM device_models $filter ORDER BY vendor_id, model_name LIMIT ? OFFSET ?";
  96. $params[] = $displayed;
  97. $params[] = $start;
  98. $t_ou = get_records_sql($db_link, $sql, $params);
  99. ?>
  100. <br>
  101. <table class="data">
  102. <tr align="center">
  103. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  104. <td><b>Id</b></td>
  105. <td><b><?php echo WEB_model_vendor; ?></b></td>
  106. <td><b><?php echo WEB_cell_name; ?></b></td>
  107. <td><b><?php echo WEB_cell_poe_in; ?></b></td>
  108. <td><b><?php echo WEB_cell_poe_out; ?></b></td>
  109. <td><b><?php echo WEB_nagios_template; ?></b></td>
  110. <td><input id='save' type="submit" name='save' value="<?php echo WEB_btn_save; ?>"></td>
  111. <td><input id='remove' type="submit" name='remove' value="<?php echo WEB_btn_delete; ?>"></td>
  112. </tr>
  113. <?php
  114. foreach ($t_ou as $row) {
  115. print "<tr align=center>\n";
  116. print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_id[] value='{$row['id']}'></td>\n";
  117. print "<td class=\"data\"><input type=\"hidden\" name='r_id[]' value='{$row['id']}'>{$row['id']}</td>\n";
  118. print "<td class=\"data\" width=150>"; print_vendor_set($db_link,'f_vendor[]',$row['vendor_id']); print "</td>\n";
  119. print "<td class=\"data\"><input type=\"text\" name='f_name[]' value='{$row['model_name']}'></td>\n";
  120. print "<td class=\"data\">";print_qa_select("f_poe_in[]", $row['poe_in']); print "</td>\n";
  121. print "<td class=\"data\">";print_qa_select("f_poe_out[]", $row['poe_out']); print "</td>\n";
  122. print "<td class=\"data\"><input type=\"text\" name='f_nagios[]' value='{$row['nagios_template']}'></td>\n";
  123. print "<td class=\"data\"></td>\n";
  124. print "<td class=\"data\"></td>\n";
  125. print "</tr>\n";
  126. }
  127. ?>
  128. </table>
  129. <div><input type=text name=new_model value="Unknown">
  130. <input type="submit" name="create" value="<?php echo WEB_btn_add; ?>">
  131. </div>
  132. </form>
  133. <?php
  134. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  135. ?>