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_parent_nodes()

Description

Get all the parent nodes of the given node, all the way back to the top of the node tree.

Parameters

ColumnTypeDefaultDescription
$noderef integer The child node ID
$detailed bool false Return all node data? false by default
$include_child bool false Include the passed node in the returned array (easier for resolving tree nodes to paths)? false by default

Return

array Array of the parent node IDs

Location

include/node_functions.php lines 1789 to 1851

Definition

 
function get_parent_nodes(int $noderef,bool $detailed false$include_child=false)
    {
    
// Get all parents. Query varies according to MySQL cte support
    
$mysql_version ps_query('SELECT LEFT(VERSION(), 3) AS ver');
    if(
version_compare($mysql_version[0]['ver'], '8.0''>=')) 
        {
        
$colsa $detailed "ref, name, parent, resource_type_field, order_by, `active`" "ref, name, parent";
        
$colsb $detailed "n.ref, n.name, n.parent, n.resource_type_field, n.order_by, n.`active`" "n.ref, n.name, n.parent";
        
$parent_nodes ps_query("
            WITH RECURSIVE cte(
$colsa,level) AS
                    (
                    SELECT 
$colsa,
                           1 AS level
                      FROM node
                     WHERE ref= ?
                 UNION ALL
                    SELECT 
$colsb,
                           level+1 AS LEVEL
                      FROM  node n
                INNER JOIN  cte
                        ON  n.ref = cte.parent
                    )
            SELECT 
$colsa
              FROM cte
          ORDER BY level ASC;"
,
        [
'i'$noderef]);
        }
    else
        {
        
$colsa $detailed columns_in("node","N2") : "ref, name";
        
$parent_nodes ps_query("
        SELECT  
$colsa
        FROM  (SELECT @r AS p_ref,
                (SELECT @r := parent FROM node WHERE ref = p_ref) AS parent,
                @l := @l + 1 AS lvl
        FROM  (SELECT @r := ?, @l := 0) vars,
                node c
        WHERE  @r <> 0) N1
        JOIN  node N2
            ON  N1.p_ref = N2.ref
        ORDER BY  N1.lvl ASC"
,
            [
'i'$noderef]);
        }

    if(!
$include_child)
        {
        
$parent_nodes array_values(array_filter($parent_nodes,function($node) use ($noderef) {return $node["ref"] != $noderef;}));
        }

    if(!
$detailed)
        {
        
$parent_nodes array_column($parent_nodes,"name""ref");
        }
    else
        {
        for(
$n=0;$n<count($parent_nodes);$n++)
            {
            
$parent_nodes[$n]["translated_name"] = i18n_get_translated($parent_nodes[$n]["name"]);
            }
        }

    return 
$parent_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.