$templates.shared.doctype
{= expand(templates.shared.content_type); =}
{=
include "templates.shared.util.convert_to_valid_node_name";
include "templates.shared.util.get_unique_node_name";
include "templates.shared.util.delete_cached_profile_files";
debug_message("\n\n\n" . node_as_string("volatile.form_processing") . "\n\n\n");
string profile_name = internal.profile_name;
string form_type = volatile.form_processing.form_type;
string report_name = volatile.form_processing.report_name; # the original report node name if in edit mode
string report_label = volatile.form_processing.report_label;
string new_report_name;
node reports_node = "profiles." . profile_name . ".statistics.reports";
# Delete original report if in edit mode & preserve original report and report element labels
if (form_type eq "edit") then (
# Note, we don't change node names when editing reports, it would be easy to update the reports menu
# and scheduler with a new node name, though we can't update report node names used in cronjobs or elsewhere.
new_report_name = report_name;
if (node_exists(reports_node . "." . report_name)) then (
# preserve the original report and report element labels ($lang_stats, etc.)
clone_node(reports_node . "." . report_name . ".label", "volatile.temp_preserved_labels.0");
node ori_report_element;
node ori_report_elements = reports_node . "." . report_name . ".report_elements";
string clone_destination;
foreach ori_report_element ori_report_elements (
clone_destination = "volatile.temp_preserved_labels." . num_subnodes("volatile.temp_preserved_labels");
clone_node(subnode_by_name(ori_report_element, "label"), clone_destination);
);
# debug_message("\n\n\n@@@@ preserved report and report element labels: " . node_as_string("volatile.temp_preserved_labels") . "\n\n\n");
# delete the original report
delete_node(reports_node . "." . report_name);
# save_changes(); SAVE CHANGES ONLY AT THE END IF FORM PROCESSING SUCCEEDS!
);
)
else (
# this is a new report, get a new unique report node name
new_report_name = convert_to_valid_node_name(report_label);
new_report_name = get_unique_node_name(reports_node, new_report_name);
);
# --------------------
# Sort report elements
# --------------------
sort("volatile.form_processing.report_elements", "field:position,integer,ascending");
# remove the position node after sorting
node report_element;
foreach report_element "volatile.form_processing.report_elements" (
if (node_exists(report_element . ".position")) then (
# delete_node(subnode_by_name(report_element, "position")); # SAWMILL CRASHES when using subnode_by_name!
delete_node(report_element . ".position");
);
);
# --------------------
# Save new report data
# --------------------
string new_report_path = reports_node . "." . new_report_name;
new_report_path . ".label" = report_label;
new_report_path . ".show_header_bar" = volatile.form_processing.show_report_header_bar;
# handle report_filter
if (node_exists("volatile.form_processing.report_filter")) then (
string report_filter = volatile.form_processing.report_filter;
new_report_path . ".filter.expression" = report_filter;
);
# handle report header and report footer
if (node_value("volatile.form_processing.report_header") ne "") then (
new_report_path . ".header" = node_value("volatile.form_processing.report_header");
);
if (node_value("volatile.form_processing.report_footer") ne "") then (
new_report_path . ".footer" = node_value("volatile.form_processing.report_footer");
);
node report_element;
string report_element_type;
string clone_destination_path;
node column;
node columns_node;
string field_name;
string header_label;
bool is_session;
int count = 0;
foreach report_element "volatile.form_processing.report_elements" (
report_element_type = node_value(subnode_by_name(report_element, "type"));
clone_destination_path = new_report_path . ".report_elements." . count;
clone_node(report_element, clone_destination_path);
# ---------------------------------------
# maintain columns sort and column labels
# ---------------------------------------
if (node_exists(clone_destination_path . ".columns")) then (
is_session = false;
if (report_element_type ne "table" and report_element_type ne "log_detail") then (
is_session = true;
);
columns_node = clone_destination_path . ".columns";
foreach column columns_node (
field_name = node_value(subnode_by_name(column, "field_name"));
if (!is_session) then (
header_label = "{=capitalize(database.fields." . field_name . ".label)=}";
)
else (
header_label = "$lang_stats.field_labels." . field_name;
);
column . ".header_label" = header_label;
);
# ----------------
# Sort the columns
# ----------------
sort(columns_node,"field:position,integer,ascending");
# delete the columns position node which is only used for sorting
foreach column columns_node (
if (node_exists(column . ".position")) then (
delete_node(column . ".position");
);
);
);
count++;
);
# -----------------------------------
# Set preserved labels if appropriate
# -----------------------------------
if (form_type eq "edit") then (
# check the report label
if (node_value(new_report_path . ".label") eq expand(node_value("volatile.temp_preserved_labels.0"))) then (
# use preserved report label
new_report_path . ".label" = volatile.temp_preserved_labels.0;
);
# check the report element labels
node report_element;
string report_element_label;
node report_elements = new_report_path . ".report_elements";
node preserved_label;
foreach report_element report_elements (
report_element_label = node_value(subnode_by_name(report_element, "label"));
# Note, we don't know which report element had which label before,
# so we simply loop trough all existing preserved labels and if
# there is a match we use the preserved label
foreach preserved_label "volatile.temp_preserved_labels" (
if (report_element_label eq expand(node_value(preserved_label))) then (
# use preserved report element label
report_element . ".label" = node_value(preserved_label);
last;
);
);
);
);
# ------------------------------
# Add reports menu if new report
# ------------------------------
if (form_type eq "new" and node_exists("volatile.form_processing.reports_menu")) then (
node reports_menu_node = "profiles." . profile_name . ".statistics.reports_menu";
string menu_label = volatile.form_processing.reports_menu.reports_menu_label;
bool visible = volatile.form_processing.reports_menu.visible;
bool visible_if_files = volatile.form_processing.reports_menu.visible_if_files;
string new_menu_name = convert_to_valid_node_name(menu_label);
# get unique reports menu node name
# Note, the reports menu node name must be unique accross the root node including all items nodes (submenus)
# because the reports menu javascript uses the node names as element ids.
# Hence create a temp node to lookup existing nodes names
if (node_exists("volatile.temp_menu_node_name_lookup")) then (
delete_node("volatile.temp_menu_node_name_lookup");
);
node menu_item;
node submenu_item;
node items_node;
foreach menu_item reports_menu_node (
"volatile.temp_menu_node_name_lookup." . node_name(menu_item) = true;
if (subnode_exists(menu_item, "items") and (num_subnodes(subnode_by_name(menu_item, "items")) > 0 )) then (
items_node = menu_item . ".items";
foreach submenu_item items_node (
"volatile.temp_menu_node_name_lookup." . node_name(submenu_item) = true;
);
);
);
new_menu_name = get_unique_node_name("volatile.temp_menu_node_name_lookup", new_menu_name);
if (node_exists("volatile.form_processing.reports_menu.menu_group_name")) then (
# -------------------------------
# add menu item to existing group
# -------------------------------
# modify destination node
string menu_group_name = volatile.form_processing.reports_menu.menu_group_name;
reports_menu_node = reports_menu_node . "." . menu_group_name . ".items";
);
reports_menu_node . "." . new_menu_name . ".type" = "view";
reports_menu_node . "." . new_menu_name . ".label" = menu_label;
reports_menu_node . "." . new_menu_name . ".view_name" = new_report_name;
reports_menu_node . "." . new_menu_name . ".visible" = visible;
reports_menu_node . "." . new_menu_name . ".visible_if_files" = visible_if_files;
);
save_changes();
# ------------------
# delete ReportCache
# ------------------
delete_cached_profile_files(profile_name);
=}