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

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). To automatically pick next order by use ''.

Return

boolean|integer

Location

include/node_functions.php lines 15 to 177

Definition

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

    if (
is_null($resource_type_field) || '' == $resource_type_field || is_null($name) || '' == $name)
        {
        return 
false;
        }
    
    
// Blank parent fixup to NULL; non-blank parent fixup to integer
    
if(!is_null($parent))
        {
        if (
$parent == ""){$parent=null;}
        else {
$parent = (int) $parent;}
        }

    
// 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(
$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"];}
                }
            }
        }
    
    
// If creating new node establish order_by if necessary
    
if(is_null($ref) && '' == $order_by)
        {
        
$order_by get_node_order_by($resource_type_field, ($resource_type_field_data['type'] == FIELD_TYPE_CATEGORY_TREE), $parent);
        }

    
$query "INSERT INTO `node` (`resource_type_field`, `name`, `parent`, `order_by`) VALUES (?, ?, ?, ?)";
    
$parameters=array  
        (
        
"i",$resource_type_field,
        
"s",$name,
        
"i",$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",$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);
        if(
$resource_type_field_data["keywords_index"] == 1)
            {
            
$is_date in_array($resource_type_field_data['type'],[FIELD_TYPE_DATE_AND_OPTIONAL_TIME,FIELD_TYPE_EXPIRY_DATE,FIELD_TYPE_DATE,FIELD_TYPE_DATE_RANGE]);
            
$is_html = ($resource_type_field_data["type"] == FIELD_TYPE_TEXT_BOX_FORMATTED_AND_CKEDITOR);
            
add_node_keyword_mappings(array('ref' => $ref'resource_type_field' => $resource_type_field'name' => $name), null$is_date$is_html);
            }
        }

    
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
        {
        if (
in_array($resource_type_field_data['type'], $FIXED_LIST_FIELD_TYPES))
            {
            
log_activity("Set metadata field option for field {$resource_type_field}"LOG_CODE_CREATED$name'node''name'$new_refnull'');
            }

        
// Handle node indexing for new nodes
        
if($resource_type_field_data["keywords_index"] == 1)
            {
            
add_node_keyword_mappings(array('ref' => $new_ref'resource_type_field' => $resource_type_field'name' => $name), null);
            }
        
$return $new_ref;
        }

    if (
in_array($resource_type_field_data['type'], $FIXED_LIST_FIELD_TYPES))
        {
        
clear_query_cache("schema");
        }

    return 
$return;
    }

This article was last updated 11th December 2024 20:35 Europe/London time based on the source file dated 6th December 2024 12:00 Europe/London time.