superedit-SYNC_GIS_TABLES.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. //
  3. // synchronizacja do serwera postgres GIS
  4. //
  5. include_once('se-lib/DB_Dump.php');
  6. include_once('se-lib/UsersHelper.php');
  7. function SYNC_GIS_TABLES() {
  8. global $db,$gdb;
  9. $mysql_zas_id=36;
  10. $gis_zas_id=13102;
  11. $loc_sync_table="_sync_to_gis_36_to_13102";
  12. $sql_cr="";
  13. $db = DB::getDB();
  14. $gdb= DB::getDB($gis_zas_id);
  15. DEBUG_S(-3,'db',$db);
  16. DEBUG_S(-3,'gis',$gdb);
  17. $tbls_to_sync[]='BUILDINGS';
  18. //$tbls_to_sync[]='MK_Budynki';
  19. $rem_field_timestamp='TIMESTAMP';
  20. //nadanie uprawnien do tabeli GIS
  21. //="dodanie wszystkich mozliwych zasobow jako role-stanowiska";
  22. function add_pg_roles() {
  23. global $db,$gdb;
  24. $groups=UsersHelper::get_group_list();
  25. DEBUG_S(-3,'Lista group ',$groups,__FILE__,__FUNCTION__,__LINE__);
  26. foreach($groups as $idg=>$val) {
  27. $sql="drop role if exists \"".$idg."\"";
  28. echo "<br> dodaje role: ".$sql;
  29. $gdb->query($sql);
  30. $sql="create role \"".$idg."\"";
  31. echo "<br> dodaje role: ".$sql;
  32. $gdb->query($sql);
  33. }
  34. }
  35. echo "<br> aktualziacja relacji obiektow do paszportyzacji optycznej";
  36. function gis_opto_relations($table_to_set,$field_index,$field_to_set,$table_from,$field_from,$ST_Relate='******F**',$filter_sql='') {
  37. global $gdb;
  38. $sql='
  39. drop table if exists gis_opto_relations;
  40. select t1.'.$field_index.' as "field_index", t2."'.$field_from.'" as "field_from"
  41. , case ST_Relate(t1."the_geom",t2."the_geom",\''.$ST_Relate.'\') when true then
  42. 1
  43. else 0 end
  44. as if_true , t2.*
  45. into temporary table gis_opto_relations
  46. FROM "'.$table_to_set.'" as t1
  47. CROSS JOIN "'.$table_from.'" as t2;
  48. CREATE INDEX myfield ON gis_opto_relations (field_from);
  49. update "'.$table_to_set.'" t1
  50. set "'.$field_to_set.'"=t2.field_from
  51. from gis_opto_relations t2
  52. where t1."'.$field_index.'"=t2.field_index and t2.if_true=1
  53. '.$filter_sql.'
  54. ;
  55. drop table gis_opto_relations;
  56. -- select t1.'.$field_index.' as "field_index", t2."'.$field_from.'" as "field_from" ,ST_Relate(t1."the_geom",t2."the_geom",\''.$ST_Relate.'\') as if_true into table gis_opto_relations_ FROM "'.$table_to_set.'" as t1 CROSS JOIN "'.$table_from.'" as t2;
  57. ';
  58. DEBUG_S(-3,"Zapytanie sql aktualizujace",$sql,__FILE__,__FUNCTION__,__LINE__);
  59. $gdb->query($sql);
  60. }
  61. function gis_opto_calculations($table_to_set,$field_index,$field_to_set,$table_from,$field_from_geom,$ST_Relate='******F**',$filter_sql='') {
  62. global $gdb;
  63. $sql='
  64. drop table if exists "'.$table_from.'_AGR" ;
  65. select t1.'.$field_index.' , t2.'.$field_index.' as gid_t2
  66. , ST_Intersects(t1."'.$field_from_geom.'",t2."'.$field_from_geom.'")
  67. , case ST_Relate(t1."'.$field_from_geom.'",t2."'.$field_from_geom.'",\''.$ST_Relate.'\') when true then
  68. ST_Length_Spheroid(t2."'.$field_from_geom.'",\'SPHEROID["WGS 84",6378137,298.257223563]\')
  69. else \'0\' end
  70. as length
  71. into temporary table "'.$table_from.'_AGR"
  72. FROM "'.$table_to_set.'" as t1
  73. CROSS JOIN "'.$table_from.'" as t2
  74. ;
  75. update "'.$table_to_set.'" t1
  76. set "'.$field_to_set.'"=(
  77. select
  78. round(sum(t2.length))
  79. from "'.$table_from.'_AGR" as t2 where t2.'.$field_index.'=t1.'.$field_index.'
  80. )
  81. ;';
  82. echo "<br>sql to : ".$sql;
  83. $gdb->query($sql);
  84. }
  85. function gis_opto_calculations_count($table_to_set,$field_index,$field_to_set,$table_from,$field_from_geom,$ST_Relate='******F**',$filter_sql='') {
  86. global $gdb;
  87. $sql='
  88. drop table if exists "'.$table_from.'_AGR" ;
  89. select t1.'.$field_index.'
  90. , ST_Intersects(t1."'.$field_from_geom.'",t2."'.$field_from_geom.'")
  91. , case ST_Relate(t1."'.$field_from_geom.'",t2."'.$field_from_geom.'",\''.$ST_Relate.'\') when true then
  92. 1
  93. else 0 end
  94. as length
  95. into temporary table "'.$table_from.'_AGR"
  96. FROM "'.$table_to_set.'" as t1
  97. CROSS JOIN "'.$table_from.'" as t2
  98. ;
  99. update "'.$table_to_set.'" t1
  100. set "'.$field_to_set.'"=(
  101. select
  102. sum(t2.length)
  103. from "'.$table_from.'_AGR" as t2 where t2.'.$field_index.'=t1.'.$field_index.'
  104. )
  105. ;';
  106. echo "<br>sql to : ".$sql;
  107. $gdb->query($sql);
  108. }
  109. gis_opto_calculations('Inwestycje_Planowane','gid','Agr_metrow_mikrorurek_5szt','Rozdzielcza_Pakiet5MikrorurekMagistrala_WSG84','the_geom','T********');
  110. gis_opto_calculations('Inwestycje_Planowane','gid','Agr_metrow_mikrorurek','Rozdzielcza_Mikrokanalizacja_do_klienta','the_geom','T********');
  111. gis_opto_calculations('Inwestycje_Planowane','gid','Agr_ilosc_wykopu','Rozdzielcza_Wykop_przedmiar_na_mikrorurki','the_geom','******F**');
  112. gis_opto_calculations('Inwestycje_Planowane','gid','Agr_ilosc_przeciskow','Rozdzielcza_Przeciski_110mm','the_geom','******F**');
  113. gis_opto_calculations('Inwestycje_Planowane','gid','Agr_ilosc_zabrukow','Rozdzielcza_Zabruki','the_geom','******F**');
  114. gis_opto_calculations_count('Inwestycje_Planowane','gid','Agr_ilosc_domow','MK_Mieszkania','the_geom','******F**');
  115. $sql_cena='update "Inwestycje_Planowane" set "Agr_metrow_mikrorurek_5szt"=0 where "Agr_metrow_mikrorurek_5szt" is null;';
  116. $gdb->query($sql_cena);
  117. $sql_cena='update "Inwestycje_Planowane" set "Agr_ilosc_zabrukow"=0 where "Agr_ilosc_zabrukow" is null;';
  118. $gdb->query($sql_cena);
  119. $sql_cena='update "Inwestycje_Planowane" set
  120. koszt_wspolny="Agr_ilosc_wykopu"*10
  121. +"Agr_metrow_mikrorurek"*3
  122. +"Agr_metrow_mikrorurek_5szt"*7
  123. +"Agr_ilosc_przeciskow"*80
  124. +"Agr_ilosc_zabrukow"*40
  125. ;'; //"Agr_metrow_w_tpsa*3 +Agr_metrow_kabel*
  126. //+ studnia +
  127. $gdb->query($sql_cena);
  128. $sql_cena='update "Inwestycje_Planowane" set "koszt_na_dom"=round("koszt_wspolny"/"Agr_ilosc_domow") where "Agr_ilosc_domow">0 ' ;
  129. $gdb->query($sql_cena);
  130. gis_opto_relations('opt_tacki','id_0','id_przelac','opt_przelacznice_mufy','id_0','T********');
  131. gis_opto_relations('opt_spawy','id_0','id_tacka','opt_tacki','id_0','T********');
  132. $gdb->query('update opt_wlokna set id_tuba=null ');
  133. gis_opto_relations('opt_wlokna','id','id_tuba','opt_tuby','id_0','T********');
  134. gis_opto_relations('opt_tuby','id_0','id_kabel','opt_kable','id_0','T********');
  135. $gdb->query('update opt_wlokna set id_tacka_a=null, id_tacka_b=null; ');
  136. gis_opto_relations('opt_wlokna','id','id_tacka_a','opt_tacki','id_0','T********','and t1.id_tacka_a is null ');
  137. gis_opto_relations('opt_wlokna','id','id_tacka_b','opt_tacki','id_0','T********','and t1.id_tacka_a!=t2.field_from and t1.id_tacka_b is null');
  138. $gdb->query('update opt_przelacznice_mufy set id_opt_lokalizacja=null; ');
  139. gis_opto_relations('opt_przelacznice_mufy','id_0','id_opt_lokalizacja','opt_lokalizacje','id_0','T********');
  140. $gdb->query('update opt_wlokna set id_spaw_a=null, id_spaw_b=null; ');
  141. gis_opto_relations('opt_wlokna','id','id_spaw_a','opt_spawy','id_0','FF10F0FF2','and t1.id_spaw_a is null');
  142. gis_opto_relations('opt_wlokna','id','id_spaw_b','opt_spawy','id_0','FF10F0FF2','and t1.id_spaw_a!=t2.field_from and t1.id_spaw_b is null');
  143. die("zapytanie: ".$sql);
  144. // add_pg_roles();
  145. //dodanie wszystkich uzytkownikow z haslem
  146. // public static function &get_users_list( $params = array(), $limit = 10, $limit_start = 0, $order_by = '', $order_dir = '' ) {
  147. //$tbl_perms=UserAcl::getTableAcl();
  148. include_once('se-lib/UserAcl.php');
  149. $Users=UsersHelper::get_users_list();
  150. DEBUG_S(-3,'UsersHelper::get_users_list()',$Users,__FILE__,__FUNCTION__,__LINE__);
  151. $userAcl = UsersHelper::getUserAcl($Users[9]->ID);
  152. // $userAcl->fetchGroups();
  153. $userAcl->fetchAllPerms();
  154. //$getTablesAcl=$userAcl->getTablesAcl();
  155. getAcl();
  156. DEBUG_S(-3,'UserAcl::getTablesAcl',$getTablesAcl,__FILE__,__FUNCTION__,__LINE__);
  157. if ($userAcl->hasTableAcl($zasobObj->ID)) {
  158. $tblAcl = $usrAcl->getTableAcl($tableZasobID);
  159. $tblAcl->init();
  160. }
  161. DEBUG_S(-3,'UsersHelper::getUserAcl',$userAcl,__FILE__,__FUNCTION__,__LINE__);
  162. //odlaczenie wszystkich rol od uzytkownikow
  163. die('zabilem skrypt');
  164. //$sql="select ID from CRM_LISTA_ZASOBOW where `TYPE`='STANOWISKO' and "
  165. foreach($tbls_to_sync as $tbl) {
  166. $res=$gdb->describe_table($tbl);
  167. $ile=$gdb->count($res);
  168. $struct=$db->STRUCTURE_GENERATE(array($tbl),null,null);
  169. DEBUG_S(-3,'Structure local',$struct,__FILE__,__FUNCTION__,__LINE__);
  170. if(!$ile) {
  171. $gdb->STRUCTURE_GENERATE_PARSE($struct);
  172. $gdb->query('alter table "'.$tbl.'" add "gid" serial primary key ;'); //todo before "ID"
  173. $gdb->query("CREATE OR REPLACE FUNCTION update_A_RECORD_UPDATE_DATE_column()
  174. RETURNS TRIGGER AS $$
  175. BEGIN
  176. NEW.\"A_RECORD_UPDATE_DATE\" = now();
  177. RETURN NEW;
  178. END;
  179. $$ language 'plpgsql';");
  180. $gdb->query("CREATE TRIGGER update_A_RECORD_UPDATE_DATE BEFORE UPDATE
  181. ON \"".$tbl."\" FOR EACH ROW EXECUTE PROCEDURE
  182. update_A_RECORD_UPDATE_DATE_column();");
  183. //$gdb->query('alter table "'.$tbl.'" add "the_geom" serial primary key ;'); //todo before "ID"
  184. } else {
  185. $gdb->STRUCTURE_GENERATE_PARSE($struct);
  186. echo "<br> Znalazlem cos, wiec !ile nie zadzialalo!(".$ile.")";
  187. }
  188. //1) try to put newer data
  189. $sql="select t1.* from ".$tbl." as t1 left join _SYNC_to_gis_36_to_13102 as t2 on t1.ID=t2.LID and t2.LTABLE='".$tbl."'
  190. where t2.LID is NULL; "; //unix_timestamp(t1.A_RECORD_UPDATE_DATE)>unix_timestamp(t2.LTS) or
  191. echo "<br>".$sql;
  192. $lres=$db->query($sql);
  193. while($h=$db->fetch($lres)) {
  194. $data=array();
  195. foreach($h as $ind=>$val) {
  196. //if($ind=='the_geom')
  197. //$data[$ind]="'01030000000100000005000000A681BD96FAAB32402B9771AC13334B40A430199F0DAC3240E91B087D12334B4092AA81BA0DAC3240A8DFAECE15334B40004534E3FAAB32401568C3D418334B40A681BD96FAAB32402B9771AC13334B40'";
  198. //else
  199. if(empty($val))
  200. $data[$ind]="null";
  201. else
  202. $data[$ind]="'".$gdb->_($val)."'";
  203. //
  204. }
  205. DEBUG_S(-3,'zminne H to ',$data);
  206. //$array=array('lastname', 'email', 'phone');
  207. //$comma_separated = implode(",", $array);
  208. //uczylem dzieciaki
  209. $keys=array_keys($data);
  210. $comma_separated = '"'.implode('","', $keys).'"';
  211. DEBUG_S(-3,'comma_separated',$comma_separated);
  212. $values_comma_separated=implode(",",$data);
  213. $sql="insert into \"".$tbl."\" ( ".$comma_separated." ) values ( ".$values_comma_separated." ) ";
  214. DEBUG_S(-3,'nasze zapytanie SQL',$sql);
  215. $gdb->query($sql) or die('blad 104');
  216. $sql="insert into _SYNC_to_gis_36_to_13102 ( LTABLE, LID, RID, RIDCOLUMN , RTS, LTS )
  217. values ( '".$tbl."',".$data['ID'].",'".$gdb->insert_id()."','gid',now(),now()) ";
  218. DEBUG_S(-3,'sql to synctable',array($sql,$gdb->insert_id()),__FILE__,__FUNCTION__,__LINE__);
  219. $db->query($sql) or die('blad 107');
  220. //die('123');
  221. //
  222. // DEBUG_S(-3,'rec data',$data);
  223. // $sql="insert into \"".$tbl."\" ( \"".implode('","', array_keys($data))."\") values (".implode(',', $data).") ";
  224. //
  225. // die($sql);
  226. }
  227. /* $sql_rem="create table if not exists ".$tbl." (GIS_TIMESTAMP timestamp) ;";
  228. $gdb->query($sql_rem);
  229. $sql_rem="select max(GIS_TIMESTAMP) as max from ".$tbl."";
  230. $res=$gdb->query($sql_rem);
  231. $last_rem=$gdb->fetch_assoc($res);
  232. if(empty($last_rem['max'])) $last_rem['max']=0;
  233. DEBUG_S(-3,'Last ts z rem',$last_rem['max']);
  234. $sql_loc="select * from ".$tbl." where unix_timestamp(A_RECORD_UPDATE_DATE)>".$last_rem['max']." ";
  235. echo $sql_loc;
  236. $res=$db->query($sql_loc);
  237. $i=0;
  238. while($h=$db->fetch($res)) {
  239. print_r($h);
  240. }
  241. */
  242. // $structure_local=DB_Dump::get_db_structure_by_table_prefix($db,$tbl);
  243. // $structure_remote=DB_Dump::get_db_structure_by_table_prefix($gdb,$tbl);
  244. // DEBUG_S(-3,'structure_local',$structure_local,__FILE__,__FUNCTION__,__LINE__);
  245. // DEBUG_S(-3,'structure_remote',$structure_remote,__FILE__,__FUNCTION__,__LINE__);
  246. }
  247. $trigger_psql_do_timestamp="CREATE OR REPLACE FUNCTION update_modified_column()
  248. RETURNS TRIGGER AS $$
  249. BEGIN
  250. NEW.modified = now();
  251. RETURN NEW;
  252. END;";
  253. }