<?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");



function init_sh_check() {
    //check if run from bash
    if(isset($_SERVER["argv"])) {
        if(@$_SERVER["argv"][1]=='--help') {
            echo "
                Options are:
                php ".$_SERVER["argv"][0]." --check   - control if we have needed tables/data
                
                .EOF
            ";
        } else if(@$_SERVER["argv"][1]=='--check') { 
            check();
        } else echo " bad option, try:
            php ".$_SERVER["argv"][0]." --help
            
            .EOF
            ";
    } else {
        echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
        check();
    }
}



function check() {
	echo "<br>\n i run check()";
    //todo to set arguments to rebuild tables etc:
    $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
                    'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');

    //$tables =  DB::getPDO()->list_tables();
    foreach($check_tbls as $tbl) {
        $query = "select ID from ".$tbl." limit 1 ";
        echo "<br>\nresult chk for tbl:".$tbl." ;";
        
        try{
            $result = DB::getPDO()->fetchall($query);
        } catch(Exception $exception) {
            // return $exception;
            echo "<br>\n jest blad z tbl:".$tbl." i get sample table structure with data from ../sql/".$tbl.".sql  - it should be manually replaced by correct/newer!";
            $sql = file_get_contents('../sql/'.$tbl.'.sql');
            
            try{
                $result = DB::getPDO()->fetchall($sql);
            } catch(Exception $exception2) {
                echo "<br>\n jest blad dump dla tbl:".$tbl;
            }
        } 
        //	print_r($exception);
        print_r($result);
    }
}

init_sh_check();   


$edge_paths_with_PE=simplexml_load_file("edge_paths_with_PE.xml");
$edge_paths_with_PE->registerXPathNamespace('bp', 'https://procesy5.pl/biale_plamy-schema.xsd');
/*
	<PE_candidates_dump xmlns:bp="https://procesy5.pl/biale_plamy-schema.xsd"
                    desc="brakuje ominiecia punktow ways tylko do przylaczy i analizy dlugosci">
   <LINESTRING ID_Way="3"
               Count="15"
               distance="1168.1143169402"
               dumb_frequency="10">
      <bp:Path_Point Pos="10" X="18.63765" Y="51.255476599079"/>
   </LINESTRING>
 */
//print_r($edge_joins);
$edge_paths_with_PE_arr=array();
foreach($edge_paths_with_PE->children() as $child)  {
 // echo "\n<br>";
	   //$attrs=$child->attributes("ID_Way");

	   foreach($child->attributes() as $a=>$b) {
		   //	echo "#87Child";print_r($b[0]);
		   if($a=='ID_Way') $ID_Way=$b[0];

		   //if($a=='Count') $Count=$b[0];
	   }
	  //echo $child->getName() . ": " . " ID_Way: ".$ID_Way. " : ";
	
	   settype($ID_Way, "integer");

	   //$ID_Way=var_dump($ID_Way);
	 //  echo "#88Child:"; print_r($ID_Way);
	   //$edge_joins_path['ID_Way'][$attrs['ID_Way']]=array();
	  // $result = $xml->xpath('/a/b/c');
	   $id_j=0; 
	   foreach($child->xpath('bp:Path_Point') as $Path_Point_xml)  {
		   $id_j++;
		   $Path_Point=$Path_Point_xml->attributes();
		   //echo " #93 ID_Join ".$attrs_join["Joins"]." ";
		   $Path_Point['Pos']=$Path_Point['Pos'][0]; settype($Path_Point['Pos'], "integer");
		   $Path_Point['X']=$Path_Point['X'][0]; settype($Path_Point['X'], "string");
		   $Path_Point['Y']=$Path_Point['Y'][0]; settype($Path_Point['Y'], "string");
		   $Path_Point['ID_Join']=$Path_Point['ID_Join'][0]; settype($Path_Point['ID_Join'], "integer");

		   $edge_paths_with_PE_arr[$ID_Way][$id_j]=$Path_Point;
	   }
}

echo "\n<br>edge_paths_with_PE_arr";
print_r($edge_paths_with_PE_arr);


 DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_PE");

unset($ID_Way);

$edge_joins_filled_paths=array();
foreach($edge_paths_with_PE_arr as $ID_Way=>$Path_Point) {
		
		foreach($Path_Point as $Path_Point_val) {
		$sql_Path_Point_val="insert into Rozdzielcza_test_bzyk_PE (the_geom,ID_Way,ID_Join)
		     values (ST_GeomFromText('POINT(".$Path_Point_val['X']." ".$Path_Point_val['Y'].")'),'".$ID_Way."' ,
		      '".$Path_Point_val['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['X'])>1 and   strlen($Path_Point_val['Y'])>1 ) DB::getPDO()->query($sql_Path_Point_val);
		    //else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
		}		
	
}



