/** * Retrieve the graph from a node * @param {Array} $ids nodes indexes * @return {Array} array nodes (as "nodes") and links (as "links") */ static function graph($ids) { $links = model::links_r($ids, array()); $values = array_values($links); for ($i = 0; $i < sizeof($links); $i++) { $ids[] = $values[$i][0]; $ids[] = $values[$i][1]; } $ids = array_values(array_unique($ids)); $array = array(); for ($i = 0; $i < sizeof($ids); $i++) { $array[] = model_json::node($ids[$i], 1, 7); } $links_json = array(); while (list($key, $val) = each($links)) { $links_json[] = array("link_id" => $key, "src_id" => $val[0], "tgt_id" => $val[1]); } $res["nodes"] = $array; $res["links"] = $links_json; return $res; }
/** * Get connected links. Recursive. Useful for graphs * Infinite loop check * @param {Array} $ids array of nodes indexes to query * @param {Array} $targets array of node indexes to filter * @return {Array} or false */ static function links_r($ids, $targets) { $query = sprintf("SELECT * FROM link WHERE tgt_id IN ( %s );", join(",", $ids)); if (!($result = mysql_query($query))) { return array(); } if (!mysql_num_rows($result)) { return array(); } $a = array(); $newids = array(); while ($row = mysql_fetch_array($result)) { $a[intval($row["id"])] = array(intval($row["src_id"]), intval($row["tgt_id"])); if (!in_array(intval($row["src_id"]), $targets)) { $newids[] = intval($row["src_id"]); } } $targets += $newids; $a += model::links_r($newids, $targets); return $a; }