Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

save_resource_data()

Parameters

ColumnTypeDefaultDescription
$ref
$multi
$autosave_field ""

Location

include/resource_functions.php lines 527 to 1158

Definition

 
function save_resource_data($ref,$multi,$autosave_field="")
    {
    
# Save all submitted data for resource $ref.
    # Also re-index all keywords from indexable fields.
    
global $lang$auto_order_checkbox$userresourcedefaults$multilingual_text_fields,
           
$languages$language$user_resources_approved_email$FIXED_LIST_FIELD_TYPES,
           
$DATE_FIELD_TYPES$date_validator$range_separator$reset_date_field$reset_date_upload_template,
           
$edit_contributed_by$new_checksums$upload_review_mode$blank_edit_template$is_template$NODE_FIELDS,
           
$userref;

    
hook("befsaveresourcedata""", array($ref));
    
// Ability to avoid editing conflicts by checking checksums.
    // NOTE: this should NOT apply to upload.
    
$check_edit_checksums true;

    
// Save resource defaults (functionality available for upload only)
    // Call it here so that if users have access to the field and want 
    // to override it, they can do so
    
if($ref)
        {
        
set_resource_defaults($ref);

        
$check_edit_checksums false;
        }

    
# Loop through the field data and save (if necessary)
    
$errors=array();
    
$fields=get_resource_field_data($ref,$multi, !hook("customgetresourceperms"));    
    
$expiry_field_edited=false;
    
$resource_data=get_resource_data($ref);
    
    if(
$resource_data["lock_user"] > && $resource_data["lock_user"] != $userref)
        {
        
$errors[] = get_resource_lock_message($resource_data["lock_user"]);
        return 
$errors;
        }
        
    
# Load the configuration for the selected resource type. Allows for alternative notification addresses, etc.
    
resource_type_config_override($resource_data["resource_type"]);                
    
    
# Set up arrays of node ids to add/remove. We can't remove all nodes as user may not have access
    
$nodes_to_add    = array();
    
$nodes_to_remove = array();   

    
// All the nodes passed for editing. Some of them were already a value
    // of the fields while others have been added/removed
    
$user_set_values getval('nodes', array());
    
    
    
// Initialise array to store new checksums that client needs after autosave, without which subsequent edits will fail
    
$new_checksums = array();        
    
    for (
$n=0;$n<count($fields);$n++)
        {
        if(!(
            
checkperm('F' $fields[$n]['ref'])
            || (
checkperm("F*") && !checkperm('F-' $fields[$n]['ref']))
            
// If we hide on upload the field, there is no need to check values passed from the UI as there shouldn't be any
            
|| (($ref || $upload_review_mode) && $fields[$n]['hide_when_uploading'])
            )
            && (
'' == $autosave_field || $autosave_field == $fields[$n]['ref']
                || (
is_array($autosave_field) && in_array($fields[$n]['ref'], $autosave_field))
            )
        )
            {
            
// Fixed list  fields use node IDs directly
            
if(in_array($fields[$n]['type'], $FIXED_LIST_FIELD_TYPES))
                {
                
debug("save_resource_data(): Checking nodes to add/ remove for field {$fields[$n]['ref']} - {$fields[$n]['title']}");

                
$val '';

                
// Get currently selected nodes for this field 
                
$current_field_nodes get_resource_nodes($ref$fields[$n]['ref']); 
                
                
// Check if resource field data has been changed between form being loaded and submitted                
                
$post_cs getval("field_" $fields[$n]['ref'] . "_checksum","");
                
$current_cs md5(implode(",",$current_field_nodes));                
                if(
$check_edit_checksums && $post_cs != "" && $post_cs != $current_cs)
                    {
                    
$errors[$fields[$n]["ref"]] = i18n_get_translated($fields[$n]['title']) . ': ' $lang["save-conflict-error"];
                    continue;
                    };
            
                
debug("save_resource_data(): Current nodes for resource " $ref ": " implode(",",$current_field_nodes));
                
                
// Work out nodes submitted by user
                
$ui_selected_node_values = array();
                
                if(isset(
$user_set_values[$fields[$n]['ref']])
                    && !
is_array($user_set_values[$fields[$n]['ref']])
                    && 
'' != $user_set_values[$fields[$n]['ref']]
                    && 
is_numeric($user_set_values[$fields[$n]['ref']]))
                    {
                    
$ui_selected_node_values[] = $user_set_values[$fields[$n]['ref']];
                    }
                else if(isset(
$user_set_values[$fields[$n]['ref']])
                    && 
is_array($user_set_values[$fields[$n]['ref']]))
                    {
                    
$ui_selected_node_values $user_set_values[$fields[$n]['ref']];
                    }

                
// Check nodes are valid for this field
                
$fieldnodes   get_nodes($fields[$n]['ref'], '', (FIELD_TYPE_CATEGORY_TREE == $fields[$n]['type']));
                
$node_options array_column($fieldnodes'name''ref');
                
$validnodes   array_column($fieldnodes'ref');

                
$ui_selected_node_values=array_intersect($ui_selected_node_values,$validnodes);    
                
natsort($ui_selected_node_values);
                
                
$added_nodes array_diff($ui_selected_node_values$current_field_nodes);

                
debug("save_resource_data(): Adding nodes to resource " $ref ": " implode(",",$added_nodes));
                
$nodes_to_add array_merge($nodes_to_add$added_nodes);
                
$removed_nodes array_diff($current_field_nodes,$ui_selected_node_values);    

                
debug("save_resource_data(): Removed nodes from resource " $ref ": " implode(",",$removed_nodes));           
                
$nodes_to_remove array_merge($nodes_to_remove$removed_nodes);
                                
                if(
count($added_nodes)>|| count($removed_nodes)>0)
                    {  
                    
# If this is a 'joined' field it still needs to add it to the resource column
                    
$joins=get_resource_table_joins();
                    if (
in_array($fields[$n]["ref"],$joins))
                        {
                        
$new_nodevals = array();
                        
// Build new value:
                        
foreach($ui_selected_node_values as $ui_selected_node_value)
                            {
                            
$new_nodevals[] = $node_options[$ui_selected_node_value];
                            }
                        
$new_nodes_val implode(","$new_nodevals);
                        
sql_query("update resource set field".$fields[$n]["ref"]."='".escape_check(truncate_join_field_value(strip_leading_comma($new_nodes_val)))."' where ref='$ref'");
                        }
                    }

                
// Required fields that didn't change get the current value
                
if(== $fields[$n]['required'] && '' == $val)
                    {
                    
// Build existing value:
                    
foreach($current_field_nodes as $current_field_node)
                        {
                        
$val .= ",{$node_options[$current_field_node]}";
                        }
                    }

                
$new_checksums[$fields[$n]['ref']] = md5(implode(','$ui_selected_node_values));
                }
            else
                {
                if(
$fields[$n]['type']==FIELD_TYPE_DATE_RANGE)
                    {
                    
# date range type
                    # each value will be a node so we end up with a pair of nodes to represent the start and end dates

                    
$daterangenodes=array();
                    
$newval="";
                    
                    if((
$date_edtf=getvalescaped("field_" $fields[$n]["ref"] . "_edtf",""))!=="")
                        {
                        
// We have been passed the range in EDTF format, check it is in the correct format
                        
$rangeregex="/^(\d{4})(-\d{2})?(-\d{2})?\/(\d{4})(-\d{2})?(-\d{2})?/";
                        if(!
preg_match($rangeregex,$date_edtf,$matches))
                            {
                            
$errors[$fields[$n]["ref"]] = $lang["information-regexp_fail"] . " : " $date_edtf;
                            continue;
                            }
                        if(
is_numeric($fields[$n]["linked_data_field"]))
                            {
                            
// Update the linked field with the raw EDTF string submitted
                            
update_field($ref,$fields[$n]["linked_data_field"],$date_edtf);
                            }
                        
$rangedates explode("/",$date_edtf);
                        
$rangestart=str_pad($rangedates[0],  10"-00");
                        
$rangeendparts=explode("-",$rangedates[1]);
                        
$rangeendyear=$rangeendparts[0];
                        
$rangeendmonth=isset($rangeendparts[1])?$rangeendparts[1]:12;
                        
$rangeendday=isset($rangeendparts[2])?$rangeendparts[2]:cal_days_in_month(CAL_GREGORIAN$rangeendmonth$rangeendyear);
                        
$rangeend=$rangeendyear "-" $rangeendmonth "-" $rangeendday;
                        
                        
$newval $rangestart $range_separator $rangeend;
                        
$daterangenodes[]=set_node(null$fields[$n]["ref"], $rangestartnullnull,true);
                        
$daterangenodes[]=set_node(null$fields[$n]["ref"], $rangeendnullnull,true);
                        }
                    else
                        {
                        
// Range has been passed via normal inputs, construct the value from the date/time dropdowns
                        
$date_parts=array("_start_","_end_");
                        
                        foreach(
$date_parts as $date_part)
                            {
                            
$val getvalescaped("field_" $fields[$n]["ref"] . $date_part "year","");
                            if (
intval($val)<=0
                                {
                                
$val="";
                                }
                            elseif ((
$field=getvalescaped("field_" $fields[$n]["ref"] . $date_part "month",""))!=""
                                {
                                
$val.="-" $field;
                                if ((
$field=getvalescaped("field_" $fields[$n]["ref"] . $date_part "day",""))!=""
                                    {
                                    
$val.="-" $field Need help? Talk to our expert team     Book a call