From f510627842cc0dd2a02e85e04d5dc14be6b6caa7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 10 Aug 2015 16:53:06 +0100 Subject: [PATCH] Bug 14544: Make the OPAC side independent of Page.pm Bug 14544: Fix redirect on editing a list If you edit a list from the list view, after saving the form, you are not redirected to the list view (but on the edit form). Bug 14544: Cosmetic: › should be a class divider Signed-off-by: Alex Arnaud Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/Objects.pm | 11 + Koha/Virtualshelf.pm | 16 +- Koha/Virtualshelves.pm | 24 +- .../virtualshelves/tables/shelves_results.tt | 2 + .../bootstrap/en/includes/masthead.inc | 14 +- .../bootstrap/en/includes/usermenu.inc | 2 +- .../bootstrap/en/modules/opac-detail.tt | 4 +- .../en/modules/opac-downloadshelf.tt | 16 +- .../en/modules/opac-sendshelfform.tt | 6 +- .../bootstrap/en/modules/opac-shareshelf.tt | 6 +- .../bootstrap/en/modules/opac-shelves-rss.tt | 6 +- .../bootstrap/en/modules/opac-shelves.tt | 638 ++++++++---------- opac/opac-downloadshelf.pl | 18 +- opac/opac-shelves.pl | 314 ++++++++- t/db_dependent/Virtualshelves.t | 76 ++- 15 files changed, 719 insertions(+), 434 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 1ff91cb468..27406cf2d9 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -126,6 +126,17 @@ sub count { return $self->_resultset()->count($params); } +=head3 Koha::Objects->pager(); + +my $pager = Koha::Objects->pager; + +=cut + +sub pager { + my ( $self ) = @_; + return $self->_resultset->pager; +} + =head3 Koha::Objects->next(); my $object = Koha::Objects->next(); diff --git a/Koha/Virtualshelf.pm b/Koha/Virtualshelf.pm index 3cd257c00c..d80f2490a0 100644 --- a/Koha/Virtualshelf.pm +++ b/Koha/Virtualshelf.pm @@ -101,12 +101,14 @@ sub is_shelfname_valid { sub get_shares { my ( $self ) = @_; - return $self->{_result}->virtualshelfshares; + my $shares = $self->{_result}->virtualshelfshares; + return $shares; } sub get_contents { my ( $self ) = @_; - return $self->{_result}->virtualshelfcontents; + my $contents = $self->{_result}->virtualshelfcontents; + return $contents; } sub share { @@ -132,6 +134,16 @@ sub is_shared { )->count; } +sub is_shared_with { + my ( $self, $borrowernumber ) = @_; + return unless $borrowernumber; + return $self->get_shares->search( + { + borrowernumber => $borrowernumber, + } + )->count; +} + sub remove_share { my ( $self, $borrowernumber ) = @_; my $shelves = Koha::Virtualshelfshares->search( diff --git a/Koha/Virtualshelves.pm b/Koha/Virtualshelves.pm index 98f9d40977..65b068059b 100644 --- a/Koha/Virtualshelves.pm +++ b/Koha/Virtualshelves.pm @@ -41,24 +41,23 @@ Koha::Virtualshelf - Koha Virtualshelf Object class sub get_private_shelves { my ( $self, $params ) = @_; - my $limit = $params->{limit}; - my $offset = $params->{offset} || 0; - my $borrowernumber = $params->{borrowernumber}; + my $page = $params->{page}; + my $rows = $params->{rows}; + my $borrowernumber = $params->{borrowernumber} || 0; $self->search( { - 'virtualshelfshares.borrowernumber' => $borrowernumber, category => 1, -or => { - 'virtualshelfshares.owner' => $borrowernumber, - 'virtualshelves.borrowernumber' => $borrowernumber, + 'virtualshelfshares.borrowernumber' => $borrowernumber, + 'me.owner' => $borrowernumber, } }, { - join => ['borrowers', 'virtualshelfcontents', 'virtualshelfshares' ], + join => [ 'virtualshelfshares' ], group_by => 'shelfnumber', order_by => 'shelfname', - limit => "$limit,$offset", + ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ), } ); } @@ -66,18 +65,17 @@ sub get_private_shelves { sub get_public_shelves { my ( $self, $params ) = @_; - my $limit = $params->{limit}; - my $offset = $params->{offset} || 0; + my $page = $params->{page}; + my $rows = $params->{rows}; $self->search( { - category => 1, + category => 2, }, { - join => [ 'virtualshelfcontents' ], group_by => 'shelfnumber', order_by => 'shelfname', - limit => "$limit,$offset", + ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ), } ); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/tables/shelves_results.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/tables/shelves_results.tt index 08d75a6868..0304dbe83d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/tables/shelves_results.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/tables/shelves_results.tt @@ -36,6 +36,7 @@ [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] + [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] [%~ END ~%] @@ -46,6 +47,7 @@ [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] + [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] [%~ action_block = action_block _ '' ~%] [%~ END ~%] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc index e034009480..0f5d1ea064 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/masthead.inc @@ -38,26 +38,26 @@ Lists
@@ -47,7 +47,7 @@

Your download should begin automatically.

[% ELSE %] -

Download list [% shelfname %]

+

Download list [% shelf.shelfname %]

+ - Cancel + Cancel
[% IF ( modal ) %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendshelfform.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendshelfform.tt index 14a91654db..06a9c064d6 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendshelfform.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendshelfform.tt @@ -15,7 +15,7 @@

The list was sent to: [% email %]

-

Back to lists

+

Back to lists

[% END %] [% IF ( error ) %]
@@ -27,7 +27,7 @@

You do not have permission to send this list.

-

Back to lists

+

Back to lists

[% ELSE %]

Sending your list

@@ -42,7 +42,7 @@
- Cancel + Cancel
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shareshelf.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shareshelf.tt index 6421138382..ac5a19bb02 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shareshelf.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shareshelf.tt @@ -41,7 +41,7 @@ [% IF errcode==6 %]
Sorry, but you did not enter a valid email address.
[% END %] [% IF errcode==7 %]
Sorry, but we could not accept this key. The invitation may have expired. Contact the patron who sent you the invitation.
[% END %] [% IF errcode==8 %]
As owner of a list you cannot accept an invitation for sharing it.
[% END %] -

Return to your lists

+

Return to your lists

[% ELSIF op=='invite' %]
@@ -59,7 +59,7 @@
- Cancel + Cancel
@@ -75,7 +75,7 @@ [% IF approvedaddress %]

You will receive an email notification if someone accepts your share within two weeks.

[% END %] -

Return to your lists

+

Return to your lists

[% ELSIF op=='accept' %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves-rss.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves-rss.tt index f25f3ed04f..2525d80c0e 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves-rss.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves-rss.tt @@ -3,9 +3,9 @@ - [% shelfname %] - [% OPACBaseURL %]/cgi-bin/koha/opac-shelves.pl?rss=1&viewshelf=[% shelfnumber %] - RSS feed for public list [% shelfname | html %] + [% shelf.shelfname %] + [% OPACBaseURL %]/cgi-bin/koha/opac-shelves.pl?rss=1&viewshelf=[% shelf.shelfnumber %] + RSS feed for public list [% shelf.shelfname | html %] [% FOREACH i IN itemsloop %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt index e2e1541fde..3b8af2c966 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-shelves.tt @@ -1,9 +1,11 @@ [% USE Koha %] -[% SET TagsShowEnabled = ( ( Koha.Preference( 'TagsEnabled' ) == 1 ) && TagsShowOnList ) %] -[% SET TagsInputEnabled = ( ( Koha.Preference( 'opacuserlogin' ) == 1 ) && ( Koha.Preference( 'TagsEnabled' ) == 1 ) && TagsInputOnList ) %] +[% SET PRIVATE = 1 %] +[% SET PUBLIC = 2 %] +[% SET TagsShowEnabled = ( ( Koha.Preference( 'TagsEnabled' ) == 1 ) && Koha.Preference('TagsShowOnList') ) %] +[% SET TagsInputEnabled = ( ( Koha.Preference( 'opacuserlogin' ) == 1 ) && ( Koha.Preference( 'TagsEnabled' ) == 1 ) && Koha.Preference('TagsInputOnList') ) %] [% INCLUDE 'doc-head-open.inc' %] -[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog › [% IF ( viewshelf ) %]Contents of [% shelfname |html %][% ELSE %]Your lists[% END %][% INCLUDE 'doc-head-close.inc' %] +[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog › [% IF op == 'view' %]Contents of [% shelf.shelfname |html %][% ELSE %]Your lists[% END %][% INCLUDE 'doc-head-close.inc' %] [% BLOCK cssinclude %][% END %] [% IF ( loggedinusername ) %][% INCLUDE 'bodytag.inc' bodyid='opac-userlists' bodyclass='scrollto' %][% ELSE %][% INCLUDE 'bodytag.inc' bodyid='opac-lists' bodyclass='scrollto' %][% END %] @@ -12,35 +14,28 @@
  •  anyone else to add entries. (The owner of a list is always allowed to add entries, but needs permission to remove.)
  •  anyone to remove his own contributed entries.
  •  anyone to remove other contributed entries.
  • [% END %] -[% BLOCK remove_share %] - [%# Use: add parameter shelfnumber and add end form tag %] -
    - - - -[% END %] [% INCLUDE 'masthead.inc' %]
    @@ -50,42 +45,36 @@
  • [% FOREACH USER_INF IN USER_INFO %][% USER_INF.title %] [% USER_INF.firstname %] [% USER_INF.surname %][% END %] ›
  • [% END %] - [% IF ( category1 ) %] - [% IF ( viewshelf || edit ) %] -
  • Your lists
  • + [% IF op != 'list' %] +
  • Lists
  • + [% ELSE %] +
  • Lists
  • + [% END %] + + [% IF shelf and shelf.category == PRIVATE %] + [% IF op == 'view' OR op == 'edit_form' %] +
  • › Your lists
  • [% ELSE %] -
  • Your lists
  • +
  • › Your lists
  • [% END %] - [% ELSIF ( category2 ) %] - [% IF ( viewshelf || edit ) %] -
  • Public lists
  • + [% ELSIF shelf AND shelf.category == PUBLIC %] + [% IF op == 'view' %] +
  • › Public lists
  • [% ELSE %] -
  • Public lists
  • - [% END %] - [% ELSIF ( showprivateshelves ) %] - [% IF ( viewshelf || edit ) %] -
  • Your lists
  • - [% ELSE %] -
  • Your lists
  • - [% END %] - [% ELSIF ( showpublicshelves ) %] - [% IF ( viewshelf || edit || shelves ) %] -
  • Public lists
  • - [% ELSE %] -
  • Public lists
  • +
  • › Public lists
  • [% END %] [% END %] - [% IF ( viewshelf ) %] -
  • › Contents of [% shelfname | html %]
  • + [% IF op == 'view' %] +
  • › Contents of [% shelf.shelfname | html %]
  • [% END %] - [% IF ( shelves ) %] + [% IF op == 'add_form' %]
  • › Create new list
  • [% END %] - [% IF ( edit ) %] -
  • › Edit list [% shelfname | html %]
  • + [% IF op == 'edit_form' %] +
  • › Edit list [% shelf.shelfname | html %]
  • [% END %] @@ -107,52 +96,65 @@ [% END %]
    - [% IF ( paramsloop ) %] - [% FOREACH paramsloo IN paramsloop %] - [% IF ( paramsloo.status ) %]
    [% paramsloo.string %]
    [% END %] - [% IF ( paramsloo.nobarcode ) %]
    ERROR: No barcode given.
    [% END %] - [% IF ( paramsloo.noshelfnumber ) %]
    ERROR: No shelfnumber given.
    [% END %] - [% IF ( paramsloo.need_confirm ) %] -
    - The list [% paramsloo.need_confirm %] is not empty. -
    It has [% paramsloo.count %] [% IF paramsloo.count == 1 %]entry[% ELSE %]entries[% END %]. -
    Use the "Confirm" button below to confirm deletion. -
    - [% END %] - [% IF ( paramsloo.nopermission ) %] -
    ERROR: You do not have adequate permission for that action on list [% paramsloo.nopermission |html%].
    - [% END %] - [% IF ( paramsloo.failgetitem ) %] -
    ERROR: No item found with barcode [% paramsloo.failgetitem %].
    - [% END %] - [% IF ( paramsloo.duplicatebiblio ) %] -
    A record matching barcode [% paramsloo.duplicatebiblio %] has already been added.
    - [% END %] - [% IF ( paramsloo.delete_ok ) %] -
    List [% paramsloo.delete_ok |html %] Deleted.
    - [% END %] - [% IF ( paramsloo.delete_fail ) %] -
    ERROR: Database error. Delete (list number [% paramsloo.delete_fail |html %]) failed.
    - [% END %] - [% IF ( paramsloo.unrecognized ) %] -
    ERROR: List number [% paramsloo.unrecognized %] unrecognized.
    - [% END %] - [% IF ( paramsloo.modifyfailure) %] -
    ERROR: Shelf could not be renamed to [% paramsloo.modifyfailure %]. This name may not be unique for this type of list. Please check.
    - [% END %] - [% IF ( paramsloo.nothingdeleted) %] -
    Warning: You could not delete any selected items from this shelf.
    - [% END %] - [% IF ( paramsloo.somedeleted) %] -
    Warning: You could not delete all selected items from this shelf.
    - [% END %] - [% END # / FOREACH paramsloop %] - [% END # / IF paramsloop %] + [% FOR m IN messages %] + [% SWITCH m.type %] + [% CASE 'message' %] + @@ -988,15 +896,9 @@ $(function() { }); [% END %] - [% IF ( loggedinusername && allowremovingitems ) %] - var removeFromListLinks = $(".removefromlist"); - removeFromListLinks.removeClass("hidden"); - // "remove from list" appearing on each line of the list output - removeFromListLinks.on("click",function(){ - $(".checkboxed").unCheckCheckboxes(); - var listitem = $("input[name="+this.id+"]"); - listitem.prop("checked",true); - $("#myform").submit(); + [% IF loggedinusername && can_remove_biblios %] + $(".removefromlist").on("click", function(e){ + return confirmDelete(MSG_REMOVE_ONE_FROM_LIST); }); $("#myform").submit(function(){ diff --git a/opac/opac-downloadshelf.pl b/opac/opac-downloadshelf.pl index 15a85dcaf0..4b932fca9b 100755 --- a/opac/opac-downloadshelf.pl +++ b/opac/opac-downloadshelf.pl @@ -46,17 +46,17 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( } ); -my $shelfid = $query->param('shelfid'); +my $shelfnumber = $query->param('shelfnumber'); my $format = $query->param('format'); my $context = $query->param('context'); -my $showprivateshelves = $query->param('showprivateshelves'); my $dbh = C4::Context->dbh; -if ( ShelfPossibleAction( (defined($borrowernumber) ? $borrowernumber : -1), $shelfid, 'view' ) ) { +my $shelf = Koha::Virtualshelves->find( $shelfnumber ); +if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { - if ($shelfid && $format) { + if ($shelfnumber && $format) { - my ($items, $totitems) = GetShelfContents($shelfid); + my ($items, $totitems) = GetShelfContents($shelfnumber); my $marcflavour = C4::Context->preference('marcflavour'); my $output; my $extension; @@ -116,13 +116,7 @@ if ( ShelfPossibleAction( (defined($borrowernumber) ? $borrowernumber : -1), $sh $template->param(fullpage => 1); } $template->param(csv_profiles => GetCsvProfilesLoop('marc')); - $template->param( - showprivateshelves => $showprivateshelves, - shelfid => $shelf->shelfnumber, - shelfnumber => $shelf->shelfnumber, - viewshelf => $shelf->shelfnumber, - shelfname => $shelf->shelfname, - ); + $template->param( shelf => $shelf ); output_html_with_http_headers $query, $cookie, $template->output; } diff --git a/opac/opac-shelves.pl b/opac/opac-shelves.pl index 2b09535a54..632177f843 100755 --- a/opac/opac-shelves.pl +++ b/opac/opac-shelves.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -# Copyright 2000-2002 Katipo Communications +# Copyright 2015 Koha Team # # This file is part of Koha. # @@ -17,13 +17,20 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; use CGI qw ( -utf8 ); -use C4::VirtualShelves::Page; +use C4::VirtualShelves; use C4::Auth; +use C4::Biblio; +use C4::Koha; +use C4::Items; +use C4::Members; +use C4::Output; +use C4::Tags qw( get_tags ); +use C4::XSLT; +use Koha::Virtualshelves; -my $query = CGI->new(); +my $query = new CGI; my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt"; @@ -34,9 +41,302 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user({ authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ), }); +my $op = $query->param('op') || 'list'; +my $referer = $query->param('referer') || $op; +my $category = $query->param('category') || 1; +my ( $shelf, $shelfnumber, @messages ); + +if ( $op eq 'add_form' ) { + # Nothing to do +} elsif ( $op eq 'edit_form' ) { + $shelfnumber = $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + + if ( $shelf ) { + $category = $shelf->category; + my $patron = GetMember( 'borrowernumber' => $shelf->owner ); + $template->param( owner => $patron, ); + unless ( $shelf->can_be_managed( $loggedinuser ) ) { + push @messages, { type => 'error', code => 'unauthorized_on_update' }; + $op = 'list'; + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } +} elsif ( $op eq 'add' ) { + eval { + $shelf = Koha::Virtualshelf->new( + { shelfname => $query->param('shelfname'), + sortfield => $query->param('sortfield'), + category => $query->param('category') || 1, + allow_add => $query->param('allow_add'), + allow_delete_own => $query->param('allow_delete_own'), + allow_delete_other => $query->param('allow_delete_other'), + owner => $query->param('owner'), + } + ); + $shelf->store; + $shelfnumber = $shelf->shelfnumber; + }; + if ($@) { + push @messages, { type => 'error', code => ref($@), msg => $@ }; + } elsif ( not $shelf ) { + push @messages, { type => 'error', code => 'error_on_insert' }; + } else { + push @messages, { type => 'message', code => 'success_on_insert' }; + $op = 'view'; + } +} elsif ( $op eq 'edit' ) { + $shelfnumber = $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + if ( $shelf ) { + $op = $referer; + if ( $shelf->can_be_managed( $loggedinuser ) ) { + $shelf->shelfname( $query->param('shelfname') ); + $shelf->sortfield( $query->param('sortfield') ); + $shelf->allow_add( $query->param('allow_add') ); + $shelf->allow_delete_own( $query->param('allow_delete_own') ); + $shelf->allow_delete_other( $query->param('allow_delete_other') ); + $shelf->category( $query->param('category') ); + eval { $shelf->store }; + + if ($@) { + push @messages, { type => 'error', code => 'error_on_update' }; + $op = 'edit_form'; + } else { + push @messages, { type => 'message', code => 'success_on_update' }; + } + } else { + push @messages, { type => 'error', code => 'unauthorized_on_update' }; + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } +} elsif ( $op eq 'delete' ) { + $shelfnumber = $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + if ($shelf) { + if ( $shelf->can_be_deleted( $loggedinuser ) ) { + eval { $shelf->delete; }; + if ($@) { + push @messages, { type => 'error', code => ref($@), msg => $@ }; + } else { + push @messages, { type => 'message', code => 'success_on_delete' }; + } + } else { + push @messages, { type => 'error', code => 'unauthorized_on_delete' }; + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } + $op = $referer; +} elsif ( $op eq 'remove_share' ) { + $shelfnumber = $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + if ($shelf) { + my $removed = eval { $shelf->remove_share( $loggedinuser ); }; + if ($@) { + push @messages, { type => 'error', code => ref($@), msg => $@ }; + } elsif ( $removed ) { + push @messages, { type => 'message', code => 'success_on_remove_share' }; + } else { + push @messages, { type => 'error', code => 'error_on_remove_share' }; + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } + $op = $referer; + +} elsif ( $op eq 'add_biblio' ) { + $shelfnumber = $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + if ($shelf) { + if( my $barcode = $query->param('barcode') ) { + my $item = GetItem( 0, $barcode); + if (defined $item && $item->{itemnumber}) { + my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} ); + if ( $shelf->can_biblios_be_added( $loggedinuser ) ) { + my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); }; + if ($@) { + push @messages, { type => 'error', code => ref($@), msg => $@ }; + } elsif ( $added ) { + push @messages, { type => 'message', code => 'success_on_add_biblio' }; + } else { + push @messages, { type => 'message', code => 'error_on_add_biblio' }; + } + } else { + push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' }; + } + } else { + push @messages, { type => 'error', code => 'item_does_not_exist' }; + } + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } + $op = $referer; +} elsif ( $op eq 'remove_biblios' ) { + $shelfnumber = $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + my @biblionumber = $query->param('biblionumber'); + if ($shelf) { + if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) { + my $number_of_biblios_removed = eval { + $shelf->remove_biblios( + { + biblionumbers => \@biblionumber, + borrowernumber => $loggedinuser, + } + ); + }; + if ($@) { + push @messages, { type => 'error', code => ref($@), msg => $@ }; + } elsif ( $number_of_biblios_removed ) { + push @messages, { type => 'message', code => 'success_on_remove_biblios' }; + } else { + push @messages, { type => 'error', code => 'no_biblio_removed' }; + } + } else { + push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' }; + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } + $op = 'view'; +} + +if ( $op eq 'view' ) { + $shelfnumber ||= $query->param('shelfnumber'); + $shelf = Koha::Virtualshelves->find($shelfnumber); + if ( $shelf ) { + if ( $shelf->can_be_viewed( $loggedinuser ) ) { + $category = $shelf->category; + my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting + my $direction = $query->param('direction') || 'asc'; + my ( $shelflimit, $shelfoffset, $itemoff ); + unless ( $query->param('print') or $query->param('rss') ) { + $shelflimit = C4::Context->preference('OPACnumSearchResults') || 20; + $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 ); + $shelfoffset = ( $itemoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving items at + } + my ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction ); + + # get biblionumbers stored in the cart + my @cart_list; + if(my $cart_list = $query->cookie('bib_list')){ + @cart_list = split(/\//, $cart_list); + } + + my $borrower = GetMember( borrowernumber => $loggedinuser ); + + for my $this_item (@$items) { + my $biblionumber = $this_item->{biblionumber}; + my $record = GetMarcBiblio($biblionumber); + + if ( C4::Context->preference("OPACXSLTResultsDisplay") ) { + $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTResultsDisplay" ); + } + + my $marcflavour = C4::Context->preference("marcflavour"); + $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'}; + $this_item->{'coins'} = GetCOinSBiblio($record); + $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $this_item->{'biblionumber'} ) ); + $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour ); + $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour ); + $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour ); + $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour ); + + unless ( defined $this_item->{size} ) { + + #TT has problems with size + $this_item->{size} = q||; + } + + # Getting items infos for location display + my @items_infos = &GetItemsLocationInfo( $this_item->{'biblionumber'} ); + $this_item->{'ITEM_RESULTS'} = \@items_infos; + + if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) { + $this_item->{TagLoop} = get_tags({ + biblionumber=>$this_item->{'biblionumber'}, approved=>1, 'sort'=>'-weight', + limit => C4::Context->preference('TagsShowOnList'), + }); + } + + $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower); + + + if ( grep {$_ eq $biblionumber} @cart_list) { + $this_item->{incart} = 1; + } + + if ( $query->param('rss') ) { + $this_item->{title} = $content->biblionumber->title; + $this_item->{author} = $content->biblionumber->author; + $this_item->{biblionumber} = $biblionumber; + } + push @items, $this_item; + } + + # Build drop-down list for 'Add To:' menu... + my ( $totalref, $pubshelves, $barshelves ) = C4::VirtualShelves::GetSomeShelfNames( $loggedinuser, 'COMBO', 1 ); + $template->param( + addbarshelves => $totalref->{bartotal}, + addbarshelvesloop => $barshelves, + addpubshelves => $totalref->{pubtotal}, + addpubshelvesloop => $pubshelves, + can_manage_shelf => $shelf->can_be_managed($loggedinuser), + can_delete_shelf => $shelf->can_be_deleted($loggedinuser), + can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser), + can_add_biblios => $shelf->can_biblios_be_added($loggedinuser), + sortfield => $sortfield, + itemsloop => $items, + sortfield => $sortfield, + direction => $direction, + ); + if ($shelflimit) { + $template->param( + pagination_bar => pagination_bar( + q||, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ), + $itemoff, "itemoff", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, } + ), + ); + } + } else { + push @messages, { type => 'error', code => 'unauthorized_on_view' }; + } + } else { + push @messages, { type => 'error', code => 'does_not_exist' }; + } +} + +if ( $op eq 'list' ) { + my $shelves; + my ( $page, $rows ) = ( $query->param('page') || 1, 20 ); + if ( $category == 1 ) { + $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, }); + } else { + $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, }); + } + + my $pager = $shelves->pager; + $template->param( + shelves => $shelves, + pagination_bar => pagination_bar( + q||, $pager->last_page - $pager->first_page + 1, + $page, "page", { op => 'list', category => $category, } + ), + ); +} + $template->param( + op => $op, + referer => $referer, + shelf => $shelf, + messages => \@messages, + category => $category, + print => $query->param('print') || 0, listsview => 1, - print => $query->param('print') ); -shelfpage('opac', $query, $template, $loggedinuser, $cookie); +output_html_with_http_headers $query, $cookie, $template->output; diff --git a/t/db_dependent/Virtualshelves.t b/t/db_dependent/Virtualshelves.t index aa8b3235f2..42c7ea941d 100644 --- a/t/db_dependent/Virtualshelves.t +++ b/t/db_dependent/Virtualshelves.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use DateTime::Duration; use C4::Context; @@ -14,10 +14,7 @@ use t::lib::TestBuilder; my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; - -$dbh->do(q|DELETE FROM virtualshelfshares|); -$dbh->do(q|DELETE FROM virtualshelfcontents|); -$dbh->do(q|DELETE FROM virtualshelves|); +teardown(); my $builder = t::lib::TestBuilder->new; @@ -81,6 +78,8 @@ subtest 'CRUD' => sub { is( $is_deleted, 1, 'The shelf has been deleted correctly' ); $number_of_shelves = Koha::Virtualshelves->search->count; is( $number_of_shelves, 1, 'To be sure the shelf has been deleted' ); + + teardown(); }; subtest 'Sharing' => sub { @@ -149,6 +148,8 @@ subtest 'Sharing' => sub { is( $shelf_to_share->remove_share( $share_with_me->{borrowernumber} ), 1, '1 share should have been removed if the shelf was shared with this patron' ); $number_of_shelves_shared = Koha::Virtualshelfshares->search->count; is( $number_of_shelves_shared, 1, 'To be sure the share has been removed' ); + + teardown(); }; subtest 'Shelf content' => sub { @@ -235,6 +236,8 @@ subtest 'Shelf content' => sub { is( $number_of_deleted_biblios, 1, ); $number_of_contents = Koha::Virtualshelfcontents->search->count; is( $number_of_contents, 1, 'The biblio should have been deleted to the shelf by the patron 2, even if it is not his own content (allow_delete_other=1)' ); + + teardown(); }; subtest 'Shelf permissions' => sub { @@ -340,4 +343,67 @@ subtest 'Shelf permissions' => sub { is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' ); is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' ); + + teardown(); }; + +subtest 'Get shelves' => sub { + plan tests => 4; + my $patron1 = $builder->build({ + source => 'Borrower', + }); + my $patron2 = $builder->build({ + source => 'Borrower', + }); + + my $private_shelf1_1 = Koha::Virtualshelf->new({ + shelfname => "private shelf 1 for patron 1", + owner => $patron1->{borrowernumber}, + category => 1, + } + )->store; + my $private_shelf1_2 = Koha::Virtualshelf->new({ + shelfname => "private shelf 2 for patron 1", + owner => $patron1->{borrowernumber}, + category => 1, + } + )->store; + my $private_shelf2_1 = Koha::Virtualshelf->new({ + shelfname => "private shelf 1 for patron 2", + owner => $patron2->{borrowernumber}, + category => 1, + } + )->store; + my $public_shelf1_1 = Koha::Virtualshelf->new({ + shelfname => "public shelf 1 for patron 1", + owner => $patron1->{borrowernumber}, + category => 2, + } + )->store; + my $public_shelf1_2 = Koha::Virtualshelf->new({ + shelfname => "public shelf 2 for patron 1", + owner => $patron1->{borrowernumber}, + category => 2, + } + )->store; + + my $private_shelves = Koha::Virtualshelves->get_private_shelves; + is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' ); + $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} }); + is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' ); + + $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber}); + $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} }); + is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' ); + + my $public_shelves = Koha::Virtualshelves->get_public_shelves; + is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' ); + + teardown(); +}; + +sub teardown { + $dbh->do(q|DELETE FROM virtualshelfshares|); + $dbh->do(q|DELETE FROM virtualshelfcontents|); + $dbh->do(q|DELETE FROM virtualshelves|); +}