Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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)
$returnexisting boolean false Return an existing node if a match is found for this field. Duplicate nodes may be required for category trees but are not desirable for non-fixed list fields

Return

boolean|integer

Location

include/node_functions.php lines 16 to 120

Definition

 
function set_node($ref$resource_type_field$name$parent$order_by,$returnexisting=false)
    {
    if(!
is_null($name))
        {
        
$name trim($name);
        }

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

    if(
is_null($ref) && '' == $order_by)
        {
        
$order_by get_node_order_by($resource_type_field, (is_null($parent) || '' == $parent), $parent);
        }

    
$query sprintf("INSERT INTO `node` (`resource_type_field`, `name`, `parent`, `order_by`) VALUES ('%s', '%s', %s, '%s')",
        
escape_check($resource_type_field),
        
escape_check($name),
        (
'' == trim($parent) ? 'NULL' "'" escape_check($parent) . "'"),
        
escape_check($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 sprintf("
                UPDATE node
                   SET resource_type_field = '%s',
                       `name` = '%s',
                       parent = %s,
                       order_by = '%s'
                 WHERE ref = '%s'
            "
,
            
escape_check($resource_type_field),
            
escape_check($name),
            (
trim($parent)=="" 'NULL' '\'' escape_check($parent) . '\''),
            
escape_check($order_by),
            
escape_check($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
        
$existingnode=sql_value("SELECT ref value FROM node WHERE resource_type_field ='" escape_check($resource_type_field) . "' AND name ='" escape_check($name) . "'",0);
        if(
$existingnode 0)
            {return 
$existingnode;}
        }

    
sql_query($query);
    
$new_ref sql_insert_id();
    if (
$new_ref == || $new_ref === false)
        {
        if (
$ref == null)
            {
            return 
sql_value("SELECT `ref` AS 'value' FROM `node` WHERE `resource_type_field`='" escape_check($resource_type_field) . "' AND `name`='" escape_check($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 25th November 2020 05:35 Europe/London time based on the source file dated 13th November 2020 16:50 Europe/London time.