Collections functions
General functions
Render functions
Theme permission functions
Resource functions

set_node()

Description

Set node - Used for both creating and saving a node in the database.
Use NULL for ref if you just want to insert a new record.

Parameters

ColumnTypeDefaultDescription
$ref integer ID of the node. To insert new record ID should be NULL
$resource_type_field integer ID of the metadata field
$name string Node name to be used (international)
$parent integer ID of the parent of this node (null for non trees)
$order_by integer Value of the order in the list (e.g. 10)

Return

boolean|integer

Location

include/node_functions.php lines 15 to 153

Definition

 
function set_node($ref$resource_type_field$name$parent$order_by)
    {
    if(!
is_null($name))
        {
        
$name trim((string) $name);
        }

    if (
is_null($resource_type_field) || '' == $resource_type_field || is_null($name) || '' == $name)
        {
        return 
false;
        }

    
// Prevent the creation of duplicate nodes unless type is category tree and the nodes have different parents in the tree.
    
$resource_type_field_data get_resource_type_field($resource_type_field);

    if (!
$resource_type_field_data)
        {
        return 
false;
        }

    if (
$resource_type_field_data['type'] != FIELD_TYPE_CATEGORY_TREE)
        {
        
$returnexisting true;
        }
    else
        {
        
$nodes_for_parent get_nodes($resource_type_field$parent);
        if (
count($nodes_for_parent) == || !in_array($namearray_column($nodes_for_parent'name')))
            {
            
$returnexisting false;
            }
        else
            {
            
$returnexisting true;
            }
        }
    
    if(
is_null($ref) && '' == $order_by)
        {
        
$order_by get_node_order_by($resource_type_field, (is_null($parent) || '' == $parent), $parent);
        }

    
$query "INSERT INTO `node` (`resource_type_field`, `name`, `parent`, `order_by`) VALUES (?, ?, ?, ?)";
    
$parameters=array  
        (
        
"i",$resource_type_field,
        
"s",$name,
        
"i",(!isset($parent) || trim($parent)=="" NULL $parent),
        
"s",$order_by
        
);

    
// Check if we only need to save the record
    
$current_node = array();
    if(
get_node($ref$current_node))
        {
        
// If nothing has changed, just return true, otherwise continue and update record
        
if($resource_type_field === $current_node['resource_type_field'] &&
            
$name === $current_node['name'] &&
            
$parent === $current_node['parent'] &&
            
$order_by === $current_node['order_by']
            )
            {
            return 
$ref;
            }

        
// When changing parent we need to make sure order by is changed as well
        // to reflect the fact that the node has just been added (ie. at the end of the list)
        
if($parent != $current_node['parent'])
            {
            
$order_by get_node_order_by($resource_type_fieldtrue$parent);
            }

        
// Order by can be changed asynchronously, so when we save a node we can pass null or an empty
        // order_by value and this will mean we can use the current order
        
if(!is_null($ref) && '' == $order_by)
            {
            
$order_by $current_node['order_by'];
            }

        
$query "
                UPDATE node
                   SET resource_type_field = ?,
                       `name` = ?,
                       parent = ?,
                       order_by = ?
                 WHERE ref = ?
            "
;
        
$parameters=array  
                (
                
"i",$resource_type_field,
                
"s",$name,
                
"i",(trim($parent)=="" NULL $parent),
                
"s",$order_by,
                
"i",$ref
                
);

        
// Handle node indexing for existing nodes
        
remove_node_keyword_mappings(array('ref' => $current_node['ref'], 'resource_type_field' => $current_node['resource_type_field'], 'name' => $current_node['name']), NULL);
        
add_node_keyword_mappings(array('ref' => $ref'resource_type_field' => $resource_type_field'name' => $name), NULL);
        }

    if(
$returnexisting)
        {
        
// Check for an existing match. MySQL checks case insensitive so case is checked on this side.
        
$existingnode=ps_query("SELECT ref,name FROM node WHERE resource_type_field = ? AND name = ?", array("i",$resource_type_field,"s",$name));
        if(
count($existingnode) > 0)
            {
            foreach (
$existingnode as $node)
                {
                if(
$node["name"]== $name){return (int)$node["ref"];}
                }
            }
        }

    
ps_query($query,$parameters);
    
$new_ref sql_insert_id();
    if (
$new_ref == || $new_ref === false)
        {
        if (
$ref == null)
            {
            return 
ps_value("SELECT `ref` AS 'value' FROM `node` WHERE `resource_type_field`=? AND `name`=?",array("i",$resource_type_field,"s",$name),0);
            }
        else
            {
            return 
$ref;
            }
        }
    else
        {
        
log_activity("Set metadata field option for field {$resource_type_field}"LOG_CODE_CREATED$name'node''name');

        
// Handle node indexing for new nodes
        
add_node_keyword_mappings(array('ref' => $new_ref'resource_type_field' => $resource_type_field'name' => $name), NULL);

        return 
$new_ref;
        }
    
    
clear_query_cache("schema");
    }

This article was last updated 29th May 2022 07:05 Europe/London time based on the source file dated 6th April 2022 16:35 Europe/London time.