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

+ 18 - 17
SE/se-lib/Teryt.php

@@ -37,13 +37,13 @@ class Teryt {
 
 		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;
+			if (!($return['WOJ'] = DB::getPDO()->fetchValue($query))) throw new Exception("wojewodztwo not found");
 			$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 (!($result = DB::getPDO()->fetchall($query))) throw new Exception("powiat not found");
 			if (count($result) > 1) throw new Exception("Found too many powiats");
 			$return['WOJ'] = $result[0]['WOJ'];
 			$return['POW'] = $result[0]['POW'];
@@ -51,9 +51,9 @@ class Teryt {
 		}
 
 		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");
+			$query = "select WOJ, POW, GMI from `" . self::TERC_TABLE. "` where NAZWA = " . DB::getPDO()->quote($args['gmina']) . " and GMI > 0 {$where} group by WOJ, POW, GMI";
+			if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("gmina not found");
+			if (count($result) > 1) throw new Exception("Found too many gminas ({$query})");
 			$return['WOJ'] = $result[0]['WOJ'];
 			$return['POW'] = $result[0]['POW'];
 			$return['GMI'] = $result[0]['GMI'];
@@ -62,19 +62,20 @@ class Teryt {
 
 		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");
+			if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("miejscowosc not found");
+			if (count($result) > 1) throw new Exception("Found too many miejscowoscs");
 			$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']}'";
+			$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM = '{$return['SYM']}') or RODZ_GMI>=8)";
                 }
 
 		if (isset($args['ulica'])) {
+			$args['ulica'] = str_replace(' ', '%', trim($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;
+				" or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like " . DB::getPDO()->quote("%{$args['ulica']}%") . ") {$where}";
+			if (!($result = DB::getPDO()->fetchall($query))) throw new Exception(json_encode(['message' => "ulica not found", 'sql' => $query]));
 			if (count($result) > 1) throw new Exception("Found too many ulicas");
 			$return['WOJ'] = $result[0]['WOJ'];
 			$return['POW'] = $result[0]['POW'];
@@ -92,7 +93,7 @@ class Teryt {
 
 		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 (!($result = DB::getPDO()->fetchall($query))) throw new Exception("ulica not found");
 		if (count($result) > 1) throw new Exception("Found too many ulicas");
 
 		if (isset($args['WOJ'])) {
@@ -112,23 +113,23 @@ class Teryt {
 		$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;
+		if (!($return['wojewodztwo'] = DB::getPDO()->fetchValue($query))) throw new Exception("wojewodztwo not found");
 
 		$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 (!($result = DB::getPDO()->fetchall($query))) throw new Exception("powiat not found");
 		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;
+		$query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}' group by WOJ, POW, GMI, NAZWA";
+		if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("gmina not found");
 		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");
+		if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("miejscowosc not found");
+		if (count($result) > 1) throw new Exception("Found too many miejscowoscs");
 		$return['miejscowosc'] = $result[0]['NAZWA'];
 
 		return $return;