<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Trixbox Graph</title>
<meta name="generator" content="Bluefish 1.0.7">
<meta name="author" content="Unknown">
<meta name="date" content="2007-01-09T22:13:32+0100">
<meta name="copyright" content="GPL">
<meta name="keywords" content="">
<meta name="description" content="">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="expires" content="0">
</head>
<body>
<form action="index.php" method="post">
<?php
include ("functions.php");
include (
"settings.php");

function 
getTarget ($dest) {
    
$targets explode (","$dest);
    if(
substr($targets[0],0,-2) == "app-announcement"){
        
$temp explode ("-"$targets[0]);
        
$targets[0] = $temp[1] . "," $temp[2];
    }
    switch(
$targets[0]){
        case 
"ext-local":
        case 
"ext-group":
        case 
"from-did-direct":
        case 
"ext-queues":
            
$tar $targets[1];
            break;
        case 
"timeconditions":
        case 
"app-daynight":
            
$tar $targets[0].",".$targets[1];
            break;
        default:
            
$tar $targets[0];
            break;
    }
    return 
$tar;
}

function 
getQueueTarget ($dest) {
    
$targets split ('[\\/\\@]'$dest);
    if (!
strcmp($targets[0],"Local")) {
        return 
$targets[1];
    }
    return 
$dest." exploded to ".$target[0]."|".$target[1];
}

$tab = new Table ();
$tab->open (1);
$tab->hcell ("Please select the type of graph to draw");
$tab->colSpan 3$tab->cell (""); $tab->colSpan 1;
$ctlGraph = new Ctrl ("graph");
$graph $ctlGraph->readFld ();
if (!
$graph$graph "digraph,TB";
$graphRankDir explode(","$graph);
$lbsGraph = new ListBoxSingle ("graph","1");
$lbsGraph->optionSelected (!strcmp ($graph"digraph,TB"), "digraph,TB""Directed graph, top-to-bottom");
$lbsGraph->optionSelected (!strcmp ($graph"digraph,LR"), "digraph,LR""Directed graph, left-to-right");
$lbsGraph->optionSelected (!strcmp ($graph"graph,TB"), "graph,TB""Undirected graph, top-to-bottom");
$lbsGraph->optionSelected (!strcmp ($graph"graph,LR"), "graph,LR""Undirected graph, left-to-right");
$lbsGraph->close ();
$tab->closeRow ();

$tab->hcell ("Additional graph options (see man dot)");
$tab->colSpan 3$tab->cell (""); $tab->colSpan 1;
$ctlAddOptions = new EditC ("addOptions"); $ctlAddOptions->m_cols 60;
$addOptions $ctlAddOptions->readFld ();
$ctlAddOptions->disp ();
$tab->closeRow ();

$tab->hcell ("Please select the image settings");
$tab->colSpan 3$tab->cell (""); $tab->colSpan 1;
$ctlImage = new Ctrl ("image");
$image $ctlImage->readFld ();
if (!
$image$image "image";
$lbsImage = new ListBoxSingle ("image","1");
$lbsImage->optionSelected (!strcmp ($image"gif"), "gif""GIF");
$lbsImage->optionSelected (!strcmp ($image"png"), "png""PNG");
$lbsImage->close ();
$tab->closeRow ();

$tab->hcell ("Place 'incoming' nodes at the top (or left)");
$ctlIncomeMin = new Ctrl ("incomeMin");
$ctlIncomeRoot = new Ctrl ("incomeRoot");
$tab->cell ("<input name=\"incomeMin\" type=\"checkbox\"".($ctlIncomeMin->readFld () ? " checked>" ">"));
$tab->hcell ("Link all 'incoming' to one node"); 
$tab->cell ("<input name=\"incomeRoot\" type=\"checkbox\"".($ctlIncomeRoot->readFld () ? " checked>" ">"));
$tab->closeRow ();

$tab->hcell ("Place 'device' nodes at the bottom (or right)");
$ctlDeviceMax = new Ctrl ("deviceMax");
$ctlDeviceRoot = new Ctrl ("deviceRoot");
$tab->cell ("<input name=\"deviceMax\" type=\"checkbox\"".($ctlDeviceMax->readFld () ? " checked>" ">"));
$tab->hcell ("Link all 'devices' to one node");
$tab->cell ("<input name=\"deviceRoot\" type=\"checkbox\"".($ctlDeviceRoot->readFld () ? " checked>" ">"));
$tab->closeRow ();

$tab->hcell ("Please select the items to render");
$tab->cell ("");
$ctlItems = new Ctrl ("items");
$items $ctlItems->readFld ();
if (!
$items$items = array("ringgroups");
$showIncoming false;
$showDevices false;
$showIVRs false;
$showAnnouncements false;
$showDaymode false;
$showRingGroups false;
$showTimeConditions false;
$showQueues false;

//Only show items if the tables exist
foreach ($items as $item) {
    if (!
strcmp ($item"ringgroups") && checkTable('ringgroups')) $showRingGroups true;
    if (!
strcmp ($item"incoming") && checktable('incoming')) $showIncoming true;
    if (!
strcmp ($item"devices") && checkTable('devices')) $showDevices true;
    if (!
strcmp ($item"ivrs") && checkTable('ivr')) $showIVRs true;
    if (!
strcmp ($item"announcements") && checkTable('announcement')) $showAnnouncements true;
    if (!
strcmp ($item"daymodes") && checkTable('daynight')) $showDaymode true;
    if (!
strcmp ($item"timecond") && checkTable('timeconditions')) $showTimeConditions true;
    if (!
strcmp ($item"queues") && checkTable('queues_details')) $showQueues true;
}
$lbmItems = new ListBoxMultiple ("items","7");
$lbmItems->optionSelected ($showIncoming,"incoming","Enumerate all incoming");
$lbmItems->optionSelected ($showDevices,"devices","Enumerate all devices (phones)");
$lbmItems->optionSelected ($showRingGroups,"ringgroups","Show all ringgroups");
$lbmItems->optionSelected ($showIVRs,"ivrs","Show all 'Digital receptionists'");
$lbmItems->optionSelected ($showAnnouncements,"announcements","Show all announcements");
$lbmItems->optionSelected ($showDaymode,"daymodes","Show all 'Day/Night Modes'");
$lbmItems->optionSelected ($showTimeConditions,"timecond","Show all time conditions");
$lbmItems->optionSelected ($showQueues,"queues","Show all Queues");
$lbmItems->close ();

$ctlOmits = new Ctrl ("omits");
$omits $ctlOmits->readFld ();
if (!
$omits$omits = array ("");

$eo = !strcmp($graphRankDir[0], "graph") ? " -- " " -> ";
$f fopen ("/tmp/trixboxGraph.dot""w");
fwrite ($f"$graphRankDir[0] {\nrankdir=\"$graphRankDir[1]\"\n");
if (
$addOptionsfwrite ($f$addOptions "\n");
fwrite ($f"node [ style=".$default["style"].", fillcolor=".$default["fillcolor"].", shape=".$default["shape"].", fontname=".$default["fontname"]." ];\n");

if (
$showQueues) {
    
$sql "select distinct id from queues_details";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        
$allItems[$obj->id] = "Queue ".$obj->id;
        if (
array_search ($obj->id$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" [ label=\"%s\", shape=".$queues["shape"].", fillcolor=".$queues["fillcolor"].", style=".$queues["style"].", fontname=".$queues["fontname"]." ];\n"$obj->id$allItems[$obj->id]));
        }
    }
    
mysql_free_result ($res);
    
$sql "select id,data from queues_details where keyword='member'";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        if (
array_search ($obj->id$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"member\", style=".$queuesMemberLine["style"].", fontname=".$queuesMemberLine["fontname"]." ];\n"$obj->idgetQueueTarget ($obj->data)));
        }
    }
    
mysql_free_result ($res);
    
$sql "select distinct extension,dest from queues_config";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        if (
array_search ($obj->extension$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"Busy\", style=".$queuesBusyLine["style"].", fontname=".$queuesBusyLine["fontname"]." ];\n"$obj->extensiongetTarget ($obj->dest)));
        }
    }
    
mysql_free_result ($res);    
}

if (
$showDaymode) {
    
$sql "select distinct ext from daynight";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        
$allItems["ext"] = $obj->ext;
        
$itm "app-daynight,$obj->ext";
        
$sql2 "select dmode,dest from daynight where ext='$obj->ext' order by dmode asc";
        if (!
$res2 mysql_query ($sql2)) {
            die (
$sql2 " => " mysql_error ());
        }
        
$allItems["day"] = mysql_result($res2,0,'dest');
        
$allItems["name"] = mysql_result($res2,1,'dest');
        
$allItems["night"] = mysql_result($res2,2,'dest');
        
        
mysql_free_result ($res2);
        if (
array_search ($obj->ext$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" [ label=\"%s\", shape=".$daymode["shape"].", fillcolor=".$daymode["fillcolor"].", style=".$daymode["style"].", fontname=".$daymode["fontname"]." ];\n"$itm$allItems["name"]));
            
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"day\", style=".$daymodeLineDay["style"].", fontname=".$daymodeLineDay["fontname"]." ];\n"$itmgetTarget ($allItems["day"])));
            
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"night\", color=".$daymodeLineNight["color"].", fontname=".$daymodeLineNight["fontname"]." ];\n"$itmgetTarget ($allItems["night"])));
        }
    }
    
mysql_free_result ($res);
}

if (
$showAnnouncements) {
    
$sql "select * from announcement";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        if (
strcmp(substr($obj->description,0,2),"__")) {
            
$itm "announcement,$obj->announcement_id";
            
$allItems[$itm] = $obj->description;
            if (
array_search ($itm$omits)===false) {
                
fwrite ($fsprintf ("\"%s\" [ label=\"Announce\\n%s\\n(%s)\", shape=".$announce["shape"].", fillcolor=".$announce["fillcolor"].", style=".$announce["style"].", fontname=".$announce["fontname"]." ];\n"$itm$obj->description$obj->recording));
                
fwrite ($fsprintf ("\"%s\" $eo \"%s\";\n"$itmgetTarget ($obj->post_dest)));
            }
        }
    }
    
mysql_free_result ($res);
}

if (
$showTimeConditions) {
    
$sql "select * from timeconditions";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        if (
strcmp(substr($obj->displayname,0,2),"__")) {
            
$itm "timeconditions,$obj->timeconditions_id";
            
$allItems[$itm] = $obj->displayname;
            if (
array_search ($itm$omits)===false) {
                
fwrite ($fsprintf ("\"%s\" [ label=\"%s\", shape=".$times["shape"].", fillcolor=".$times["fillcolor"].", fontname=".$times["fontname"]." ];\n"$itm$obj->displayname));
                
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"Open\", color=".$timesLineOpen["color"].", style=".$timesLineOpen["style"].", fontname=".$timesLineOpen["fontname"]." ];\n"$itmgetTarget ($obj->truegoto)));
                
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"Closed\", color=".$timesLineClosed["color"].", fontname=".$timesLineClosed["fontname"]." ];\n"$itmgetTarget ($obj->falsegoto)));
            }
        }
    }
    
mysql_free_result ($res);
}

if (
$showIVRs) {
    
$sql "select ivr_id,displayname from ivr";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        if (
strcmp(substr($obj->displayname,0,2),"__")) {
            
$itm "ivr-$obj->ivr_id";
            
$allItems[$itm] = $itm.":".$obj->displayname;
            if (
array_search ($itm$omits)===false) {
                
fwrite ($fsprintf ("\"%s\" [ label=\"%s\", shape=".$ivr["shape"].", fillcolor=".$ivr["fillcolor"]." fontname=".$ivr["fontname"]." ];\n"
                    
$itm$obj->displayname));
            }
        }
    }
    
mysql_free_result ($res);
    
$sql "select * from ivr_dests";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        
$itm "ivr-$obj->ivr_id";
        if (
array_search ($itm$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"%s\", fontname=".$ivrDestLine["fontname"]." ];\n"$itmgetTarget ($obj->dest), $obj->selection));
        }
    }
    
mysql_free_result ($res);
}

if (
$showDevices) {
    
$sql "select * from devices";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    if (
$ctlDeviceMax->readFld () && !$ctlDeviceRoot->readFld ()) fwrite ($f"{\nrank=\"max\"\n");
    if (
$ctlDeviceMax->readFld () && $ctlDeviceRoot->readFld ()) fwrite ($f"{\nrank=\"max\"\nDevices;\n}\n");
    while (
$obj mysql_fetch_object ($res)) {
        
$allItems[$obj->id] = $obj->id.":".$obj->description;
        if (
array_search ($obj->id$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" [ label=\"%s\\n%s\", shape=".$device["shape"]." , fillcolor=".$device["fillcolor"].", fontname=".$device["fontname"]." ];\n",    $obj->id$obj->id$obj->description));
            if (
$ctlDeviceRoot->readFld ()) {
                
fwrite ($f"\"$obj->id\" $eo Devices [ weight=2.0 ];\n");
            }
        }
    }
    if (
$ctlDeviceMax->readFld () && !$ctlDeviceRoot->readFld ()) fwrite ($f"}\n");
    
mysql_free_result ($res);
}

if (
$showRingGroups) {
    
$sql "select * from ringgroups";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        
$allItems[$obj->grpnum] = $obj->grpnum.":".$obj->description;
        if (
array_search ($obj->grpnum$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" [ label=\"Ringgroup\\n%s (%s)\", shape=".$ringgroup["shape"].", fillcolor=".$ringgroup["fillcolor"].", style=".$ringgroup["style"].", fontname=".$ringgroup["fontname"]." ];\n"
                
$obj->grpnum$obj->grpnum$obj->description)); 
            
$targets explode("-"$obj->grplist);
            
            foreach (
$targets as $tok) {
                
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"%s(%s)\", fontname=".$ringgroupLine["fontname"]." ];\n"$obj->grpnum$tok$obj->strategy$obj->grptime));
            }
            
$tok2 strtok($obj->postdest',${}');
            
            if (
$tok2 !== false) {
                if (!
strcmp ($tok2"ext-local")) {
                       
$tok2 strtok(',${}');
                       
$tok2 strtok(',${}');
                    
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"postdest\", fontname=".$ringgroupLine["fontname"]." ];\n"$obj->grpnum$tok2));
                } else if (!
strcmp ($tok2"ext-group")) {
                       
$tok2 strtok(',${}');
                    
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"postdest\", fontname=".$ringgroupLine["fontname"]." ];\n"$obj->grpnum$tok2));
                } else {
                    
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"postdest\", fontname=".$ringgroupLine["fontname"]." ];\n"$obj->grpnum$tok2));
                }
            }
        }
    }
    
mysql_free_result ($res);
}

if (
$showIncoming) {
    
$sql "select * from incoming";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    if (
$ctlIncomeMin->readFld () && !$ctlIncomeRoot->readFld ()) fwrite ($f"{\nrank=\"min\"\n");
    if (
$ctlIncomeMin->readFld () && $ctlIncomeRoot->readFld ()) fwrite ($f"{\nrank=\"min\"\nIncoming;\n}\n");
    while (
$obj mysql_fetch_object ($res)) {
        
$allItems[$obj->extension] = $obj->extension;
        if (
array_search ($obj->extension$omits)===false) {
            
fwrite ($fsprintf ("\"%s\" [ label=\"incoming\\n%s\", shape=".$incoming["shape"].", fillcolor=".$incoming["fillcolor"].", style=".$incoming["style"].", fontname=".$incoming["fontname"]." ];\n"$obj->extension$obj->extension));
        }
    }
    if (
$ctlIncomeMin->readFld () && !$ctlIncomeRoot->readFld ()) fwrite ($f"}\n");
    
mysql_free_result ($res);
    
    
$sql "select * from incoming";
    if (!
$res mysql_query ($sql)) {
        die (
$sql " => " mysql_error ());
    }
    while (
$obj mysql_fetch_object ($res)) {
        if (
array_search ($obj->extension$omits)===false) {
            if (
$ctlIncomeRoot->readFld ()) fwrite ($f"Incoming $eo \"$obj->extension\" [ weight=2.0 ];\n"); 
            
fwrite ($fsprintf ("\"%s\" $eo \"%s\" [ label=\"incoming\", style=".$incomingLine["style"].", fontname=".$incomingLine["fontname"]." ];\n"$obj->extensiongetTarget ($obj->destination)));
        }
    }
    
mysql_free_result ($res);
}

fwrite ($f"}\n");
fclose ($f);
system ("dot -T$image -o/var/www/html/trixboxGraph/trixboxGraph.$image /tmp/trixboxGraph.dot");
system ("dot -Tps2 -o/var/www/html/trixboxGraph/trixboxGraph.eps /tmp/trixboxGraph.dot");
system ("ps2pdf /var/www/html/trixboxGraph/trixboxGraph.eps");
$tab->hcell ("Exclude nodes (use Ctrl to select/deselect)");
$tab->cell ("");
$lbmOmits = new ListBoxMultiple ("omits","10");
foreach (
$allItems as $key => $value) {
    
$lbmOmits->optionSelected (array_search ($key$omits)!==false,$key,$value);
}
$lbmItems->close ();

$tab->closeRow ();

$tab->close ();
?>
<input type="submit">
</form>
<p>
<hr>
<a href="trixboxGraph.pdf">Download the last rendered graph as PDF.</a>
<?php
echo "<img src=\"trixboxGraph.$image\" alt=\"Image\" />\n";
?>
</p>
</body>
</html>