Mariusz Muszyński пре 8 година
родитељ
комит
c30a79ebe0
1 измењених фајлова са 137 додато и 0 уклоњено
  1. 137 0
      SE/se-lib/Teryt.php

+ 137 - 0
SE/se-lib/Teryt.php

@@ -0,0 +1,137 @@
+<?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;
+	}
+
+}