# # # # snapon_parameters_util.cfv # Utilities to create JSON snapon parameters when attaching # a snapon in Config/Snapons or in the New Profile Wizard. # # # get_snapon_parameter_item_select_options_source() # # get_snapon_parameter_item_custom_fields_dat() # get_snapon_condition_parameters_dat() # get_snapon_parameter_item_dat() # get_snapon_parameters_form_dat() # # get_snapon_parameter_fields_list() # get_snapon_parameter_report_groups_list() # # # # # get_snapon_parameter_item_select_options_source() # # # #subroutine(get_snapon_parameter_item_select_options_source( # node condition), ( # # string select_options_source = ""; # # if (?(condition . ".parameter_info.select_options_source")) then ( # select_options_source = @(condition . ".parameter_info.select_options_source"); # ); # # select_options_source; #)); # # # # get_snapon_parameter_item_custom_fields_dat() # # # subroutine(get_snapon_parameter_item_custom_fields_dat( node parameter_item), ( string fields_dat = "["; if (parameter_item?{"select_options"}) then ( string item_dat; node item; node select_options = parameter_item{"select_options"}; string option_value; string label; foreach item select_options ( option_value = @item{"option_value"}; label = get_expanded_label(@item{"label"}); item_dat = "{"; item_dat .= add_json("label", label, "string"); item_dat .= add_json("name", option_value, "string"); item_dat = close_json(item_dat); fields_dat .= item_dat . ","; ); ); if (fields_dat ne "[") then ( fields_dat = replace_last(fields_dat, ",", "]"); ) else ( fields_dat .= "]"; ); # Return fields_dat; )); # # # # get_snapon_condition_parameters_dat() # # # subroutine(get_snapon_condition_parameters_dat( node the_parameters, string condition), ( # Returns an array with parameters which are used in the condition expression. # Add parameter name and form_element_type for quick lookup string condition_parameters_dat = "["; node item; string parameter_name; string form_element_type; string item_dat; foreach item the_parameters ( parameter_name = node_name(item); if (matches_regular_expression(condition, parameter_name)) then ( # parameter is used in condition expression, add it. form_element_type = @item{"form_element_type"}; item_dat = "{"; item_dat .= add_json("name", parameter_name, "string"); item_dat .= add_json("formElementType", form_element_type, "string"); item_dat = close_json(item_dat); condition_parameters_dat .= item_dat . ','; ); ); if (condition_parameters_dat ne "[") then ( condition_parameters_dat = replace_last(condition_parameters_dat, ",", "]"); ) else ( # This case shouldn't be possible because there is no parameter in the condition, # just close the array. condition_parameters_dat .= "]"; ); # Return condition_parameters_dat; )); # # # # get_snapon_parameter_item_dat() # # # subroutine(get_snapon_parameter_item_dat( node the_parameters, node requires_items, string parameter_item_name), ( debug_message("\n#### get_snapon_parameter_item_dat() \n"); node item = the_parameters{parameter_item_name}; debug_message("\n" . node_as_string(item) . "\n"); # string type = @item{"type"}; string parameter_value = if (item?{"parameter_value"}) then (@item{"parameter_value"}) else (""); string validation_type = if (item?{"validation_type"}) then (@item{"validation_type"}) else (""); # KHP 18/May/2011, we must expand every parameter_value, not just labels, # however, we use get_expanded_label in all cases assuming that the parameter_value # is an expression or language variable. #* if (validation_type eq "field_label" or validation_type eq "report_label" or validation_type eq "report_group_label" or validation_type eq "snapon_label") then ( parameter_value = get_expanded_label(parameter_value); ); *# parameter_value = get_expanded_label(parameter_value); string form_element_label; if (item?{"form_element_label"} and (@item{"form_element_label"} ne "")) then ( form_element_label = get_expanded_label(@item{"form_element_label"}); ) else ( form_element_label = parameter_item_name; ); string form_element_type = if(item?{"form_element_type"} and @item{"form_element_type"} ne "") then (@item{"form_element_type"}) else ("text"); string description = if(item?{"description"}) then (get_expanded_label(@item{"description"})) else (""); string dat = "{"; dat .= add_json("name", parameter_item_name, "string"); dat .= add_json("parameterValue", parameter_value, "string"); dat .= add_json("validationType", validation_type, "string"); dat .= add_json("formElementLabel", form_element_label, "string"); dat .= add_json("formElementType", form_element_type, "string"); dat .= add_json("description", description, "string"); # # Parameter options depending on validation_type # if (validation_type eq "int" or validation_type eq "float") then ( if (item?{"min"} and (@item{"min"} ne "")) then ( dat .= add_json("min", @item{"min"}, "float"); ); if (item?{"max"} and (@item{"max"} ne "")) then ( dat .= add_json("max", @item{"max"}, "float"); ); ) else if (validation_type eq "field_label") then ( node validate_field_label_for = item{"validate_field_label_for"}; bool validate_field_label_for_is_defined = false; if (validate_field_label_for?{"log_fields"}) then ( @requires_items{"log_fields"} = true; dat .= add_json("isLogFieldLabel", true, "bool"); validate_field_label_for_is_defined = true; ); if (validate_field_label_for?{"database_fields"}) then ( @requires_items{"database_fields"} = true; dat .= add_json("isDatabaseFieldLabel", true, "bool"); validate_field_label_for_is_defined = true; ); if (validate_field_label_for?{"report_fields"}) then ( @requires_items{"report_fields"} = true; dat .= add_json("isReportFieldLabel", true, "bool"); validate_field_label_for_is_defined = true; ); if (!validate_field_label_for_is_defined) then ( # Throw error because validate_field_label_for is undefined. error(lang_admin.snapons.missing_validate_field_label_for_message); ); ) else if (validation_type eq "report_label") then ( @requires_items{"reports"} = true; ) else if (validation_type eq "report_group_label") then ( @requires_items{"report_groups"} = true; ); # # Parameter options depending on form_element_type # if (form_element_type eq "text") then ( int form_element_width = if (item?{"form_element_width"} and @item{"form_element_width"} ne "") then (@item{"form_element_width"}) else (240); dat .= add_json("formElementWidth", form_element_width, "int"); ) else if (form_element_type eq "select") then ( string select_options_source = @item{"select_options_source"}; dat .= add_json("selectOptionsSource", select_options_source, "string"); if (select_options_source ne "custom_fields") then ( # Track the select_options_source in requires_items so that we know # which item name and labels we have to collect. @requires_items{select_options_source} = true; ) else ( # Assemble custom fields string custom_fields_dat = get_snapon_parameter_item_custom_fields_dat(item); dat .= add_json("customFields", custom_fields_dat, "obj"); ); ); # Add condition, if any if (item?{"condition"} and @item{"condition"} ne "") then ( string condition = @item{"condition"}; # Get condition_parameters, these are the parameters used in the condition expression. string condition_parameters = get_snapon_condition_parameters_dat(the_parameters, condition); dat .= add_json("condition", condition, "string"); dat .= add_json("conditionParameters", condition_parameters, "obj"); ); dat = close_json(dat); # Return dat; debug_message("#### get_snapon_parameter_item_dat END \n"); )); # # # # get_snapon_parameters_form_dat() # # # subroutine(get_snapon_parameters_form_dat( string snapon_id, # a unique ID per snapon, i.e. "i0" node the_parameters, node the_parameters_form, node requires_items), ( debug_message("\n#### get_snapon_parameters_form_dat START \n"); # the_parameters is a node reference to the snapon parameters # or the log format plug-in snapon parameters or a new node which # combines the snapon and log format plug-in parameters. # the_parameters_form is a node reference to the snapon paramaters_form # or the log format plug-in paramaters_form. # Set requires_items default values @requires_items{"log_fields"} = false; @requires_items{"database_fields"} = false; @requires_items{"report_fields"} = false; @requires_items{"reports"} = false; @requires_items{"report_groups"} = false; string parameters_form_dat = "["; node group; node item; # string label; string description; node group_parameters; string group_parameters_dat; string group_dat; string parameter_item_name; node parameter_item; foreach group the_parameters_form ( # label = get_expanded_label(@group{"label"}); description = get_expanded_label(@group{"description"}); group_parameters = group{"parameters"}; group_parameters_dat = "["; foreach item group_parameters ( # debug_message("\n\n#### item value: " . @item . "\n"); if (@item) then ( parameter_item_name = node_name(item); parameter_item = the_parameters{parameter_item_name}; string s = get_snapon_parameter_item_dat(the_parameters, requires_items, parameter_item_name); debug_message("#### #### s: " . s . "\n"); group_parameters_dat .= s . ","; ); ); group_parameters_dat = replace_last(group_parameters_dat, ",", "]"); group_dat = "{"; # group_dat .= add_json("label", label, "string"); group_dat .= add_json("snaponId", snapon_id, "string"); # A unique ID per snapon, used to distingusih multiple snapons in new profile wizard group_dat .= add_json("elementIdPrefix", "", "string"); # Unique HTML element ID prefix per group, generated on client side group_dat .= add_json("description", description, "string"); group_dat .= add_json("parameters", group_parameters_dat, "obj"); group_dat = close_json(group_dat); parameters_form_dat .= group_dat . ","; ); parameters_form_dat = replace_last(parameters_form_dat, ",", "]"); parameters_form_dat; debug_message("#### get_snapon_parameters_form_dat END \n"); )); # # # # get_snapon_parameter_fields_list() # # # subroutine(get_snapon_parameter_fields_list( node fields), ( debug_message("#### get_snapon_parameter_fields_list()\n"); debug_message("#### fields: " . fields . "\n"); debug_message("#### sorting fields start \n"); # sort the fields sort(fields, "field:label,alphabetical,ascending", true); debug_message("#### sorting fields done \n"); string fields_dat = "["; string item_dat; node item; string label; foreach item fields ( debug_message("#### field item node_name: " . node_name(item) . "\n"); label = get_expanded_label(@item{"label"}); item_dat = "{"; item_dat .= add_json("name", node_name(item), "string"); item_dat .= add_json("label", label, "string"); item_dat = close_json(item_dat); fields_dat .= item_dat . ","; ); if (fields_dat ne "[") then ( fields_dat = replace_last(fields_dat, ",", "]"); ) else ( fields_dat .= "]"; ); # Return fields_dat; )); # # # # get_snapon_parameter_report_groups_list() # # # subroutine(get_snapon_parameter_report_groups_list( node profile), ( string report_groups_dat = "["; node reports_menu = profile . ".statistics.reports_menu"; node item; string item_dat; string label; foreach item reports_menu ( if (item?{"items"}) then ( # This must be a report group, add it to report_groups_dat. label = get_expanded_label(@item{"label"}); item_dat = "{"; item_dat .= add_json("name", node_name(item), "string"); item_dat .= add_json("label", label, "string"); item_dat = close_json(item_dat); report_groups_dat .= item_dat . ","; ); ); if (report_groups_dat ne "[") then ( report_groups_dat = replace_last(report_groups_dat, ",", "]"); ) else ( report_groups_dat .= "]"; ); report_groups_dat; ));