6aecf52f79a4d530f2d35a85db02ff7609c7f7d5
[koha-ffzg.git] / Koha / Template / Plugin / TablesSettings.pm
1 package Koha::Template::Plugin::TablesSettings;
2
3 # This file is part of Koha.
4 #
5 # Copyright BibLibre 2014
6 #
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.
11 #
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.
16 #
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>.
19
20 =head1 NAME
21
22 Koha::Template::Plugin::TablesSettings
23
24 =head2 SYNOPSYS
25
26     [% USE TablesSettings %]
27
28     . . .
29
30     [% UNLESS TablesSettings.is_hidden( 'module', 'page', 'table', 'column') %]
31         <th id="column" data-colname="column">Column title</th>
32     [% END %]
33
34     . . .
35
36     [% UNLESS TablesSettings.is_hidden( 'module', 'page', 'table', 'column') %]
37         <td>[% row.column %]</td>
38     [% END %]
39
40     . . .
41
42     <script>
43         var columns_settings = [% TablesSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
44         var table = KohaTable("id", { "bAutoWidth": false }, columns_settings );
45     </script>
46
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.
49
50 =cut
51
52 use Modern::Perl;
53
54 use Template::Plugin;
55 use base qw( Template::Plugin );
56
57 use YAML qw( LoadFile );
58 use JSON qw( to_json );
59
60 use C4::Context qw( config );
61 use C4::Utils::DataTables::TablesSettings;
62
63 =head1 FUNCTIONS
64
65 =head2 GetColumns
66
67     <script>
68         var tables_settings = [% TablesSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
69         var table = KohaTable("id", { "bAutoWidth": false }, tables_settings );
70     </script>
71
72 Used to get the full column settings configuration for datatables, usually requires a format of 'json' to pass into
73 datatables instantiator.
74
75 =cut
76
77 =head3 GetColumns
78
79 var columns_settings = [% TablesSettings.GetColumns( module, page, table 'json' ) | $raw%]
80
81 This method is usually be used to retrieve the columns settings for a DataTable init.
82
83 So the 'json' format will be provided and the columns_settings JS var will be
84 passed as argument of the constructor.
85
86 =cut
87
88 sub GetColumns {
89     my ( $self, $module, $page, $table, $format ) = @_;
90     $format //= q{};
91
92     my $columns = C4::Utils::DataTables::TablesSettings::get_columns( $module, $page, $table );
93
94     return $format eq 'json'
95         ? to_json( $columns )
96         : $columns
97 }
98
99 =head2 is_hidden
100
101     [% UNLESS TablesSettings.is_hidden( 'module', 'page', 'table', 'column') %]
102         <th id="column" data-colname="column">Column title</th>
103     [% END %]
104
105 Used to fetch an individual columns display status so we can fully hide a column in the markup for cases where
106 it may contain confidential information and should be fully hidden rather than just hidden from display.
107
108 =cut
109
110 sub is_hidden {
111     my ( $self, $module, $page, $table, $column_name ) = @_;
112     my $columns = C4::Utils::DataTables::TablesSettings::get_columns( $module, $page, $table );
113     foreach my $keys(@$columns){
114         if($keys->{'columnname'} eq $column_name){
115             return $keys->{'is_hidden'};
116         }
117     }
118     return 0;
119 }
120
121 =head3 GetTableSettings
122
123 [% SET table_settings = GetTableSettings( module, page, table ) %]
124
125 This method is used to retrieve the tables settings (like table_settings.default_display_length and
126 table_settings.default_sort_order).
127 They can be passed to the DataTable constructor (for iDisplayLength and order parameters)
128
129 =cut
130
131 sub GetTableSettings {
132     my ( $self, $module, $page, $table, $format ) = @_;
133     $format //= q{};
134
135     my $settings = C4::Utils::DataTables::TablesSettings::get_table_settings( $module, $page, $table );
136
137     return $format eq 'json'
138         ? to_json( $settings || {} )
139         : $settings
140 }
141
142 1;