From 074b94cedfccacc366170f99dfe5301b218d0340 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 5 Dec 2019 17:55:00 +0100 Subject: [PATCH] Bug 24156: Make sort order and number of items to display configurable (basket page) This patch is the main patch of this patchset, you will find the description and the test plan. The idea of this new enhancement is to add the ability to define the default sort order and the default number of rows displayed on the acquisition basket page. The existing "columns settings" feature was replaced by a "tables settings" feature. To prepare the ground, there were some works that were needed: * rename variables and files * Modify the structure of the yml files * Create a new DB table to store the tables settings Test plan: 0) a. Execute the update DB entry to create the new table b. Restart all (to get a new version of the yml file, that is cached by memcached) c. Create several orders for a given basket 1) Go to the basket view page => The default values are the same than without this patchset, the number of entries to display is set to "20" and the table is sorted by basket number (first column) 2) Go to the "Columns settings" page 3) Unfold the "Acquisition" tab => Notice the 2 dropdown lists at the bottom of the basket table 4) Select different values for "Default display length" and "Default sort order" 5) Refresh the basket view page => Notice that the default settings are now effective on the table QA note: We can decide to replace the different occurrences of "Columns settings" by "Tables settings" if needed. Sponsored-by: Institute of Technology Tallaght Signed-off-by: Liz Rea Signed-off-by: Liz Rea Signed-off-by: Alex Arnaud --- C4/Utils/DataTables/TablesSettings.pm | 61 +++++++- Koha/Template/Plugin/TablesSettings.pm | 32 ++++ admin/columns_settings.pl | 18 +++ .../intranet-tmpl/prog/en/modules/acqui/basket.tt | 5 +- .../prog/en/modules/admin/columns_settings.tt | 171 +++++++++++++------- t/db_dependent/TablesSettings.t | 173 ++++++++++++--------- 6 files changed, 319 insertions(+), 141 deletions(-) diff --git a/C4/Utils/DataTables/TablesSettings.pm b/C4/Utils/DataTables/TablesSettings.pm index 1c52dbbba8..a343944407 100644 --- a/C4/Utils/DataTables/TablesSettings.pm +++ b/C4/Utils/DataTables/TablesSettings.pm @@ -14,7 +14,7 @@ sub get_yaml { unless ($yaml) { $yaml = eval { YAML::LoadFile($yml_path) }; - warn "ERROR: the yaml file for DT::TablesSettings is not correctly formated: $@" + warn "ERROR: the yaml file for DT::TablesSettings is not correctly formatted: $@" if $@; $cache->set_in_cache( 'TablesSettingsYaml', $yaml, { expiry => 3600 } ); } @@ -39,12 +39,12 @@ sub get_columns { while ( my $c = $rs->next ) { my $column = first { $c->columnname eq $_->{columnname} } - @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } }; + @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename }{ columns } }; $column->{is_hidden} = $c->is_hidden; $column->{cannot_be_toggled} = $c->cannot_be_toggled; } - my $columns = $list->{modules}{$module}{$page}{$tablename} || []; + my $columns = $list->{modules}{$module}{$page}{$tablename}{columns} || []; # Assign default value if does not exist $columns = [ map { @@ -59,6 +59,22 @@ sub get_columns { return $columns; } +sub get_table_settings { + my ( $module, $page, $tablename ) = @_; + my $list = get_yaml; + + my $schema = Koha::Database->new->schema; + + my $rs = $schema->resultset('TablesSetting')->search( + { + module => $module, + page => $page, + tablename => $tablename, + } + )->next; + return $rs ? $rs : $list->{modules}{$module}{$page}{$tablename}; +} + sub get_modules { my $list = get_yaml; @@ -67,7 +83,7 @@ sub get_modules { while ( my $c = $rs->next ) { my $column = first { $c->columnname eq $_->{columnname} } - @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } }; + @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename }{columns} }; $column->{is_hidden} = $c->is_hidden; $column->{cannot_be_toggled} = $c->cannot_be_toggled; $column->{cannot_be_modified} = 0 @@ -100,4 +116,41 @@ sub update_columns { } } +=head3 update_table_settings + +C4::Utils::DataTables::TablesSettings::update_table_settings( + { + module => $module, + pag => $page, + tablename => $tablename, + default_display_length => $default_display_length, + default_sort_order => $default_sort_order + } +); + +Will update the default_display_length and default_sort_order for the given table. + +=cut + +sub update_table_settings { + my ($params) = @_; + my $module = $params->{module}; + my $page = $params->{page}; + my $tablename = $params->{tablename}; + my $default_display_length = $params->{default_display_length}; + my $default_sort_order = $params->{default_sort_order}; + + my $schema = Koha::Database->new->schema; + + $schema->resultset('TablesSetting')->update_or_create( + { + module => $module, + page => $page, + tablename => $tablename, + default_display_length => $default_display_length, + default_sort_order => $default_sort_order, + } + ); +} + 1; diff --git a/Koha/Template/Plugin/TablesSettings.pm b/Koha/Template/Plugin/TablesSettings.pm index 2ed567c7cd..6aecf52f79 100644 --- a/Koha/Template/Plugin/TablesSettings.pm +++ b/Koha/Template/Plugin/TablesSettings.pm @@ -74,6 +74,17 @@ datatables instantiator. =cut +=head3 GetColumns + +var columns_settings = [% TablesSettings.GetColumns( module, page, table 'json' ) | $raw%] + +This method is usually be used to retrieve the columns settings for a DataTable init. + +So the 'json' format will be provided and the columns_settings JS var will be +passed as argument of the constructor. + +=cut + sub GetColumns { my ( $self, $module, $page, $table, $format ) = @_; $format //= q{}; @@ -107,4 +118,25 @@ sub is_hidden { return 0; } +=head3 GetTableSettings + +[% SET table_settings = GetTableSettings( module, page, table ) %] + +This method is used to retrieve the tables settings (like table_settings.default_display_length and +table_settings.default_sort_order). +They can be passed to the DataTable constructor (for iDisplayLength and order parameters) + +=cut + +sub GetTableSettings { + my ( $self, $module, $page, $table, $format ) = @_; + $format //= q{}; + + my $settings = C4::Utils::DataTables::TablesSettings::get_table_settings( $module, $page, $table ); + + return $format eq 'json' + ? to_json( $settings || {} ) + : $settings +} + 1; diff --git a/admin/columns_settings.pl b/admin/columns_settings.pl index 11117de41a..afa33c7dbd 100755 --- a/admin/columns_settings.pl +++ b/admin/columns_settings.pl @@ -48,6 +48,24 @@ if ( $action eq 'save' ) { } ); + my @table_ids = $input->multi_param('table_id'); + for my $table_id (@table_ids) { + next unless $table_id =~ m|^([^#]*)#(.*)$|; + my $default_display_length = $input->param( $table_id . '_default_display_length' ); + my $default_sort_order = $input->param( $table_id . '_default_sort_order' ); + if ( $default_display_length && $default_sort_order ) { + C4::Utils::DataTables::TablesSettings::update_table_settings( + { + module => $module, + page => $1, + tablename => $2, + default_display_length => $default_display_length, + default_sort_order => $default_sort_order, + } + ); + } + } + $action = 'list'; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt index e709ec845b..784838b6d9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt @@ -925,6 +925,7 @@ [% END %]