<?php
//
// synchronizacja do serwera postgres GIS
//

include_once('se-lib/DB_Dump.php');
include_once('se-lib/UsersHelper.php');




class GIS_OPT_STRUCTURES2 {


		function __construct() {
			$this->width=800;
			
		}


		function init() {
			
				$this->graph = new stdClass();
				$this->graph->htmlID = 'graph_proces_id_' . $id_proces;
				$this->graph->procesTreeFlat = array();
				$this->graph->procesTreeGoto = array();
				$this->graph->treeItems = array();

				$this->graph->elements = new stdClass();
				$this->graph->elements->nodes = array();
				$this->graph->elements->edges = array();
			
		}


		function set_mysql_zas_id($mysql_zas_id) {
			$this->mysql_zas_id=$mysql_zas_id;
		}


		function set_gis_zas_id($gis_zas_id) {
			$this->gis_zas_id=$gis_zas_id;
			$this->gdb=DB::getDB($this->gis_zas_id);

		}
		
		
		function get_opt_objects($src_table,$src_index,$src_name,$map_table_id,$map_table_name,$parent_table_id,$parent_table,$limit=null,$ids_ok) {
			
			
			$sql='select \''.$src_table.'\' as src_table , "'.$src_index.'" as src_index ';
			if(!empty($src_name)) $sql.=', "'.$src_name.'" as src_name ';
			$sql.=', "'.$parent_table_id.'"  as parent_table_id ,  \''.$parent_table.'\' as parent_table  from "'.$src_table.'" '.$limit.' ;';
			$res=$this->gdb->query($sql);
			while($h=$this->gdb->fetch($res)) {
				if(!empty($limit)) {
					//bierzemy wszystkie rekordy bo byl limit
					$arr[]=$h;
					$this->tables->$src_table->src_index[]=$h->src_index;
					$this->tables->$src_table->data[$h->src_index]=$h;

						


				} else {
					//tylko te rekordy dla ktorych byly powiazania
					if(in_array($h->parent_table_id, $this->tables->$parent_table->src_index)) {
						$this->tables->$src_table->src_index[]=$h->src_index;
			//			echo "<br> <font color=green>id ".$h->parent_table_id." IS found in ".$parent_table." parent table</font>";
						$arr[]=$h;
						$this->tables->$src_table->data[$h->src_index]=$h;
					//	$this->tables->$src_table->childs[$h->src_index]=array();
					
						if(!in_array($h->src_index, $this->tables->$parent_table->childs[$h->parent_table_id]->tables[$src_table])) {
							$this->tables->$parent_table->childs[$h->parent_table_id]->tables[$src_table][]=$h->src_index;
							$this->tables->$parent_table->childs[$h->parent_table_id]->objects[]=$src_table."_".$h->src_index;
						
							$this->tables->$parent_table->child_recurs[$h->parent_table_id]->tables[$src_table][]=$h->src_index;

//							echo "<br> Table to ".$src_table."  a parent to ".$parent_table;
						}
					
					} //else echo "<br> id ".$h->parent_table_id." not found in ".$parent_table." parent table";
				}
				
			}
			return $arr;
			
		}
		
		
		function get_opt_objects_parent($src_table,$src_index,$src_name,$map_table_id,$map_table_name,$parent_table_id,$parent_table,$limit=null,$child_table,$child_joinfield,$child_index){
			$i=0;
			$where_in=implode(",", $this->tables->$child_table->src_index);
			
			$sql='select \''.$src_table.'\' as src_table , t1."'.$src_index.'" as src_index ';
			if(!empty($src_name)) $sql.=', t1."'.$src_name.'" as src_name ';
			$sql.=', t1."'.$parent_table_id.'"  as parent_table_id ,  \''.$parent_table.'\' as parent_table  from "'.$src_table.'"  as t1
			  inner join "'.$child_table.'" as t2 on t1."'.$src_index.'"=t2."'.$child_joinfield.'" 
			 where  t2."'.$child_index.'" in ('.$where_in.') ;';
			
		//	echo "<br> sql: ".$sql;
			$res=$this->gdb->query($sql);
			while($h=$this->gdb->fetch($res)) {
					$i++;
//				if($i>5) continue;
					if(!in_array($h->src_index, $this->tables->$src_table->src_index)) {
					$this->tables->$src_table->src_index[]=$h->src_index;						
						$this->tables->$src_table->data[$h->src_index]=$h;
						$arr[]=$h;
					}
					if($h->parent_table_id)
						if(!in_array($h->src_index, $this->tables->$parent_table->childs[$h->parent_table_id]->tables[$src_table])) {
							$this->tables->$parent_table->childs[$h->parent_table_id]->tables[$src_table][]=$h->src_index;
							$this->tables->$parent_table->childs[$h->parent_table_id]->objects[]=$src_table."_".$h->src_index;

						} 
				//	echo "<br> (".$src_table.") child_joinfield is ".$child_joinfield." from ".$parent_table."  val is ".$h->parent_table_id;

			}
			return $arr;
			
		}
	
	function set_layer_deepth($src_table,$deepth) {
		$this->tables->$src_table->deepth=$deepth;
	}
	
	function get_layer_deepth($src_table) {
		return $this->tables->$src_table->deepth;
	}
	
	function invert_layer_deepth() {
		$deepth=0;
		foreach($this->tables as $src_table=>$obj_data) {
			if($obj_data->deepth>$deepth) $deepth=$obj_data->deepth;
		}
		
		foreach($this->tables as $src_table=>$obj_data) {
			$this->tables->$src_table->deepth=$deepth-$this->tables->$src_table->deepth;
		//echo "<br>182 Deepht".$deepth."  w ".$src_table;
		}
	}
	
	
	//rekursywne nadanie wszystkim obiektom powiazan/sciezek, aby mozna bylo je przesortowac i poukladac po kolei - aby obiekty root znaly swoje powiazania
	function find_connect_paths($src_table,$deepth=0) {
		self::set_layer_deepth($src_table,$deepth);
		$deepth++;
		//najpierw zjezdzamy w dol po dzieciach dajac znac na sam dol o ID nadrzenego
		//echo "<hr> odpalam funkcje path dla ".$src_table;
		//for($i = 1; $i <= 10; $i++) {
			foreach($this->tables->$src_table->childs as $id_parent=>$child) {
//				echo "<br> Szumam dzieci..";
				foreach($child->tables as $table=>$child_) {
					foreach($child_ as $id ) {
					//	echo "<br> Wpisalem do dziecka ".$table."  , ze parent is ".$id_parent." /".$child_;echo " = ".$id;
						$this->tables->$table->parents->$src_table->tables[$id][]=$id_parent;
						if(!in_array($table, $tables_to_connect)) $tables_to_connect[]=$table;
					}
				}
				foreach($this->tables->$src_table->parents as $parent_table=>$parent_data) {
				//echo "<br> 140: parent_table= ".$parent_table;
				
					foreach($parent_data->tables as $src_id=>$parents) {
					//	echo " 142: (".$src_id.")";
						foreach($parents as $dst_id) {

								foreach($this->tables->$src_table->childs[$src_id]->tables as $child_table=>$child_id_arr) {

									foreach($child_id_arr as $child_id) {
										
										// if(!in_array($dst_id, $this->tables->$child_table->parents->$parent_table->tables[$child_id])) $this->tables->$child_table->parents->$parent_table->tables[$child_id][]=$dst_id;
										self::notify_child_node_about_parents($child_table,$parent_table,$child_id,$dst_id);
										self::add_link_from_recurse_struct($child_table,$parent_table,$child_id);

										$this->tables->$parent_table->child_recurs[$dst_id]->tables[$child_table][$child_id]=1;
										//						$this->tables->$parent_table->size[$dst_id]->all++;
									}
								}
								//$this->tables->$src_table->parents->$parent_table->tables[$id][]=$id_parent;
						}
					}
				}
			}
			
	//	}
		//potem patrzymy czy wystepuje jakis obiekt naszego typu w dol, jezeli tak, to zapisujemy to , ze ma polaczenie przez obiekty podrzedne
		foreach($tables_to_connect as $table) {
			self::find_connect_paths($table,$deepth);
		}
		
	}
	
	
	//funkcja informuje dzieci obiektu o tym, jakich ma rodzicow
	function notify_child_node_about_parents($child_table,$parent_table,$child_id,$dst_id) {
		if(!in_array($dst_id, $this->tables->$child_table->parents->$parent_table->tables[$child_id])) {
			//DEBUG_S(-3,"Nadpisze parenta dla tabeli ".$parent_table."(".$dst_id.") w tabeli ".$src_table." child ".$child_table." = src ".$src_id." id parent ".$id_parent,null,__FILE__,__FUNCTION__,__LINE__);										
			$this->tables->$child_table->parents->$parent_table->tables[$child_id][]=$dst_id;
		}
	}
	
	
	//funkcja informujaca, ze opt_wezly maja ze soba powiazania w links, na postawie tego, ze gdzies na dole wlokna ida poprzez strukture do tych wezlow
	function add_link_from_recurse_struct($child_table,$parent_table,$child_id) {
		if(count($this->tables->$child_table->parents->$parent_table->tables[$child_id])>1) {
			DEBUG_S(3,'Moge powiazac tabele '.$parent_table.' na podstawie info z '.$child_table,$this->tables->$child_table->parents->$parent_table->tables[$child_id],__FILE__,__FUNCTION__,__LINE__);
				$first=false;
			foreach($this->tables->$child_table->parents->$parent_table->tables[$child_id] as $link) {
				if(!$first) $first=$link; 
				   else {
					$this->tables->$parent_table->links->$first->$link=true;
					$this->tables->$parent_table->links->$link->$first=true;

				}
			}
				
		}
		
	}
	

	function set_layer_coords($src_table,$start_x,$start_y) {
			$this->tables->$src_table->cursor->x=$start_x;
			$this->tables->$src_table->cursor->y=$start_y;
	}
	
	function offset_layer_coords($src_table,$x,$y) {
			$this->tables->$src_table->cursor->x+=$x;
			$this->tables->$src_table->cursor->y+=$y;
	}
	
	function set_obj_coord_by_layer($src_table,$obj) {
		$this->tables->$src_table->obj_cursor[$obj]->x=$this->tables->$src_table->cursor->x;
		$this->tables->$src_table->obj_cursor[$obj]->y=$this->tables->$src_table->cursor->y;
	}
	
	function set_obj_size_width($src_table,$obj,$width) {
		$this->tables->$src_table->obj_width[$obj]=$width+20;
	}
	
	
	function set_obj_min_y_size($src_table,$obj,$size) {
		$this->tables->$src_table->min_y_size[$obj]=$size;
	}
	
	function set_obj_size_width_by_min_y_size($src_table,$obj) {
		self::set_obj_size_width($src_table,$obj,$this->tables->$src_table->min_x_size[$obj]);
	}
	
	
	function set_obj_size_width_by_parent($parent_table,$parent_obj,$src_table,$obj,$opts) {
		if(isset($opts['ratio'])) 
			$this->tables->$src_table->obj_width[$obj]=$this->tables->$parent_table->obj_width[$parent_obj]*$opts['ratio'];
	}
	
	function get_obj_size_width($src_table,$obj) {
		return $this->tables->$src_table->obj_width[$obj];
	}
	
	

	
	function set_obj_coords_by_parent($parent_table,$parent_obj,$src_table,$obj) {
		echo "\n// set_obj_coords_by_parent  (".$parent_table.",".$parent_obj.",".$src_table.",".$obj.") ";
		$this->tables->$src_table->obj_cursor[$obj]->x=$this->tables->$parent_table->obj_cursor[$parent_obj]->x+10;
		$this->tables->$src_table->obj_cursor[$obj]->y=$this->tables->$parent_table->obj_cursor[$parent_obj]->y+10;
		echo " - i setted ".$this->tables->$parent_table->obj_cursor[$parent_obj]->x."/ ".$this->tables->$parent_table->obj_cursor[$parent_obj]->y;
	}
	

	
	
	function calculate_obj_size_by_child_recurs($src_table,$obj) {
			$count=0;$width=0;
		echo "\n//Licze size dla ".$src_table.' / '.$obj;
		foreach($this->tables->$src_table->child_recurs[$obj]->tables as $child_table=>$child_table_arr) {
			if(self::get_type_table($child_table)=='Connect') continue;

			echo " - ".$child_table;
			$count+=count($child_table_arr)*20;

			if(strlen(self::get_obj_name($src_table,$obj)) > $width) $width=strlen(self::get_obj_name($src_table,$obj))*5;
		}
			$count_local=count($this->tables->$src_table->childs[$obj]->objects)*20;
		
		if($width<100) $width=100;   
		if($count_local>$count) $count=$count_local;
		$count=$count+15+self::get_layer_deepth($src_table)*15;
//		$this->tables->$src_table->min_y_size[$obj]=$count;
		
		self::set_obj_min_y_size($src_table,$obj,$count);
		$this->tables->$src_table->min_x_size[$obj]=$width;


		
	}
	
	function get_obj_size_by_child_recurs($src_table,$obj) {
		echo "\n // get_obj_size_by_child_recurs dla ".$src_table." wynosi ".$obj." = ".$this->tables->$src_table->min_y_size[$obj];
		return $this->tables->$src_table->min_y_size[$obj];
		
	}
	
	function get_obj_name($src_table,$obj) {
		return $this->tables->$src_table->data[$obj]->src_name;
	}
	
	
	//funkcja do zwracania pierwszego obiektu wg roznych kryteriow : najmniej_powiazan, powiazanie z poprzednim, etc...
	function get_next_obj($src_table,$situation) {
		echo "\n /* 313: Situation <-- ";
		print_r($situation);
		print_r($this->get_next_obj);
		echo "*/\n";
		
		if($situation->count>1) {
			if($situation->min_links==0) $ret=$situation->min_links_id;
			else if($situation->min_links==1) { echo "\n// 320:Sytuacja min_links==1";
				if(isset($this->get_next_obj->last_obj)) {
					if(isset($this->get_next_obj->last_obj_count)) {
						
						echo "\n //324::szukamy kogos, do kogo mamy podpiecie ";
						$var=$this->get_next_obj->last_obj;
						foreach(self::get_next_obj_unlayed($src_table) as $obj) {
								if(isset($this->tables->$src_table->links->$obj->$var))
									if(in_array($obj, $situation->min_links_id_arr))
									  $ret=$obj;
								}
					} else  {
						//poprzedni nie mial zadnego powiazania
						$ret=$situation->min_links_id;
					}
				}
			}
		} else $ret=$situation->id;
		
		$this->get_next_obj->last_obj=$ret;
		
		
		
		

		if(!isset($ret)) $ret=$situation->id;
		return $ret;	

		
	}
	
	
	
	
	function analyze_arrange_situation($src_table) {
		$var=$this->get_next_obj->last_obj;
		if(isset($this->get_next_obj->last_obj)) {

		 if(isset($this->tables->$src_table->links->$var)) {
		 		echo "\n // 354: analyze_arrange_situation dla ".$this->get_next_obj->last_obj." ";

			$co=0;
		  	foreach($this->tables->$src_table->links->$var as $cos=>$cos_) {
			  	$co++;
		  	}
		  	$this->get_next_obj->last_obj_count=$co;
		  } else echo "\n //353: no exists links for ".$var;
		 }
		 $max_links=0;
	
	
		 foreach(self::get_next_obj_unlayed($src_table) as $obj) {
		 	 $cnt[]=$obj;
		 	echo "\n //334: max_links=".$max_links." id ".$max_links_id." min is ".$min_links." id is ".$min_links_id." obj[".$obj."]";

		 	  $count=0;
			 if(isset($this->tables->$src_table->links->$obj)) {
				
				 foreach($this->tables->$src_table->links->$obj as $link) {
					 $count++; 
				 }
			  }
				 echo "\n// 315:jeszcze nie pojawil sie ".$src_table." [".$obj."] count: ".$count;

				 if(isset($max_links)) {
					 if($count>$max_links)  {
					 $max_links=$count;
					 $max_links_id=$obj;
					 unset($max_links_id_arr);
					 $max_links_id_arr[]=$obj;
					 } else if($count==$max_links)  $max_links_id_arr[]=$obj;
				 }
				 if(isset($min_links)) {
					 if($count<$min_links) {
					 	 $min_links=$count;
					 	 $min_links_id=$obj;
					 	 unset($min_links_id_arr);
					 	 $min_links_id_arr[]=$obj;

					 } else if($count==$min_links)  $min_links_id_arr[]=$obj;
				 }

				 
			
			 	if(!isset($min_links)) {
			 	 	$min_links=$count;$min_links_id=$obj;$min_links_id_arr[]=$obj;
			 	}
			 	if(!isset($max_links)) {
			 	 	$max_links=$count;$max_links_id=$obj;$max_links_id_arr[]=$obj;
			 	}
			 
			}
		 

		 	$ret->min_links=$min_links;
		 	$ret->max_links=$max_links;
		 	$ret->min_links_id=$min_links_id;
		 	$ret->max_links_id=$max_links_id;
		 	$ret->max_links_id_arr=$max_links_id_arr;
		 	$ret->min_links_id_arr=$min_links_id_arr;
		 	$ret->count=count($cnt);
			$ret->id=$cnt[0];
			
			return $ret;
			
		
	
	
	}
		
		
	
	
	function get_next_obj_unlayed($src_table) {
		foreach($this->tables->$src_table->src_index as $obj) {
			if(!self::check_if_obj_is_layed($src_table,$obj)) {
				$ret[]=$obj;
			}
		}


		return $ret;
		
	}
	
	function lay_object($src_table,$obj,$start_x,$start_y) {
		
		$min_x=200;$min_y=0;
		//wykryj jakie X/Y jest aktualne
			
	

				//echo "\nvar ".$src_table."_".$obj." = new joint.shapes.basic.Rect({
				echo "\nvar ".$src_table."_".$obj." = new joint.shapes.devs.".self::get_type_table($src_table)."({
				

				position: { x: ".$this->tables->$src_table->obj_cursor[$obj]->x.", y: ".$this->tables->$src_table->obj_cursor[$obj]->y." },
				size: { width: ".self::get_obj_size_width($src_table,$obj).", height: ".self::get_obj_size_by_child_recurs($src_table,$obj)." }
				, attrs: {
				    text: { text: '".self::get_obj_name($src_table,$obj).$obj."', fill: 'white' , 'y-alignment': 'top', 'x-alignment': 'left'  } ,
				    'inPorts circle': { fill: 'PaleGreen' }
				  } , label: { text: 'Dupa' }

				".self::get_ports_for_obj($src_table,$obj)."				  
				});\n";
			self::notify_layed_object($src_table,$obj);
			self::lay_object_child($src_table,$obj);
	}
	
	function check_if_obj_is_layed($src_table,$obj) {
		  return in_array($src_table."_".$obj, $this->cells);
	}
	
	function notify_layed_object($src_table,$obj) {
		$this->cells[]=$src_table."_".$obj;
	}
	
	function lay_object_child($src_table,$obj) {
		self::offset_layer_coords($src_table,5,0);
		foreach($this->tables->$src_table->childs[$obj]->tables as $child_table=>$child_table_arr) {
			foreach($child_table_arr as $child_id) {
				if($this->tables->$child_table->set_type_table=='Atomic') {
					self::calculate_obj_size_by_child_recurs($child_table,$child_id);
					self::set_obj_coords_by_parent($src_table,$obj,$child_table,$child_id);
					self::set_obj_size_width_by_parent($src_table,$obj,$child_table,$child_id,array('ratio'=>0.3) );
					self::lay_object($child_table,$child_id,$this->tables->$src_table->obj_cursor[$obj]->x,$this->tables->$src_table->obj_cursor[$obj]->y);
				} else if($this->tables->$child_table->set_type_table=='Ports') {
				//	echo "\n //282 Trafilem na Ports";
				}
			}
			
		}
		
	}
	
	function get_ports_for_obj($src_table,$obj) {
		//do wykrycia portow dla tabeli typu 
			echo "\n // 293 get_ports_for_obj".$src_table."/".$obj;

		if(isset($this->tables->$src_table->childs[$obj]->tables)) {
				foreach($this->tables->$src_table->childs[$obj]->tables as $child_tbl=>$child_id_arr) {
		
					if(self::get_type_table($child_tbl)=='Ports') {
						foreach($child_id_arr as $child_obj) { 
							$ret[]=$child_tbl."_".$child_obj;
							$this->ports_obj[$child_tbl."_".$child_obj]=$src_table."_".$obj;
							$this->ports_tables[$child_tbl]=1;
							$this->ports_to_add_connect_link[]=array($child_tbl,$child_obj); //zapisanie do cache co ma sie wyzwolic potem
							//self::add_connect_links($child_tbl,$child_obj);
						}
							
					}
				}
		}
		if(!empty($ret)) {
			 $ret=",outPorts: ['".implode("','", $ret)."']";
			 return $ret;
		}
	}
	
	
	
	function add_connect_links($src_table,$obj) {
		echo "\n // 316 add_connect_links ".$src_table." / ".$obj;
			foreach($this->ports_tables as $port_table=>$val) {
				foreach($this->tables->$port_table->childs as $obj=>$val_) {
				foreach($this->tables->$port_table->childs[$obj]->tables as $child_table=>$child_table_arr) {
					foreach($child_table_arr as $child_obj) {
					//	echo " i znalzalem ".$child_table." ".$child_obj;
						if(self::get_type_table($child_table)=='Connect') {
		
								
								if(count($this->tables->$child_table->parents->$port_table->tables[$child_obj]==2)) {
		//						foreach($this->tables->$child_table->parents->$port_table->tables[$child_obj] as $link_id){
					//				echo " (324) dodatk.".$link_id." ".$port_table."_".$this->tables->$child_table->parents->$port_table->tables[$child_obj][0];
									//jakia jest nazwa obiektu 
									if(!empty($this->tables->$child_table->parents->$port_table->tables[$child_obj][1])) 
									echo "\n connect(".$this->ports_obj[$port_table."_".$this->tables->$child_table->parents->$port_table->tables[$child_obj][0]].",'".$port_table."_".$this->tables->$child_table->parents->$port_table->tables[$child_obj][0]."',".$this->ports_obj[$port_table."_".$this->tables->$child_table->parents->$port_table->tables[$child_obj][1]].",'".$port_table."_".$this->tables->$child_table->parents->$port_table->tables[$child_obj][1]."'); ";
								}
								  	
							} 
						}
					}
					}
					
				}
		
	}
	
	function joint_addCell_js() {
		$cells=implode(').addCell(', $this->cells);
		echo "\ngraph.addCell(".$cells.");\n";
	}
	
		
	
	
	
	function set_type_table($src_table,$type) {
		$this->tables->$src_table->set_type_table=$type;
	}
	
	function get_type_table($src_table) {
		return $this->tables->$src_table->set_type_table;
	}



	function DEBUG($src_table) {
		
		DEBUG_S(-3,$src_table,$this->tables->$src_table,__FILE__,__FUNCTION__,__LINE__);
		
	}
	
	function DEBUG_ports_obj() {
		
		DEBUG_S(-3,'DEBUG_ports_obj',$this->ports_obj,__FILE__,__FUNCTION__,__LINE__);
		DEBUG_S(-3,'DEBUG_ports_tables',$this->ports_tables,__FILE__,__FUNCTION__,__LINE__);
		DEBUG_S(-3,'ports_to_add_connect_link',$this->ports_to_add_connect_link,__FILE__,__FUNCTION__,__LINE__);

	}


	

	function to_html($src_table) {
	
	
	
//	DEBUG_S(-3,'Graph obj',$this->graph,__FILE__,__FUNCTION__,__LINE__);
//	DEBUG_S(-3,'tables',$this->tables,__FILE__,__FUNCTION__,__LINE__);


   ?>
<link rel="stylesheet" href="stuff/joint-js/joint.css" />

<div id="paper" class="paper"/></div>
<script src="stuff/joint-js/joint.js"></script>
<script src="stuff/joint-js/joint.shapes.devs.js"></script>
<script>var graph = new joint.dia.Graph;

var paper = new joint.dia.Paper({
    el: $('#paper'),
    width: 2800,
    height: 2600,
    gridSize: 1,
    model: graph
});

var connect = function(source, sourcePort, target, targetPort) {
    var link = new joint.shapes.devs.Link({
        source: { id: source.id, selector: source.getPortSelector(sourcePort) },
        target: { id: target.id, selector: target.getPortSelector(targetPort) }
    });
    graph.addCell(link);
};

<? 
	
	//funkcja do znalezienia glownych obiektow, ktore maja byc narysowane obok siebie
	// idziemy od elementu o najmniejszej liczbie powiazan w prawo,
	// trzeceie powiazanie idzie pietro nizej (todo kiedy?)
	//function arrange_objects_layer($src_table,$minwidth=50,$minheight=100) {
		//1. poszukaj elementu z najmniejsza iloscia powiazan
		//	$this->tables->$src_table->cursor->x=0;
		//	$this->tables->$src_table->cursor->y=0;
			self::set_layer_coords($src_table,0,0);
		//	self::get_next_obj($src_table,null,array('less_connections'));
		$situation=self::analyze_arrange_situation($src_table);
		
	//	print_r($situation);
		
		while($obj=self::get_next_obj($src_table,$situation)) {
	
			echo "\n//631:: wzialem ID ".$obj;
		//foreach($this->tables->$src_table->src_index as $obj) {
			
			//DEBUG_S(-3,"Rysuje obiekt ".$src_table." o id ".$obj,null,__FILE__,__FUNCTION__,__LINE__);
			//self::add_obj()	
			
			self::set_obj_coord_by_layer($src_table,$obj);
			self::calculate_obj_size_by_child_recurs($src_table,$obj);
//			self::set_obj_size_width($src_table,$obj,200);
			self::set_obj_size_width_by_min_y_size($src_table,$obj);
		//	self::offset_layer_coords($src_table,0,0);

			self::lay_object($src_table,$obj);
			echo "\n //606: robie offset o ".self::get_obj_size_width($src_table,$obj)." dla obj ".$obj." bo x:".$this->tables->$src_table->obj_cursor[$obj]->x ." a width:".self::get_obj_size_width($src_table,$obj);
			self::offset_layer_coords($src_table,self::get_obj_size_width($src_table,$obj)+100,0);
			$situation=self::analyze_arrange_situation($src_table);

		}
		
		//malujemy teraz powiazania pomiedzy portami - nie mozna wczesniej, gdyz wszystkie obiekty musza sie pierw namalowac
		//foreach($this->ports_to_add_connect_link as $port_to_add_connect_link) {
		//	self::add_connect_links($port_to_add_connect_link[0],$port_to_add_connect_link[1]);
		//}
	//}

	
?>



var a1 = new joint.shapes.devs.Atomic({
    position: { x: 360, y: 360 },
    inPorts: ['port XY'],
    outPorts: ['x','y']
});

var a2 = new joint.shapes.devs.Atomic({
    position: { x: 50, y: 260 },
    outPorts: ['out']
});

var a3 = new joint.shapes.devs.Atomic({
    position: { x: 650, y: 150 },
    size: { width: 100, height: 300 },
    inPorts: ['a','b']
});

<?
  self::joint_addCell_js();
  		self::add_connect_links();		

?>
// graph.addCell(opt_przelacznice_mufy_39).addCell(opt_przelacznice_mufy_38).addCell(opt_przelacznice_mufy_40);
// a2.embed(a1);


</script>

   <?   



	
		
	}
	
	
	
 
	
}


function GIS_OPT_STRUCTURES2() {


	global $db,$gdb;

	$mysql_zas_id=36;
	$gis_zas_id=13102;
	
	
	
	$db = DB::getDB();
	$gdb= DB::getDB($gis_zas_id);
	
	
	DEBUG_S(-3,'db',$db);
	DEBUG_S(-3,'gis',$gdb);
		
	 $cp=new GIS_OPT_STRUCTURES2;
	 $cp->init();
	 $cp->set_gis_zas_id($gis_zas_id);
	 
	 
	 //$wezly=$cp->get_opt_objects('opt_przelacznice_mufy','id_0','Nazwa','id_wezel','Rozdzielcza_wezly','id_opt_lokalizacja','opt_lokalizacje',"where \"Nazwa\" like '%test%'");
	 $wezly=$cp->get_opt_objects('opt_przelacznice_mufy','id_0','Nazwa','id_wezel','Rozdzielcza_wezly','id_opt_lokalizacja','opt_lokalizacje',"where \"id_0\" in(23,24,32,31,25,28,27) ");


	 $cp->set_type_table('opt_przelacznice_mufy','Coupled');
	 $tacki=$cp->get_opt_objects('opt_tacki','id_0',null,null,null,'id_przelac','opt_przelacznice_mufy');

	 $cp->set_type_table('opt_tacki','Atomic');
	 $spawy=$cp->get_opt_objects('opt_spawy','id_0',null,null,null,'id_tacka','opt_tacki');

	 $cp->set_type_table('opt_spawy','Ports');

	 $wlokna=$cp->get_opt_objects('opt_wlokna','id',null,null,null,'id_spaw_a','opt_spawy');
	 $cp->set_type_table('opt_wlokna','Connect');

	 $wlokna2=$cp->get_opt_objects('opt_wlokna','id',null,null,null,'id_spaw_b','opt_spawy');

	
	 $spawy_b=$cp->get_opt_objects_parent('opt_spawy','id_0',null,null,null,'id_tacka','opt_tacki','1=1','opt_wlokna','id_spaw_b','id'); //chce znalezc te sprawy ktore sa w wloknach jako B
	 $spawy_b2=$cp->get_opt_objects_parent('opt_spawy','id_0',null,null,null,'id_tacka','opt_tacki','1=1','opt_wlokna','id_spaw_a','id'); //chce znalezc te sprawy ktore sa w wloknach jako B

	 $tacki_b=$cp->get_opt_objects_parent('opt_tacki','id_0',null,null,null,'id_przelac','opt_przelacznice_mufy','1=1','opt_spawy','id_tacka','id_0'); //chce znalezc te sprawy ktore sa w wloknach jako B	
	 $wezly_b=$cp->get_opt_objects_parent('opt_przelacznice_mufy','id_0','Nazwa','id_wezel','Rozdzielcza_wezly','id_opt_lokalizacja','opt_lokalizacje','1=1','opt_tacki','id_przelac','id_0');	 

	// $cp->layer_arrange(1,array('opt_przelacznice_mufy'));
	
	
	/* 
	 $cp->add_array_to_graph($wezly,'PROCES_INIT');
	 $cp->add_array_to_graph($tacki,null);
	 $cp->add_array_to_graph($spawy,null);
	 $cp->add_array_to_graph($wlokna,null);
	 $cp->add_array_to_graph($wlokna2,null);

	 $cp->add_array_to_graph($spawy_b,null);
	 $cp->add_array_to_graph($spawy_b2,null);

	 $cp->add_array_to_graph($tacki_b,null);
	 $cp->add_array_to_graph($wezly_b,'PROCES_INIT');
	 */


	 	 $cp->find_connect_paths('opt_przelacznice_mufy');
		 $cp->invert_layer_deepth();

	 //	 $cp->arrange_objects_layer('opt_przelacznice_mufy');


	 DEBUG_S(-3,'Wezly do dodania',$wezly,__FILE__,__FUNCTION__,__LINE__);
	 DEBUG_S(-3,'tacki do dodania',$tacki,__FILE__,__FUNCTION__,__LINE__);

	 $cp->to_html('opt_przelacznice_mufy');
	 
	 
	 $cp->DEBUG('opt_przelacznice_mufy');	 $cp->DEBUG('opt_spawy');
	 $cp->DEBUG('opt_tacki');
	 $cp->DEBUG('opt_spawy');
	 $cp->DEBUG('opt_wlokna');
	 $cp->DEBUG_ports_obj();

//	 die();
	 
	 	//	  foreach($obj->INTERNAL_LINK as $link) graph__addNodeID($id, $graph,$tree->Viev_Zpc[$link]->DESC,null,$link,null);
		//	  foreach($obj->EXTERNAL_LINK as $link) graph__addNodeID($link, $graph,$tree->Viev_Zpc[$link]->DESC,null,$id,'PROCES_INIT');

	 
	 
	 
	}
	
	
	

	
	
	
	
	
	


?>

