Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

get_nodes()

Description

Get all nodes from database for a specific metadata field or parent.

Use $parent = NULL and recursive = TRUE to get all nodes for a category tree field

Use $offset and $rows only when returning a subset.

IMPORTANT: this should be used only with category trees
IMPORTANT! For non-fixed list fields this is capped at 10000
to avoid out of memory errors

Parameters

ColumnTypeDefaultDescription
$resource_type_field integer ID of the metadata field
$parent integer null ID of parent node
$recursive boolean false Set to true to get children nodes as well.
$offset integer null Specifies the offset of the first row to return
$rows integer null Specifies the maximum number of rows to return.
$name string '' Filter by name of node
$use_count boolean false Show how many resources use a particular node in the node properties
$order_by_translated_name boolean false Flag to order by translated names rather then the order_by column

Return

array

Location

include/node_functions.php lines 289 to 401

Definition

 
function get_nodes($resource_type_field$parent null$recursive false$offset null$rows null$name ''
    
$use_count false$order_by_translated_name false)
    {
    global 
$FIXED_LIST_FIELD_TYPES;
    
debug_function_call("get_nodes"func_get_args());

    if(!
is_int_loose$resource_type_field))
        {
        return [];    
        }
        
    if(!
is_null($parent))
        {
        if (
$parent == ""){$parent=null;}
        else {
$parent = (int) $parent;}
        }

    
$fieldinfo  get_resource_type_field($resource_type_field);
    if(
$fieldinfo === false){return false;}
    if(!
in_array($fieldinfo["type"],$FIXED_LIST_FIELD_TYPES) && (is_null($rows) || (int)$rows 10000 ))
        {
        
$rows 10000;
        }

    
$return_nodes = array();

    
$parameters= [];
    
$sql "";
    
add_sql_node_language($sql,$parameters);

    
$parameters[] = "i";$parameters[] = $resource_type_field;

    
// Filter by name if required
    
$filter_by_name '';
    if(
'' != $name)
        {
        
$filter_by_name " AND `name` LIKE ?";
        
$parameters[]="s";$parameters[]="%" $name "%";
        }

    
// Option to include a usage count alongside each node
    
$use_count_sql="";
    if(
$use_count)
        {
        
$use_count_sql ",(SELECT count(resource) FROM resource_node WHERE resource_node.resource > 0 AND resource_node.node = node.ref) AS use_count";
        }  

    
$parent_sql is_null($parent) ? ($recursive "TRUE" "parent IS NULL") : ("parent = ?");
    if (
strpos($parent_sql,"?")!==false) {$parameters[]="i";$parameters[]=$parent;}
    
    
// Order by translated_name or order_by based on flag
    
$order_by $order_by_translated_name "translated_name" "order_by";

    
// Check if limiting is required
    
$limit '';
    if(!
is_null($offset) && is_int($offset)) # Offset specified
        
{
        if(!
is_null($rows) && is_int($rows)) # Row limit specified
            
{
            
$limit "LIMIT ?,?";
            
$parameters[]="i";$parameters[]=$offset;
            
$parameters[]="i";$parameters[]=$rows;
            }
        else 
# Row limit absent
            
{
            
$limit "LIMIT ?,999999999"# Use a large arbitrary limit
            
$parameters[]="i";$parameters[]=$offset;
            }
        }
    else 
# Offset not specified
        
{
        if(!
is_null($rows) && is_int($rows)) # Row limit specified
            
{
            
$limit "LIMIT ?";
            
$parameters[]="i";$parameters[]=$rows;
            }
        }
        
    
$query "SELECT " columns_in("node") . $sql $use_count_sql "
        FROM node 
        WHERE resource_type_field = ?
        " 
$filter_by_name "
        AND " 
$parent_sql "
        ORDER BY " 
$order_by ", ref ASC
        " 
$limit;

    
$sqlcache in_array($fieldinfo["type"],$FIXED_LIST_FIELD_TYPES) ? "schema" "";
    
$nodes ps_query($query,$parameters,$sqlcache);
  
    
// No need to recurse if no parent was specified as we already have all nodes
    
if($recursive && (int)$parent 0)
        {
        foreach(
$nodes as $node)
            {
            foreach(
get_nodes($resource_type_field$node['ref'], true) as $sub_node)
                {
                
array_push($nodes$sub_node);
                }
            }
        }
    else
        {
        
$return_nodes $nodes;
        }

    if(
$recursive)
        {
        
// Need to reorder so that parents are ordered by first, with children between (query will have returned them all according to the passed order_by)
        
$return_nodes order_tree_nodes($return_nodes);
        }
   
    return 
$return_nodes;
    }

This article was last updated 18th April 2024 09:35 Europe/London time based on the source file dated 15th April 2024 11:30 Europe/London time.