Collections functions
General functions
Render functions
Theme permission functions
Resource functions

save_resource_data_multi()

Parameters

ColumnTypeDefaultDescription
$collection
$editsearch array

Location

include/resource_functions.php lines 1275 to 2199

Definition

 
function save_resource_data_multi($collection,$editsearch = array())
    {
    global 
$FIXED_LIST_FIELD_TYPES,$DATE_FIELD_TYPES$range_separator$date_validator$edit_contributed_by$TEXT_FIELD_TYPES$userref$lang$multilingual_text_fields$languages$language;

    
# 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());

    
// Arrays of nodes to add/ remove from all resources
    
$all_nodes_to_add        = [];
    
$all_nodes_to_remove     = [];
    
// Nodes to add/remove for specific resources (resource as key)
    
$resource_nodes_remove   = [];
    
$resource_nodes_add      = [];
    
// Other changes to make
    
$nodes_check_delete      = [];
    
$resource_log_updates    = [];
    
$resource_update_sql_arr = [];
    
$resource_update_params  = [];

    
$successfully_edited_resources = [];

    
$fields array_values(array_filter($fields,function($field){
        return 
getval('editthis_field_' $field['ref'], '') != '' || hook('save_resource_data_multi_field_decision''', array($field['ref']));
        }));

    
// Get all existing nodes for the edited resources
    
$existing_nodes get_resource_nodes_batch($list,array_column($fields,"ref"));
    
$joins get_resource_table_joins();

    for (
$n=0;$n<count($fields);$n++)
        {
        
$nodes_to_add       = [];
        
$nodes_to_remove    = [];
        
$oldnodenames       = [];

        
// Append option(s) mode?
        
$mode getval("modeselect_" $fields[$n]["ref"],"");
        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);

            if (
$mode=="AP")
                {
                
$nodes_to_add $ui_selected_node_values;
                
$all_nodes_to_add    array_merge($all_nodes_to_add,$nodes_to_add);
                }
            elseif (
$mode=="RM")
                {
                
// Remove option(s) mode
                
$nodes_to_remove $ui_selected_node_values;
                
$all_nodes_to_remove array_merge($all_nodes_to_remove,$nodes_to_remove);
                
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);
                }

            if(
$fields[$n]["required"] == && count($nodes_to_add) == 0)
                {
                
// Required field and no value now set, revert to existing and add to array of failed edits
                
if(!isset($errors[$fields[$n]["ref"]]))
                    {
                    
$errors[$fields[$n]["ref"]]=$lang["requiredfield"] . ". " $lang["error_batch_edit_resources"] . ": " ;
                    }
                
$errors[$fields[$n]["ref"]] .=  implode(","$list);
                
$nodes_to_remove = [];
                continue;
                }

            
// Loop through all the resources and check current node values so we can check if we need to log this as a change
            
for ($m=0;$m<count($list);$m++)
                {
                
$ref            $list[$m];
                
$value_changed  false;

                
$current_field_nodes $existing_nodes[$ref][$fields[$n]['ref']] ?? [];
                
debug('Current nodes for resource #' $ref ' : ' implode(',',$current_field_nodes));

                
$added_nodes array_diff($nodes_to_add,$current_field_nodes);
                
debug('Adding nodes to resource #' $ref ' : ' implode(',',$added_nodes));

                
$removed_nodes array_intersect($nodes_to_remove,$current_field_nodes);
                
debug('Removed nodes from resource #' $ref ' : ' 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)
                    {
                    
$existing_nodes_value '';
                    
$new_nodes_val        '';
                    
$log_nodes_old = [];
                    
$log_nodes_new = [];

                    
$successfully_edited_resources[] = $ref;

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

                    
$resource_log_updates[$ref][] = [
                        
'ref'   => $ref,
                        
'type'  => LOG_CODE_EDITED,
                        
'field' => $fields[$n]["ref"],
                        
'notes' => '',
                        
'from'  => implode(NODE_NAME_STRING_SEPARATOR$log_nodes_old),
                        
'to'    => implode(NODE_NAME_STRING_SEPARATOR$log_nodes_new),
                        ];

                    
// If this is a 'joined' field it still needs to add it to the resource column
                    
if(in_array($fields[$n]['ref'], $joins))
                        {
                        
$resource_update_sql_arr[$ref][] = "field" . (int)$fields[$n]["ref"] . " = ?";
                        
$resource_update_params[$ref][]="s";$resource_update_params[$ref][]=$new_nodes_val;
                        }
                    }
                }
            } 
// 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=getval("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"] . " : " $rangeregex;
                    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