audit_krs_con_digger.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php
  2. $_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
  3. //when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
  4. if( file_exists ( "../../se-lib/bootstrap.php"))
  5. require("../../se-lib/bootstrap.php");
  6. //when used in in native SE
  7. else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");
  8. Lib::loadClass("Vendor_Geophp");
  9. $db = DB::getDB();
  10. // DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_Cables_to_PE");
  11. //unset($ID_Way);
  12. /*foreach($cables_from_joins_to_PE_arr as $id_j=>$Path_Point_val) {
  13. $sql_Path_Point_val="insert into Rozdzielcza_test_bzyk_Cables_to_PE (the_geom,ID_Way,ID_Point,len,PE_ID_Join)
  14. values (ST_GeomFromText('".$Path_Point_val['LINESTRING']."'),'".$Path_Point_val['ID_Way']."',
  15. '".$Path_Point_val['ID_Point']."' ,'".$Path_Point_val['len']."','".$Path_Point_val['PE_ID_Join']."' ) ";
  16. echo "#216Query path ins:: ".$sql_Path_Point_val." ";
  17. //if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
  18. if(strlen($Path_Point_val['LINESTRING'])>1)
  19. DB::getPDO()->query($sql_Path_Point_val);
  20. else echo " ERROR blad danych!!! dla ID_Point:".$Path_Point_val['ID_Point'];
  21. //else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
  22. }
  23. */
  24. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['pesel']='pesel';
  25. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['nip']='nip';
  26. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['regon']='regon';
  27. $tables['Y']['BI_audit_ENERGA_PRACOWNICY']['rel']['ID_BI_audit_ENERGA_PRACOWNICY']='ID';
  28. //$tables['X']['BI_audit_KW_requested']['rel_search']['pesel']='Seller_Person';
  29. //$tables['X']['BI_audit_KW_requested']['rel']['pesel']['search']='like';
  30. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel']['ID_BI_audit_ENERGA_PRACOWNICY']='ID_BI_audit_ENERGA_PRACOWNICY';
  31. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  32. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  33. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['budynek']='nrBudynku';
  34. $tables['Z']['BI_audit_ENERGA_PRACOWNICY_adresy']['rel_and']['TERYT']['nrLokalu']='nrLokalu';
  35. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['BI_audit_ENERGA_RUM_KONTRAHENCI']='ID';
  36. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['nip']='NIP';
  37. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['regon']='REGON';
  38. $tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_like']['pesel']='PESEL';
  39. $tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['BI_audit_ENERGA_RUM_KONTRAHENCI']='ID';
  40. $tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  41. $tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  42. $tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['nrLokalu']='Numer_mieszkania_lokalu';
  43. $tables['Z']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel_and']['TERYT']['budynek']='Numer_budynku';
  44. //$tables['X']['BI_audit_ENERGA_RUM_KONTRAHENCI']['rel']['teryt']=1;
  45. //$tables['X']['BI_audit_ENERGA_RUM_UMOWY']['rel_like']['nip']='Strony_umowy';
  46. $tables['Z']['BI_audit_CEIDG']['rel']['nip']='nip';
  47. $tables['Z']['BI_audit_CEIDG']['rel']['regon']='regon';
  48. //$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['miejscowosc']='miejscowosc';
  49. //$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['ulica']='ulica';
  50. //$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  51. //$tables['Z']['BI_audit_CEIDG']['rel_and']['adres']['budynek']='budynek';
  52. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  53. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  54. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['nrLokalu']='lokal';
  55. $tables['Z']['BI_audit_CEIDG']['rel_and']['TERYT']['budynek']='budynek';
  56. $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  57. $tables['Z']['BI_audit_CEIDG']['ref'][18]='BI_audit_CEIDG_powiazania';
  58. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel']['nip']='nip';
  59. //$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['miejscowosc']='miejscowosc';
  60. //$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['ulica']='ulica';
  61. //$tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['adres']['kodPocztowy']='kodPocztowy';
  62. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  63. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  64. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['nrLokalu']='lokal';
  65. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['rel_and']['TERYT']['budynek']='budynek';
  66. $tables['Z']['BI_audit_CEIDG_pelnomocnicy']['backref'][19]='BI_audit_CEIDG';
  67. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['nip']='nip';
  68. $tables['Z']['BI_audit_CEIDG_powiazania']['rel']['regon']='regon';
  69. $tables['Z']['BI_audit_CEIDG_powiazania']['backref'][18]='BI_audit_CEIDG';
  70. $tables['Z']['BI_audit_KRS']['rel']['nip']='nip';
  71. $tables['Z']['BI_audit_KRS']['rel']['regon']='regon';
  72. $tables['Z']['BI_audit_KRS']['rel']['krs']='krs';
  73. //$tables['Z']['BI_audit_KRS']['rel_and']['adres']['miejscowosc']='A_miejscowosc';
  74. //$tables['Z']['BI_audit_KRS']['rel_and']['adres']['ulica']='A_ulica';
  75. //$tables['Z']['BI_audit_KRS']['rel_and']['adres']['kodPocztowy']='A_kod';
  76. //$tables['Z']['BI_audit_KRS']['rel_and']['adres']['budynek']='A_nrDomu';
  77. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['TERYT_SYM']='TERYT_SYM';
  78. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['TERYT_SYM_UL']='TERYT_SYM_UL';
  79. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['nrLokalu']='A_nrLokalu';
  80. $tables['Z']['BI_audit_KRS']['rel_and']['TERYT']['budynek']='A_nrDomu';
  81. $tables['Z']['BI_audit_KRS']['ref'][12]='BI_audit_KRS_person';
  82. $tables['Z']['BI_audit_KRS']['ref'][11]='BI_audit_KRS_company';
  83. $tables['Z']['BI_audit_KRS_company']['rel']['regon']='regon';
  84. $tables['Z']['BI_audit_KRS_company']['rel']['krs']='krs';
  85. $tables['Z']['BI_audit_KRS_company']['backref'][11]='BI_audit_KRS';
  86. $tables['Z']['BI_audit_KRS_person']['rel']['pesel']='pesel';
  87. $tables['Z']['BI_audit_KRS_person']['backref'][12]='BI_audit_KRS';
  88. function start_dig($tables) {
  89. global $db;
  90. foreach($tables['Y'] as $Y=>$t) {
  91. $sql='select * from '.$Y.' where ID=12 or ID=19 or 1=1 limit 100' ;
  92. $res=$db->query($sql);
  93. while($h=$db->fetch($res)) {
  94. $hist_loop[$Y][]=$h;
  95. $hist_time[][$Y]=$h;
  96. dig_next($hist_loop,$t,$h,1,$hist_time,$Y,add_path('//',$Y));
  97. unset($hist_time);
  98. }
  99. }
  100. }
  101. function add_path($path,$last_tbl) {
  102. $path.="/".$last_tbl;
  103. return $path;
  104. }
  105. function sql($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl,$path,$table_class) {
  106. global $tables;
  107. foreach($tables[$table_class] as $Z=>$z) {
  108. //rels in tbls
  109. foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
  110. // echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
  111. if( isset($prev_tbl['rel'][$z_rel_from])) {
  112. if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 0 ) {
  113. $sql[$Z]['rel'][$z_rel_to] = $prev_dig->$prev_tbl['rel'][$z_rel_from];
  114. $path_rel[$Z][]="(".$z_rel_to.")";
  115. }
  116. }
  117. }
  118. //search addr
  119. if(isset($z['rel_and']) and isset($prev_tbl['rel_and'])) {
  120. foreach($z['rel_and'] as $z_rel_and=>$z_rel_and_name) {
  121. $rel_and_ok=1;
  122. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  123. 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)
  124. //warunek - nie ma elementu - nie szukamy po nim
  125. } 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)
  126. if($rel_and_ok<>0) $rel_and_ok=2;
  127. // 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;
  128. } else {
  129. // echo "<br>\n #127 UNSET rel_and from:".$last_tbl." name ".$z_rel_and_name_cur." / ".$z_rel_and_name_col_to." ";
  130. $rel_and_ok=0;
  131. }
  132. }
  133. if($rel_and_ok==2) {
  134. $path_rel_and = array();
  135. // echo "\n<br>#133 Z=".$Z." ok rel_and_ok=".$rel_and_ok;
  136. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  137. // 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;
  138. if(strlen($prev_dig->$prev_tbl['rel_and'][$z_rel_and][$z_rel_and_name_cur]) == 0 ) {
  139. //warunek nie szukamy po tym elemencie
  140. } else {
  141. $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];
  142. $path_rel_and[]="(".$z_rel_and_name_col_to.")";
  143. }
  144. }
  145. } else {
  146. // echo "<br>#137 error rel_and_ok=".$rel_and_ok;
  147. }
  148. }
  149. }
  150. }
  151. if( isset($sql_rel_and)) {
  152. foreach($sql_rel_and as $T=>$z_rel_and_arr) {
  153. foreach($z_rel_and_arr as $z_rel_and_name=>$z_rel_and_name_col) {
  154. //echo "<br>156 z_rel_and_name=".$z_rel_and_name." ";
  155. $sql_rel_and_sql[$T][$z_rel_and_name]="select * from ".$T." where ( ";
  156. $and=0;
  157. foreach($z_rel_and_name_col as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  158. 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 '' " ;
  159. 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 '' " ;
  160. // if($z_rel_and_name_cur=='miejscowosc') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `miejscowosc` not like '' " ;
  161. // if($z_rel_and_name_cur=='A_ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_ulica` not like '' " ;
  162. // if($z_rel_and_name_cur=='ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `ulica` not like '' " ;
  163. // if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` not like '' " ;
  164. // 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."' " ;
  165. // 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."' " ;
  166. // 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."' " ;
  167. $and=1;
  168. }
  169. $sql_rel_and_sql[$T][$z_rel_and_name].=" ) " ;
  170. if( isset($hist_loop[$table_class][$T]) ) {
  171. foreach($hist_loop[$table_class][$T] as $hist_rec) {
  172. $sql_rel_and_sql[$T][$z_rel_and_name].=" and ID!='".$hist_rec->ID."' ";
  173. }
  174. }
  175. $sql_rel_and_sql[$T][$z_rel_and_name].=" ; " ;
  176. dig_arr_cur($hist_loop,$T,$sql_rel_and_sql[$T][$z_rel_and_name],$hist_time,$loop,$last_tbl,$path,$table_class);
  177. }
  178. }
  179. }
  180. if(isset($sql)) {
  181. foreach($sql as $T => $rel) {
  182. $sql_t[$T]=" select * from ".$T." where ( ";
  183. foreach($rel['rel'] as $col=>$val) {
  184. if( isset($sql_t_str[$T])) $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
  185. else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
  186. }
  187. $sql_t[$T].=$sql_t_str[$T]." ) ";
  188. //not loop
  189. if( isset($hist_loop[$table_class][$T]) ) {
  190. // echo "<br> hist loop cnt: ". count($hist_loop[$table_class][$T]);
  191. foreach($hist_loop[$table_class][$T] as $hist_rec) {
  192. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  193. }
  194. }
  195. }
  196. if( isset($sql_t)) {
  197. foreach($sql_t as $T => $sql_ext) {
  198. // $path.=$path_rel[$T][ key($sql_ext) ];
  199. dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path,$table_class);
  200. }
  201. }
  202. }
  203. }
  204. function dig_next($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl='n/s',$path) {
  205. global $db,$tables;
  206. $loop++;
  207. echo "\n<hr>###LOOP ".$loop." path:".$path;
  208. if($loop<12) {
  209. //detect next ref tbls
  210. $sql = array();
  211. $sql_RELAND = array();
  212. sql($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl,$path,'Z');
  213. sql($hist_loop,$prev_tbl,$prev_dig,$loop,$hist_time,$last_tbl,$path,'X');
  214. /*foreach($tables['Z'] as $Z=>$z) {
  215. //rels in tbls
  216. foreach($z['rel'] as $z_rel_from=>$z_rel_to) {
  217. // echo "110 test z_rel_fr ".$z_rel_from." to z_rel_to ".$z_rel_to ;
  218. if( isset($prev_tbl['rel'][$z_rel_from])) {
  219. if( strlen($prev_dig->$prev_tbl['rel'][$z_rel_from]) > 0 ) {
  220. $sql[$Z]['rel'][$z_rel_to] = $prev_dig->$prev_tbl['rel'][$z_rel_from];
  221. $path_rel[$Z][]="(".$z_rel_to.")";
  222. }
  223. }
  224. }
  225. //search addr
  226. if(isset($z['rel_and'])) {
  227. foreach($z['rel_and'] as $z_rel_and=>$z_rel_and_name) {
  228. $rel_and_ok=1;
  229. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  230. 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)
  231. if($rel_and_ok<>0) $rel_and_ok=2;
  232. // 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;
  233. } else {
  234. // echo "<br> #127 UNSET ".$prev_dig->$z_rel_and_name_cur." / ".$z_rel_and_name_col_to." ";
  235. $rel_and_ok=0;
  236. }
  237. }
  238. if($rel_and_ok==2) {
  239. $path_rel_and = array();
  240. // echo "\n<br>#133 Z=".$Z." ok rel_and_ok=".$rel_and_ok;
  241. foreach($z_rel_and_name as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  242. // 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;
  243. $sql_rel_and[$Z][$z_rel_and][$z_rel_and_name_col_to]=$prev_dig->$z_rel_and_name_col_to;
  244. $path_rel_and[]="(".$z_rel_and_name_col_to.")";
  245. }
  246. } else {
  247. // echo "<br>#137 error rel_and_ok=".$rel_and_ok;
  248. }
  249. }
  250. }
  251. }
  252. // echo "<br>135:sql_rel_and <pre>";
  253. // print_r($sql_rel_and);
  254. // echo " .EOF</pre>";
  255. if( isset($sql_rel_and)) {
  256. foreach($sql_rel_and as $T=>$z_rel_and_arr) {
  257. foreach($z_rel_and_arr as $z_rel_and_name=>$z_rel_and_name_col) {
  258. //echo "<br>156 z_rel_and_name=".$z_rel_and_name." ";
  259. $sql_rel_and_sql[$T][$z_rel_and_name]="select * from ".$T." where ( ";
  260. $and=0;
  261. foreach($z_rel_and_name_col as $z_rel_and_name_cur=>$z_rel_and_name_col_to) {
  262. 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 '' " ;
  263. 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 '' " ;
  264. // if($z_rel_and_name_cur=='miejscowosc') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `miejscowosc` not like '' " ;
  265. // if($z_rel_and_name_cur=='A_ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `A_ulica` not like '' " ;
  266. // if($z_rel_and_name_cur=='ulica') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `ulica` not like '' " ;
  267. // if($z_rel_and_name_cur=='kodPocztowy') $sql_rel_and_sql[$T][$z_rel_and_name].=" and `kodPocztowy` not like '' " ;
  268. // 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."' " ;
  269. // 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."' " ;
  270. // 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."' " ;
  271. $and=1;
  272. }
  273. $sql_rel_and_sql[$T][$z_rel_and_name].=" ) " ;
  274. if( isset($hist_loop[$T])) {
  275. foreach($hist_loop[$T] as $hist_rec) {
  276. $sql_rel_and_sql[$T][$z_rel_and_name].=" and ID!='".$hist_rec->ID."' ";
  277. }
  278. }
  279. $sql_rel_and_sql[$T][$z_rel_and_name].=" ; " ;
  280. dig_arr_cur($hist_loop,$T,$sql_rel_and_sql[$T][$z_rel_and_name],$hist_time,$loop,$last_tbl,$path);
  281. }
  282. }
  283. }
  284. */
  285. {
  286. //echo "<br>164:sql_rel_and_sql <pre>";
  287. // print_r($sql_rel_and_sql);
  288. // echo " .EOF</pre>";
  289. //X test
  290. //echo " <br> 129 prev <pre>";
  291. // print_r($prev_dig);
  292. // echo "<br> prevtbl";
  293. // print_r($prev_tbl);
  294. // echo "</pre>";
  295. }
  296. /*
  297. foreach($tables['X'] as $X=>$x) {
  298. //rels in tbls
  299. if( isset($x['rel']) ) {
  300. foreach($x['rel'] as $x_rel_from=>$x_rel_to) {
  301. //echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
  302. if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])
  303. // 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].")
  304. if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from]) and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { //
  305. $sql_X[$X]['rel'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
  306. }
  307. }
  308. }
  309. }
  310. //rel_like
  311. if( isset($x['rel_like'])) {
  312. foreach($x['rel_like'] as $x_rel_from=>$x_rel_to) {
  313. //echo "\n<br> #133test tbl ".$X." z_rel_fr ".$x_rel_from." to z_rel_to ".$x_rel_to." prev tbl " ;
  314. if( isset($prev_tbl['rel'][$x_rel_from]) ) { //isset($prev_dig->$prev_tbl['rel'][$x_rel_to])
  315. // 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].")
  316. if( isset($prev_dig->$prev_tbl['rel'][$x_rel_from]) and strlen($prev_dig->$prev_tbl['rel'][$x_rel_from]) > 1 ) { //
  317. $sql_X1[$X]['rel_like'][$x_rel_to]= $prev_dig->$prev_tbl['rel'][$x_rel_from];
  318. }
  319. }
  320. }
  321. }
  322. }
  323. flush();
  324. // echo "\n<br>sql_X<pre>";
  325. // print_r($sql_X);
  326. // echo "</pre>";
  327. if( isset($sql_X)) {
  328. foreach($sql_X as $T => $rel) {
  329. $sql_x[$T]=" select * from ".$T." where ( ";
  330. foreach($rel['rel'] as $col=>$val) {
  331. if( isset($sql_x_str[$T])) $sql_x_str[$T].=" or `".$col."` = '".$val."' " ;
  332. else $sql_x_str[$T]=" `".$col."` = '".$val."' " ;
  333. }
  334. $sql_x[$T].=$sql_x_str[$T]." ) ";
  335. //not loop
  336. if( isset($hist_loop[$T])) {
  337. foreach($hist_loop[$T] as $hist_rec) {
  338. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  339. }
  340. }
  341. }
  342. }
  343. // echo "<br> sql_x" ;
  344. //print_r($sql_x);
  345. if( isset($sql_X1)) {
  346. foreach($sql_X1 as $T => $rel) {
  347. $sql_x1[$T]=" select * from ".$T." where ( ";
  348. foreach($rel['rel_like'] as $col=>$val) {
  349. if( isset($sql_x1_str[$T])) $sql_x1_str[$T].=" or `".$col."` like '%".$val."'%" ;
  350. else $sql_x1_str[$T]=" `".$col."` like '%".$val."%' " ;
  351. }
  352. $sql_x1[$T].=$sql_x1_str[$T]." ) ";
  353. //not loop
  354. if( isset($hist_loop[$T])) {
  355. foreach($hist_loop[$T] as $hist_rec) {
  356. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  357. }
  358. }
  359. }
  360. }
  361. if( isset($sql_x)) {
  362. foreach($sql_x as $X => $sql_ext) {
  363. echo "\n<br>174 Querying :".$sql_ext ;
  364. $res=$db->query($sql_ext) or die('175 blad');
  365. while($h=$db->fetch($res)) {
  366. echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA X <pre>";
  367. print_r($h);
  368. echo "<br> Historia sledzen:";
  369. print_r($hist_time);
  370. echo "#177 eof </pre>";
  371. }
  372. }
  373. }
  374. if( isset($sql_x1)) {
  375. foreach($sql_x1 as $X => $sql_ext) {
  376. echo "\n<br>306 Querying :".$sql_ext ;
  377. $res=$db->query($sql_ext) or die('307 blad');
  378. while($h=$db->fetch($res)) {
  379. echo "\n<hr> #309 POWIAZANIE ZNALEZIONE DLA X2 <pre>";
  380. print_r($h);
  381. echo "<br> Historia sledzen:";
  382. print_r($hist_time);
  383. echo "<br>relacje:";
  384. echo "#177 eof </pre>";
  385. }
  386. }
  387. }
  388. //X test eof
  389. */
  390. /*
  391. foreach($sql as $T => $rel) {
  392. $sql_t[$T]=" select * from ".$T." where ( ";
  393. foreach($rel['rel'] as $col=>$val) {
  394. if( isset($sql_t_str[$T])) $sql_t_str[$T].=" or `".$col."` = '".$val."' " ;
  395. else $sql_t_str[$T]=" `".$col."` = '".$val."' " ;
  396. }
  397. $sql_t[$T].=$sql_t_str[$T]." ) ";
  398. //not loop
  399. if( isset($hist_loop[$T])) {
  400. echo "<br> hist loop cnt: ". count($hist_loop[$T]);
  401. foreach($hist_loop[$T] as $hist_rec) {
  402. $sql_t[$T].=" and ID!='".$hist_rec->ID."' ";
  403. }
  404. }
  405. }
  406. // echo "<br> 202 hist loop cnt: <pre> ". count($hist_loop);
  407. // print_r($hist_loop);
  408. // echo "</pre>";
  409. // echo "<pre>sql_t";
  410. // print_r($sql);
  411. // echo "<br>sql_t ";
  412. // print_r($sql_t);
  413. // echo "</pre>";
  414. if( isset($sql_t)) {
  415. foreach($sql_t as $T => $sql_ext) {
  416. // $path.=$path_rel[$T][ key($sql_ext) ];
  417. dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path);
  418. }
  419. }
  420. //todo look in Y and die()
  421. */
  422. } else {
  423. echo "\n<hr> #377 Przekroczony limit zagnieżdzeń dla wątku: path=".$path."; <pre>\n";
  424. //print_r($hist_time);
  425. echo "</pre>";
  426. }
  427. echo "<br>#243 glebokosc:".$loop;
  428. }
  429. function dig_arr_cur($hist_loop,$T,$sql_ext,$hist_time,$loop,$last_tbl,$path,$table_class) {
  430. global $db,$tables;
  431. echo "\n<br>216: from:".$last_tbl." Querying :T ".$T." ext: ".$sql_ext ;//$sql_ext
  432. // echo "\n<br> las fou"
  433. flush();
  434. $res=$db->query($sql_ext) or die('211 blad '.$sql_ext);
  435. while($h=$db->fetch($res)) {
  436. echo "\n<br> #149 found ".$h->ID;
  437. if($table_class=='X') {
  438. echo "\n<hr> #177 POWIAZANIE ZNALEZIONE DLA X <pre>";
  439. print_r($h);
  440. echo "<br> Historia sledzen:";
  441. print_r($hist_time);
  442. echo "#177 eof </pre>";
  443. }
  444. // echo "<pre>"; print_r($h); echo "</pre>";
  445. // $hist_loop[$T][]=$h;
  446. // $hist_time[][$T]=$sql_ext." ##408 ".$path;
  447. // $hist_time[][$T]=$h;
  448. $hist_loop_root_cur['hist_loop']=$hist_loop;
  449. $hist_loop_root_cur['hist_time']=$hist_time;
  450. $hist_loop_root_cur['hist_loop'][$table_class][$T][]=$h;
  451. $hist_loop_root_cur['hist_time'][]=$sql_ext." ##408 ".$path;
  452. $hist_loop_root_cur['hist_time'][]=$h;
  453. // print_r($hist_loop);
  454. $path_root=add_path($path,$T);
  455. dig_next($hist_loop_root_cur['hist_loop'], $tables[$table_class][$T],$h,$loop,$hist_loop_root_cur['hist_time'],$last_tbl,$path_root);
  456. //rel dzieci $tables['Z']['BI_audit_CEIDG']['ref'][19]='BI_audit_CEIDG_pelnomocnicy';
  457. if(isset($tables[$table_class][$T]['ref'])) {
  458. // echo "<br>#296 try to ref tables for T ".$T."<pre>";
  459. // print_r($tables['Z'][$T]['ref']);
  460. // echo "</pre>";
  461. foreach($tables[$table_class][$T]['ref'] as $ref_id => $ref_table) {
  462. $sql_ref=" select REMOTE_PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `PRIMARY_KEY`=".$h->ID." ";
  463. $res_rel=$db->query($sql_ref) or die('211 blad '.$sql_ref);
  464. echo "<br>#297 ".$sql_ref;
  465. while($h_rel=$db->fetch($res_rel)){
  466. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->REMOTE_PRIMARY_KEY." ";
  467. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  468. echo "<br>#159 ref tbl ".$sql_rel_rec;
  469. while($h_rel_rec=$db->fetch($res_rel_rec)){
  470. // echo "#315 fnd:".$h_rel_rec->ID;
  471. $hist_loop_cur=$hist_loop_root_cur;
  472. // $hist_loop_cur['hist_time']=$hist_time;
  473. //$hist_loop[$ref_table][]=$h_rel_rec;
  474. $hist_loop_cur['hist_loop'][$table_class][$ref_table][]=$h_rel_rec;
  475. // $hist_time[][$ref_table]=$sql_rel_rec;
  476. // $hist_time[][$ref_table]=$h_rel_rec;
  477. $hist_loop_cur['hist_time'][]=$sql_rel_rec." #435".$path;
  478. $hist_loop_cur['hist_time'][]=$h_rel_rec;
  479. 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));
  480. }
  481. }
  482. }
  483. } else echo "#322 not ref to ".$T;
  484. if(isset($tables[$table_class][$T]['backref'])) {
  485. //echo "<br>#296 try to backref tables T ".$T."<pre>";
  486. // print_r($tables['Z'][$T]['backref']);
  487. //echo "</pre>";
  488. foreach($tables[$table_class][$T]['backref'] as $ref_id => $ref_table) {
  489. $sql_ref=" select PRIMARY_KEY from `CRM__#REF_TABLE__".$ref_id."` where `REMOTE_PRIMARY_KEY`=".$h->ID." ";
  490. $res_rel=$db->query($sql_ref) or die('318 blad '.$sql_ref);
  491. echo "<br>#319 backref ".$sql_ref;
  492. while($h_rel=$db->fetch($res_rel)){
  493. $sql_rel_rec="select * from `".$ref_table."` where ID=".$h_rel->PRIMARY_KEY." ";
  494. $res_rel_rec=$db->query($sql_rel_rec) or die('211 blad '.$sql_rel_rec);
  495. echo "<br>#324 ref tbl ".$sql_rel_rec;
  496. while($h_rel_rec=$db->fetch($res_rel_rec)){
  497. // echo "#315 fnd:".$h_rel_rec->ID;
  498. $hist_loop_cur=$hist_loop_root_cur;
  499. // $hist_loop_cur['hist_time']=$hist_time;
  500. // $hist_loop[$ref_table][]=$h_rel_rec;
  501. $hist_loop_cur['hist_loop'][$table_class][$ref_table][]=$h_rel_rec;
  502. //$hist_time[][$ref_table]=$sql_rel_rec;
  503. //$hist_time[][$ref_table]=$h_rel_rec;
  504. $hist_loop_cur['hist_time'][$ref_table][]=$sql_rel_rec." #464".$path;
  505. $hist_loop_cur['hist_time'][$ref_table][]=$h_rel_rec;
  506. 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));
  507. }
  508. }
  509. }
  510. } else echo "#322 not backref to ".$T;
  511. }
  512. }
  513. start_dig($tables);
  514. echo "<hr>end of raport ";