Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 is normally used with category trees, but can also be used with
other fixed field types which allow multiple values eg. dynamic keywords list and checkbox list.
This allows field type changes to be made between these three types without losing sight of all nodes.
IMPORTANT! For non-fixed list fields this is capped at 10000
to avoid out of memory errors
matches containing the $name string.

Parameters

ColumnTypeDefaultDescription
$resource_type_field integer null 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
$exact_match boolean false When $name is supplied, match the exact name only instead of returning

Return

array

Location

include/node_functions.php lines 293 to 422

Definition

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

    if(!
is_int_loose$resource_type_field) && !is_null($resource_type_field))
        {
        return [];    
        }

    if(!
is_null($parent))
        {
        if (
$parent == ""){$parent=null;}
        else {
$parent = (int) $parent;}
        }

    if (!
is_null($resource_type_field))
        {
        
$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);

    
// Filter by resource type if required
    
$filter_by_resource_type_field="true";
    if (!
is_null($resource_type_field))
        {
        
$filter_by_resource_type_field="resource_type_field=?";
        
$parameters[]="i";$parameters[]=$resource_type_field;
        }

    
// Filter by name if required
    
$filter_by_name '';
    if(
'' != $name)
        {
        if (
$exact_match)
            {
            
$filter_by_name " AND `name` = ?";
            
$parameters[] = "s"$parameters[] = $name;
            }
        else
            {
            
$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 
        " 
$filter_by_resource_type_field $filter_by_name "
        AND " 
$parent_sql "
        ORDER BY " 
$order_by ", ref ASC
        " 
$limit;

    
$sqlcache = (is_null($resource_type_field) || 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 4th December 2024 08:35 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.