# # # # fix_duplicate_labels() # # # include "templates.util.base_util"; subroutine(fix_duplicate_labels( node source), ( debug_message("\n\n#### fix_duplicate_field_labels() START \n\n"); # This subroutine checks for any duplicate labels in the given source node and automatically # fixes them if it finds any so that the source node only consists of unique labels. # The subroutine returns is_fixed_labels so that the callee knows when a profile should be saved or not. # We don't save the profile within this subroutine because it is also used in the profile setup where it shouldn't # be saved in this place. # is_fixed_labels is true if duplicate labels have been found and fixed, else false. bool is_fixed_labels = false; bool is_report_fields = (node_name(source) eq "report_fields"); debug_message("#### is_report_fields: " . is_report_fields . "\n"); node item; string label; string expanded_label; string expanded_label_checksum; bool is_unique; int count; string suffix; string draft_label; delete_node("v.label_checksums_lookup"); v.label_checksums_lookup = ""; node label_checksums_lookup = "v.label_checksums_lookup"; foreach item source ( label = @item{"label"}; expanded_label = get_expanded_label(label); # debug_message("\n#### item name: " . node_name(item) . "\n"); # debug_message("#### label: " . label . "\n"); # debug_message("#### expanded_label: " . expanded_label . "\n"); expanded_label_checksum = md5_digest(expanded_label); if (!label_checksums_lookup?{expanded_label_checksum}) then ( # There is no duplicate label in the lookup, # add checksum to label_checksums_lookup label_checksums_lookup{expanded_label_checksum} = true; ) else ( # This label alreay exists, so we create a new label for it. debug_message("#### found duplicate label: " . label . "\n"); is_unique = false; count = 2; while (!is_unique) ( suffix = " (" . count . ")"; draft_label = expanded_label . suffix; expanded_label_checksum = md5_digest(draft_label); if (!label_checksums_lookup?{expanded_label_checksum}) then ( # We got a unique label. # Note, we concat the label with the suffix, not the expanded_label # so that language variables are resolved. label_checksums_lookup{expanded_label_checksum} = true; # Replace existing label in source node item{"label"} = label . suffix; # If this is a report_fields node then use the initial duplicate label # as column_label so that we don't change the field label within reports. if (is_report_fields) then ( item{"column_label"} = label; ); is_unique = true; debug_message("#### new unique label: " . @item{"label"} . "\n\n"); ) else ( # No unique label yet, continue count++; ); ); is_fixed_labels = true; ); ); debug_message("#### is_fixed_labels: " . is_fixed_labels . "\n"); is_fixed_labels; debug_message("\n\n#### fix_duplicate_field_labels() END\n\n"); ));