Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
4.0 KiB

  1. package Koha::Template::Plugin::TablesSettings;
  2. # This file is part of Koha.
  3. #
  4. # Copyright BibLibre 2014
  5. #
  6. # Koha is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Koha is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  18. =head1 NAME
  19. Koha::Template::Plugin::TablesSettings
  20. =head2 SYNOPSYS
  21. [% USE TablesSettings %]
  22. . . .
  23. [% UNLESS TablesSettings.is_hidden( 'module', 'page', 'table', 'column') %]
  24. <th id="column" data-colname="column">Column title</th>
  25. [% END %]
  26. . . .
  27. [% UNLESS TablesSettings.is_hidden( 'module', 'page', 'table', 'column') %]
  28. <td>[% row.column %]</td>
  29. [% END %]
  30. . . .
  31. <script>
  32. var columns_settings = [% TablesSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
  33. var table = KohaTable("id", { "bAutoWidth": false }, columns_settings );
  34. </script>
  35. This plugin allows to get the column configuration for a table. It should be used both in table markup
  36. and as the input for datatables visibility settings to take full effect.
  37. =cut
  38. use Modern::Perl;
  39. use Template::Plugin;
  40. use base qw( Template::Plugin );
  41. use YAML qw( LoadFile );
  42. use JSON qw( to_json );
  43. use C4::Context qw( config );
  44. use C4::Utils::DataTables::TablesSettings;
  45. =head1 FUNCTIONS
  46. =head2 GetColumns
  47. <script>
  48. var tables_settings = [% TablesSettings.GetColumns( 'module', 'page', 'table', 'json' ) | $raw %];
  49. var table = KohaTable("id", { "bAutoWidth": false }, tables_settings );
  50. </script>
  51. Used to get the full column settings configuration for datatables, usually requires a format of 'json' to pass into
  52. datatables instantiator.
  53. =cut
  54. =head3 GetColumns
  55. var columns_settings = [% TablesSettings.GetColumns( module, page, table 'json' ) | $raw%]
  56. This method is usually be used to retrieve the columns settings for a DataTable init.
  57. So the 'json' format will be provided and the columns_settings JS var will be
  58. passed as argument of the constructor.
  59. =cut
  60. sub GetColumns {
  61. my ( $self, $module, $page, $table, $format ) = @_;
  62. $format //= q{};
  63. my $columns = C4::Utils::DataTables::TablesSettings::get_columns( $module, $page, $table );
  64. return $format eq 'json'
  65. ? to_json( $columns )
  66. : $columns
  67. }
  68. =head2 is_hidden
  69. [% UNLESS TablesSettings.is_hidden( 'module', 'page', 'table', 'column') %]
  70. <th id="column" data-colname="column">Column title</th>
  71. [% END %]
  72. Used to fetch an individual columns display status so we can fully hide a column in the markup for cases where
  73. it may contain confidential information and should be fully hidden rather than just hidden from display.
  74. =cut
  75. sub is_hidden {
  76. my ( $self, $module, $page, $table, $column_name ) = @_;
  77. my $columns = C4::Utils::DataTables::TablesSettings::get_columns( $module, $page, $table );
  78. foreach my $keys(@$columns){
  79. if($keys->{'columnname'} eq $column_name){
  80. return $keys->{'is_hidden'};
  81. }
  82. }
  83. return 0;
  84. }
  85. =head3 GetTableSettings
  86. [% SET table_settings = GetTableSettings( module, page, table ) %]
  87. This method is used to retrieve the tables settings (like table_settings.default_display_length and
  88. table_settings.default_sort_order).
  89. They can be passed to the DataTable constructor (for iDisplayLength and order parameters)
  90. =cut
  91. sub GetTableSettings {
  92. my ( $self, $module, $page, $table, $format ) = @_;
  93. $format //= q{};
  94. my $settings = C4::Utils::DataTables::TablesSettings::get_table_settings( $module, $page, $table );
  95. return $format eq 'json'
  96. ? to_json( $settings || {} )
  97. : $settings
  98. }
  99. 1;