<?
// display, analyze, and visualize social network when selected from socialnetworks.php

include_once("config.php");
include_once(
"includes/baseclass.php");
include_once(
"includes/db_networks.php");
include_once(
"includes/db_people.php");
include_once(
"includes/functions.php");
include_once(
"includes/functions_sna.php");

// if user is not logged in, redirect to homepage
if(!isset($_SESSION['uID'])){header("Location: index.php");exit;}

$db_new=new DB($mysql_hostname,$mysql_database,$mysql_user,$mysql_password);
$db_lookup_networks=new db_lookup_networks($db_new);

include_once(
"includes/header.php");
include_once(
"includes/javascript.php");

// process split data if requested
if(isset($_REQUEST["network_split"])){
    include_once(
"includes/split_update.php");
}

include_once(
"includes/options.php");

// display/analyze/visualize/all for single networkif requested
if(isset($_REQUEST['action']) && isset($_REQUEST['network']) &&
    (
$_REQUEST['action']=="all" || $_REQUEST['action']=="display" ||
    
$_REQUEST['action']=="analyze" || $_REQUEST['action']=="visualize")
){
    
$networks_db_array=$db_lookup_networks->findBy("network",$_REQUEST['network'],"uID",$_SESSION['uID']);
    
$network_fields_array=$networks_db_array[0];
    
$single_network=true;

    if(
count($networks_db_array) !=|| $network_fields_array->Get("uID") !=$_SESSION['uID']){
        echo 
"<center><font class='Red'>Sorry, cannot locate network</font>";
    }else{
        if(isset(
$_REQUEST["egoperson"])){
            
$network_fields_ego_array=$network_fields_array->computeEgo($_REQUEST['egoperson']);
            
$ego_network=true;
        }
        if(
$_REQUEST['action']=="all" || $_REQUEST['action']=="display"){
            include_once(
"includes/display.php");
        }
        if(
$_REQUEST['action']=="all" || $_REQUEST['action']=="analyze"){

            
// create all possible split value networks

            
$network_min=min(preg_split("/,/",$network_fields_array->Get("rawmatrix")));
            
$network_max=max(preg_split("/,/",$network_fields_array->Get("rawmatrix")));

            
$networks_array=array();
            if(
$network_min==0){
                
$i_start=1;
            }else{
                
$i_start=(int)$network_min;
            }
            for(
$i=$i_start ;$i <=$network_max ;$i++){
                
$networks_array[]=$network_fields_array->getSplitNetwork($i);
            }
            
$multiple_split=true;
            include_once(
"includes/analyze.php");
        }
        if(
$_REQUEST['action']=="all" || $_REQUEST['action']=="visualize"){

            
// detect Java version (to determine if Swing exists)
            
if(!isset($_SESSION['applet']) || $_SESSION['applet']=="nv2d1" || (isset($_REQUEST['applet']) && $_REQUEST['applet']=="nv2d1")){
                
$_SESSION['applet']="nv2d1";

                
$_SESSION['request']="";
                while (list(
$key$val)=each($_REQUEST)) {
                    if(
$key && $val && $key!="applet"){
                        
$_SESSION['request'].="{$key}={$val}&";
                    }
                }
                
$_SESSION['request']=rtrim($_SESSION['request'],"&");

                if(
$_SESSION['serverName']=="127.0.0.1"){
                    echo 
"<applet CODE='swingDetectLocalHost.class' WIDTH='-1' HEIGHT='-1' ALT=\"To best visualize a network, please download the (free) version of Java 2 at http://www.java.com/en/download/\">";
                }elseif(
$_SESSION['serverName']=="netvis.mit.edu"){
                    echo 
"<applet CODE='swingDetectNetVis.class' WIDTH='-1' HEIGHT='-1' ALT=\"To best visualize a network, please download the (free) version of Java 2 at http://www.java.com/en/download/\">";
                }else{
                    echo 
"<applet CODE='swingDetect.class' WIDTH='-1' HEIGHT='-1' ALT=\"To best visualize a network, please download the (free) version of Java 2 at http://www.java.com/en/download/\">";
                }
                echo 
"<center><font class=Red>To best visualize a network, please download the (free) version of <a class=Blue href='http://www.java.com/en/download/' target=blank>Java 2</a></font></center><br>";
                echo 
"</applet>";
                exit;
            }

            include_once(
"includes/visualize.php");
        }
    }
// display/analyze/visualize/all for multiple networks if requested
}elseif(isset($_REQUEST['action']) && ($_REQUEST['action']=="display_all" || $_REQUEST['action']=="analyze_all" || $_REQUEST['action']=="visualize_all")){

    
// load all the ego networks from this network and convert all
    
if(isset($_REQUEST['network'])) {
        
$ego_network=true;
        
$networks_db_array=$db_lookup_networks->findBy("uID",$_SESSION['uID'],"network",$_REQUEST['network']);
        
$network_fields_array=$networks_db_array[0];
        if(!
$network_fields_array || $network_fields_array->Get("uID") !=$_SESSION['uID']){
            die(
"sorry, cannot access network");
        }
        
$nodes_array=preg_split("/,/",$network_fields_array->Get("nodes"));
        
$networks_array=array();
        foreach(
$nodes_array as $node){
            
$networks_array[]=$network_fields_array->computeEgo($node);
        }
    }else{
        
$networks_db_array=$db_lookup_networks->findBy("uID",$_SESSION['uID']);
    }

    if(
$_REQUEST['action']=="display_all"){
        include_once(
"includes/display.php");
    }
    if(
$_REQUEST['action']=="analyze_all"){
        include_once(
"includes/analyze.php");
    }
    if(
$_REQUEST['action']=="visualize_all"){

        if(isset(
$ego_network)){
            
$networks_db_array=$db_lookup_networks->findBy("network",$_REQUEST['network'],"uID",$_SESSION['uID']);
        }
        
$visualize_all=true;
        include_once(
"includes/visualize.php");
    }
}

include_once(
"includes/permission.php");
include_once(
"includes/shortcuts.php");
include_once(
"includes/disclaimer.php");
include_once(
"includes/footer.php");

?>