index-tree.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  4. unset($_POST);
  5. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  6. print_device_submenu($page_url);
  7. ?>
  8. <script>
  9. $.getScript("/js/jstree/jstree.min.js")
  10. .fail(function(jqxhr, settings, exception) {
  11. window.location.href = '/admin/devices/index-tree-simple.php';
  12. });
  13. </script>
  14. <div id="cont">
  15. <br>
  16. <?php
  17. function print_child($device_id, $hash, $processed_ids = array())
  18. {
  19. // Защита от циклических ссылок - проверяем, не обрабатывали ли уже это устройство
  20. if (in_array($device_id, $processed_ids)) {
  21. return '';
  22. }
  23. // Добавляем текущее устройство в список обработанных
  24. $processed_ids[] = $device_id;
  25. $output = '';
  26. foreach ($hash as $device) {
  27. if (!isset($device['parent_id'])) { continue; }
  28. if ($device['parent_id'] !== $device_id) { continue; }
  29. $dev_icon = '/img/server.png';
  30. if ($device['type'] == 0) { $dev_icon = '/img/router.png'; }
  31. if ($device['type'] == 1) { $dev_icon = '/img/switch16.png'; }
  32. if ($device['type'] == 2) { $dev_icon = '/img/gateway.png'; }
  33. if ($device['type'] == 3) { $dev_icon = '/img/server.png'; }
  34. if ($device['type'] == 4) { $dev_icon = '/img/ap.png'; }
  35. $output .= '{ "text" : "' . $device['parent_port'].'->'.$device['uplink'].'&nbsp'.$device['name'].'&nbsp::&nbsp<small><i>['.$device['model_name'].']</i></small>' . '", "icon" : "'.$dev_icon.'", "id" : "'.$device['id'].'","state" : { "opened" : true },';
  36. $output .= '"a_attr" : { "href": "'.reencodeurl('/admin/devices/editdevice.php?id='.$device['id']).'"},';
  37. $output .= '"children" : [' . print_child($device['id'], $hash, $processed_ids) . ']';
  38. $output .= "},\n";
  39. }
  40. return $output;
  41. }
  42. $dSQL = 'SELECT D.*,DM.model_name FROM devices as D,device_models as DM WHERE D.device_model_id=DM.id AND (deleted=0 and device_type<=2)';
  43. $switches = get_records_sql($db_link,$dSQL);
  44. $dev_hash = NULL;
  45. // Сначала соберем все устройства
  46. foreach ($switches as $row) {
  47. $dev_id = $row['id'];
  48. $dev_hash[$dev_id]['id'] = $dev_id;
  49. $dev_hash[$dev_id]['name'] = $row['device_name'];
  50. $dev_hash[$dev_id]['type'] = $row['device_type'];
  51. $dev_hash[$dev_id]['model_name'] = $row['model_name'];
  52. $dev_hash[$dev_id]['parent_id'] = null; // инициализируем
  53. $pSQL = 'SELECT * FROM device_ports WHERE uplink = 1 and device_id='.$dev_id;
  54. $uplink = get_record_sql($db_link,$pSQL);
  55. if (empty($uplink)) { continue; }
  56. if (empty($uplink['target_port_id'])) { continue; }
  57. $dev_hash[$dev_id]['uplink'] = $uplink['port_name'];
  58. $parentSQL = 'SELECT * FROM device_ports WHERE device_ports.id='.$uplink['target_port_id'];
  59. $parent = get_record_sql($db_link,$parentSQL);
  60. // Защита от ссылки на самого себя
  61. if ($parent['device_id'] == $dev_id) {
  62. // Устройство ссылается само на себя - пропускаем эту связь
  63. $dev_hash[$dev_id]['parent_id'] = null;
  64. continue;
  65. }
  66. $dev_hash[$dev_id]['parent_id'] = $parent['device_id'];
  67. $dev_hash[$dev_id]['parent_port'] = $parent['port_name'];
  68. }
  69. print '<div id="frmt" class="tree"></div>';
  70. print "\n";
  71. print '<script>';
  72. print "$('#frmt').jstree({";
  73. print '"themes" : { "theme" : "default", "dots" : false, "icons" : false }, "plugins" : [ "themes", "html_data", "ui", "sort" ],';
  74. print "'core' : { 'data' : [";
  75. print "\n";
  76. foreach ($dev_hash as $device) {
  77. // Пропускаем устройства, которые имеют родителя (они будут отображены как дети)
  78. if (isset($device['parent_id']) && $device['parent_id'] !== null) {
  79. continue;
  80. }
  81. $dev_icon = '/img/server.png';
  82. if ($device['type'] == 0) { $dev_icon = '/img/router.png'; }
  83. if ($device['type'] == 1) { $dev_icon = '/img/switch16.png'; }
  84. if ($device['type'] == 2) { $dev_icon = '/img/gateway.png'; }
  85. if ($device['type'] == 3) { $dev_icon = '/img/server.png'; }
  86. if ($device['type'] == 4) { $dev_icon = '/img/ap.png'; }
  87. print '{ "text" : "';
  88. print_url($device['name'],'/admin/devices/editdevice.php?id='.$device['id']);
  89. print '","icon" : "'.$dev_icon.'", "id" : "'.$device['id'].'" ,"state" : { "opened" : true },';
  90. print '"a_attr" : { "href": "'.reencodeurl('/admin/devices/editdevice.php?id='.$device['id']).'"},';
  91. print '"children" : [';
  92. print print_child($device['id'], $dev_hash, array());
  93. print "]";
  94. print "},\n";
  95. }
  96. ?>
  97. ]}
  98. }).on('changed.jstree', function (e, data) {
  99. if (data == null) return '';
  100. if (data.node == null) return '';
  101. var href = data.node.a_attr.href;
  102. var parentId = data.node.a_attr.parent_id;
  103. if(href == '#') return '';
  104. var win = window.open(href, "_blank");
  105. });
  106. </script>
  107. <?php
  108. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  109. ?>