audit_krs_con_digger.php 27 KB

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