فهرست منبع

Merge branch 'master' of bn.git:plabudda/se

Piotr Labudda 8 سال پیش
والد
کامیت
88ff696205
4فایلهای تغییر یافته به همراه986 افزوده شده و 3 حذف شده
  1. 635 0
      SE/dev/audit_krs_con_digger/audit_krs_con_digger.php
  2. 319 0
      SE/se-lib/Teryt.php
  3. 27 0
      SE/se-lib/V.php
  4. 5 3
      SE/superedit-STATYSTYKA_TABELE.php

+ 635 - 0
SE/dev/audit_krs_con_digger/audit_krs_con_digger.php

@@ -0,0 +1,635 @@
+<?php
+$_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
+
+
+//when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
+if( file_exists ( "../../se-lib/bootstrap.php"))
+require("../../se-lib/bootstrap.php");
+//when used in  in native SE
+else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");
+
+Lib::loadClass("Vendor_Geophp");
+
+
+ $db = DB::getDB();
+
+
+// DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_Cables_to_PE");
+
+//unset($ID_Way);
+
+/*foreach($cables_from_joins_to_PE_arr as $id_j=>$Path_Point_val) {
+		
+		$sql_Path_Point_val="insert into Rozdzielcza_test_bzyk_Cables_to_PE (the_geom,ID_Way,ID_Point,len,PE_ID_Join)
+		     values (ST_GeomFromText('".$Path_Point_val['LINESTRING']."'),'".$Path_Point_val['ID_Way']."',
+		     '".$Path_Point_val['ID_Point']."' ,'".$Path_Point_val['len']."','".$Path_Point_val['PE_ID_Join']."'  )   ";
+		    echo "#216Query path ins:: ".$sql_Path_Point_val."  ";
+		    //if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
+		    if(strlen($Path_Point_val['LINESTRING'])>1) 
+		      DB::getPDO()->query($sql_Path_Point_val);
+		    else echo " ERROR blad danych!!! dla ID_Point:".$Path_Point_val['ID_Point'];
+		    //else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
+			
+	
+}
+*/
+
+$tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['pesel']='pesel';
+$tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['nip']='nip';
+$tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['regon']='regon';
+
+$tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['ID_BI_audit_ENERGA_PRACOWNICY']='ID';
+
+//$tables['X']['BI_audit_KW_requested']['rel_search']['pesel']='Seller_Person';
+//$tables['X']['BI_audit_KW_requested']['rel']['pesel']['search']='like';
+
+$tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel']['ID_BI_audit_ENERGA_PRACOWNICY']='ID_BI_audit_ENERGA_PRACOWNICY';
+$tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
+$tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
+$tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['budynek']='nrBudynku';
+ $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['nrLokalu']='nrLokalu';
+
+
+$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['BI_audit_ENERGA_RUM_KONTRAHENCI']='ID';
+$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['nip']='NIP';
+$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_like']['regon']='REGON';
+$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_like']['pesel']='PESEL';
+
+
+$tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['BI_audit_ENERGA_RUM_KONTRAHENCI']='ID';
+$tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
+$tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
+ $tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['nrLokalu']='Numer_mieszkania_lokalu';
+$tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['budynek']='Numer_budynku';
+
+//$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['teryt']=1;
+
+//$tables['X']['BI_audit_ENERGA_RUM_UMOWY']['rel_like']['Numer_kontrahenta']='Strony_umowy';
+//$tables['X']['BI_audit_ENERGA_RUM_UMOWY']['rel_like']['nip']='Strony_umowy';
+
+
+
+$tables['Z']['BI_audit_CEIDG']['rel']['nip']='nip';
+$tables['Z']['BI_audit_CEIDG']['rel']['regon']='regon';
+//$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['miejscowosc']='miejscowosc';
+//$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['ulica']='ulica';
+//$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
+//$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['budynek']='budynek';
+$tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
+$tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
+ $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['nrLokalu']='lokal';
+$tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['budynek']='budynek';
+$tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
+$tables['Z']['BI_audit_CEIDG']['ref'][18]='BI_audit_CEIDG_powiazania';
+
+$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel']['nip']='nip';
+//$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['miejscowosc']='miejscowosc';
+//$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['ulica']='ulica';
+//$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
+$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
+$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
+ $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['nrLokalu']='lokal';
+$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['budynek']='budynek';
+
+$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['backref'][19]='BI_audit_CEIDG';
+
+
+$tables['Z']['BI_audit_CEIDG_powiazania']['rel']['nip']='nip';
+$tables['Z']['BI_audit_CEIDG_powiazania']['rel']['regon']='regon';
+$tables['Z']['BI_audit_CEIDG_powiazania']['backref'][18]='BI_audit_CEIDG';
+
+
+$tables['Z']['BI_audit_KRS']['rel']['nip']='nip';
+$tables['Z']['BI_audit_KRS']['rel']['regon']='regon';
+$tables['Z']['BI_audit_KRS']['rel']['krs']='krs';
+//$tables['Z']['BI_audit_KRS']['rel_and']['adres']['miejscowosc']='A_miejscowosc';
+//$tables['Z']['BI_audit_KRS']['rel_and']['adres']['ulica']='A_ulica';
+//$tables['Z']['BI_audit_KRS']['rel_and']['adres']['kodPocztowy']='A_kod';
+//$tables['Z']['BI_audit_KRS']['rel_and']['adres']['budynek']='A_nrDomu';
+$tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
+$tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
+ $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['nrLokalu']='A_nrLokalu';
+$tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['budynek']='A_nrDomu';
+
+$tables['Z']['BI_audit_KRS']['ref'][12]='BI_audit_KRS_person';
+$tables['Z']['BI_audit_KRS']['ref'][11]='BI_audit_KRS_company';
+
+
+
+$tables['Z']['BI_audit_KRS_company']['rel']['regon']='regon';
+$tables['Z']['BI_audit_KRS_company']['rel']['krs']='krs';
+$tables['Z']['BI_audit_KRS_company']['backref'][11]='BI_audit_KRS';
+
+
+$tables['Z']['BI_audit_KRS_person']['rel']['pesel']='pesel';
+$tables['Z']['BI_audit_KRS_person']['backref'][12]='BI_audit_KRS';
+
+
+
+
+
+
+
+
+
+function start_dig($tables) {
+	global $db;
+	foreach($tables['Y'] as $Y=>$t)  {
+		$sql='select * from '.$Y.' where ID=12 or ID=19 or 1=1 limit 100' ;
+		$res=$db->query($sql);
+		 while($h=$db->fetch($res)) {
+			 $hist_loop[$Y][]=$h;
+			 $hist_time[][$Y]=$h;
+			 dig_next($hist_loop,$t,$h,1,$hist_time,$Y,add_path('//',$Y,'Y'));
+			 unset($hist_time);
+		 }
+	}
+}
+
+
+
+function add_path($path,$last_tbl,$table_class) {
+	$path.="/".$last_tbl."(".$table_class.")";
+	return $path;
+}
+
+
+function sql($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl,$path,$table_class) {
+	global $tables;
+	foreach($tables[$table_class] as $Z=>$z) {
+				 	//rels in tbls
+				 	foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
+				 	//	echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
+				 		if( isset($prev_tbl['rel'][$z_rel_from])) {
+					 		if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 0  ) {
+						 		$sql[$Z]['rel'][$z_rel_to] = $prev_dig->$prev_tbl['rel'][$z_rel_from];
+						 		$path_rel[$Z][]="(".$z_rel_to.")";
+					 		}
+				 		}
+				 	}
+				 	
+				 	
+				 	//search addr
+				 	if(isset($z['rel_and']) and isset($prev_tbl['rel_and'])) {
+					 	foreach($z['rel_and'] as $z_rel_and=>$z_rel_and_name) {
+						 	$rel_and_ok=1;
+		
+						 	foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
+							 	if(isset($prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur]) and ( strlen($prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur])) == 0  ) {//and ( strlen($prev_dig->$z_rel_and_name_cur) > 2) 
+									//warunek - nie ma elementu - nie szukamy po nim
+								} else 	if(isset($prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur]) and ( strlen($prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur])) > 0  ) {//and ( strlen($prev_dig->$z_rel_and_name_cur) > 2) 
+								 	if($rel_and_ok<>0) $rel_and_ok=2;
+								 //	echo "\n<br>121 rel_and from:".$last_tbl." name: ".$z_rel_and_name_cur." OK for tbl ".$Z." z_rel_and_name_col_to: ".$z_rel_and_name_col_to." value:".$prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur]." rel_and_ok=".$rel_and_ok;
+								} else {
+							 //	echo "<br>\n #127 UNSET rel_and from:".$last_tbl." name ".$z_rel_and_name_cur." / ".$z_rel_and_name_col_to." ";
+								 	$rel_and_ok=0;
+								}
+						 	}
+
+						  if($rel_and_ok==2) {
+							  $path_rel_and = array();
+						//	  echo "\n<br>#133 Z=".$Z." ok rel_and_ok=".$rel_and_ok;
+								  foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
+							//		  echo "\n<br> 135... ok. Z=".$Z."/ z_rel_and_name=".$z_rel_and_name_cur."/ z_rel_and_name_col_to=".$z_rel_and_name_col_to."/ prev_dig-z_rel_and_name_cur=".$prev_dig->$z_rel_and_name_cur;
+									  	if(strlen($prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur]) == 0 ) {
+										  	//warunek nie szukamy po tym elemencie
+									  	} else {
+										  	$sql_rel_and[$Z][$z_rel_and][$z_rel_and_name_col_to]=$prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur];
+										  	$path_rel_and[]="(".$z_rel_and_name_col_to.")";
+										}
+								  }
+						  } else {
+							//  echo "<br>#137 error rel_and_ok=".$rel_and_ok;
+						  }
+					 	}
+					 	
+				 	}
+			 }
+	
+	
+	
+	if( isset($sql_rel_and)) {
+			foreach($sql_rel_and as $T=>$z_rel_and_arr) {
+				foreach($z_rel_and_arr as $z_rel_and_name=>$z_rel_and_name_col) {
+					//echo "<br>156 z_rel_and_name=".$z_rel_and_name." ";
+					$sql_rel_and_sql[$T][$z_rel_and_name]="select * from ".$T." where ( ";
+					$and=0;
+					foreach($z_rel_and_name_col as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
+						if( $and==1) $sql_rel_and_sql[$T][$z_rel_and_name].=" and  `".$z_rel_and_name_cur."` = '".$z_rel_and_name_col_to."' and `".$z_rel_and_name_cur."` not like '%--%' and   `".$z_rel_and_name_cur."` not like '' " ;
+						else $sql_rel_and_sql[$T][$z_rel_and_name].="  `".$z_rel_and_name_cur."` like '".$z_rel_and_name_col_to."' and `".$z_rel_and_name_cur."` not like '%--%' and   `".$z_rel_and_name_cur."` not like ''  " ;
+					//	if($z_rel_and_name_cur=='miejscowosc') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `miejscowosc` not like '' " ; 
+					//	if($z_rel_and_name_cur=='A_ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_ulica` not like '' " ; 
+					//	if($z_rel_and_name_cur=='ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `ulica` not like '' " ; 						
+					//	if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` not like '' " ; 						
+					//	if($z_rel_and_name_cur=='budynek') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `budynek` like '".$z_rel_and_name_col_to."' " ; 	
+					//	if($z_rel_and_name_cur=='A_nrDomu') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_nrDomu` like '".$z_rel_and_name_col_to."' " ; 	
+					//	if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` like '".$z_rel_and_name_col_to."' " ; 							
+						
+											
+						$and=1;
+					}
+					$sql_rel_and_sql[$T][$z_rel_and_name].="  ) " ;
+					if( isset($hist_loop[$table_class][$T]) ) {
+					 	 foreach($hist_loop[$table_class][$T] as $hist_rec) {
+						 	 $sql_rel_and_sql[$T][$z_rel_and_name].=" and ID!='".$hist_rec->ID."' ";
+					 	 }
+				 	 }
+					$sql_rel_and_sql[$T][$z_rel_and_name].=" ; " ;
+				 	 dig_arr_cur($hist_loop,$T,$sql_rel_and_sql[$T][$z_rel_and_name],$hist_time,$loop,$last_tbl,$path,$table_class);
+				}
+			}	  
+			  
+		}
+		
+		if(isset($sql)) {
+			foreach($sql as $T => $rel) {
+					 	 $sql_t[$T]=" select * from ".$T." where ( ";
+					 	 foreach($rel['rel'] as $col=>$val) {
+						 	 if( isset($sql_t_str[$T]))  $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
+						 	 else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
+					 	 }
+					 	 $sql_t[$T].=$sql_t_str[$T]." ) ";
+					 	 //not loop
+					 	 if( isset($hist_loop[$table_class][$T])  ) {
+			//			 	 echo "<br> hist loop cnt: ". count($hist_loop[$table_class][$T]);
+						 	 foreach($hist_loop[$table_class][$T] as $hist_rec) {
+							 	 $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
+						 	 }
+					 	 }
+					 	 
+				 	 }
+		
+				 	 if( isset($sql_t)) {
+					  foreach($sql_t as $T => $sql_ext)  { 
+						 // $path.=$path_rel[$T][ key($sql_ext) ];
+						  dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path,$table_class);
+					  }
+				  }
+		}
+	
+}
+
+
+function dig_next($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl='n/s',$path,$table_class) {
+	 global $db,$tables;
+	 $loop++;
+	 echo "\n<hr>###LOOP ".$loop." path:".$path;
+	 if($loop<12) {
+			//detect next ref tbls
+			 $sql = array();
+			 $sql_RELAND = array();
+			 
+			 if($table_class=='X') { 
+				 echo "\n<br># 282 finished on last X";
+			 } else {
+			 	sql($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl,$path,'Z');
+			 }
+			 sql($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl,$path,'X');
+			 
+			 /*foreach($tables['Z'] as $Z=>$z) {
+				 	//rels in tbls
+				 	foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
+				 	//	echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
+				 		if( isset($prev_tbl['rel'][$z_rel_from])) {
+					 		if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 0  ) {
+						 		$sql[$Z]['rel'][$z_rel_to] = $prev_dig->$prev_tbl['rel'][$z_rel_from];
+						 		$path_rel[$Z][]="(".$z_rel_to.")";
+					 		}
+				 		}
+				 	}
+				 	
+				 	
+				 	//search addr
+				 	if(isset($z['rel_and'])) {
+					 	foreach($z['rel_and'] as $z_rel_and=>$z_rel_and_name) {
+						 	$rel_and_ok=1;
+		
+						 	foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
+							 	if(isset($prev_dig->$z_rel_and_name_col_to) and ( strlen($prev_dig->$z_rel_and_name_col_to)) > 0  ) {//and ( strlen($prev_dig->$z_rel_and_name_cur) > 2) 
+								 	if($rel_and_ok<>0) $rel_and_ok=2;
+								// 	echo "\n<br>121 rel_and from:".$last_tbl." OK for tbl ".$Z." z_rel_and_name_col_to: ".$z_rel_and_name_col_to." value:".$prev_dig->$z_rel_and_name_cur." rel_and_ok=".$rel_and_ok;
+								} else {
+							//	 	echo "<br> #127 UNSET ".$prev_dig->$z_rel_and_name_cur." / ".$z_rel_and_name_col_to." ";
+								 	$rel_and_ok=0;
+								}
+						 	}
+
+						  if($rel_and_ok==2) {
+							  $path_rel_and = array();
+						//	  echo "\n<br>#133 Z=".$Z." ok rel_and_ok=".$rel_and_ok;
+							  foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
+						//		  echo "\n<br> 135... ok. Z=".$Z."/ z_rel_and_name=".$z_rel_and_name_cur."/ z_rel_and_name_col_to=".$z_rel_and_name_col_to."/ prev_dig-z_rel_and_name_cur=".$prev_dig->$z_rel_and_name_cur;
+							  	$sql_rel_and[$Z][$z_rel_and][$z_rel_and_name_col_to]=$prev_dig->$z_rel_and_name_col_to;
+							  	$path_rel_and[]="(".$z_rel_and_name_col_to.")";
+							  }
+						  } else {
+							//  echo "<br>#137 error rel_and_ok=".$rel_and_ok;
+						  }
+					 	}
+					 	
+				 	}
+			 }
+			 
+			 
+			// echo "<br>135:sql_rel_and <pre>";
+			//		 	print_r($sql_rel_and);
+			//		 	echo " .EOF</pre>";
+			if( isset($sql_rel_and)) {
+			foreach($sql_rel_and as $T=>$z_rel_and_arr) {
+				foreach($z_rel_and_arr as $z_rel_and_name=>$z_rel_and_name_col) {
+					//echo "<br>156 z_rel_and_name=".$z_rel_and_name." ";
+					$sql_rel_and_sql[$T][$z_rel_and_name]="select * from ".$T." where ( ";
+					$and=0;
+					foreach($z_rel_and_name_col as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
+						if( $and==1) $sql_rel_and_sql[$T][$z_rel_and_name].=" and  `".$z_rel_and_name_cur."` = '".$z_rel_and_name_col_to."' and `".$z_rel_and_name_cur."` not like '%--%' and   `".$z_rel_and_name_cur."` not like '' " ;
+						else $sql_rel_and_sql[$T][$z_rel_and_name].="  `".$z_rel_and_name_cur."` like '".$z_rel_and_name_col_to."' and `".$z_rel_and_name_cur."` not like '%--%' and   `".$z_rel_and_name_cur."` not like ''  " ;
+					//	if($z_rel_and_name_cur=='miejscowosc') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `miejscowosc` not like '' " ; 
+					//	if($z_rel_and_name_cur=='A_ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_ulica` not like '' " ; 
+					//	if($z_rel_and_name_cur=='ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `ulica` not like '' " ; 						
+					//	if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` not like '' " ; 						
+					//	if($z_rel_and_name_cur=='budynek') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `budynek` like '".$z_rel_and_name_col_to."' " ; 	
+					//	if($z_rel_and_name_cur=='A_nrDomu') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_nrDomu` like '".$z_rel_and_name_col_to."' " ; 	
+					//	if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` like '".$z_rel_and_name_col_to."' " ; 							
+						
+											
+						$and=1;
+					}
+					$sql_rel_and_sql[$T][$z_rel_and_name].="  ) " ;
+					if( isset($hist_loop[$T])) {
+					 	 foreach($hist_loop[$T] as $hist_rec) {
+						 	 $sql_rel_and_sql[$T][$z_rel_and_name].=" and ID!='".$hist_rec->ID."' ";
+					 	 }
+				 	 }
+					$sql_rel_and_sql[$T][$z_rel_and_name].=" ; " ;
+				 	 dig_arr_cur($hist_loop,$T,$sql_rel_and_sql[$T][$z_rel_and_name],$hist_time,$loop,$last_tbl,$path);
+				}
+			}
+			}
+			 */
+			 {
+			 //echo "<br>164:sql_rel_and_sql <pre>";
+				//	 	print_r($sql_rel_and_sql);
+				//	 	echo " .EOF</pre>";
+			
+			 //X test
+			 
+			 //echo " <br> 129 prev <pre>";
+			 // print_r($prev_dig);
+			 // echo "<br> prevtbl";
+			  //	  print_r($prev_tbl);
+			 
+			//  echo "</pre>";
+			}
+			 /*			 
+			 foreach($tables['X'] as $X=>$x) {
+				 	//rels in tbls
+				 	if( isset($x['rel']) ) {
+					 	foreach($x['rel'] as $x_rel_from=>$x_rel_to) {
+					 		//echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
+					 		if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])  
+						// 	echo "<br>141 rel from ".$x_rel_from." exists .. testing field ".$prev_tbl['rel'][$x_rel_from]." <br>";//strlen(".$prev_dig->$prev_tbl['rel'][$x_rel_to].") 
+			
+						 		if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from])  and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { // 
+							 		$sql_X[$X]['rel'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
+						 		}
+					 		}		 	 
+					 	}
+				 	}
+				 	//rel_like
+				 	if( isset($x['rel_like'])) {
+					 	foreach($x['rel_like'] as $x_rel_from=>$x_rel_to) {
+					 		//echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
+					 		if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])  
+						// 	echo "<br>141 rel from ".$x_rel_from." exists .. testing field ".$prev_tbl['rel'][$x_rel_from]." <br>";//strlen(".$prev_dig->$prev_tbl['rel'][$x_rel_to].") 
+			
+						 		if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from])  and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { // 
+							 		$sql_X1[$X]['rel_like'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
+						 		}
+					 		}		 	 
+					 	}
+					 }
+			 }
+			 
+			  flush();
+		//	 echo "\n<br>sql_X<pre>";
+		//	 print_r($sql_X);
+		//	 echo "</pre>";
+			 if( isset($sql_X)) {
+			 foreach($sql_X as $T => $rel) {
+				 	 $sql_x[$T]=" select * from ".$T." where ( ";
+				 	 foreach($rel['rel'] as $col=>$val) {
+					 	 if( isset($sql_x_str[$T]))  $sql_x_str[$T].=" or `".$col."` = '".$val."' " ;
+					 	 else $sql_x_str[$T]=" `".$col."` = '".$val."' " ;
+				 	 }
+				 	 $sql_x[$T].=$sql_x_str[$T]." ) ";
+				 	 //not loop
+				 	 if( isset($hist_loop[$T])) {
+					 	 foreach($hist_loop[$T] as $hist_rec) {
+						 	 $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
+					 	 }
+				 	 }
+			 	 }
+			 }
+			// echo "<br> sql_x" ;
+			 //print_r($sql_x);
+			 
+			 if( isset($sql_X1)) {
+			 foreach($sql_X1 as $T => $rel) {
+				 	 $sql_x1[$T]=" select * from ".$T." where ( ";
+				 	 foreach($rel['rel_like'] as $col=>$val) {
+					 	 if( isset($sql_x1_str[$T]))  $sql_x1_str[$T].=" or `".$col."` like '%".$val."'%" ;
+					 	 else $sql_x1_str[$T]=" `".$col."` like  '%".$val."%' " ;
+				 	 }
+				 	 $sql_x1[$T].=$sql_x1_str[$T]." ) ";
+				 	 //not loop
+				 	 if( isset($hist_loop[$T])) {
+					 	 foreach($hist_loop[$T] as $hist_rec) {
+						 	 $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
+					 	 }
+				 	 }
+			 	 }
+			 }
+			 
+			 
+			 if( isset($sql_x)) {
+				 foreach($sql_x as $X => $sql_ext) {
+					 echo "\n<br>174 Querying :".$sql_ext ;
+					  $res=$db->query($sql_ext) or die('175 blad');
+					  while($h=$db->fetch($res)) {
+						  echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA X <pre>";
+						  print_r($h);
+						  echo "<br> Historia sledzen:";
+						  print_r($hist_time);
+						  echo "#177 eof </pre>";
+					  }
+				 }
+			 }
+			 if( isset($sql_x1)) {
+				 foreach($sql_x1 as $X => $sql_ext) {
+					 echo "\n<br>306 Querying :".$sql_ext ;
+					  $res=$db->query($sql_ext) or die('307 blad');
+					  while($h=$db->fetch($res)) {
+						  echo "\n<hr> #309 POWIAZANIE ZNALEZIONE DLA X2 <pre>";
+						  print_r($h);
+						  echo "<br> Historia sledzen:";
+						  print_r($hist_time);
+						  echo "<br>relacje:";
+						  
+						  echo "#177 eof </pre>";
+					  }
+				 }
+			 }
+			 //X test eof
+			 */
+			/* 
+			 
+			 	 foreach($sql as $T => $rel) {
+				 	 $sql_t[$T]=" select * from ".$T." where ( ";
+				 	 foreach($rel['rel'] as $col=>$val) {
+					 	 if( isset($sql_t_str[$T]))  $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
+					 	 else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
+				 	 }
+				 	 $sql_t[$T].=$sql_t_str[$T]." ) ";
+				 	 //not loop
+				 	 if( isset($hist_loop[$T])) {
+					 	 echo "<br> hist loop cnt: ". count($hist_loop[$T]);
+					 	 foreach($hist_loop[$T] as $hist_rec) {
+						 	 $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
+					 	 }
+				 	 }
+				 	 
+			 	 }
+			 	 
+			// echo "<br> 202 hist  loop cnt: <pre> ". count($hist_loop);
+			//	 	  print_r($hist_loop);
+			//	 	 echo "</pre>";
+			// echo "<pre>sql_t";
+			//  print_r($sql);
+			// echo "<br>sql_t ";
+			//  print_r($sql_t);
+			// echo "</pre>";
+			  
+			  if( isset($sql_t)) {
+				  foreach($sql_t as $T => $sql_ext)  { 
+					 // $path.=$path_rel[$T][ key($sql_ext) ];
+					  dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path);
+				  }
+			  }
+			  //todo look in Y and die()
+			  */
+	}	else {
+		echo "\n<hr> #377 Przekroczony limit zagnieżdzeń dla wątku: path=".$path."; <pre>\n";
+		//print_r($hist_time);
+		echo "</pre>";
+	}
+	echo "<br>#243 glebokosc:".$loop;
+}
+
+
+function dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path,$table_class) {
+	global $db,$tables;
+	
+	  echo "\n<br>216: from:".$last_tbl." Querying  :T ".$T." ext: ".$sql_ext ;//$sql_ext
+//	  echo "\n<br> las fou"
+	  flush();
+			  $res=$db->query($sql_ext) or die('211 blad '.$sql_ext);
+			  while($h=$db->fetch($res)) {
+				  echo "\n<br> #149 found ".$h->ID;
+				  if($table_class=='X') {
+					   echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA X <pre>";
+						  print_r($h);
+						  echo "<br> Historia sledzen:";
+						  print_r($hist_time);
+						  echo "<br> obiekty powiazane z BI_audit_ENERGA_RUM_UMOWY";
+						  $sql_BI_audit_ENERGA_RUM_UMOWY="select * from BI_audit_ENERGA_RUM_UMOWY where Strony_umowy REGEXP '^.*(;".$h->Numer_kontrahenta.")(;|$)'";
+						  	$res_BI_audit_ENERGA_RUM_UMOWY=$db->query($sql_BI_audit_ENERGA_RUM_UMOWY) or die('545 blad '.$sql_BI_audit_ENERGA_RUM_UMOWY);
+						  	while($h_BI_audit_ENERGA_RUM_UMOWY=$db->fetch($res_BI_audit_ENERGA_RUM_UMOWY)) {
+							  	print_r($h_BI_audit_ENERGA_RUM_UMOWY);
+							}
+						  echo "#177 eof </pre>";
+				  }
+				 // echo "<pre>"; print_r($h); echo "</pre>";
+//				 $hist_loop[$T][]=$h;
+//				 $hist_time[][$T]=$sql_ext." ##408 ".$path;
+//				 $hist_time[][$T]=$h;
+				  $hist_loop_root_cur['hist_loop']=$hist_loop;
+				  $hist_loop_root_cur['hist_time']=$hist_time;
+				  $hist_loop_root_cur['hist_loop'][$table_class][$T][]=$h;
+				  $hist_loop_root_cur['hist_time'][]=$sql_ext." ##408 ".$path;
+				  $hist_loop_root_cur['hist_time'][]=$h;
+				// print_r($hist_loop);
+				 $path_root=add_path($path,$T,$table_class);
+				 dig_next($hist_loop_root_cur['hist_loop'], $tables[$table_class][$T],$h,$loop,$hist_loop_root_cur['hist_time'],$last_tbl,$path_root,$table_class);
+				 //rel dzieci $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
+				 if(isset($tables[$table_class][$T]['ref'])) {
+				// echo "<br>#296 try to ref tables for T ".$T."<pre>";
+				//  	print_r($tables['Z'][$T]['ref']);
+				// echo "</pre>";
+				 	
+					 foreach($tables[$table_class][$T]['ref'] as $ref_id => $ref_table) {
+						 $sql_ref=" select REMOTE_PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `PRIMARY_KEY`=".$h->ID." ";
+						 $res_rel=$db->query($sql_ref) or die('211 blad '.$sql_ref);
+						 echo "<br>#297 ".$sql_ref;
+						 while($h_rel=$db->fetch($res_rel)){
+							 $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->REMOTE_PRIMARY_KEY." ";
+							 $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
+							 echo "<br>#159 ref tbl ".$sql_rel_rec;
+							 while($h_rel_rec=$db->fetch($res_rel_rec)){
+								// echo "#315 fnd:".$h_rel_rec->ID;
+								 $hist_loop_cur=$hist_loop_root_cur;
+//									 $hist_loop_cur['hist_time']=$hist_time;
+								 //$hist_loop[$ref_table][]=$h_rel_rec;
+								 $hist_loop_cur['hist_loop'][$table_class][$ref_table][]=$h_rel_rec;
+//								 $hist_time[][$ref_table]=$sql_rel_rec;
+//								 $hist_time[][$ref_table]=$h_rel_rec;
+								 $hist_loop_cur['hist_time'][]=$sql_rel_rec." #435".$path;
+								 $hist_loop_cur['hist_time'][]=$h_rel_rec;
+								 dig_next($hist_loop_cur['hist_loop'],$tables[$table_class][$ref_table],$h_rel_rec,$loop,$hist_loop_cur['hist_time'],$ref_table,add_path($path_root,$ref_table,$table_class),$table_class);
+							 }
+						 }
+					 }
+				 
+				 } else echo "#322 not ref to ".$T;
+				 if(isset($tables[$table_class][$T]['backref'])) {
+				 //echo "<br>#296 try to backref tables  T ".$T."<pre>";
+				 // 	print_r($tables['Z'][$T]['backref']);
+				 //echo "</pre>";
+					 foreach($tables[$table_class][$T]['backref'] as $ref_id => $ref_table) {
+						 $sql_ref=" select PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `REMOTE_PRIMARY_KEY`=".$h->ID." ";
+						 $res_rel=$db->query($sql_ref) or die('318 blad '.$sql_ref);
+						 echo "<br>#319 backref ".$sql_ref;
+						 while($h_rel=$db->fetch($res_rel)){
+							 $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->PRIMARY_KEY." ";
+							 $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
+							 echo "<br>#324 ref tbl ".$sql_rel_rec;
+							 while($h_rel_rec=$db->fetch($res_rel_rec)){
+							//	 echo "#315 fnd:".$h_rel_rec->ID;
+								 $hist_loop_cur=$hist_loop_root_cur;
+//  								 $hist_loop_cur['hist_time']=$hist_time;
+						//		 $hist_loop[$ref_table][]=$h_rel_rec;
+								 $hist_loop_cur['hist_loop'][$table_class][$ref_table][]=$h_rel_rec;
+
+								 //$hist_time[][$ref_table]=$sql_rel_rec;
+								 //$hist_time[][$ref_table]=$h_rel_rec;
+								 $hist_loop_cur['hist_time'][$ref_table][]=$sql_rel_rec." #464".$path;
+								 $hist_loop_cur['hist_time'][$ref_table][]=$h_rel_rec;
+								 dig_next($hist_loop_cur['hist_loop'],$tables[$table_class][$ref_table],$h_rel_rec,$loop,$hist_loop_cur['hist_time'],$ref_table,add_path($path_root,$ref_table,$table_class),$table_class);
+							 }
+						 }
+					 }
+				 
+				 } else echo "#322 not backref to ".$T;
+				 
+			 } 
+		  
+}
+
+
+start_dig($tables);
+
+echo "<hr>end of raport ";
+
+
+
+
+

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

@@ -0,0 +1,319 @@
+<?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}");
+		}
+	}
+
+	private static function throwException($msg, $sql) {
+		throw new Exception(json_encode(['msg' => $msg, 'sql' => $sql]));
+	}
+
+	public static function search($args = null) {
+		$argsAvailable = ["kodPocztowy", "wojewodztwo", "powiat", "gmina", "miejscowosc", "ulica"];
+		self::checkArgs($args, $argsAvailable);
+		foreach ($args as &$arg) $arg = trim($arg);
+
+		$where = "";
+		$return = null;
+
+		if (isset($args['kodPocztowy'])) {
+			$args['kodPocztowy'] = str_replace(' ', '', $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) {
+				$kodPocztowy['wojewodztwo'] = $result[0]['wojewodztwo'];
+				$kodPocztowy['powiat'] = $result[0]['powiat'];
+				$kodPocztowy['gmina'] = $result[0]['gmina'];
+			}
+		}
+
+		try {
+			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))) throw new Exception("wojewodztwo not found");
+				$where = "and WOJ = '{$return['WOJ']}'";
+			}
+
+			if (isset($args['powiat'])) {
+				$args['powiat'] = preg_replace('/^M. /i', '', $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))) 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'];
+				$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}'";
+			}
+
+			if (isset($args['gmina'])) {
+				$args['gmina'] = preg_replace('/^M. /i', '', $args['gmina']);
+				$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'];
+					$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
+				}
+			}
+
+			if (!isset($return['WOJ'])) throw new Exception("Try with data from kodPocztowy");
+
+			if (isset($args['miejscowosc'])) {
+				$args['miejscowosc'] = preg_replace('/^M. /i', '', $args['miejscowosc']);
+				$args['miejscowosc'] = str_replace(' - ', '-', $args['miejscowosc']);
+				$args['miejscowosc'] = str_replace(' ', '%', $args['miejscowosc']);
+				$query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA like " . DB::getPDO()->quote($args['miejscowosc']) . " {$where}";
+				if (!($result = DB::getPDO()->fetchall($query))) self::throwException("miejscowosc not found", $query);
+				$return['WOJ'] = $result[0]['WOJ'];
+				$return['POW'] = $result[0]['POW'];
+				$return['GMI'] = $result[0]['GMI'];
+				if (count($result) > 1) {//self::throwException("Found too many miejscowoscs", $query);
+					$SYMarr = array_map(function ($a) {return $a['SYM'];}, $result);
+					$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM in (" . implode(",", $SYMarr) . ")) or RODZ_GMI>=8)";
+				} else {
+					$return['SYM'] = $result[0]['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)";
+				}
+			}
+		} catch (Exception $e) {
+			if (!isset($kodPocztowy)) throw new Exception($e->getMessage());
+
+			$query = "select WOJ from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($kodPocztowy['wojewodztwo']) . " and POW = 0";
+			if (!($return['WOJ'] = DB::getPDO()->fetchValue($query))) throw new Exception("wojewodztwo not found");
+			$where = "and WOJ = '{$return['WOJ']}'";
+
+			$query = "select WOJ, POW from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($kodPocztowy['powiat']) . " and POW > 0 and GMI = 0 {$where}";
+			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'];
+			$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}'";
+
+			$query = "select WOJ, POW, GMI from `" . self::TERC_TABLE. "` where NAZWA = " . DB::getPDO()->quote($kodPocztowy['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) {
+				$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'])) {
+				$args['miejscowosc'] = preg_replace('/^M. /i', '', $args['miejscowosc']);
+				$args['miejscowosc'] = str_replace(' - ', '-', $args['miejscowosc']);
+				$args['miejscowosc'] = str_replace(' ', '%', $args['miejscowosc']);
+				$query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA like " . DB::getPDO()->quote($args['miejscowosc']) . " {$where}";
+				if (!($result = DB::getPDO()->fetchall($query))) self::throwException("miejscowosc not found", $query);
+				$return['WOJ'] = $result[0]['WOJ'];
+				$return['POW'] = $result[0]['POW'];
+				$return['GMI'] = $result[0]['GMI'];
+				if (count($result) > 1) {//self::throwException("Found too many miejscowoscs", $query);
+					$SYMarr = array_map(function ($a) {return $a['SYM'];}, $result);
+					$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM in (" . implode(",", $SYMarr) . ")) or RODZ_GMI>=8)";
+				} else {
+					$return['SYM'] = $result[0]['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'] = trim(preg_replace('/osiedle/i', 'os.', $args['ulica']));
+			$args['ulica'] = trim(preg_replace('/aleja/i', 'al.', $args['ulica']));
+			$args['ulica'] = trim(preg_replace('/plac/i', 'pl.', $args['ulica']));
+			$args['ulica'] = trim(preg_replace('/kardynała/i', 'kard', $args['ulica']));
+			$args['ulica'] = trim(preg_replace('/\(.*\)/', '', $args['ulica']));
+			$args['ulica'] = trim(preg_replace('/-go/i', '', $args['ulica']));
+			$args['ulica'] = trim(preg_replace('/\.$/', '%', $args['ulica']));
+			$search = [' ', '..'];
+			$replace = ['%', '.'];
+			$args['ulica'] = str_replace($search, $replace, $args['ulica']);
+
+			$ulica = DB::getPDO()->quote("{$args['ulica']}");
+			$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica} " .
+				"or concat(CECHA, NAZWA_1) like {$ulica} or concat(CECHA, NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+			$result = DB::getPDO()->fetchall($query);
+
+			if (!$result) {
+				$ulica = DB::getPDO()->quote("ul. {$args['ulica']}");
+				$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica}) " .
+					"{$where} group by WOJ, POW, SYM_UL";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				$ulica = DB::getPDO()->quote("ul. {$args['ulica']}%");
+				$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica} " .
+					"or concat(CECHA, NAZWA_1) like {$ulica} or concat(CECHA, NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				$ulica = DB::getPDO()->quote("{$args['ulica']}%");
+				$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica} " .
+					"or concat(CECHA, NAZWA_1) like {$ulica} or concat(CECHA, NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				$ulica = DB::getPDO()->quote("{$args['ulica']}%");
+				$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (NAZWA_1 like {$ulica} or concat(NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				$ulica = DB::getPDO()->quote("%{$args['ulica']}");
+				$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (NAZWA_1 like {$ulica} or concat(NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				$ulica = DB::getPDO()->quote("%{$args['ulica']}%");
+				$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (NAZWA_1 like {$ulica} or concat(NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				preg_match('/^([^\.]*)\.([^\.]*)\.(.*)$/', $args['ulica'], $matches);
+				if (count($matches) == 4) {
+					$ulica = DB::getPDO()->quote(trim($matches[1]) . ". " . trim($matches[2]) . "% " . trim($matches[3]) . "%");
+					$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} " .
+						"or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+					$result = DB::getPDO()->fetchall($query);
+
+					if (!$result) {
+						$ulica = DB::getPDO()->quote(trim($matches[1]) . ". " . trim($matches[3]) . "%");
+						$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
+						$result = DB::getPDO()->fetchall($query);
+					}
+
+					if (!$result) {
+						$ulica = DB::getPDO()->quote("ul. " . trim($matches[3]) . "%");
+						$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
+						$result = DB::getPDO()->fetchall($query);
+					}
+				}
+			}
+
+			if (!$result) {
+				preg_match('/^([^\.]*)\.(.*)$/', $args['ulica'], $matches);
+				if (count($matches) == 3) {
+					$ulica = DB::getPDO()->quote("ul. " . trim($matches[1]) . "% " . trim($matches[2]) . "%");
+					$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} " .
+						"or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
+					$result = DB::getPDO()->fetchall($query);
+
+					if (!$result) {
+						$ulica = DB::getPDO()->quote("ul. " . trim($matches[2]) . "%");
+						$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
+						$result = DB::getPDO()->fetchall($query);
+					}
+
+					if (!$result) {
+						$ulica = DB::getPDO()->quote(trim($matches[1]) . ". " . trim($matches[2]) . "%");
+						$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
+						$result = DB::getPDO()->fetchall($query);
+					}
+
+					if (!$result) {
+						$ulica = DB::getPDO()->quote(trim($matches[1]) . ". %" . trim($matches[2]));
+						$query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
+						$result = DB::getPDO()->fetchall($query);
+					}
+				}
+			}
+
+			if (!$result) {
+				$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
+				$query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA like " . DB::getPDO()->quote($args['ulica']) . " {$where}";
+				$result = DB::getPDO()->fetchall($query);
+			}
+
+			if (!$result) {
+				if (isset($return['SYM'])) $where2 = "and SYM = '{$return['SYM']}'";
+				else $where2 = "";
+				$where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' {$where2}) or RODZ_GMI>=8)";
+				$query = "select count(*) from `" . self::ULIC_TABLE . "` where 1 = 1 {$where}";
+				if ($liczbaUlic = DB::getPDO()->fetchValue($query)) throw new Exception(json_encode(['msg' => "ulica not found", 'sql' => $query]));
+			} else {
+				if (count($result) > 1) throw new Exception(json_encode(['msg' => "Found too many ulicas", 'sql' => $query]));
+				$return['WOJ'] = $result[0]['WOJ'];
+				$return['POW'] = $result[0]['POW'];
+				$return['GMI'] = $result[0]['GMI'];
+				$return['SYM'] = $result[0]['SYM'];
+				if (isset($result[0]['SYM_UL'])) $return['SYM_UL'] = $result[0]['SYM_UL'];
+			}
+
+			if (!isset($return['SYM'])) {
+				if (isset($SYMarr)) $return['SYM'] = $SYMarr[0];
+				else throw new Exception("miejscowosc not found");
+			}
+		}
+
+		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))) throw new Exception("ulica not found");
+		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))) 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))) 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']}' 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))) throw new Exception("miejscowosc not found");
+		if (count($result) > 1) throw new Exception("Found too many miejscowoscs");
+		$return['miejscowosc'] = $result[0]['NAZWA'];
+
+		return $return;
+	}
+
+}

+ 27 - 0
SE/se-lib/V.php

@@ -542,4 +542,31 @@ EOF';
 		return $items;
 	}
 
+	public static function arrayToXML($array, $formatOutput = false, $root = "root") {
+
+		function arrayToXML_rec($data, $dom, $node, $parent = null) {
+			$child = $dom->createElement($node);
+			if (!$parent) $parent = $dom;
+			if (is_array($data)) {
+				foreach ($data as $key => $value) {
+					if (is_numeric($key)) arrayToXML_rec($value, $dom, $node, $parent);
+					else arrayToXML_rec($value, $dom, $key, $child);
+				}
+			} else {
+				if ($data) {
+					if ($data == htmlspecialchars($data)) $child->nodeValue = $data;
+					else $child->appendChild($dom->createCDATASection($data));
+				} else $parent->appendChild($child);
+			}
+			if ($child->hasChildNodes()) $parent->appendChild($child);
+		}
+
+		if (!is_array($array)) throw new Exception("First argument need to be an array");
+		$dom = new DOMDocument('1.0', 'UTF-8');
+		$dom->preserveWhiteSpace = false;
+		$dom->formatOutput = $formatOutput;
+		arrayToXML_rec($array, $dom, $root);
+		return $dom->saveXML();
+	}
+		
 }

+ 5 - 3
SE/superedit-STATYSTYKA_TABELE.php

@@ -131,7 +131,8 @@ function STATYSTYKA_TABELE() {
 
 		 $sqlList[]="update BUILDINGS set `__Rozdzielcza_Kabel_Swiatlowodowy_wsg84`=null ; ";
 		 $sqlList[]="update BUILDINGS t1, Rozdzielcza_Kabel_Swiatlowodowy_wsg84 t2 set t1.__Rozdzielcza_Kabel_Swiatlowodowy_wsg84=t2.ID where t1.ID=t2.ID_BUILDINGS ; ";
-		 $sqlList[]="update BUILDINGS t1, MK_Rewiry t2 set t1.`__MK_Rewiry__STATUS`=t2.A_STATUS where t1.M_REWIR=t2.NAZWA_REWI ; ";
+		 $sqlList[]="update BUILDINGS t1, MK_Rewiry t2 set t1.`__MK_Rewiry__STATUS`=t2.A_STATUS where t1.M_REWIR=t2.NAZWA_REWI and t1.M_REWIR like '%_%' ; ";
+		 $sqlList[]="update USERS2_MARKETING t1, MK_Rewiry t2 set t1.`__MK_Rewiry__STATUS`=t2.A_STATUS where t1.M_REWIR=t2.NAZWA_REWI and t1.M_REWIR like '%_%' ; ";
 
 
 
@@ -725,9 +726,10 @@ function STATYSTYKA_TABELE() {
 							) as p on   ST_Intersects(p.the_geom,b.the_geom)
 							set b.ID_PROJECT=p.ID where  b.ID_PROJECT<1  ;";
 							
-			$sqlList[] = "	update PROBLEMS_temp as b, PROBLEMS b1 set b1.ID_PROJECT=b.ID_PROJECT where b1.ID=b.ID;
+			$sqlList[] = "	update PROBLEMS_temp as b, PROBLEMS b1 set b1.ID_PROJECT=b.ID_PROJECT where b1.ID=b.ID;";
 							
-							drop table PROBLEMS_temp; ";
+			$sqlList[] = "	drop table IF EXISTS PROBLEMS_temp; ";
+
 							
 
 		}