Teryt.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class Teryt {
  3. const SIMC_TABLE = "TERYT_SIMC";
  4. const TERC_TABLE = "TERYT_TERC";
  5. const ULIC_TABLE = "TERYT_ULIC";
  6. const PNA_TABLE = "PNA";
  7. private static function checkArgs($args, $argsAvailable) {
  8. if ($args == null) throw new Exception("Missing argument");
  9. if (!is_array($args)) throw new Exception("Armument must be an array");
  10. if (!count($args)) throw new Exception("Argument list cannot be empty");
  11. foreach ($args as $arg => $value) {
  12. if (!in_array($arg, $argsAvailable)) throw new Exception("Unknown argument {$arg}");
  13. }
  14. }
  15. public static function search($args = null) {
  16. $argsAvailable = ["kodPocztowy", "wojewodztwo", "powiat", "gmina", "miejscowosc", "ulica"];
  17. self::checkArgs($args, $argsAvailable);
  18. $where = "";
  19. $return = null;
  20. if (isset($args['kodPocztowy'])) {
  21. if (isset($args['miejscowosc'])) $whereKod = "and miejscowosc = " . DB::getPDO()->quote($args['miejscowosc']);
  22. else $whereKod = "";
  23. $query = "select wojewodztwo, powiat, gmina from `" . self::PNA_TABLE . "` where kodPocztowy = " . DB::getPDO()->quote($args['kodPocztowy']) . " $whereKod and nazwa = '' group by wojewodztwo, powiat, gmina";
  24. $result = DB::getPDO()->fetchall($query);
  25. if (count($result) == 1) {
  26. if (!isset($args['wojewodztwo'])) $args['wojewodztwo'] = $result[0]['wojewodztwo'];
  27. if (!isset($args['powiat'])) $args['powiat'] = $result[0]['powiat'];
  28. if (!isset($args['gmina'])) $args['gmina'] = $result[0]['gmina'];
  29. }
  30. }
  31. if (isset($args['wojewodztwo'])) {
  32. $query = "select WOJ from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['wojewodztwo']) . " and POW = 0";
  33. if (!($return['WOJ'] = DB::getPDO()->fetchValue($query))) return null;
  34. $where = "and WOJ = '{$return['WOJ']}'";
  35. }
  36. if (isset($args['powiat'])) {
  37. $query = "select WOJ, POW from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['powiat']) . " and POW > 0 and GMI = 0 {$where}";
  38. if (!($result = DB::getPDO()->fetchall($query))) return null;
  39. if (count($result) > 1) throw new Exception("Found too many powiats");
  40. $return['WOJ'] = $result[0]['WOJ'];
  41. $return['POW'] = $result[0]['POW'];
  42. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}'";
  43. }
  44. if (isset($args['gmina'])) {
  45. $query = "select WOJ, POW, GMI from `" . self::TERC_TABLE. "` where NAZWA = " . DB::getPDO()->quote($args['gmina']) . " and GMI > 0 {$where}";
  46. if (!($result = DB::getPDO()->fetchall($query))) return null;
  47. if (count($result) > 1) throw new Exception("Found too many gminass");
  48. $return['WOJ'] = $result[0]['WOJ'];
  49. $return['POW'] = $result[0]['POW'];
  50. $return['GMI'] = $result[0]['GMI'];
  51. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
  52. }
  53. if (isset($args['miejscowosc'])) {
  54. $query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['miejscowosc']) . " {$where}";
  55. if (!($result = DB::getPDO()->fetchall($query))) return null;
  56. if (count($result) > 1) throw new Exception("Found too many miejscowośćs");
  57. $return['WOJ'] = $result[0]['WOJ'];
  58. $return['POW'] = $result[0]['POW'];
  59. $return['GMI'] = $result[0]['GMI'];
  60. $return['SYM'] = $result[0]['SYM'];
  61. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}' and SYM = '{$return['SYM']}'";
  62. }
  63. if (isset($args['ulica'])) {
  64. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like " . DB::getPDO()->quote("%{$args['ulica']}%") .
  65. " or concat(CECHA, ' ', NAZWA_1, ' ', NAZWA_2) like " . DB::getPDO()->quote("%{$args['ulica']}%") . ") {$where}";
  66. if (!($result = DB::getPDO()->fetchall($query))) return null;
  67. if (count($result) > 1) throw new Exception("Found too many ulicas");
  68. $return['WOJ'] = $result[0]['WOJ'];
  69. $return['POW'] = $result[0]['POW'];
  70. $return['GMI'] = $result[0]['GMI'];
  71. $return['SYM'] = $result[0]['SYM'];
  72. $return['SYM_UL'] = $result[0]['SYM_UL'];
  73. }
  74. return $return;
  75. }
  76. public static function getNames($args) {
  77. $argsAvailable = ["WOJ", "POW", "GMI", "SYM", "SYM_UL"];
  78. self::checkArgs($args, $argsAvailable);
  79. if (!(isset($args['SYM']) && isset($args['SYM_UL']))) throw new Exception("Missing SYM or/and SYM_UL");
  80. $query = "select WOJ, POW, GMI, CECHA, NAZWA_1, NAZWA_2 from `" . self::ULIC_TABLE . "` where SYM = " . DB::getPDO()->quote($args['SYM']) . " and SYM_UL = " . DB::getPDO()->quote($args['SYM_UL']);
  81. if (!($result = DB::getPDO()->fetchall($query))) return null;
  82. if (count($result) > 1) throw new Exception("Found too many ulicas");
  83. if (isset($args['WOJ'])) {
  84. if ($args['WOJ'] != $result[0]['WOJ']) throw new Exception("Invalid WOJ");
  85. } else $args['WOJ'] = $result[0]['WOJ'];
  86. if (isset($args['POW'])) {
  87. if ($args['POW'] != $result[0]['POW']) throw new Exception("Invalid POW");
  88. } else $args['POW'] = $result[0]['POW'];
  89. if (isset($args['GMI'])) {
  90. if ($args['GMI'] != $result[0]['GMI']) throw new Exception("Invalid POW");
  91. } else $args['GMI'] = $result[0]['GMI'];
  92. $return['ulica_cecha'] = $result[0]['CECHA'];
  93. $return['ulica_nazwa_1'] = $result[0]['NAZWA_1'];
  94. $return['ulica_nazwa_2'] = $result[0]['NAZWA_2'];
  95. $query = "select NAZWA from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = 0";
  96. if (!($return['wojewodztwo'] = DB::getPDO()->fetchValue($query))) return null;
  97. $query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = 0";
  98. if (!($result = DB::getPDO()->fetchall($query))) return null;
  99. if (count($result) > 1) throw new Exception("Found too many powiats");
  100. $return['powiat'] = $result[0]['NAZWA'];
  101. $return['powiat_rodzaj'] = $result[0]['NAZWA_DOD'];
  102. $query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}'";
  103. if (!($result = DB::getPDO()->fetchall($query))) return null;
  104. if (count($result) > 1) throw new Exception("Found too many gminas");
  105. $return['gmina'] = $result[0]['NAZWA'];
  106. $return['gmina_rodzaj'] = $result[0]['NAZWA_DOD'];
  107. $query = "select NAZWA from `" . self::SIMC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}' and SYM = '{$args['SYM']}'";
  108. if (!($result = DB::getPDO()->fetchall($query))) return null;
  109. if (count($result) > 1) throw new Exception("Found too many miejscowośćs");
  110. $return['miejscowosc'] = $result[0]['NAZWA'];
  111. return $return;
  112. }
  113. }