Collections functions
General functions
Render functions
Theme permission functions
Resource functions

update_field()

Description

Updates resource field. Works out the previous value, so this is
not efficient if we already know what this previous value is (hence
it is not used for edit where multiple fields are saved)

Parameters

ColumnTypeDefaultDescription
$resource integer Resource ID
$field integer Field ID
$value string The new value
&$errors array array Any errors that may occur during update
$log boolean true Log this change in the resource log?
$nodevalues boolean false Set to TRUE to process the value as a comma separated list of node IDs

Return

boolean

Location

include/resource_functions.php lines 2216 to 2549

Definition

 
function update_field($resource$field$value, array &$errors = array(), $log=true$nodevalues=false)
    {
    global 
$category_tree_add_parents$userref$FIXED_LIST_FIELD_TYPES$lang;

    
$resource_data get_resource_data($resource);
    if (
$resource_data["lock_user"] > && $resource_data["lock_user"] != $userref)
        {
        
$errors[] = get_resource_lock_message($resource_data["lock_user"]);
        return 
false;
        }

    
// accept shortnames in addition to field refs
    
if(!is_int_loose($field))
        {
        
$field ps_value("SELECT ref AS `value` FROM resource_type_field WHERE name = ?", ["s",$field], ''"schema");
        }

    
// Fetch some information about the field
    
$fieldinfo get_resource_type_field($field);

    if(!
$fieldinfo)
        {
        
$errors[] = "No field information about field ID '{$field}'";
        return 
false;
        }

    if (!
in_array($fieldinfo['resource_type'], array(0$resource_data['resource_type'])))
        {
        
$errors[] = "Field is not valid for this resource type";
        return 
false;
        }

    
$value trim((string)$value);
    if(
$value === '' && $fieldinfo['required'])
        {
        
$errors[] = i18n_get_translated($fieldinfo['title']) . ": {$lang['requiredfield']}";;
        return 
false;
        }

    if (
in_array($fieldinfo['type'], $FIXED_LIST_FIELD_TYPES))
        {
        
// Standard node fields
        // Set up arrays of node ids to add/remove and all new nodes.
        
$nodes_to_add    = array();
        
$nodes_to_remove = array();
        
$newnodes        = array();
        
$existingnodes   = array();
        
$fieldnodes      get_nodes($field,null,$fieldinfo['type'] == FIELD_TYPE_CATEGORY_TREE);
        
$node_options array_column($fieldnodes'name''ref');

        
// Get all the new values into an array
        
if (strlen($value) > && (($value[0] == "'" && $value[strlen($value)-1] == "'")
            ||
            (
$value[0] == "\"" && $value[strlen($value)-1] == "\""))
            )
            {
            
// Quoted value - don't attempt to split on comma.
            
$newvalues[] = substr($value,1,-1);
            }
        else
            {
            
$newvalues trim_array(str_getcsv($value));
            }

        
// Get currently selected nodes for this field
        
$current_field_nodes get_resource_nodes($resource$fieldtrue);
        
$current_field_noderefs array_column($current_field_nodes,"ref");
        
// Build 'existing' value
        
if($fieldinfo['type']==FIELD_TYPE_CATEGORY_TREE)
            {
            
$treetext_arr get_tree_strings($current_field_nodes);
            
$existing implode(",",$treetext_arr);
            }
        else
            {
            foreach(
$current_field_noderefs as $current_field_node)
                {
                
$existingnodes[] = $node_options[$current_field_node];
                }
            
$existing implode(",",$existingnodes);
            }
        if(
$nodevalues)
            {
            
// An array of node IDs has been passed, we can use these directly
            
$sent_nodes explode(",",$value);
            if(
in_array($fieldinfo['type'],[FIELD_TYPE_RADIO_BUTTONS,FIELD_TYPE_DROP_DOWN_LIST]) && count($sent_nodes) > 1)
                {
                
// Only a single value allowed
                
return false;
                }

            foreach(
$fieldnodes as $fieldnode)
                {
                
// Add to array of nodes, unless it has been added to array already as a parent for a previous node
                
if (in_array($fieldnode["ref"],$sent_nodes) && !in_array($fieldnode["ref"],$nodes_to_add))
                    {
                    if(!
in_array($fieldnode["ref"],$current_field_noderefs))
                        {
                        
$nodes_to_add[] = $fieldnode["ref"];
                        if(
$fieldinfo['type']==FIELD_TYPE_CATEGORY_TREE && $category_tree_add_parents)
                            {
                            
// Add all parent nodes for category trees
                            
$parent_nodes=get_parent_nodes($fieldnode["ref"]);
                            foreach(
$parent_nodes as $parent_node_ref=>$parent_node_name)
                                {
                                
$nodes_to_add[]=$parent_node_ref;
                                }
                            }
                        }

                    
$newnodes[] = $fieldnode["ref"];
                    }
                else if(
in_array($fieldnode["ref"],$current_field_noderefs) && !in_array($fieldnode["name"],$sent_nodes))
                    {
                    
$nodes_to_remove[] = $fieldnode["ref"];
                    }
                }
            if(
count($newnodes) != count($sent_nodes))
                {
                
// Unable to find all node values that were passed
                
return false;
                }
            
// Build array of new values
            
foreach($newnodes as $newnode)
                {
                
$newvalues[] = $node_options[$newnode];
                }
            }
        else
            {
            
// Not node IDs - value has been passed as normal string value
            
if($fieldinfo['type'] == FIELD_TYPE_DATE_RANGE)
                {
                
// If this is a date range field we need to add values to the field options
                
$newvalues array_map('trim'explode('/'$value));
                
$currentoptions = array();

                foreach(
$newvalues as $newvalue)
                    {
                    
# Check if each new value exists in current options list
                    
if('' != $newvalue && !in_array($newvalue$currentoptions))
                        {
                        
# Append the option and update the field
                        
$newnode          set_node(null$fieldtrim($newvalue), nullnull);
                        
$nodes_to_add[]   = $newnode;
                        
$currentoptions[] = trim($newvalue);

                        
debug("update_field: field option added: '" trim($newvalue));
                        }
                    }
                }
            elseif(
$fieldinfo['type'] == FIELD_TYPE_DYNAMIC_KEYWORDS_LIST && !checkperm('bdk' $field))
                {
                
// If this is a dynamic keyword field need to add any new entries to the field nodes
                
$currentoptions = array();
                foreach(
$fieldnodes as $fieldnode)
                    {
                    
$fieldoptiontranslations explode('~'$fieldnode['name']);
                    if(
count($fieldoptiontranslations) < 2)
                        {
                        
$currentoptions[]=trim($fieldnode['name']); # Not a translatable field
                        
debug("update_field: current field option: '" trim($fieldnode['name']));
                        }
                    else
                        {
                        for (
$n=1;$n<count($fieldoptiontranslations);$n++)
                            {
                            
# Not a translated string, return as-is
                            
if (substr($fieldoptiontranslations[$n],2,1)!=":" && substr($fieldoptiontranslations[$n],5,1)!=":" && substr($fieldoptiontranslations[$n],0,1)!=":")
                                {
                                
$currentoptions[]=trim($fieldnode['name']);
                                
debug("update_field: current field option: '" $fieldnode['name']);
                                }
                            else
                                {
                                
# Support both 2 character and 5 character language codes (for example en, en-US).
                                
$p=strpos($fieldoptiontranslations[$n],':');
                                
$currentoptions[]=trim(substr($fieldoptiontranslations[$n],$p+1));
                                
debug("update_field: current field option: '" trim(substr($fieldoptiontranslations[$n],$p+1)));
                                }
                            }
                        }
                    }
                foreach(
$newvalues as $newvalue)
                    {
                    
# Check if each new value exists in current options list
                    
if(!in_array($newvalue$currentoptions) && $newvalue != '')
                        {
                        
# Append the option and update the field
                        
$newnode            set_node(null$fieldtrim($newvalue), nullnull);
                        
$nodes_to_add[]     = $newnode;
                        
$currentoptions[]   = trim($newvalue);
                        
$fieldnodes[]       = array("ref" => $newnode,"name" => trim($newvalue));
                        
debug("update_field: field option added: '" trim($newvalue));
                        }
                    }
                }

            
$newvalues_translated $newvalues;
            
array_walk(
                
$newvalues_translated,
                function (&
$value$index)
                    {
                    
$value mb_strtolower(i18n_get_translated($value));
                    }
                );
            foreach(
$fieldnodes as $fieldnode)
                {
                
// Add to array of nodes, unless it has been added to array already as a parent for a previous node
                
if (in_array(mb_strtolower(i18n_get_translated($fieldnode["name"])), $newvalues_translated)
                    && !
in_array($fieldnode["ref"], $nodes_to_add)
                    )
                    {
                    
$nodes_to_add[] = $fieldnode["ref"];
                    
// We need to add all parent nodes for category trees
                    
if($fieldinfo['type']==FIELD_TYPE_CATEGORY_TREE && $category_tree_add_parents)
                        {
                        
$parent_nodes=get_parent_nodes($fieldnode["ref"]);
                        foreach(
$parent_nodes as $parent_node_ref=>$parent_node_name)
                            {
                            
$nodes_to_add[]=$parent_node_ref;
                            if (!
in_array(mb_strtolower(i18n_get_translated($parent_node_name)), $newvalues_translated))
                                {
                                
$value $parent_node_name "," $value;
                                }
                            }
                        }
                    }
                elseif(!
in_array($fieldnode["ref"], $nodes_to_add))
                    {
 &nb