<?php
# Return an array with the following information
# about a connection starting or ending at object "id" and item "item"
#
# array (
# [id_connection] => id of the connection
# [name] => name of the connection
# [startobject] => id of the object, same as "id"
# [startitem] => item of the object, same as "item"
# [endobject] => id of the ending object
# [enditem] => item of the ending object
# [startfromitemspan] => start item of the span the connection is inside, start object side
# [starttoitemspan] => end item of the span the connection is inside, start object side
# [endfromitemspan] => start item of the span the connection is inside, end object side
# [endtoitemspan] => end item of the sapn the connection is inside, end object side
# [startspan] => the span of the start connection
# [endspan] => the span of the end connection
# [startconnectorhtml]
# [endconnectorhtml]
# [cablehtml]
# [conndescription] => connection description
# )
require_once "getobject.php";
function getconnection($id,$item) {
global $mysql_conn, $DEBUG;
if (is_numeric($id)) {
$startobject=getobject($id);
$outname=explode(";",$startobject['out_names']);
$itemposition=array_search($item,$outname);
$itemstartposition=$itemposition;
do {
$sql="select * from connections,conntypes where connections.connection_type=conntypes.id_conn_type and ( ( connections.start_obj='$id' and connections.start_item='".$outname[$itemposition]."' ) or ( connections.end_obj='$id' and connections.end_item='".$outname[$itemposition]."' ) )";
if ($DEBUG==1) print "<!-- locateconnection ($itemposition) SQL: $sql -->";
$query=mysql_query($sql,$mysql_conn);
if (mysql_num_rows($query)==0) {
$itemposition--;
}
} while ( (mysql_num_rows($query)==0) and ($itemposition>=0) );
if (mysql_num_rows($query)>0) {
$preresult=mysql_fetch_assoc($query);
if ($DEBUG==1) {
print "<!-- locateconnection ($itemposition) Result:";
print_r($preresult);
print "-->";
}
// Test if we are inside the connection
if ( ($preresult['start_obj']==$id) and ($preresult['start_item']==$outname[$itemposition]) ) {
if (($itemstartposition - $itemposition)>=$preresult['start_outspan']) {
return(FALSE);
}
} else {
if (($itemstartposition - $itemposition)>=$preresult['end_outspan']) {
return(FALSE);
}
}
$sql="select * from connectors where id_connector_type='".$preresult['start_connector_type']."'";
if ($DEBUG==1) print "<!-- locateconnection (left connector) SQL: $sql -->";
$query=mysql_query($sql,$mysql_conn);
$subleftresult=mysql_fetch_assoc($query);
if ($DEBUG==1) {
print "<!-- locateconnection (left connector) Result:";
print_r($subleftresult);
print "-->";
}
$sql="select * from connectors where id_connector_type='".$preresult['end_connector_type']."'";
if ($DEBUG==1) print "<!-- locateconnection (right connector) SQL: $sql -->";
$query=mysql_query($sql,$mysql_conn);
$subrightresult=mysql_fetch_assoc($query);
if ($DEBUG==1) {
print "<!-- locateconnection (right connector) Result:";
print_r($subrightresult);
print "-->";
}
$sql="select * from cabletypes where id_cable_type='".$preresult['cable_type']."'";
if ($DEBUG==1) print "<!-- locateconnection (cable type) SQL: $sql -->";
$query=mysql_query($sql,$mysql_conn);
$subcableresult=mysql_fetch_assoc($query);
if ($DEBUG==1) {
print "<!-- locateconnection (cable type) Result:";
print_r($subcableresult);
print "-->";
}
$result['cablehtml']=$subcableresult['html'];
$startspread=array_search($item,$outname)-$itemposition;
$result['id_connection']=$preresult['id_connection'];
$result['name']=$preresult['name'];
$result['startobject']=$id;
$result['startitem']=$item;
$result['conndescription']=$preresult['description'];
if ( ($preresult['start_obj']==$id) and ($preresult['start_item']==$outname[$itemposition]) ) {
$result['endobject']=$preresult['end_obj'];
$result['startfromitemspan']=$preresult['start_item'];
$result['endfromitemspan']=$preresult['end_item'];
$result['startspan']=$preresult['start_outspan'];
$result['endspan']=$preresult['end_outspan'];
$result['startconnectorhtml']=$subleftresult['connleft'];
$result['endconnectorhtml']=$subrightresult['connright'];
} else {
$result['endobject']=$preresult['start_obj'];
$result['startfromitemspan']=$preresult['end_item'];
$result['endfromitemspan']=$preresult['start_item'];
$result['startspan']= $preresult['end_outspan'];
$result['endspan']= $preresult['start_outspan'];
$result['startconnectorhtml']=$subrightresult['connleft'];
$result['endconnectorhtml']=$subleftresult['connright'];
}
$result['starttoitemspan']=$outname[$itemposition + $result['startspan'] - 1 ];
$objendresult=getobject($result['endobject']);
$objendoutname=explode(";",$objendresult['out_names']);
$objendposition=array_search($result['endfromitemspan'],$objendoutname);
$result['endtoitemspan']=$objendoutname[$objendposition + $result['endspan'] -1 ];
$result['enditem']=$objendoutname[$objendposition+$startspread];
} else {
return(FALSE);
}
}
return($result);
}
?>