building.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.php");
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  4. if (getPOST("remove")) {
  5. $fid = getPOST("f_id", null, []);
  6. $valid_ids = array_filter(array_map('intval', $fid), fn($id) => $id > 1);
  7. foreach ($valid_ids as $val) {
  8. LOG_INFO($db_link, 'Remove building id: ' . $val . ' ' . dump_record($db_link, 'building', 'id=?', [$val]));
  9. delete_record($db_link, "building", "id=?", [$val]);
  10. }
  11. header("Location: " . $_SERVER["REQUEST_URI"]);
  12. exit;
  13. }
  14. if (getPOST("save")) {
  15. $r_id = getPOST("r_id",null, []);
  16. $f_building_name = getPOST("f_building_name", null, []);
  17. $f_building_description = getPOST("f_building_description", null, []);
  18. $save_flags = getPOST("save", null, []);
  19. $r_id = array_map('intval', $r_id);
  20. $save_flags = array_map('intval', $save_flags);
  21. foreach ($save_flags as $i => $save_id) {
  22. if ($save_id <= 0) continue;
  23. $found_index = array_search($save_id, $r_id, true);
  24. if ($found_index === false) continue;
  25. $name = trim($f_building_name[$found_index] ?? '');
  26. $description = trim($f_building_description[$found_index] ?? '');
  27. if ($name !== '') {
  28. $new = ['name' => $name, 'description' => $description];
  29. LOG_INFO($db_link, "Change building id='{$save_id}': name=" . $name . " description=" . $description);
  30. update_record($db_link, "building", "id=?", $new, [$save_id]);
  31. }
  32. }
  33. header("Location: " . $_SERVER["REQUEST_URI"]);
  34. exit;
  35. }
  36. if (getPOST("create")) {
  37. $building_name = trim(getPOST("new_building", null, ''));
  38. if ($building_name !== '') {
  39. $new = ['name' => $building_name];
  40. LOG_INFO($db_link, "Add building: " . $building_name);
  41. insert_record($db_link, "building", $new);
  42. }
  43. header("Location: " . $_SERVER["REQUEST_URI"]);
  44. exit;
  45. }
  46. unset($_POST);
  47. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/header.php");
  48. print_control_submenu($page_url);
  49. ?>
  50. <div id="cont">
  51. <form name="def" action="building.php" method="post">
  52. <table class="data">
  53. <tr align="center">
  54. <td><input type="checkbox" onClick="checkAll(this.checked);"></td>
  55. <td><b>id</b></td>
  56. <td><b><?php echo WEB_cell_name; ?></b></td>
  57. <td><b><?php echo WEB_cell_description; ?></b></td>
  58. <td>
  59. <input type="submit" onclick="return confirm('<?php print WEB_btn_delete; ?>?')" name="remove" value="<?php print WEB_btn_remove; ?>">
  60. </td>
  61. </tr>
  62. <?php
  63. $t_building = get_records_sql($db_link,'SELECT * FROM building ORDER BY name');
  64. foreach ($t_building as $row) {
  65. print "<tr align=center>\n";
  66. print "<td class=\"data\" style='padding:0'><input type=checkbox name=f_id[] value='{$row['id']}'></td>\n";
  67. print "<td class=\"data\"><input type=\"hidden\" name='r_id[]' value='{$row['id']}'>{$row['id']}</td>\n";
  68. print "<td class=\"data\"><input type=\"text\" name='f_building_name[]' value='{$row['name']}'></td>\n";
  69. print "<td class=\"data\"><input type=\"text\" name='f_building_description[]' value='{$row['description']}'></td>\n";
  70. print "<td class=\"data\"><button name='save[]' value='{$row['id']}'>".WEB_btn_save."</button></td>\n";
  71. print "</tr>\n";
  72. }
  73. ?>
  74. </table>
  75. <table>
  76. <tr>
  77. <td><input type=text name=new_building value="Unknown"></td>
  78. <td>
  79. <input type="submit" name="create" value="<?php print WEB_btn_add; ?>">
  80. </td>
  81. <td align="right"></td>
  82. </tr>
  83. </table>
  84. </form>
  85. <?php
  86. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/footer.php");
  87. ?>