Bug 24156: DBIC changes
[koha.git] / Koha / Template / Plugin / ColumnsSettings.pm
blob50c2131eceb0c96f6eb1e600373f439e1502ff3d
1 package Koha::Template::Plugin::ColumnsSettings;
3 # This file is part of Koha.
5 # Copyright BibLibre 2014
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 NAME
22 Koha::Template::Plugin::ColumnsSettings
24 =head2 SYNOPSYS
26 [% USE ColumnsSettings %]
28 . . .
30 [% UNLESS ColumnsSettings.is_hidden( 'module', 'page', 'table', 'column') %]
31 <th id="column" data-colname="column">Column title</th>
32 [% END %]
34 . . .
36 [% UNLESS ColumnsSettings.is_hidden( 'module', 'page', 'table', 'column') %]
37 <td>[% row.column %]</td>
38 [% END %]
40 . . .
42 <script>
43 var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
44 var table = KohaTable("id", { "bAutoWidth": false }, columns_settings );
45 </script>
47 This plugin allows to get the column configuration for a table. It should be used both in table markup
48 and as the input for datatables visibility settings to take full effect.
50 =cut
52 use Modern::Perl;
54 use Template::Plugin;
55 use base qw( Template::Plugin );
57 use YAML qw( LoadFile );
58 use JSON qw( to_json );
60 use C4::Context qw( config );
61 use C4::Utils::DataTables::ColumnsSettings;
63 =head1 FUNCTIONS
65 =head2 GetColumns
67 <script>
68 var columns_settings = [% ColumnsSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
69 var table = KohaTable("id", { "bAutoWidth": false }, columns_settings );
70 </script>
72 Used to get the full column settings configuration for datatables, usually requires a format of 'json' to pass into
73 datatables instantiator.
75 =cut
77 sub GetColumns {
78 my ( $self, $module, $page, $table, $format ) = @_;
79 $format //= q{};
81 my $columns = C4::Utils::DataTables::ColumnsSettings::get_columns( $module, $page, $table );
83 return $format eq 'json'
84 ? to_json( $columns )
85 : $columns
88 =head2 is_hidden
90 [% UNLESS ColumnsSettings.is_hidden( 'module', 'page', 'table', 'column') %]
91 <th id="column" data-colname="column">Column title</th>
92 [% END %]
94 Used to fetch an individual columns display status so we can fully hide a column in the markup for cases where
95 it may contain confidential information and should be fully hidden rather than just hidden from display.
97 =cut
99 sub is_hidden {
100 my ( $self, $module, $page, $table, $column_name ) = @_;
101 my $columns = C4::Utils::DataTables::ColumnsSettings::get_columns( $module, $page, $table );
102 foreach my $keys(@$columns){
103 if($keys->{'columnname'} eq $column_name){
104 return $keys->{'is_hidden'};
107 return 0;