Location: PHPKode > projects > OpenCabling > opencabling/search_path.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
<link rel="stylesheet" href="css/opencabling.css" type="text/css"></link>
<?php
require_once 'js/functions.js';
require_once 'phplayersmenu.inc.php';
$mid->printHeader();

?>

</head>
<body>
<?php

function upgradecounter() {
  global $seen,$totconn;
  
  $numseen=sizeof($seen);
  
  $progress=($numseen*100)/$totconn;
  
  echo "<script type=\"text/javascript\">";
  echo "setCount(".$progress.")";
  echo "</script>";
}

function fullcounter() {
  
  echo "<script type=\"text/javascript\">";
  echo "setCount(100)";
  echo "</script>";
  
}


$mid->printMenu('hormenu1');

include "opendb.php";
include "primitives/getobject.php";
include "primitives/getconnection.php";

$start_id=$_REQUEST['start_id'];
$end_id=$_REQUEST['end_id'];
$start_name=$_REQUEST['start_name'];
$end_name=$_REQUEST['end_name'];

$found=0;

if ($start_id!='' && $end_id!='') {

  echo "<span class=normaltext><b>Search path between objects</b>";
  echo "<p></p>";
  echo "<b>Warning: This function is experimental</b></span>";
  echo "<table border=0 class='normaltext'>";
  echo "<tr><td align=right class=normaltext><b>Start from object:</b></td><td>".$start_name."</td></tr>";
  echo "<tr><td align=right class=normaltext><b>End with object:</b></td><td>".$end_name."</td></tr>";
  echo "<tr><td align=right class=normaltext><b>Searching:</b></td><td>";
  echo "<script language=\"javascript\" src=\"js/percent_bar.js\">";
  echo "/*";
  echo "Event-based progress bar- By Brian Gosselin at http://scriptasylum.com/bgaudiodr";
  echo "Featured on DynamicDrive.com";
  echo "For full source, visit http://www.dynamicdrive.com";
  echo "*/";
  echo "</script>";
  echo "</td></tr>";
  echo "</table>";
   
  echo "<p></p>";

  $sql="select count(*) as totconn from connections";
  $query=mysql_query($sql,$mysql_conn);
  $countconn=mysql_fetch_assoc($query);

  $totconn=$countconn['totconn']*2;

  $stack=array();
  $seen=array();
  
  $element=array( 'id' => $start_id, 'seen' => 'no', 'conn' => '', 'pos' => '', 'port' => '');

  // id   - id of the object
  // seen - if already examinated or not
  // conn - id of the connection with the previous object
  // pos  - position in the previous object
  // port - port in the local object where the connection from the previous object
  //        is coming. 

  array_push($stack,$element);
  array_push($seen,array($element['id'],$element['conn'],$element['port']));

  // echo "<!-- STARTING -->";

  // echo "<!-- PUSHED ".$element['id']." seen ".$element['seen']." from pos ".$element['pos']." from conn ".$element['conn']." from port ".$element['port']."-->";

  //  echo "<!-- STACK ";
  //print_r($stack);
  //echo "-->";

  //echo "<!-- SEEN ";
  //print_r($seen);
  //echo "-->";

  $found=0;
  
  while ((($found==0) && ($element=array_pop($stack))!=NULL)) {

    upgradecounter();

    // echo "<!-- POPPED ".$element['id']." seen ".$element['seen']." from pos ".$element['pos']." from conn ".$element['conn']." from port ".$element['port']."-->";

    //echo "<!-- STACK ";
    //    echo "<pre>";
    //print_r($stack);
    //echo "-->";
    //echo "</pre>";
    
    //echo "<!-- SEEN ";
    //print_r($seen);
    //echo "-->";
  
    $elementid=$element['id'];
    $elementpos=$element['pos'];
    $elementport=$element['port'];
    $elementconn=$element['conn'];
    $elementseen=$element['seen'];

    // echo "<!-- TESTING IF ".$elementid." IS EQUAL ".$end_id." -->";
        
    if (($elementseen=='no') && ($elementid==$end_id)) {
      $element['seen']='yes';
      array_push($stack,$element);
      
      $found=1;
      
      fullcounter();
      
      // echo "<!-- ************************* FOUND ************************* -->";
      
    } elseif ($elementseen=='no') {
      
      $element['seen']='yes';
      
      array_push($stack,$element);

      // echo "<!-- PUSHED ".$element['id']." seen ".$element['seen']." from pos ".$element['pos']." from conn ".$element['conn']." from port ".$element['port']."-->";

      //echo "<!-- STACK ";
      //print_r($stack);
      //echo "-->";

      //echo "<!-- SEEN ";
      //print_r($seen);
      //echo "-->";
      
      //
      // Get full object info 
      //

      $object=getobject($elementid);

      //
      // Look if the parent object has type fullconnection, insert all elements
      //

      // echo "<!-- LOOK IF THE PARENT OBJECT HAS TYPE FULLCONNECTION -->";
      // echo "<!-- PARENT OBJ: ".$object['parent_obj']." -->";

      $parentobj=getobject($object['parent_obj']);
      
      if (stristr($parentobj['options'],"fullconnection")!==FALSE) {

	// echo "<!-- YES, PARENT HAS FULLCONNECTION -->";

	// The position of the children inside the parent is saved reverse signed
	$parentelement=array('id' => $parentobj['id_object'], 'seen' => 'no', 'conn' => '', 'pos' => -$object['position'], 'port' => '');
	if ((array_search(array($parentelement['id'],$parentelement['conn'],$parentelement['port']),$seen)===FALSE) && 
	    (array_search(array($parentelement['id'],'',''),$seen)===FALSE)) {

	  array_push($stack,$parentelement);
	  array_push($seen,array($parentelement['id'],$parentelement['conn'],$parentelement['port']));

	  // echo "<!-- PUSHED PARENT ".$parentelement['id']." seen ".$parentelement['seen']." from pos ".$parentelement['pos']." from conn ".$parentelement['conn']." from port ".$parentelement['port']."-->";
	  
	  //	  echo "<!-- STACK ";
	  //print_r($stack);
	  //echo "-->";
	  
	  //echo "<!-- SEEN ";
	  //print_r($seen);
	  //echo "-->";

	}
      }

      //
      // Look for type of object and options
      //

      // echo "<!-- LOOK IF OBJECT HAS TYPE FULLCONNECTION -->";

      if (stristr($object['options'],"fullconnection")!==FALSE) {

	// echo "<!-- IT HAS FULLCONNECTION -->";

	//
	// Fullconnection means all inside elements and all outputs are connected
	//

	//echo "<!-- PUSHING ALL INSIDE ELEMENTS -->";

	//
	// Push all inside elements
	//

	$sql="select * from objects where parent_obj='".$elementid."'";
      	$query=mysql_query($sql,$mysql_conn);
	while ($subobject=mysql_fetch_assoc($query)) {

	  upgradecounter();

	  $newelement=array('id' => $subobject['id_object'], 'seen' => 'no', 'conn' => '', 'pos' => $subobject['position'], 'port' => '');
	  if ((array_search(array($newelement['id'],$newelement['conn'],$newelement['port']),$seen)===FALSE) &&
	      (array_search(array($newelement['id'],'',''),$seen)===FALSE)) {
	    array_push($stack,$newelement);
	    array_push($seen,array($newelement['id'],$newelement['conn'],$newelement['port']));

	    // echo "<!-- PUSHED ".$newelement['id']." seen ".$newelement['seen']." from pos ".$newelement['pos']." from conn ".$newelement['conn']." from port ".$newelement['port']."-->";

	    //echo "<!-- STACK ";
	    //print_r($stack);
	    //echo "-->";

	    //echo "<!-- SEEN ";
	    //print_r($seen);
	    //echo "-->";


	  }
	}

	// echo "<!-- PUSH ALL CONNECTED ELEMENTS -->";

	//
	// Push all connected elements, note to exclude the connection you came from and test if you already
	// seen a connection from such element
	//

	unset($out_names);
	$out_names=array();
	$sql="select * from connections where start_obj='".$element['id']."' and id_connection<>'".$element['conn']."'";
        //echo "$sql";

	$query=mysql_query($sql,$mysql_conn);
        while ($connobject=mysql_fetch_assoc($query)) {
	  array_push($out_names,$connobject['start_item']);
	}

	$sql="select * from connections where end_obj='".$element['id']."' and id_connection<>'".$element['conn']."'";
        //echo "$sql";
        $query=mysql_query($sql,$mysql_conn);
        while ($connobject=mysql_fetch_assoc($query)) {
	  array_push($out_names,$connobject['end_item']);
	}

	//echo "Found the following endpoint:";
	//print_r($out_names);
	//echo "end Found";
	
	foreach ($out_names as $item) {

	  //echo "Inside foreach";

	  upgradecounter();
	  
	  //
	  // Avoid fake bis elements 
	  //

	  if ((stristr($item,'bis')===FALSE) and ($item<>'')) {
	    // echo "<!-- GETTING CONNECTION STARTING FROM OBJECT ".$object['id_object']." AND ITEM $item -->";
	    $connection=getconnection($object['id_object'],$item);
	    if ($connection!=FALSE) {
	      // echo "<!-- CONNECTION FOUND: ".$connection['id_connection']." -->";
	      if ($connection['id_connection']<>$elementconn) {
		$newelement=array('id' => $connection['endobject'], 'seen' => 'no', 'conn' => $connection['id_connection'], 'pos' => '', 'port' => $connection['enditem']);
		
		if ((array_search(array($newelement['id'],$newelement['conn'],$newelement['port']),$seen)===FALSE) &&
		    (array_search(array($newelement['id'],'',''),$seen)===FALSE)) {
		  array_push($stack,$newelement);
		  array_push($seen,array($newelement['id'],$newelement['conn'],$newelement['port']));
		  
		  // echo "<!-- PUSHED ".$newelement['id']." seen ".$newelement['seen']." from pos ".$newelement['pos']." from conn ".$newelement['conn']." from port ".$newelement['port']."-->";

		  //echo "<!-- STACK ";
		  //print_r($stack);
		  //echo "-->";

		  //echo "<!-- SEEN ";
		  //print_r($seen);
		  //echo "-->";

		  		  
		}
	      }
	    }
	  }
	}
      } elseif (stristr($object['options'],"frontbackconnection")!==FALSE) {

	// echo "<!-- IT HAS FRONTBACKCONNECTION --!>";

	// Determine the front/back port

	// echo "<!-- DETERMINING THE FRONTBACK PORT OF ".$elementport." -->";

	$elementbackport=$elementport."b";
	$out_names=explode(";",$object['out_names']);
	//	echo "<!-- OUTNAMES = ".$out_names."-->";
	if (array_search($elementbackport,$out_names)===FALSE) {
	  $frontbackport=substr($elementport,0,strlen($elementport)-1);
	} else {
	  $frontbackport=$elementbackport;
	}

	// echo "<!-- FRONTBACK PORT IS $frontbackport -->";
	
	// Search for objects connected to the frontback port starting with this object

	// echo "<!-- SEARCH FOR CONNECTION TO THE FRONTBACK PORT -->";

	$connection=getconnection($object['id_object'],$frontbackport);
	if ($connection) {
	  if ($connection['id_connection']<>$elementconn) {
	    $newelement=array('id' => $connection['endobject'], 'seen' => 'no', 'conn' => $connection['id_connection'], 'pos' => '', 'port' => $connection['enditem']);
	    
	    if ((array_search(array($newelement['id'],$newelement['conn'],$newelement['port']),$seen)===FALSE) &&
		(array_search(array($newelement['id'],'',''),$seen)===FALSE)) {
	      array_push($stack,$newelement);
	      array_push($seen,array($newelement['id'],$newelement['conn'],$newelement['port']));	      

	      //echo "<!-- PUSHED ".$newelement['id']." seen ".$newelement['seen']." from pos ".$newelement['pos']." from conn ".$newelement['conn']." from port ".$newelement['port']."-->";

	      //echo "<!-- STACK ";
	      //print_r($stack);
	      //echo "-->";

	      //echo "<!-- SEEN ";
	      //print_r($seen);
	      //echo "-->";

	    }
	  }
	}
      }
    }
  }  
  if ($found==1) {
    
    //
    // Element found
    //
    
    echo "<!-- Element Found -->";
    echo "<!-- STACK ";
    print_r($stack);
    echo "-->";
    
    //echo "<!-- SEEN ";
    //print_r($seen);
    //echo "-->";
    $i=0;
    unset($link,$deslink);
    foreach ($stack as $element) {
      
      //echo "<!-- Popped element to draw -->";
      //echo "<!--";
      //print_r($element);
      //echo "-->";
      
      if ($element['seen']=='yes') {
	$object=getobject($element['id']);
	$obj[$i]=$object['id_object'];
	$small[$i]=str_replace("@@HEIGHT@@"," height=50 ",$object['html_small']);
	$name[$i]=$object['name'];
	$description[$i]=$object['brand']." - ".$object['type'];
	if ($element['pos']>0) {
	  $link[$i]="<td align='center' valign='middle'><img src='images/lesser.png' title='".$element['pos']."'></img></td>";
	  $deslink[$i]="<td></td>";
	} elseif ($element['pos']<0) {
	  $link[$i]="<td align='center' valign='middle'><img src='images/greater.png' title='".(-$element['pos'])."'></img></td>";
	  $deslink[$i]="<td></td>";
	} elseif ($element['conn']>0) {
	  
	  echo "<!-- Getting connection from element ".$element['id']." and item ".$element['port']." -->";
	  
	  $connection=getconnection($element['id'],$element['port']);
	  
	  if (($connection['endspan']>1) && ($connection['endfromitemspan']."bis"<>$connection['endtoitemspan'])) {
	    $link[$i]="<td width=15 align=center><p class='smalltext'>".$connection['endfromitemspan']." - ".$connection['endtoitemspan']."</p></td>";
	  } else {
	    $link[$i]="<td width=15 align=center><p class='smalltext'>".$connection['enditem']."</p></td>";
	  }
	  $deslink[$i]=$deslink[$i]."<td></td>";
	  
	  $link[$i]=$link[$i]."<td title='".$connection['conndescription']."'>".$connection['startconnectorhtml']."</td><td><a href=\"edit_field_connection.php?id_conn=".$connection['id_connection']."\" target='right'>".$connection['cablehtml']."</td><td>".$connection['endconnectorhtml']."</a></td>";
	  $deslink[$i]=$deslink[$i]."<td></td><td align=center><p class='smalltext'>".$connection['name']."</p></td><td></td>";
	  if (($connection['startspan']>1) && ($connection['startfromitemspan']."bis"<>$connection['starttoitemspan'])) {
	    $link[$i]=$link[$i]."<td width=15 align=center><p class='smalltext'>".$connection['startfromitemspan']." - ".$connection['starttoitemspan']."</p></td>";
	  } else {
	    $link[$i]=$link[$i]."<td width=15 align=center><p class='smalltext'>".$connection['startitem']."</p></td>";
	  }  
	  $deslink[$i]=$deslink[$i]."<td></td>";
	  
	}
	$i++;
      }
    }
    $depth=$i;
    echo "<table border=0 cellspacing=0 cellpadding=0><tr>";
    for ($i=0;$i<$depth;$i++) {
      print $link[$i];
      print "<td align='center' valign='middle' title='".$description[$i]."'><a href='javascript:select_obj(".$obj[$i].")'>".$small[$i]."</a></td>";
    }
    echo "</tr><tr>";
    for ($i=0;$i<$depth;$i++) {
      print $deslink[$i];
      print "<td align='center' valign='top'><p class='smalltext'>".$name[$i]."</p></td>";
    }
    echo "</tr></table>";
  } else {
    echo "<span class=normatext>Connection not found</span>";
  }
} else {
  print "<span class=normaltext><B>Search path between objects</B></span>";
  
  print "<form name='choose' id='choose' action='search_path.php'>";
  print "<br>";
  print "<table border=0>";
  print "<tr><td align=right><span class=normaltext>From object:</span></td>";
  print "<td><input type=text size=5 name=start_id id='start_id' value='".$start_object."' onchange='ShowObjectName(\"start_id\",\"start_name\")'></input>";
  print "<input type=text readonly name=start_name id='start_name'></input></td>";
  print "<td><button onClick='ChooseObject(\"start_id\",\"start_name\")' type='button'>Browse</button></td>";
  print "</tr>";
  print "<tr><td align=right><span class=normaltext>To object:</span></td>";
  print "<td><input type=text size=5 name=end_id id='end_id' onchange='ShowObjectName(\"end_id\",\"end_name\")'></input>";
  print "<input type=text readonly name=end_name id='end_name'></input></td>";
  print "<td><button onClick='ChooseObject(\"end_id\",\"end_name\")' type='button'>Browse</button></td>";
  print "</tr>";
  print "<tr><td></td><td><input type=submit value='Search'></input></td></tr>";
  print "</table>";

}
  mysql_close($mysql_conn);

?>
    
<?php
$mid->printFooter();
?>
     
</body>
</html>
Return current item: OpenCabling