From cb77c4741c66da3c485f4cbff28baa66a51ea353 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Fri, 8 Jul 2016 15:53:50 +0200 Subject: [PATCH] Bug 16881 - Apply KohaTable and ColVis plugin on MARCdetail's items table Also, the columns displayed are saved for each users in database. Test plan: - apply this BZ, - launch perl installer/data/mysql/updatedatabase.pl - launch perl misc/devel/update_dbix_class_files.pl - go to the items tab on cgi-bin/koha/catalogue/MARCdetail.pl page you should be able to hide/show each columns. - leave this page and come back with the same koha account: You should have the same columns shown/hidden as when you left. Rebased-by: Alex Arnaud (2018-09-19) --- C4/Utils/DataTables/ColumnsSettings.pm | 80 +++++++---- C4/Utils/DataTables/ColumnsSettings/Dynamic.pm | 74 +++++++++++ Koha/REST/V1/UserColumnsSettings.pm | 64 +++++++++ Koha/Template/Plugin/ColumnsSettings.pm | 5 +- Koha/UsersColumnsSetting.pm | 50 +++++++ Koha/UsersColumnsSettings.pm | 54 ++++++++ api/v1/swagger/definitions.json | 6 + api/v1/swagger/definitions/column.json | 13 ++ .../swagger/definitions/user_columns_settings.json | 31 +++++ api/v1/swagger/paths.json | 3 + api/v1/swagger/paths/user_columns_settings.json | 44 ++++++ catalogue/MARCdetail.pl | 5 +- .../add-table-users_columns_settings.sql | 13 ++ installer/data/mysql/kohastructure.sql | 18 +++ .../prog/en/includes/columns_settings.inc | 34 ++++- .../prog/en/modules/catalogue/MARCdetail.tt | 28 +++- t/db_dependent/ColumnsSettings.t | 1 + t/db_dependent/api/v1/user_columns_settings.t | 148 +++++++++++++++++++++ 18 files changed, 638 insertions(+), 33 deletions(-) create mode 100644 C4/Utils/DataTables/ColumnsSettings/Dynamic.pm create mode 100644 Koha/REST/V1/UserColumnsSettings.pm create mode 100644 Koha/UsersColumnsSetting.pm create mode 100644 Koha/UsersColumnsSettings.pm create mode 100644 api/v1/swagger/definitions/column.json create mode 100644 api/v1/swagger/definitions/user_columns_settings.json create mode 100644 api/v1/swagger/paths/user_columns_settings.json create mode 100644 installer/data/mysql/atomicupdate/add-table-users_columns_settings.sql create mode 100644 t/db_dependent/api/v1/user_columns_settings.t diff --git a/C4/Utils/DataTables/ColumnsSettings.pm b/C4/Utils/DataTables/ColumnsSettings.pm index 8713489..b8e1a0e 100644 --- a/C4/Utils/DataTables/ColumnsSettings.pm +++ b/C4/Utils/DataTables/ColumnsSettings.pm @@ -6,8 +6,13 @@ use YAML; use C4::Context; use Koha::Database; use Koha::Caches; +use Koha::UsersColumnsSettings; +use Koha::UsersColumnsSetting; +use C4::Utils::DataTables::ColumnsSettings::Dynamic; sub get_yaml { + my ( $module, $page, $tablename ) = @_; + my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml'; my $cache = Koha::Caches->get_instance(); my $yaml = $cache->get_from_cache('ColumnsSettingsYaml'); @@ -19,42 +24,63 @@ sub get_yaml { $cache->set_in_cache( 'ColumnsSettingsYaml', $yaml, { expiry => 3600 } ); } - return $yaml; + unless ( $module || $page || $tablename ) { + return $yaml; + } + + if (my $list = $yaml->{modules}{$module}{$page}{$tablename}) { + return $list; + } + + return ''; } sub get_columns { - my ( $module, $page, $tablename ) = @_; - - my $list = get_yaml; + my ( $module, $page, $tablename, $additional_param ) = @_; + + my $columns = get_yaml($module, $page, $tablename); + # Fall back on a dynamic way to retrieve default columns. + unless ($columns) { + my $sub = $module . '_' . $page . '_' . $tablename; + if (my $s = C4::Utils::DataTables::ColumnsSettings::Dynamic->can($sub)) { + $columns = $s->($additional_param); + } + } my $schema = Koha::Database->new->schema; - - my $rs = $schema->resultset('ColumnsSetting')->search( - { - module => $module, - page => $page, - tablename => $tablename, + my $borrowernumber = C4::Context::userenv->{number}; + + foreach my $c (@$columns) { + # User settings. + my $user_column = Koha::UsersColumnsSettings->search({ + borrowernumber => $borrowernumber, + module => $module, + page => $page, + tablename => $tablename, + columnname => $c->{columnname}, + additional_param => $additional_param + })->next(); + + if ($user_column) { + $c->{is_hidden} = $user_column->is_hidden || 0; } - ); - - while ( my $c = $rs->next ) { - my $column = first { $c->columnname eq $_->{columnname} } - @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } }; - $column->{is_hidden} = $c->is_hidden; - $column->{cannot_be_toggled} = $c->cannot_be_toggled; - } - my $columns = $list->{modules}{$module}{$page}{$tablename} || []; + # Admin settings. + my $column = $schema->resultset('ColumnsSetting')->search({ + module => $module, + page => $page, + tablename => $tablename, + columnname => $c->{columnname}, + })->next(); - # Assign default value if does not exist - $columns = [ map { - { - cannot_be_toggled => exists $_->{cannot_be_toggled} ? $_->{cannot_be_toggled} : 0, - cannot_be_modified => exists $_->{cannot_be_modified} ? $_->{cannot_be_modified} : 0, - is_hidden => exists $_->{is_hidden} ? $_->{is_hidden} : 0, - columnname => $_->{columnname}, + if ($column) { + $c->{cannot_be_toggled} = $column->cannot_be_toggled; } - } @$columns ]; + + $c->{is_hidden} ||= 0; + $c->{cannot_be_toggled} ||= 0; + $c->{cannot_be_modified} ||= 0; + } return $columns; } diff --git a/C4/Utils/DataTables/ColumnsSettings/Dynamic.pm b/C4/Utils/DataTables/ColumnsSettings/Dynamic.pm new file mode 100644 index 0000000..cfda84f --- /dev/null +++ b/C4/Utils/DataTables/ColumnsSettings/Dynamic.pm @@ -0,0 +1,74 @@ +package C4::Utils::DataTables::ColumnsSettings::Dynamic; + +# Copyright 2017 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +require Exporter; + +use vars qw(@ISA @EXPORT); + +use C4::Biblio; +use Koha::Database; + +BEGIN { + + @ISA = qw(Exporter); + @EXPORT = qw(dt_build_orderby dt_build_having dt_get_params dt_build_query); +} + +=head1 NAME + +C4::Utils::DataTables::ColumnsSettings::Dynamic - Subs providing dynamic columns for jquery datatables. + +=head1 SYNOPSIS + +use C4::Utils::DataTables::ColumnsSettings::Dynamic; + +=head1 DESCRIPTION + +This modules is intented to contain subs that return columns for KohaTable with columns setting plugin. + +=head1 FUNCTIONS + +=head2 catalogue_marcdetail_itemst + + my $columns = catalogue_marcdetail_itemst($frameworkcode); + This function returns items columns depending on the framework. + +=cut + +sub catalogue_marcdetail_itemst { + my ($frameworkcode) = @_; + + my $columns; + my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode ); + my ($itemfield) = C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', $frameworkcode ); + foreach my $subfield ( keys %{ $tagslib->{$itemfield} } ) { + next unless $subfield =~ /^\w$/; + next if ( $tagslib->{$itemfield}->{$subfield}->{tab} ne 10 ); + next if ( $tagslib->{$itemfield}->{$subfield}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ ); + + push @$columns, { + columnname => $subfield, + is_hidden => 0, + } + } + return $columns; +} + +1; diff --git a/Koha/REST/V1/UserColumnsSettings.pm b/Koha/REST/V1/UserColumnsSettings.pm new file mode 100644 index 0000000..1e671f6 --- /dev/null +++ b/Koha/REST/V1/UserColumnsSettings.pm @@ -0,0 +1,64 @@ +package Koha::REST::V1::UserColumnsSettings; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use C4::Auth qw( haspermission ); +use Koha::UsersColumnsSetting; +use Koha::UsersColumnsSettings; + +use Try::Tiny; + +sub add { + my $c = shift->openapi->valid_input or return; + + return try { + my $added; + my $body = $c->validation->param('body'); + foreach my $column (@{ $body->{columns} }) { + my $params = { + borrowernumber => $body->{'patron_id'}, + module => $body->{module}, + page => $body->{page}, + tablename => $body->{tablename}, + additional_param => $body->{additional_param}, + columnname => $column->{name}, + }; + + my $user_column; + unless ( $user_column = Koha::UsersColumnsSettings->search($params)->next ) { + $user_column = Koha::UsersColumnsSetting->new($params); + } + $user_column->is_hidden($column->{is_hidden}); + + $user_column->store; + push @$added, $user_column->unblessed; + } + + return $c->render( status => 200, openapi => { openapi => $added } ); + } + catch { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check Koha logs for details." } + ); + } +} + +1; diff --git a/Koha/Template/Plugin/ColumnsSettings.pm b/Koha/Template/Plugin/ColumnsSettings.pm index 4617969..7ef74fc 100644 --- a/Koha/Template/Plugin/ColumnsSettings.pm +++ b/Koha/Template/Plugin/ColumnsSettings.pm @@ -27,6 +27,7 @@ use JSON qw( to_json ); use C4::Context qw( config ); use C4::Utils::DataTables::ColumnsSettings; +use C4::Utils::DataTables::ColumnsSettings::Dynamic; =pod @@ -42,10 +43,10 @@ For example: [% ColumnsSettings.GetColumns( 'circ', 'circulation', 'holdst' ) %] =cut sub GetColumns { - my ( $self, $module, $page, $table, $format ) = @_; + my ( $self, $module, $page, $table, $format, $additional_params ) = @_; $format //= q{}; - my $columns = C4::Utils::DataTables::ColumnsSettings::get_columns( $module, $page, $table ); + my $columns = C4::Utils::DataTables::ColumnsSettings::get_columns( $module, $page, $table, $additional_params ); return $format eq 'json' ? to_json( $columns ) diff --git a/Koha/UsersColumnsSetting.pm b/Koha/UsersColumnsSetting.pm new file mode 100644 index 0000000..55111a5 --- /dev/null +++ b/Koha/UsersColumnsSetting.pm @@ -0,0 +1,50 @@ +package Koha::UsersColumnsSetting; + +# Copyright Biblibre 2017 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::UsersColumnsSetting - Koha UsersColumnsSetting Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'UsersColumnsSetting'; +} + +=head1 AUTHOR + +Alex Arnaud + +=cut + +1; diff --git a/Koha/UsersColumnsSettings.pm b/Koha/UsersColumnsSettings.pm new file mode 100644 index 0000000..cab57d4 --- /dev/null +++ b/Koha/UsersColumnsSettings.pm @@ -0,0 +1,54 @@ +package Koha::UsersColumnsSettings; + +# Copyright Biblibre 2017 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::UsersColumnsSettings - Koha UsersColumnsSettings Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'UsersColumnsSetting'; +} + +sub object_class { + return 'Koha::UsersColumnsSetting'; +} + +=head1 AUTHOR + +Alex Arnaud + +=cut + +1; \ No newline at end of file diff --git a/api/v1/swagger/definitions.json b/api/v1/swagger/definitions.json index 6509432..104899b 100644 --- a/api/v1/swagger/definitions.json +++ b/api/v1/swagger/definitions.json @@ -25,5 +25,11 @@ }, "vendor": { "$ref": "definitions/vendor.json" + }, + "user_columns_settings": { + "$ref": "definitions/user_columns_settings.json" + }, + "column": { + "$ref": "definitions/column.json" } } diff --git a/api/v1/swagger/definitions/column.json b/api/v1/swagger/definitions/column.json new file mode 100644 index 0000000..1d66489 --- /dev/null +++ b/api/v1/swagger/definitions/column.json @@ -0,0 +1,13 @@ +{ + "type": "object", + "properties": { + "name": { + "description": "Column name", + "type": "string" + }, + "is_hidden": { + "description": "Column is hidden or not", + "type": "boolean" + } + } +} diff --git a/api/v1/swagger/definitions/user_columns_settings.json b/api/v1/swagger/definitions/user_columns_settings.json new file mode 100644 index 0000000..d8886c9 --- /dev/null +++ b/api/v1/swagger/definitions/user_columns_settings.json @@ -0,0 +1,31 @@ +{ + "type": "object", + "properties": { + "patron_id": { + "$ref": "../x-primitives.json#/patron_id" + }, + "module": { + "description": "Module name", + "type": "string" + }, + "page": { + "description": "Page name", + "type": "string" + }, + "tablename": { + "description": "Table name", + "type": "string" + }, + "additional_param": { + "description": "Additional parameter", + "type": "string" + }, + "columns": { + "description": "Columns list", + "type": "array", + "items": { + "$ref": "column.json" + } + } + } +} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index c9d8e88..ad0d642 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -34,5 +34,8 @@ }, "/illrequests": { "$ref": "paths/illrequests.json#/~1illrequests" + }, + "/user_columns_settings": { + "$ref": "paths/user_columns_settings.json#/~1user_columns_settings" } } diff --git a/api/v1/swagger/paths/user_columns_settings.json b/api/v1/swagger/paths/user_columns_settings.json new file mode 100644 index 0000000..4125b2d --- /dev/null +++ b/api/v1/swagger/paths/user_columns_settings.json @@ -0,0 +1,44 @@ +{ + "/user_columns_settings": { + "post": { + "x-mojo-to": "UserColumnsSettings#add", + "operationId": "add", + "tags": ["user_columns_settings"], + "parameters": [{ + "name": "body", + "in": "body", + "description": "A JSON object containing informations about the columns settings", + "required": true, + "schema": { + "$ref": "../definitions.json#/user_columns_settings" + } + }], + "produces": ["application/json"], + "responses": { + "200": { + "description": "Columns settings added", + "schema": { + "$ref": "../definitions.json#/user_columns_settings" + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal error", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "catalogue": "1" + } + } + } + } +} diff --git a/catalogue/MARCdetail.pl b/catalogue/MARCdetail.pl index b4adcfd..affb927 100755 --- a/catalogue/MARCdetail.pl +++ b/catalogue/MARCdetail.pl @@ -86,6 +86,9 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( debug => 1, } ); +$template->param( + frameworkcode => $frameworkcode +); my $record = GetMarcBiblio({ biblionumber => $biblionumber, @@ -286,7 +289,7 @@ my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbran # fill item info my @item_header_loop; for my $subfield_code ( @item_subfield_codes ) { - push @item_header_loop, $witness{$subfield_code}; + push @item_header_loop, { colname => $subfield_code, label => $witness{$subfield_code} }; for my $item_data ( @item_loop ) { $item_data->{$subfield_code} ||= " " } diff --git a/installer/data/mysql/atomicupdate/add-table-users_columns_settings.sql b/installer/data/mysql/atomicupdate/add-table-users_columns_settings.sql new file mode 100644 index 0000000..8388632 --- /dev/null +++ b/installer/data/mysql/atomicupdate/add-table-users_columns_settings.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS users_columns_settings ( + id int(11) NOT NULL auto_increment, + borrowernumber int(11) NOT NULL, + module varchar(50) NOT NULL, + page varchar(50) NOT NULL, + tablename varchar(50) NOT NULL, + additional_param varchar(255) NOT NULL, + columnname varchar(50) NOT NULL, + is_hidden int(1) NOT NULL DEFAULT 0, + PRIMARY KEY(id), + UNIQUE KEY user_column_elements (borrowernumber, module, page, tablename, columnname, additional_param), + CONSTRAINT borrower_columns_settings_ibfk1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; \ No newline at end of file diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index d5368d4..e2110ff 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3618,6 +3618,24 @@ CREATE TABLE IF NOT EXISTS columns_settings ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- +-- Table structure for table `users_columns_settings` +-- + +CREATE TABLE IF NOT EXISTS users_columns_settings ( + id int(11) NOT NULL auto_increment, + borrowernumber int(11) NOT NULL, + module varchar(50) NOT NULL, + page varchar(50) NOT NULL, + tablename varchar(50) NOT NULL, + additional_param varchar(255) NOT NULL, + columnname varchar(50) NOT NULL, + is_hidden int(1) NOT NULL DEFAULT 0, + PRIMARY KEY(id), + UNIQUE KEY user_column_elements (borrowernumber, module, page, tablename, columnname, additional_param), + CONSTRAINT borrower_columns_settings_ibfk1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + +-- -- Table structure for table 'items_search_fields' -- diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc index 636cc5a..6cd8663 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/columns_settings.inc @@ -1,14 +1,15 @@ [% USE ColumnsSettings %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt index 4b0ef79..9e57c34 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/MARCdetail.tt @@ -1,6 +1,7 @@ [% USE raw %] [% USE Asset %] [% SET footerjs = 1 %] +[% USE ColumnsSettings %] [% INCLUDE 'doc-head-open.inc' %] Koha › Catalog › [% IF ( unknownbiblionumber ) %] @@ -9,6 +10,7 @@ MARC details for [% bibliotitle | html %] [% END %] + [% INCLUDE 'doc-head-close.inc' %] @@ -153,11 +155,14 @@ [% IF ( tab10XX ) %]
+ [% FOREACH header IN item_header_loop %] - + [% END %] + + [% FOREACH item IN item_loop %] [% FOREACH sf_code IN item_subfield_codes %] @@ -165,6 +170,7 @@ [% END %] [% END %] +
[% header | html %][% header.label | html %]
[% END %] @@ -182,6 +188,8 @@ [% Asset.js("js/catalog.js") | $raw %] [% INCLUDE 'browser-strings.inc' %] [% Asset.js("js/browser.js") | $raw %] + [% INCLUDE 'datatables.inc' %] + [% INCLUDE 'columns_settings.inc' %]