Example #1
0
/**
 * Get the connections for the given network search parameters from the given node.
 *
 * @param string $nodeid the id of the node to search outward from.
 * @param string $linklabels the string of link types.
 * @param string $userid optional for searching only a specified user's data. (only used if scope is 'all') - NOT USED AT PRESENT
 * @param string $scope (either 'all' or 'my', default 'all')
 * @param string $linkgroup (optional, either Positive, Negative, or Neutral - default: empty string);
 * @param integer $depth (optional, 1-7, or 7 for full depth;
 * @param string $direction (optional, 'outgoing', 'incoming', or 'both - default: 'both',
 * @param string $labelmatch (optional, 'true', 'false' - default: false;
 * @param string $nodetypes a comman separated list of the node type names to include in the search.
 * @param String $style (optional - default 'long') may be 'short' or 'long'
 * @param integer $status, defaults to 0. (0 - active, 1 - reported, 2 - retired)
 * @return ConnectionSet or Error
 */
function getConnectionsByPath($nodeid, $linklabels, $userid, $scope = 'all', $linkgroup = '', $depth = 7, $direction = "both", $labelmatch = 'false', $nodetypes = '', $style = 'long', $status = 0)
{
    global $DB, $USER, $CFG;
    $searchLinkLabels = "";
    $searchLinkLabelsArray = array();
    //$searchLinkLabels = getSQLForLinkTypeIDsForLabels(&$searchLinkLabelsArray, $linklabels)
    if ($linklabels != "" && $linkgroup == "") {
        $pieces = explode(",", $linklabels);
        $loopCount = 0;
        foreach ($pieces as $value) {
            $searchLinkLabelsArray[$loopCount] = $value;
            if ($loopCount == 0) {
                $searchLinkLabels .= "?";
            } else {
                $searchLinkLabels .= ",?";
            }
            $loopCount++;
        }
    }
    $nodeTypeNames = "";
    $nodeTypeNamesArray = array();
    //$nodeTypeNames = getSQLForNodeTypeIDsForLabels($nodeTypeNamesArray,$nodetypes);
    if ($nodetypes != "") {
        $nodeTypeNames = "";
        $pieces = explode(",", $nodetypes);
        $loopCount = 0;
        foreach ($pieces as $value) {
            $nodeTypeNamesArray[$loopCount] = $value;
            if ($loopCount == 0) {
                $nodeTypeNames .= "?";
            } else {
                $nodeTypeNames .= ",?";
            }
            $loopCount++;
        }
    }
    // GET TEXT FOR PASSED IDEA ID IF REQUIRED
    $text = "";
    if ($labelmatch == 'true') {
        $params = array();
        $params[0] = $nodeid;
        $qry = $HUB_SQL->APILIB_NODE_NAME_BY_ID_SELECT;
        $resArray = $DB->select($sql, $params);
        if ($resArray !== false && count($resArray) > 0) {
            $text = $resArray[0]['Name'];
        } else {
            return database_error();
        }
    }
    $matchesFound = array();
    if ($labelmatch == 'true' && $text != "" || $labelmatch == 'false' && $nodeid != "") {
        $checkConnections = array();
        $matchedConnections = null;
        if ($labelmatch == 'true') {
            $nextNodes[0] = $text;
        } else {
            $nextNodes[0] = $nodeid;
        }
        $matchesFound = searchNetworkConnections($checkConnections, $matchedConnections, $nextNodes, $searchLinkLabels, $searchLinkLabelsArray, $linkgroup, $labelmatch, $depth, 0, $direction, $nodeTypeNames, $nodeTypeNamesArray, $scope, $status);
    }
    //return database_error($matchesFound);
    //print_r($matchesFound);
    $cs = new ConnectionSet($matchesFound);
    return $cs->loadConnections($matchesFound, $style);
}
/**
 * Walk the network matching connection based on the passed criteria.
 * Starting from the given list of node labels/ids. (which always starts with 1, the focal node);
 * @param integer $status, defaults to 0. (0 - active, 1 - reported, 2 - retired)
 */
function searchNetworkConnections($checkConnections, $matches, $nextNodes, $linklabels = '', $linkLabelsArray, $linkgroup = '', $labelmatch = 'false', $depth = 7, $currentdepth = 0, $direction = 'both', $nodeTypeNames = '', $nodeTypeNamesArray, $scope = 'all', $status = 0)
{
    global $DB, $USER, $CFG, $HUB_SQL;
    $message = "";
    $message .= "currentdepth=" . $currentdepth;
    $currentdepth++;
    // if depth is set to 7, then keep going as 7 = full depth.
    if ($currentdepth > $depth) {
        return $matches;
    }
    $params = array();
    $currentuser = '';
    if (isset($USER->userid)) {
        $currentuser = $USER->userid;
    }
    $tempNodes = null;
    $allConnections = null;
    $searchNodeArray = array();
    $searchNodes = "";
    $loopCount = 0;
    foreach ($nextNodes as $next) {
        $searchNodeArray[$loopCount] = $next;
        if ($loopCount == 0) {
            $searchNodes .= "?";
        } else {
            $searchNodes .= ",?";
        }
        $loopCount++;
    }
    $message .= ":searchNodes=" . $searchNodes;
    $message .= ":linkgroup=" . $linkgroup;
    $message .= ":linklabels=" . $linklabels;
    $message .= ":nodeTypeNames=" . $nodeTypeNames;
    if ($searchNodes != "") {
        $qry = $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_SELECT;
        // Add the node matching
        if ($labelmatch == 'true') {
            if ($direction == "outgoing") {
                $params = array_merge($params, $searchNodeArray);
                $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROM_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROM_PART2 . $HUB_SQL->AND;
            } else {
                if ($direction == "incoming") {
                    $params = array_merge($params, $searchNodeArray);
                    $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TO_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TO_PART2 . $HUB_SQL->AND;
                } else {
                    if ($direction == "both") {
                        $params = array_merge($params, $searchNodeArray);
                        $qry .= $HUB_SQL->OPENING_BRACKET . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROM_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROM_PART2;
                        $params = array_merge($params, $searchNodeArray);
                        $qry .= $HUB_SQL->OR . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TO_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TO_PART2 . $HUB_SQL->CLOSING_BRACKET . $HUB_SQL->AND;
                    }
                }
            }
        } else {
            if ($direction == "outgoing") {
                $params = array_merge($params, $searchNodeArray);
                $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROMID_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROMID_PART2 . $HUB_SQL->AND;
            } else {
                if ($direction == "incoming") {
                    $params = array_merge($params, $searchNodeArray);
                    $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TOID_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TOID_PART2 . $HUB_SQL->AND;
                } else {
                    if ($direction == "both") {
                        $params = array_merge($params, $searchNodeArray);
                        $qry .= $HUB_SQL->OPENING_BRACKET . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROMID_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_FROMID_PART2;
                        $params = array_merge($params, $searchNodeArray);
                        $qry .= $HUB_SQL->OR . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TOID_PART1 . $searchNodes . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TOID_PART2 . $HUB_SQL->CLOSING_BRACKET . $HUB_SQL->AND;
                    }
                }
            }
        }
        if ($nodeTypeNames != "") {
            $params = array_merge($params, $nodeTypeNamesArray);
            $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TYPES_FROM_PART1 . $nodeTypeNames . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TYPES_FROM_PART2 . $HUB_SQL->AND;
            $params = array_merge($params, $nodeTypeNamesArray);
            $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TYPES_TO_PART1 . $nodeTypeNames . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_NODE_TYPES_TO_PART2 . $HUB_SQL->AND;
        }
        /** Add the link matching **/
        if ($linkgroup != "" && $linkgroup != "All") {
            $params[count($params)] = $linkgroup;
            $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_LINKGROUP . $HUB_SQL->AND;
        } else {
            if ($linklabels != "") {
                $params = array_merge($params, $linkLabelsArray);
                $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_LINKLABEL_PART1 . $linklabels . $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_LINKLABEL_PART2 . $HUB_SQL->AND;
            }
        }
        /** Add the privacy / user permissions **/
        if ($scope == "my") {
            $params[count($params)] = $currentuser;
            $qry .= $HUB_SQL->FILTER_USER . $USER->userid . "'";
        } else {
            $params[count($params)] = 'N';
            $params[count($params)] = $currentuser;
            $params[count($params)] = $currentuser;
            $params[count($params)] = 'N';
            $params[count($params)] = $currentuser;
            $params[count($params)] = $currentuser;
            $qry .= $HUB_SQL->APILIB_CONNECTIONS_BY_GLOBAL_PERMISSIONS_NODES;
        }
        // FILTER STATUS
        $params[count($params)] = $status;
        $qry .= $HUB_SQL->AND . $HUB_SQL->APILIB_FILTER_STATUS;
        $qry .= $HUB_SQL->UTILLIB_NETWORK_CONNECTIONS_ORDER_BY;
        //$message .= ":".$qry;
        //return $message;
        //echo $qry;
        $allConnections = $DB->select($qry, $params);
        //error_log(print_r($qry, true));
        if ($allConnections === false) {
            return database_error();
        }
    }
    $count = count($allConnections);
    $innercount = count($nextNodes);
    $found = false;
    for ($i = 0; $i < $count; $i++) {
        $nextarray = $allConnections[$i];
        $message .= ":" . $nextarray['TripleID'];
        if (!isset($checkConnections[(string) $nextarray['TripleID']]) || $checkConnections[(string) $nextarray['TripleID']] == "") {
            $checkConnections[(string) $nextarray['TripleID']] = $nextarray;
            $found = false;
            if ($labelmatch == 'true') {
                for ($j = 0; $j < $innercount; $j++) {
                    $label = $nextNodes[$j];
                    if ($direction == "outgoing") {
                        if ($label == $nextarray['FromLabel']) {
                            $found = true;
                            $tempNodes[count($tempNodes)] = $nextarray['ToLabel'];
                            break;
                        }
                    } else {
                        if ($direction == "incoming") {
                            if ($label == $nextarray['ToLabel']) {
                                $found = true;
                                $tempNodes[count($tempNodes)] = $nextarray['FromLabel'];
                                break;
                            }
                        } else {
                            if ($direction == "both") {
                                if ($label == $nextarray['FromLabel'] || $label == $nextarray['ToLabel']) {
                                    $found = true;
                                    if ($label == $nextarray['FromLabel']) {
                                        $tempNodes[count($tempNodes)] = $nextarray['ToLabel'];
                                    }
                                    if ($label == $nextarray['ToLabel']) {
                                        $tempNodes[count($tempNodes)] = $nextarray['FromLabel'];
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            } else {
                for ($j = 0; $j < $innercount; $j++) {
                    $next = $nextNodes[$j];
                    if ($direction == "outgoing") {
                        if ($next == $nextarray['FromID']) {
                            $found = true;
                            $tempNodes[count($tempNodes)] = $nextarray['ToID'];
                            break;
                        }
                    } else {
                        if ($direction == "incoming") {
                            if ($next == $nextarray['ToID']) {
                                $found = true;
                                $tempNodes[count($tempNodes)] = $nextarray['FromID'];
                                break;
                            }
                        } else {
                            if ($direction == "both") {
                                if ($next == $nextarray['FromID'] || $next == $nextarray['ToID']) {
                                    $found = true;
                                    if ($next == $nextarray['FromID']) {
                                        $tempNodes[count($tempNodes)] = $nextarray['ToID'];
                                    }
                                    if ($next == $nextarray['ToID']) {
                                        $tempNodes[count($tempNodes)] = $nextarray['FromID'];
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if ($found == true) {
                $message .= ":found" . $nextarray['TripleID'];
                $matches[count($matches)] = $nextarray;
            }
        }
    }
    if (count($tempNodes) > 0) {
        $message .= ":" . count($tempNodes);
        $matches = searchNetworkConnections($checkConnections, $matches, $tempNodes, $linklabels, $linkLabelsArray, $linkgroup, $labelmatch, $depth, $currentdepth, $direction, $nodeTypeNames, $nodeTypeNamesArray, $scope, $status);
    }
    //return $message;
    return $matches;
}