Collections functions
Encryption functions
General functions
Theme permission 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 field

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

IMPORTANT: this should be used only with category trees

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 218 to 323

Definition

 
function get_nodes($resource_type_field$parent NULL$recursive FALSE$offset NULL$rows NULL$name ''
    
$use_count false$order_by_translated_name false)
    {
    
debug_function_call("get_nodes"func_get_args());
    global 
$language,$defaultlanguage;
    
$asdefaultlanguage=$defaultlanguage;

    if (!isset(
$asdefaultlanguage))
        
$asdefaultlanguage='en';

    
// Use langauge specified if not use default
    
isset($language)?$language_in_use $language:$language_in_use $defaultlanguage;

    
$return_nodes = array();

    
// 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 {$offset},{$rows}";
            }
        else 
# Row limit absent
            
{
            
$limit "LIMIT {$offset},999999999"# Use a large arbitrary limit
            
}
        }
    else 
# Offset not specified
        
{
        if(!
is_null($rows) && is_int($rows)) # Row limit specified
            
{
            
$limit "LIMIT {$rows}";
            }
        }

    
// Filter by name if required
    
$filter_by_name '';
    if(
'' != $name)
        {
        
$filter_by_name " AND `name` LIKE '%" escape_check($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";
        }

    
// Order by translated_name or order_by based on flag
    
$order_by $order_by_translated_name "translated_name" "order_by";
    
    
// Get length of language string + 2 (for ~ and :) for usuage in SQL below
    
$language_string_length = (strlen($language_in_use) + 2);

    
$parent_sql trim($parent) == "" ? ($recursive "TRUE" "parent IS NULL") : ("parent = '" escape_check($parent) . "'");
   
    
$query "
        SELECT 
            *,
            CASE
                WHEN
                    POSITION('~" 
$language_in_use "' IN name) > 0
                THEN
                    TRIM(SUBSTRING(name,
                            POSITION('~" 
$language_in_use ":' IN name) + " $language_string_length ",
                            CASE
                                WHEN
                                    POSITION('~' IN SUBSTRING(name,
                                            POSITION('~" 
$language_in_use ":' IN name) + " $language_string_length ",
                                            LENGTH(name) - 1)) > 0
                                THEN
                                    POSITION('~' IN SUBSTRING(name,
                                            POSITION('~" 
$language_in_use ":' IN name) + " $language_string_length ",
                                            LENGTH(name) - 1)) - 1
                                ELSE LENGTH(name)
                            END))
                ELSE TRIM(name)
            END AS translated_name
            " 
$use_count_sql "
        FROM node 
        WHERE resource_type_field = " 
escape_check($resource_type_field) . "
        " 
$filter_by_name "
        AND " 
$parent_sql "
        ORDER BY " 
$order_by " ASC
        " 
$limit;

    
$nodes sql_query($query);

    foreach(
$nodes as $node)
        {
        
array_push($return_nodes$node);

        
// No need to recurse if no parent was specified as we already have all nodes
        
if($recursive && (int)$parent 0)
            {
            foreach(
get_nodes($resource_type_field$node['ref'], TRUE) as $sub_node)
                {
                
array_push($return_nodes$sub_node);
                }
            }
        }
    return 
$return_nodes;
    }

This article was last updated 10th July 2020 20:35 Europe/London time based on the source file dated 6th July 2020 18:09 Europe/London time.