| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- class Teryt {
- const SIMC_TABLE = "TERYT_SIMC";
- const TERC_TABLE = "TERYT_TERC";
- const ULIC_TABLE = "TERYT_ULIC";
- const PNA_TABLE = "PNA";
- private static function checkArgs($args, $argsAvailable) {
- if ($args == null) throw new Exception("Missing argument");
- if (!is_array($args)) throw new Exception("Armument must be an array");
- if (!count($args)) throw new Exception("Argument list cannot be empty");
- foreach ($args as $arg => $value) {
- if (!in_array($arg, $argsAvailable)) throw new Exception("Unknown argument {$arg}");
- }
- }
- public static function search($args = null) {
- $argsAvailable = ["kodPocztowy", "wojewodztwo", "powiat", "gmina", "miejscowosc", "ulica"];
- self::checkArgs($args, $argsAvailable);
- $where = "";
- $return = null;
- if (isset($args['kodPocztowy'])) {
- if (isset($args['miejscowosc'])) $whereKod = "and miejscowosc = " . DB::getPDO()->quote($args['miejscowosc']);
- else $whereKod = "";
- $query = "select wojewodztwo, powiat, gmina from `" . self::PNA_TABLE . "` where kodPocztowy = " . DB::getPDO()->quote($args['kodPocztowy']) . " $whereKod and nazwa = '' group by wojewodztwo, powiat, gmina";
- $result = DB::getPDO()->fetchall($query);
- if (count($result) == 1) {
- if (!isset($args['wojewodztwo'])) $args['wojewodztwo'] = $result[0]['wojewodztwo'];
- if (!isset($args['powiat'])) $args['powiat'] = $result[0]['powiat'];
- if (!isset($args['gmina'])) $args['gmina'] = $result[0]['gmina'];
- }
- }
- if (isset($args['wojewodztwo'])) {
- $query = "select WOJ from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['wojewodztwo']) . " and POW = 0";
- if (!($return['WOJ'] = DB::getPDO()->fetchValue($query))) return null;
- $where = "and WOJ = '{$return['WOJ']}'";
- }
- if (isset($args['powiat'])) {
- $query = "select WOJ, POW from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['powiat']) . " and POW > 0 and GMI = 0 {$where}";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many powiats");
- $return['WOJ'] = $result[0]['WOJ'];
- $return['POW'] = $result[0]['POW'];
- $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}'";
- }
- if (isset($args['gmina'])) {
- $query = "select WOJ, POW, GMI from `" . self::TERC_TABLE. "` where NAZWA = " . DB::getPDO()->quote($args['gmina']) . " and GMI > 0 {$where}";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many gminass");
- $return['WOJ'] = $result[0]['WOJ'];
- $return['POW'] = $result[0]['POW'];
- $return['GMI'] = $result[0]['GMI'];
- $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
- }
- if (isset($args['miejscowosc'])) {
- $query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['miejscowosc']) . " {$where}";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many miejscowośćs");
- $return['WOJ'] = $result[0]['WOJ'];
- $return['POW'] = $result[0]['POW'];
- $return['GMI'] = $result[0]['GMI'];
- $return['SYM'] = $result[0]['SYM'];
- $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}' and SYM = '{$return['SYM']}'";
- }
- if (isset($args['ulica'])) {
- $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like " . DB::getPDO()->quote("%{$args['ulica']}%") .
- " or concat(CECHA, ' ', NAZWA_1, ' ', NAZWA_2) like " . DB::getPDO()->quote("%{$args['ulica']}%") . ") {$where}";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many ulicas");
- $return['WOJ'] = $result[0]['WOJ'];
- $return['POW'] = $result[0]['POW'];
- $return['GMI'] = $result[0]['GMI'];
- $return['SYM'] = $result[0]['SYM'];
- $return['SYM_UL'] = $result[0]['SYM_UL'];
- }
- return $return;
- }
- public static function getNames($args) {
- $argsAvailable = ["WOJ", "POW", "GMI", "SYM", "SYM_UL"];
- self::checkArgs($args, $argsAvailable);
- if (!(isset($args['SYM']) && isset($args['SYM_UL']))) throw new Exception("Missing SYM or/and SYM_UL");
- $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']);
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many ulicas");
- if (isset($args['WOJ'])) {
- if ($args['WOJ'] != $result[0]['WOJ']) throw new Exception("Invalid WOJ");
- } else $args['WOJ'] = $result[0]['WOJ'];
- if (isset($args['POW'])) {
- if ($args['POW'] != $result[0]['POW']) throw new Exception("Invalid POW");
- } else $args['POW'] = $result[0]['POW'];
- if (isset($args['GMI'])) {
- if ($args['GMI'] != $result[0]['GMI']) throw new Exception("Invalid POW");
- } else $args['GMI'] = $result[0]['GMI'];
- $return['ulica_cecha'] = $result[0]['CECHA'];
- $return['ulica_nazwa_1'] = $result[0]['NAZWA_1'];
- $return['ulica_nazwa_2'] = $result[0]['NAZWA_2'];
- $query = "select NAZWA from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = 0";
- if (!($return['wojewodztwo'] = DB::getPDO()->fetchValue($query))) return null;
- $query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = 0";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many powiats");
- $return['powiat'] = $result[0]['NAZWA'];
- $return['powiat_rodzaj'] = $result[0]['NAZWA_DOD'];
- $query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}'";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many gminas");
- $return['gmina'] = $result[0]['NAZWA'];
- $return['gmina_rodzaj'] = $result[0]['NAZWA_DOD'];
- $query = "select NAZWA from `" . self::SIMC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}' and SYM = '{$args['SYM']}'";
- if (!($result = DB::getPDO()->fetchall($query))) return null;
- if (count($result) > 1) throw new Exception("Found too many miejscowośćs");
- $return['miejscowosc'] = $result[0]['NAZWA'];
- return $return;
- }
- }
|