Skip to content

Commit 91a77ca

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
fiix combined status page excel export
- when calling checkall and cols blocks, take care when changing the prefix. template toolkit does not have scoping when calling other template blocks. the code was appending 'host' and 'service' twice and not deleting it. select / deslect all buttons did not work properly - stop removing the prefix from the permanent link. it was causing problems in the combined export, where some inputs had to be prefixed with 'host' and other had to be prefixed with 'service' - when determining selected host/service columns prefixes correctly, and set extra_hst/service_columns using grep. Previous regex was converting the array to a string representation. It was not looking for 'contacts' string inside the array? I am unsure.
1 parent e280767 commit 91a77ca

2 files changed

Lines changed: 50 additions & 19 deletions

File tree

lib/Thruk/Controller/status.pm

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,18 @@ sub _process_combined_page {
11881188
my $view_mode = $c->req->parameters->{'view_mode'} || 'html';
11891189

11901190
my $user_data = Thruk::Utils::get_user_data($c);
1191-
my $selected_hst_columns = $c->req->parameters->{'hst_columns'} || $user_data->{'columns'}->{'hst'} || $c->config->{'default_host_columns'};
1192-
my $selected_svc_columns = $c->req->parameters->{'svc_columns'} || $user_data->{'columns'}->{'svc'} || $c->config->{'default_service_columns'};
1191+
1192+
Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$c->req->parameters");
1193+
Thruk::Utils::Log::_info($c->req->parameters);
1194+
1195+
my $selected_hst_columns = $c->req->parameters->{'host_columns'} || $c->req->parameters->{'hst_columns'} || $user_data->{'columns'}->{'hst'} || $c->config->{'default_host_columns'};
1196+
Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$selected_hst_columns");
1197+
Thruk::Utils::Log::_info($selected_hst_columns);
1198+
1199+
my $selected_svc_columns = $c->req->parameters->{'service_columns'} || $c->req->parameters->{'svc_columns'} || $user_data->{'columns'}->{'svc'} || $c->config->{'default_service_columns'};
1200+
Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$selected_svc_columns");
1201+
Thruk::Utils::Log::_info($selected_svc_columns);
1202+
11931203
$c->stash->{'show_host_attempts'} = defined $c->config->{'show_host_attempts'} ? $c->config->{'show_host_attempts'} : 1;
11941204
$c->stash->{'default_columns'}->{'hst_'} = Thruk::Utils::Status::get_host_columns($c);
11951205
$c->stash->{'default_columns'}->{'svc_'} = Thruk::Utils::Status::get_service_columns($c);
@@ -1254,7 +1264,9 @@ sub _process_combined_page {
12541264
push @{$extra_svc_columns}, 'long_plugin_output';
12551265
push @{$extra_hst_columns}, 'long_plugin_output';
12561266
}
1257-
push @{$extra_svc_columns}, 'contacts' if ($selected_svc_columns && $selected_svc_columns =~ m/contacts/imx);
1267+
push @{$extra_svc_columns}, 'contacts' if (ref $selected_svc_columns eq 'ARRAY' && grep { /^contacts$/i } @{$selected_svc_columns});
1268+
Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$extra_svc_columns");
1269+
Thruk::Utils::Log::_info($extra_svc_columns);
12581270

12591271
my $services = $c->db->get_services( filter => [ Thruk::Utils::Auth::get_auth_filter( $c, 'services' ), $servicefilter ],
12601272
sort => { $order => $sortoptions->{$sortoption}->[0] },
@@ -1294,7 +1306,9 @@ sub _process_combined_page {
12941306
$sortoption = 1 if !defined $sortoptions->{$sortoption};
12951307
$c->stash->{'hst_orderby'} = $sortoptions->{$sortoption}->[1];
12961308
$c->stash->{'hst_orderdir'} = $order;
1297-
push @{$extra_hst_columns}, 'contacts' if ($selected_hst_columns && $selected_hst_columns =~ m/contacts/imx);
1309+
push @{$extra_hst_columns}, 'contacts' if (ref $selected_hst_columns eq 'ARRAY' && grep { /^contacts$/i } @{$selected_hst_columns});
1310+
Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$extra_hst_columns");
1311+
Thruk::Utils::Log::_info($extra_hst_columns);
12981312

12991313
my $hosts = $c->db->get_hosts( filter => [ Thruk::Utils::Auth::get_auth_filter( $c, 'hosts' ), $hostfilter ],
13001314
sort => { $order => $sortoptions->{$sortoption}->[0] },
@@ -1303,11 +1317,17 @@ sub _process_combined_page {
13031317
$c->stash->{'hosts'} = $hosts;
13041318
if( $sortoption == 6 and defined $hosts ) { @{ $c->stash->{'hosts'} } = reverse @{ $c->stash->{'hosts'} }; }
13051319

1320+
# Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$c->stash->{'hosts'}");
1321+
# Thruk::Utils::Log::_info($c->stash->{'hosts'});
1322+
1323+
# Thruk::Utils::Log::_info("lib::Thruk::Controller | status.pm | _process_combined_page | \$c->stash->{'services'}");
1324+
# Thruk::Utils::Log::_info($c->stash->{'services'});
1325+
13061326
$c->stash->{'hosts_limit_hit'} = 0;
13071327
$c->stash->{'services_limit_hit'} = 0;
13081328

13091329
if( $view_mode eq 'xls' ) {
1310-
Thruk::Utils::Status::set_selected_columns($c, ['status_combined_excel_host_','status_combined_excel_service_']);
1330+
Thruk::Utils::Status::set_selected_columns($c, ['host_', 'service_']);
13111331
Thruk::Utils::Status::set_comments_and_downtimes($c);
13121332
$c->res->headers->header( 'Content-Disposition', 'attachment; filename="status.xls"' );
13131333
$c->stash->{'hosts'} = $hosts;

templates/_excel_export.tt

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1-
[%# Variables for this template %]
2-
[%# prefix:
3-
Used as a prefix for the <input> IDs in the form. Make sure its unique on the rendered page, otherwise the (de)selectAll function does not work properly due to collisions with other inputs.
1+
[%# This template is used to open up a popup menu and select columns for excel export. %]
2+
3+
[%# Variables %]
4+
[%# prefix: Used as a prefix for the <input> IDs in the form. Make sure its unique on the rendered page, otherwise the (de)selectAll function does not work properly due to collisions with other inputs.
45
%]
6+
<!-- _excel_export.tt | prefix value is: [% prefix %]' -->
7+
8+
59
[% BLOCK cols %]
10+
<!-- _excel_export.tt BLOCK cols | prefix value is: '[% prefix %]' -->
11+
<!-- _excel_export.tt BLOCK cols | columns value is: [[% columns.join(',') %]] -->
612
<table class="sortable_col_table rowhover">
713
[%- counter = 0 -%]
814
[%- FOR column IN columns -%]
915
[%- counter = counter + 1 -%]
1016
[%- meta = column.split(':'); column = meta.0 -%]
1117
[%- IF meta.size == 1; meta.1 = 1; END %]
1218
<tr class="sortable_row">
13-
<td class="px-2 py-0.5"><input type="checkbox" value="[% column | html %]" id="[% prefix %]ht[% counter %]" name="[% prefix %]columns" onclick=" updateExcelPermanentLink('[% prefix %]');"[% IF meta.1 == 1 %] checked[% END %]></td>
14-
<td class='clickable pr-2' id="[% prefix %]ht[% counter %]n" onclick="toggleFilterCheckBox(this.id); updateExcelPermanentLink('[% prefix %]');" width="90%">[% column | html %]</td>
19+
<td class="px-2 py-0.5"><input type="checkbox" value="[% column | html %]" id="[% prefix %]ht[% counter %]" name="[% prefix %]columns" onclick=" updateExcelPermanentLink();"[% IF meta.1 == 1 %] checked[% END %]></td>
20+
<td class='clickable pr-2' id="[% prefix %]ht[% counter %]n" onclick="toggleFilterCheckBox(this.id); updateExcelPermanentLink();" width="90%">[% column | html %]</td>
1521
</tr>
1622
[% END -%]
1723
[%- FOREACH v IN get_exposed_custom_vars(1) %]
1824
[%- counter = counter + 1 -%]
1925
<tr class="sortable_row">
20-
<td class="px-2 py-0.5"><input type="checkbox" value="[% v | html %]" id="[% prefix %]ht[% counter %]" name="[% prefix %]columns" onclick=" updateExcelPermanentLink('[% prefix %]');"></td>
21-
<td class='clickable pr-2' id="[% prefix %]ht[% counter %]n" onclick="toggleFilterCheckBox(this.id); updateExcelPermanentLink('[% prefix %]');" width="90%">[% v | html %]</td>
26+
<td class="px-2 py-0.5"><input type="checkbox" value="[% v | html %]" id="[% prefix %]ht[% counter %]" name="[% prefix %]columns" onclick=" updateExcelPermanentLink();"></td>
27+
<td class='clickable pr-2' id="[% prefix %]ht[% counter %]n" onclick="toggleFilterCheckBox(this.id); updateExcelPermanentLink();" width="90%">[% v | html %]</td>
2228
</tr>
2329
[% END %]
2430
</table>
2531
[% END %]
2632
[% BLOCK checkall %]
33+
<!-- _excel_export.tt BLOCK checkall | prefix value is: [% prefix %]' -->
2734
<table class="rowhover">
2835
<tr>
29-
<td class="px-2 py-0.5"><input type="checkbox" id="[% prefix %]ht0" value="" name="all_col" onclick="toggleAllFilterCheckBox('[% prefix %]'); updateExcelPermanentLink('[% prefix %]');"></td>
30-
<td class='clickable font-bold' id="[% prefix %]ht0n" onclick="toggleFilterCheckBox(this.id); toggleAllFilterCheckBox('[% prefix %]'); updateExcelPermanentLink('[% prefix %]');" width="90%">Select all</td>
36+
<td class="px-2 py-0.5"><input type="checkbox" id="[% prefix %]ht0" value="" name="all_col" onclick="toggleAllFilterCheckBox('[% prefix %]'); updateExcelPermanentLink();"></td>
37+
<td class='clickable font-bold' id="[% prefix %]ht0n" onclick="toggleFilterCheckBox(this.id); toggleAllFilterCheckBox('[% prefix %]'); updateExcelPermanentLink();" width="90%">Select all</td>
3138
</tr>
3239
</table>
3340
[% END %]
3441

3542
<div class="flex relative">
36-
<a href="[% uri_with(c, 'view_mode' => 'xls') %]" class="button round small" onclick="toggleElement('excel_export_columns', undefined, true); updateExcelPermanentLink('[% prefix %]'); return false;" id='excelexportlink' title="Export to Excel">
43+
<a href="[% uri_with(c, 'view_mode' => 'xls') %]" class="button round small" onclick="toggleElement('excel_export_columns', undefined, true); updateExcelPermanentLink(); return false;" id='excelexportlink' title="Export to Excel">
3744
<i class="uil uil-table"></i>
3845
</a>
3946
<div class="card shadow-float absolute z-20 top-7" style="display: none;" id="excel_export_columns">
@@ -65,13 +72,17 @@ Used as a prefix for the <input> IDs in the form. Make sure its unique on the re
6572
<tr>
6673
<td valign="top">
6774
<div class="px-2 font-semibold">Hosts:</div>
68-
[% PROCESS checkall prefix='host_' %]
69-
[% PROCESS cols prefix=(prefix _ 'host_') columns=hostcolumns %]
75+
[%# Variables in template toolkit are global and not scoped. Adding the _host suffix once is enough #%]
76+
[% PROCESS checkall prefix=(prefix _ 'host_') %]
77+
[% PROCESS cols prefix=prefix columns=hostcolumns %]
78+
[%# Restore the original prefix #%]
79+
[%- prefix = prefix.substr(0,prefix.length - 5) -%]
7080
</td>
7181
<td valign="top">
7282
<div class="px-2 font-semibold">Services:</div>
73-
[% PROCESS checkall prefix='service_' %]
74-
[% PROCESS cols prefix=(prefix _ 'service_') columns=servicecolumns %]
83+
[% PROCESS checkall prefix=(prefix _ 'service_') %]
84+
[% PROCESS cols prefix=prefix columns=servicecolumns %]
85+
[%- prefix = prefix.substr(0,prefix.length - 8) -%]
7586
</td>
7687
</tr>
7788
[% END %]

0 commit comments

Comments
 (0)