Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

save_resource_data_multi()

Parameters

ColumnTypeDefaultDescription
$collection
$editsearch array

Location

include/resource_functions.php lines 1236 to 1924

Definition

 
function save_resource_data_multi($collection,$editsearch = array())
    {
    global 
$auto_order_checkbox,$auto_order_checkbox_case_insensitive,  $FIXED_LIST_FIELD_TYPES,$DATE_FIELD_TYPES,
    
$range_separator$edit_contributed_by$TEXT_FIELD_TYPES$userref$lang;

    
# Save all submitted data for collection $collection or a search result set, this is for the 'edit multiple resources' feature

    
$errors = array();
    if(
$collection == && isset($editsearch["search"]))
        {
        
// Editing a result set, not a collection
        
$edititems  do_search($editsearch["search"],$editsearch["restypes"],'resourceid',$editsearch["archive"],-1,'ASC',false,0,false,false,'',false,falsetruetrue);
        
$list       array_column($edititems,"ref");
        }
    else
        {
        
# Save all submitted data for collection $collection, 
        
$list   get_collection_resources($collection);
        }
    
    
// Check that user can edit all resources, edit access and not locked by another user
    
$noeditaccess = array();
    
$lockedresources = array();
    foreach(
$list as $listresource)
        {
        
$resource_data[$listresource]  = get_resource_data($listresourcetrue);
        if(!
get_edit_access($listresource,$resource_data[$listresource]["archive"], $resource_data[$listresource]))
            {
            
$noeditaccess[] = $listresource;
            }
        if(
$resource_data[$listresource]["lock_user"] > && $resource_data[$listresource]["lock_user"] != $userref)
            {
            
$lockedresources[] = $listresource;
            }
        }

    if(
count($noeditaccess) > 0)
        {
        
$errors[] = $lang["error-edit_noaccess_resources"] . implode(",",$noeditaccess);
        }
    if (
count($lockedresources) > 0)
        {
        
$errors[] = $lang["error-edit_locked_resources"] . implode(",",$lockedresources);            
        }

    if(
count($errors) > 0)
        {
        return 
$errors;
        }

    
$tmp    hook("altercollist""", array("save_resource_data_multi"$list));
    if(
is_array($tmp))
        {
        if(
count($tmp) > 0)
            {
            
$list $tmp;
            }
        else
            {
            return 
true;
            }
        }

    
$ref                 $list[0];
    
$fields              get_resource_field_data($ref,true);
    
$expiry_field_edited false;

    
// 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());
    
    
// set up arays to add to all resources to make query more efficient when only appending or removing options
    
$all_nodes_to_add    = array();
    
$all_nodes_to_remove = array();
    for (
$n=0;$n<count($fields);$n++)
        {
        if(
'' != getval('editthis_field_' $fields[$n]['ref'], '') || hook('save_resource_data_multi_field_decision''', array($fields[$n]['ref'])))
            {
            
$nodes_to_add    = array();
            
$nodes_to_remove    = array();
            if(
in_array($fields[$n]['type'], $FIXED_LIST_FIELD_TYPES))
                {
                
// Set up arrays of node ids selected and we will later resolve these to add/remove. Don't remove all nodes since user may not have access
                
$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');
                
$valid_nodes  array_column($fieldnodes'ref');

                
// Store selected/deselected values in array
                
$ui_selected_node_values=array_intersect($ui_selected_node_values,$valid_nodes);   
                
$ui_deselected_node_values array_diff($valid_nodes$ui_selected_node_values);

                
// Append option(s) mode?
                
if (getval("modeselect_" $fields[$n]["ref"],"")=="AP")
                   {
                   
$nodes_to_add $ui_selected_node_values;
                   }
                elseif (
getval("modeselect_" $fields[$n]["ref"],"")=="RM")
                    {
                    
// Remove option(s) mode
                    
$nodes_to_remove $ui_selected_node_values;                    
                    
debug("Removing nodes: " .  implode(",",$nodes_to_remove));
                    }
                else
                    {
                    
// Replace option(s) mode
                    
$nodes_to_add  $ui_selected_node_values;
                    
$nodes_to_remove $ui_deselected_node_values;
                    }

                
$all_nodes_to_add    array_merge($all_nodes_to_add,$nodes_to_add);                
                
$all_nodes_to_remove array_merge($all_nodes_to_remove,$nodes_to_remove);
                
                
// Loop through all the resources and check current node values so we can check if we need to log this as a chsnge
                
for ($m=0;$m<count($list);$m++)
                    {
                    
$ref            $list[$m];
                    
$value_changed  false;
                    
                    
$current_field_nodes get_resource_nodes($ref$fields[$n]['ref']);                    
                    
debug('Current nodes: ' implode(',',$current_field_nodes));

                    
$added_nodes array_diff($nodes_to_add,$current_field_nodes);
                    
debug('Adding nodes: ' implode(',',$added_nodes));

                    
$removed_nodes array_intersect($nodes_to_remove,$current_field_nodes);
                    
debug('Removed nodes: ' implode(',',$removed_nodes));
    
                    
// Work out what new nodes for this resource  will be
                    
$new_nodes array_diff(array_merge($current_field_nodes$added_nodes), $removed_nodes);      
                    
debug('New nodes: ' implode(',',$new_nodes));

                    if(
count($added_nodes)>|| count($removed_nodes)>0){$value_changed  true;}
                    
                       if(
$value_changed)
                        {
                        
$existing_nodes_value '';
                        
$new_nodes_val        '';

                        
// Build new value:
                        
foreach($new_nodes as $new_node)
                            {
                            
$new_nodes_val .= ",{$node_options[$new_node]}";
                            }
                        
// Build existing value:
                        
foreach($current_field_nodes as $current_field_node)
                            {
                            
$existing_nodes_value .= ",{$node_options[$current_field_node]}";
                            }
                        
$val $new_nodes_val;

                        
log_node_changes($ref,$new_nodes,$removed_nodes);

                        
// 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))
                            {
                            if(
',' == substr($val01))
                                {
                                
$val substr($val1);
                                }

                            
sql_query("UPDATE resource SET field{$fields[$n]['ref']} = '" escape_check(truncate_join_field_value(substr($new_nodes_val1)))."' WHERE ref = '{$ref}'");
                            }
                        }
                    }
                } 
// End of fixed list field section
            
elseif($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"] . " : " $val;
                        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_");